Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IndianaJones

macrumors newbie
Original poster
Jan 30, 2009
4
0
Hi all,

I have a small script that doesnt work well enough. Here is the code.

#!/bin/bash

echo $1
app=$1

procname=`ps -ef | grep -i $app | grep -v grep`
echo $procname

output
-------
./example firefox

firefox
501 247 134 0 16:13.29 ?? 67:29.49 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_131104 501 4079 2386 0 0:00.00 ttys000 0:00.01 /bin/bash ./example firefox 501 4080 4079 0 0:00.00 ttys000 0:00.00 /bin/bash ./example firefox

But when I execute the the following code on the bash shell the output is different.

app=firefox
procname=`ps -ef | grep -i $app | grep -v grep`
echo $procname

output
-------
501 247 134 0 16:41.88 ?? 68:48.90 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_131104

The outputs are different so what could be the problem?
Thanks
 

lostngone

macrumors 65816
Aug 11, 2003
1,431
3,804
Anchorage
You are greping for $app so grep is seeing the terminal/vty line of you entering your script name and what you are looking for(firefox).

There are a LOT better ways of doing it but here is the dirty way to get it to work.

procname=`ps -ef | grep -i $app | grep -v grep | grep -v example`
 

belvdr

macrumors 603
Aug 15, 2005
5,945
1,372
You are greping for $app so grep is seeing the terminal/vty line of you entering your script name and what you are looking for(firefox).

There are a LOT better ways of doing it but here is the dirty way to get it to work.

procname=`ps -ef | grep -i $app | grep -v grep | grep -v example`

To ensure you can rename the script and not have to modify the code:

Code:
PGM=`basename $0`
procname=`ps -ef | grep -i $app | grep -v grep | grep -v ${PGM}`
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.