Often you want to automate tasks. This can be done by writing a bash script, a text file that uses a relatively simple programming language and standard linux commands. The commands are executed in order. A script does not have all the bells and whistles that more sophisticated languages like C and C++ have but is usually easier to write since it can explicitly use linux commands. We’ll learn a bit of scripting through examples. Scripting will be covered in more depth later in the week.
Let’s start with a simple minded example. Use a text editor to insert the following characters in a file named basher.sh:
#!/bin/bash
echo " hello $USER. What are you up to?"
echo " "
Note the first line. This is mandatory for all bash scripts. You have already seen the echo command. USER is a predefined variable name that is set to your username. It must be capitalized. The syntax $USER means use the value of the variable following the $ sign. Finally, the use of double quotation marks means that $USER will not be interpreted literally as a 5 character word but will interpreted as the value of USER. Try to execute basher.sh:
% basher.sh
% bash basher.sh
What happens? You need to tell the shell that basher.sh is an executable file. Do this using the chmod command:
% chmod +x basher.sh
You should be ready to go:
% basher.sh
Better? You can always use the longer manner:
% bash basher.sh
There is an easy way to concatenate characters and strings if you already have an initialized variable. Modify the file basher.sh to read:
#!/bin/bash
echo " hello ${USER}oid. What planet did you arrive from?"
echo " "
The braces delimit the initialized variable and only the initialized variable.
One way to step through a list of tasks is to use a for loop. This is best illustrated by example. Inside basher.sh, enter at the end:
for X in red white blue green orange purple
do
echo $X
done
Y="goofy colors"
echo $Y
Note the structure. You have the for and the in keywords. The X and the various colors are arbitrary and you choose them. Next you have the keywords do and done. Indent them the same amount. The action commands in between them should be indented further for easy reading. Style matters. Finally, if the variable you define is supposed to be initialized to something that contains spaces, you need to use a pair of double quotation marks to enclose that something. No spaces aound the equals sign!
You can use this structure to count the number of files of a certain type in a directory. Suppose you have, or can create, a bunch of text files with the extension .log and you want to know how many there are since each one corresponds to, say, a job you ran. You can modify basher.sh by adding this code at the end:
let " count = 0"
for X in *.log
do
let " ++count"
done
echo "$count"
Go ahead and create some known number of files with the suffix .log Run the modified script. Does it work? Why did it work? The builtin command let tells the shell that an arithmetic calculation is to be performed. Enclose the expression to be evaluated in double quotation marks. In our case we are initializing the variable count to zero. Inside the for loop we increment the value of count every time a file with the .log extension is found. The let command does not need the $ sign in front of the variable name. Finally, outside the for loop we print the value of count. Here we do need the $ sign since we are using echo and not let.
By the way, let is used with a variety of arithemetic and logical operators. See the man pages for a list.
You can make decisions based on some criterion using the if-then-else control structure. For example, make a new file basher2.sh and include:
#!/bin/bash
if test -e $1
then
echo "$1 exits. congrats."
else
echo " no dice. $1 ain't there."
fi
Note the if-then-else structure terminated by fi. The key word test is followed by a builtin switch -e that tests whether the file represented by $1 exists. There are other switches that test whether a file exists and is executable (-x), exists and is a directory (-d), plus some others. The variable $1 refers to the first argument on the command line after the name of the command. You need to quote it the second time since there are spaces in what you are trying to echo. Here is how you execute basher2.sh to test if the file basher.sh exists. (OK, we already know it does.)
% bash basher2.sh basher.sh
To be clear, in our example $1 refers to basher.sh since that is the first argument after the basher2.sh command. You could add other files names after basher.sh on the command line but the script would ignore them. You should verify this statement.
There is more to writing scripts but this is probably enough to get you going. You can, of course, always google “bash script tutorial.”