time to start learning bash scripting

Tim ODriscoll yellowdog-general@lists.terrasoftsolutions.com
Tue Aug 26 14:58:02 2003


On Tue, 26 Aug 2003, Stefan Jeglinski wrote:
> Being the screaming newbie I am at this, can someone give me a place 
> to start? From my naive world view, I know whether the program is 
> running based on
> 
> 	ps caux |grep name_of_program
> 
> as just one example of the many ways I'm sure there are to do this.
> 
>  From my reading, I'm thinking that I can (should?) use awk inside of 
> an if statement to extract the info in a form that can be branched 
> on. In pseudocode:
> 
> if(name_of_program is in ps listing) {
> 	move file
> } else {
> 	print error message and exit
> }
> 
Are you wanting to run this script as a normal user or as root?

If root, then I'd use the 'pidof' command, but if not then you can use 
your 'ps' variant with something like this:

<snip>
pid=`pidof $1`         # get the pid of the first argument
if [ ! -x $pid ]; then    # if $pid contains a value, continue
        echo " \"$1\" Found!"
else
        echo "Error: \"$1\" not found"
fi
<snip>

This works on my machine anyway :)

You can loose the '!' in the if statement, but you'll need to swap the 
'found' and 'not found' pieces round, as I'm sure you've guesses :)

Have fun,

Tim