Tuesday 31 August 2010

Bourne Shell Script Commands

Here's a bunch of bourne shell script commands I use on a regular basis
Using variables
VARNAME=3
echo The value is $VARNAME


Concatenating a list of text files
This is most useful when the list of files is long.

files=(
a.txt
b.txt
c.txt)
for p in "${files[@]}"; do cat "$p"; done > output.txt


Check to see if file exists

if [ -f filename ];
then
echo "OK"
else
echo "ERROR: File not found"
exit
fi


Note that the spaces before the -f and after the filename are essential. Can be irritating if you forget!

Check to see if a directory exists

if [ -d directoryName ];
then
echo "Directory OK"
else
echo "ERROR: Can't find directory"
exit
fi


Checking a script is running on a particular machine
if [ "`hostname`" == "myhost.calvium.com" ];
then
echo "this is the right server";
fi;


Note that the spaces around the [, == and ] are ESSENTIAL. This gets me every time..

No comments:

Post a Comment