Introduction to bash scripting

If you’ve ever had more than a brief glimpse over linux, you’ve probably had some interaction with the terminal environment (aka the command line). This is the place where you can do pretty much anything you can do with a GUI and so much more. You have complete control over your system (provided you have root access) and can get it do almost anything you want.
The rest of this article will assume you have a basic understanding of the command line and .bashrc. If you don’t go and google it and come back when you do.
Now you may know that you edit your .bashrc to have aliases and functions to make your life easier. For example:
alias ll='ls -l'
so typing ll will now be the same as typing ls -l into the command line.
Now you may have used scripts to perform some actions. They typically have the .sh extension and are run from the command line with something like:
./some_script.sh
Let’s have a look at what they look like.

#! /bin/bash
# script to print Hello World to the command line
echo "Hello World"

The first line indicates it is a bash script and the second line is a comment about what the script does (#’s are comments in bash scripts). The next line actually performs the action of echo-ing “Hello World” to the command line.
You can also use variables in these scripts as follows:

#! /bin/bash
# script to print Hello World to the command line
SUBJECT="World"
echo "Hello $SUBJECT"

which will do the same thing as above.
Any valid actions from the command line will work in a bash script to so it would be perfectly valid (if not a little stupid) to have:

#! /bin/bash
# a script to list the contents of the current directory
# the script is called listall.sh
ls -a

Now it’s useful to be able to pass in arguments from the command line so you can do some more complex processing within a script but that raises the question of how to do it. If you’re always going to be supplying arguments in the same order you could just do something like:

#! /bin/bash
# a script to print the command line argument to the console
# note the $0 variable is the name of the script
FIRST_COMMAND_LINE_VARIABLE=$1
SECOND_COMMAND_LINE_VARIABLE=$2
...
Nth_COMMAND_LINE_VARIABLE=$n

This is really only useful when you’re only supplying one argument to a script or when you’re supplying a list of arguments (e.g. a list of files for processing/renaming etc).
For more complex situations where you supply switches (think ffmpeg) my personal favourite way is to use getopts. The next example demonstrates how a script could get command line arguments.

#! /bin/bash
# a script to print the command line argument to the console
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args?
then
exit $1
fi

while getopts “a:b:cd” Option
do
case $Option in
a ) A_VAR=$OPTARG;;
b ) B_VAR=$OPTARG;;
c ) echo “Option C was chosen”;;
d ) echo “Option $Option was chosen”;;
* ) echo “Unimplemented option chosen. $Option”;; # DEFAULT
esac
done
echo “A VAR: $A_VAR”
echo “B VAR: $B_VAR”
exit 0
You could then run this script with ./script.sh -a HELLO -c and the output to the console would be:
Option C was chosen
A VAR: HELLO

Hopefully that’s given you an introduction to bash scripting. I’m just writing a script that will checkout a branch from CVS, tag it and then create a new branch for you to work. It’s got several command line switches; some of which are optional, some of which are mandatory and uses if-else logic etc. I’ll post a tutorial here after it’s done and walk you through it.

Leave a Comment

Your email address will not be published. Required fields are marked *