Hello, On Fri, 26 Nov 2010, David C. Rankin wrote: [..]
parray=( $(ps ax | grep -v "grep\|${0##*/}" | grep ${psearch}) )
Variant 1 (not available on other Unices(?), e.g. MacOS X, not sure since when it's been available on Linux (IIRC >10 yrs)): pidof "$psearch" ### uses 'killall' behind the scenes, ### which potentionally might be a ### security risk Variant 2 (portable as much as the 'ps ax' output is, when in doubt, IIRC you can further boil that down to variant 2portable): ps ax | awk -vpsearch="$psearch" '$5 ~ psearch { print $1; }' Variant 2portable? (at least a lot more AFAIK): ps -eo pid,cmd | awk -vpsearch="$psearch" '$2 ~ psearch { print $1; }' AFAIR 'ps -eo' is POSIX and as portable as you can get in regard with 'ps'. man ps / info ps has a lot to say about (non-)portability of various flags and fields for -eo and such ;) And without a subshell (helps handling variables), but bash-specific process substitution: Variant 2bashism: awk -vpsearch="$psearch" '$5 ~ psearch { print $1; }' < <(ps ax) or awk -vpsearch="$psearch" '$2 ~ psearch { print $1; }' < <(ps -eo pid,cmd) Tried & tested with "xterm" for "psearch" and 9 of those running. Using just $5 also avoids the usual problem with "grepping" for the name of a name process, where the grep may appear in the output as well, but $5 is just the name of the executable, i.e. xterm itself or awk (both with or without path, but none of it's arguments which may contain "xterm"). At least use IFS and read/set to parse columns: Variant 3: ps ax | grep '[x]term' | while read -r pid rest; do echo $pid; done (you need to massage the 'grep' argument, else you end up with grep in the result more often than not, so not easy if you want to grep for a variable, and once you do anything more than that one simple grep, you might as well just use awk already! David C.R. & ALL: if you want to do _anything_ with "fields", "colums" or some such, or simply "text" generally, forget bash, cut and alike, just use awk or perl rightaway, it saves you _tons_ of trouble. bash's internal string stuff, cut, etc. pp. _have_ their uses, but "parsing" text (in columns or not) is not it (aside from splitting fields using "IFS" and set/read). Know thy tools, eh? ;) HTH & go RTFM (again) ;P -dnh, regularly reRTFMing, reading 'man bash' for over 10 years now, regularly, and regularly discovering new stuff (with the 10year old bash I had on my old box!). Sadly though, I've finally decommissioned that old box, so now I've to rely on my memory and on the current bash/documentation on the new box (running mainly oS 11.2 for now). -- The question I have is if you take the 'bugs/spyware/IE/Media player/ other undesirables' (you were talking about 'fixing' it) out of Windows, is it going to be worth using? Would it then fit on a single density floppy? -- J. Ramsey [2004/02/13] -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org