Register
It is currently Fri Oct 24, 2014 6:01 am

If statement and integer equality


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Dec 07, 2010 11:50 am   

Joined: Mon Dec 06, 2010 5:27 pm
Posts: 9
Ok, so tell me what foolish mistake I'm making here. Essentially, I'm trying to check if firefox is running before I continue with the script (I want it to NOT be running). My intent is to see if ps returns any lines when I look for a firefox process, and if I do, then abort the script. Otherwise, keep on going. The following is test snippet of my script to handle this check.

Code:
if [ `ps -ef | grep firefox | grep -v grep | wc -l` -eq 0 ]
then
   echo "Cannot proceed: A firefox process is currently running."
   exit 1
fi
exit 0


The test does not seem to work, never detects if firefox is running. The code in backticks reports back the result I expect, but I can't seem to get the if test to behave. Any ideas?


Top
 Profile  
 PostPosted: Tue Dec 07, 2010 6:05 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Strange, works for me. Instead of two greps, you can use just one, then you do not have to count lines either:
Code:
if ps -ef | grep -q [f]irefox ; then
  echo firefox running
fi


Top
 Profile  
 PostPosted: Tue Dec 07, 2010 7:56 pm   

Joined: Mon Dec 06, 2010 5:27 pm
Posts: 9
Well, that's weird. I like what you're doing, it's a bit cleaner, but it always echos for me, whether or not any process with firefox in the entry is running or not.

Very weird...


Top
 Profile  
 PostPosted: Wed Dec 08, 2010 1:35 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Remove the -q part and see what matches...


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 7:24 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Also, you can consider pgrep(1) which is designed for this.

Code:
if pgrep firefox > /dev/null; then
    # firefox(1) is running
fi


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP