grep & egrep script problem

Joe Smith jes at martnet.com
Fri Nov 3 02:13:46 UTC 2006


Paul Ward wrote:
> ...
> I am wanting to find multiple proccess and then produce a true exit
> code if they are all there and a false if they are not.
> ...

It might make things simple if you use ps to do the selection as well. 
Since you seem to know the PIDs you're looking for, you can ask ps to 
look for them specifically. Unfortunately, it won't return an error if 
any are missing, but you can have it print the ones it finds, and see if 
you get the same number out as you put in:

------- snip -------
#!/bin/sh

# the PIDs we want to check
PIDS='2530 2514 4016 4017'

XPIDS=`ps --no-headers -o pid -p "$PIDS"`

if [ `echo "$XPIDS" | wc -l` = `echo $PIDS | wc -w` ]
then
   echo "All PIDs present: ($PIDS)"
else
   echo "Something's missing: need ($PIDS), found (`echo $XPIDS`)"
fi
------- snip -------

I tried to stick with generic sh and avoid any bash-isms.

Here's what it looks like on a run:

$ sh -x tmp/fpids.sh
+ PIDS='2530 2514 4016 4017'
++ ps --no-headers -o pid -p '2530 2514 4016 4017'
+ XPIDS=' 2514
  2530
  4016'
++ echo ' 2514
  2530
  4016'
++ wc -l
++ echo 2530 2514 4016 4017
++ wc -w
+ '[' 3 = 4 ']'
++ echo 2514 2530 4016
+ echo 'Something'\''s missing: need (2530 2514 4016 4017), found (2514 
2530 4016)'
Something's missing: need (2530 2514 4016 4017), found (2514 2530 4016)




More information about the fedora-list mailing list