Error message...why?
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hey everyone,
Any thoughts on why I'm getting the following error for the script below?
syntax error near unexpected token `fi'
#!/bin/sh
if [ ps -ef | grep LogMeIn ]
then
echo "LogMeIn is already running"
else
open /Library/Application Support/LogMeIn/bin/LogMeIn.app
echo "Now LogMeIn is running"
fi
6 replies
Sign in or join Beachball Help to reply.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi Jason,
I'm not sure, because I get a slightly different error when I test your script. But it's related to the test, and I think it's because you'll always get at least one match with yours. And if LogMeIn is running, you'll get two lines.
Try this:
#!/bin/sh
if [ `ps -ef | grep LogMeIn | grep -v grep | wc -l` != 0 ]
then
echo "LogMeIn is already running"
else
open /Library/Application Support/LogMeIn/bin/LogMeIn.app
echo "Now LogMeIn is running"
fiWhat I did with my test is to get the list of processes, then look for LogMeIn. But I'll always get a result (the grep command), even if it's not running:
$ ps -ef | grep LogMeIn
501 18236 2060 0 0:00.00 ttys000 0:00.00 grep LogMeInSo I piped the output to grep one more time to tell it to look for anything but grep, then piped that to wc to give me just a raw line count, which I can then test against "0". Otherwise, if I try to test against the raw results of the grep commands, I get errors as the shell tries to expand it inside the test construct.
Also note the backticks around the commands inside the brackets.
charlie
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Works great. Thanks!
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
The correct form would be:
#!/bin/sh
if ps -ef | grep LogMeIn
then
echo "LogMeIn is already running"
else
open /Library/Application Support/LogMeIn/bin/LogMeIn.app
echo "Now LogMeIn is running"
fiWITHOUT the square brackets. What you wrote would be the same as
if test ps -ef | grep LogMeInand if you check "man test" you will see that "ps -ef" are not proper "test" program arguments.
If you look at the programs /bin/test and /bin/[ you will see that they are really the same program. One if a hardlink to the same file in /bin
ls -il /bin/test /bin/[
57880389 -r-xr-xr-x 2 root wheel 46720 Jun 17 2009 /bin/[
57880389 -r-xr-xr-x 2 root wheel 46720 Jun 17 2009 /bin/test
# inode permissions hardlink count ...And just for fun, you may want to throw in the grep -q option which will just set the grep exit status, but not print anything to stdout.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi Bob,
I knew you'd know the ins and outs. But there's just one thing: as it is, this will never return false:
if ps -ef | grep LogMeInWhy? Because you'll always get the grep command (at least on 10.5.8, which is where I'm looking at it right now). You can try that with any process name and it'll always be true:
if ps -ef | grep -q SomeArbitraryNonExistingProcessNameSo, unless there's a way to keep grep from finding itself in the process list, that's why I suggested piping it one more time:
if ps -ef | grep LogMeIn | grep -q -v grepIn this case, I added the -q option so it just sets the exit status.
charlie
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I knew you'd know the ins and outs. But there's just one thing: as it is, this will never return false:
if ps -ef | grep LogMeInWhy? Because you'll always get the grep command (at least on 10.5.8, which is where I'm looking at it right now). You can try that with any process name and it'll always be true:
Your right. I got caught up in the syntax rules, and lost sight of the target.
This is a trick I use to avoid grep finding itself
if ps ax | grep [L]ogMeInFrom a regular expression standpoint the
[L]ogMeInwork the same as
LogMeInbut the grep command when viewed via ps, does not include the simple string LogMeIn. So you do not get false positives.
Since I do not have LogMeIn server running at the moment, I'll pick on another process as my example:
# false positive
ps ax|grep vim
20405 s000 R+ 0:00.00 grep -E -i --color vim
20375 s002 S+ 0:00.09 /usr/bin/vim
echo $?
0
# excluding grep
ps ax|grep [v]im
20375 s002 S+ 0:00.09 /usr/bin/vim
echo $?
0
# with no Vim running
ps ax|grep [v]im
echo $?
1 - 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
BobHarris wrote:
This is a trick I use to avoid grep finding itself
if ps ax | grep [L]ogMeInOoh, that's a very cool trick. I'll have to remember that one...
charlie