Register
It is currently Fri Nov 21, 2014 8:59 pm

should be simple


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Aug 04, 2009 9:22 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hiya all,

Should be simple this one but although I can do this in other languages I have yet to think of a good way using bash.
This is for an init script that may be run on a variety of redhat based systems, therefore it needs to be compatible with bash versions 3.2.25 and 2.05b and therefore cannot use bash regex matching.

Once the service has started I attempt to grab pid information using 'pidofproc' my issue is that sometimes this will output nothing (""), sometimes 1 pid ("1243") and sometimes multiple pids ("1243 8756") and I need to tell the difference between the 3 options.

Code:
FCOMPID=$(pidofproc binary)
if [ -z "$FCOMPID" ]; then
  # do my pid is empty stuff
elif [ what_shall_i_put_here ]; then
  # do i have too many pids stuff
else
  # only 1 pid so do cool stuff
fi


Solutions I have thought of are ugly or use perl or somesuch :-X
pidofproc comes from redhat's init functions library.

I think this is the first time I've actually asked a question on this site o_O

Of course, what would actually be handy is if the company we pay tens of thousands of pounds to for the software actually provided tools to use the software in a real environment. Sadly changing the software is a project for 2011, until then I have to play +:)


Top
 Profile  
 PostPosted: Tue Aug 04, 2009 11:22 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Guessing the pidofproc only returns the pids compared to pidof which also returns the search string.

Code:
pids=$(pidofproc binary)
x=0
for i in $pids; do
    if [ ! -z $i ]; then
       (( x++ ))
    fi
done

if [ $x -eq 0 ]; then
   # What to do if there is no pids
elif [ $x -eq 1 ]; then
   # What to do if there is just 1 pid
else
   # What to do if there is more then 1 pid
fi


Something like this might do the trick?

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Aug 05, 2009 7:33 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
simple, should have thought of it myself considering how many times we tell people to beware of using for loops when the data has spaces in lol

thanks Fredrik


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 6 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