Wednesday 6 June 2007

Shell scripts

The following script is not an example of great programming, but shows some of the standard commands available in the UNIX shell:

l=1
for FILE in *.gif
do if test $l -ge 100
then mv $FILE $l.gif
elif test $l -ge 10
then mv $FILE 0$l.gif
else mv $FILE 00$l.gif
fi
l=$((l+1))
done

A standard loop can be programmed via

for (( n=1 ; n<9 ; n=$((n+1)) )); do echo $n; done

1 comment:

Bas van der Linden said...

These comments only work in Bourne shells, i.e. either bash or zsh. Many PhD students tend to use tcsh or csh (you can check this by "echo $SHELL" on the command line). Bash and zsh are much easier to use when you want to automate repetitive and tedious tasks: contact BCF how the can help you switch.