[dundee] if in bash scri[pting

Andrew Clayton dundee at lists.lug.org.uk
Mon Jun 16 21:08:00 2003


On Mon, 2003-06-16 at 20:11, Keir Lawson wrote:
> Im trying to write a script in the bsh scriupting language (whatever its

Normally just called a shell script.

Never come across bsh before... on my system it is a symlink to ash.

Most shell scripts you will come across are written for /bin/sh which is
generally a symlink to /bin/bash (on Linux systems at least).

Not familiar with ash. But here is a quick example of "if" under bash

#!/bin/sh
#
if [ "$1" -eq 0 ]
then
        echo "O"
else
        echo "non 0"
fi


running this script with a 0 as a parameter prints "0", running it with
a non 0 number prints "non 0", simple really.

the [ & ] basically do the same as the test command. man test, will show
you the list of tests you can do on integers and strings.


> called) and was wondering: A. how to do ifs B. how to detect if a progam
> is already running.
> 

hmm.. some programs will put their pid in someplace like
/var/run/command.pid so you could check for the existence of such a
file. But this probably isn't 100 percent reliable.
 
A better thing to use would be the pgrep utility. You can do a pgrep
processname and it will return the pid of that process if it is running.

man pgrep for more info and exit status codes.


> keir


--
Andrew Clayton