While Do Statement with blank/empty grep return?
Solution 1
Instead of
if [ "$pid"="" ]
please try
if [ "$pid" = "" ]
The whitespace is around =
is important.
You can also try
if [ -z "$pid" ]
Solution 2
I'd do
while pgrep -fl "mylittleprogram"; do sleep 6; done
exit # process has ended
(pgrep is in package psmisc
IIRC)
I've just tested it. You could redirect the output of pgrep
to /dev/null
if you wanted the waiting to be silent. Add some more spice to make things uninterruptible:
{
trap "" INT
while pgrep -fl "mylittleprogram" >/dev/null
do
sleep 6
done
true
} && exit
Solution 3
The zero test is if [ -z "$pid" ]
bikerben
I’m Parker Software’s ThinkAutomation Technical Sales Consultant. With a technical background spanning over 20 years in various areas of the IT industry.
Updated on July 05, 2022Comments
-
bikerben almost 2 years
This is the code for my foobar.sh:
!#/bin/bash while [ 1 ] do pid=`ps -ef | grep "mylittleprogram" | grep -v grep | awk ' {print $2}'` echo $pid if [ "$pid"="" ] then echo "Process has ended lets get this show on the road..." exit else echo "Process has not ended yet" fi sleep 6 done
I'm basically running a infinate loop which will execute command X once a monitored process has ended but I end up getting the following message as my script loops:
./foobar.sh: line 7: [: missing `]' Process has not ended yet
Is there a way of making the script accept that zero feed back will trigger my 'Then' statement and execute command X since it is not liking the current method.