back
shell scripting with bash
basic commands
echo prints out what comes after it on a line
tests for equality, existence, etc. to use in control
structures
= is the string comparison operator. Note the two
spaces, one on either side. Miss these out and bash will complain.
Indeed, a="thing" (without the spaces) is the assignment
rather than the comparison operator
File related tests: stolen from this
shell scripting
tutorial
- -b file = True if the file exists and is block special file.
- -c file = True if the file exists and is character special file.
- -d file = True if the file exists and is a directory.
- -e file = True if the file exists.
- -f file = True if the file exists and is a regular file
- -g file = True if the file exists and the set-group-id bit is set.
- -k file = True if the files' "sticky" bit is set.
- -L file = True if the file exists and is a symbolic link.
- -p file = True if the file exists and is a named pipe.
- -r file = True if the file exists and is readable.
- -s file = True if the file exists and its size is greater than zero.
- -s file = True if the file exists and is a socket.
- -t fd = True if the file descriptor is opened on a terminal.
- -u file = True if the file exists and its set-user-id bit is set.
- -w file = True if the file exists and is writable.
- -x file = True if the file exists and is executable.
- -O file = True if the file exists and is owned by the effective user id.
- -G file = True if the file exists and is owned by the effective group id.
- file1 -nt file2 = True if file1 is newer, by modification date, than file2.
- file1 ot file2 = True if file1 is older than file2.
- file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
- -z string = True if the length of the string is 0.
- -n string = True if the length of the string is non-zero.
- string1 = string2 = True if the strings are equal.
- string1 != string2 = True if the strings are not equal.
- !expr = True if the expr evaluates to false.
- expr1 -a expr2 = True if both expr1 and expr2 are true.
- expr1 -o expr2 = True is either expr1 or expr2 is true.
Usage: if [ -d directory ] ; then echo "directory exists";
fi
control structures
The if statement takes the following form:
if [ condition ];
echo "do something";
fi
To do if ... else, you need to add a "then"
if [ condition ];
then
echo "do something";
else
echo "do something else";
fi
doing mathematics in bash
To compare two integers use
- -eq for equals
- -ne for not-equals
- -gt (or > within double brackets)
- -ge (or >= within double brackets)
- -lt (or < within double brackets)
- -le (or <= within double brackets)
So if [ $a -gt $b ] is true if the value of $a is bigger
than the value of $b
To add subtract etc... use subshells. So
$k = $(( $i + $j ))
command line arguments
Using command line arguments in a shell script - the equivalent of
argv and argc etc. in c:
- $@ gives you all the arguments
- $# gives you the number of arguments
- $0 gives you the current file name (the name of the script)
- $1 gives you the first argument, $2 the second, and so on.
consider this script, called cl.sh
#!/bin/bash
echo there are $# arguments to $0: $*
echo the first argument: $1
echo the second argument: $2
echo the third argument: $3
echo here are all the arguments again: $@
which is not very interesting, but shows what's what. Run it with
the arguments a b c d and this is the output:
~/bin $ ./cl.sh a b c d
there are 4 arguments to ./cl.sh: a b c d
the first argument: a
the second argument: b
the third argument: c
here are all the arguments again: a b c d
~/bin $