The purpose of this lab is to familiarise you with the basics of Bourne shell scripting. There are various scripts included in this material which illustrate the points being made, and which you can copy into a file and run to see what they do. At the end of this file are some exercises which test your knowledge of shell scripting.
In shell scripting the Bourne shell (sh) and its extension the Bourne again shell (Bash) are very similar. On robin, type
sh or /bin/sh
to invoke the Bourne shell. (To invoke Bash, type bash)
You must have execute permission on this script in order to run it. Make the file executable by typing the following at the command prompt and hitting return:
chmod u+x hello.sh
You can then run the file as follows:
./hello.sh Hello World!
Command-line arguments and variables
The Command-line parameters provide scripts with a way to access data from the shell. These reserved parameter variables start with the name of the script, $0, and are assigned by position for each argument $i. The number of parameters is set in $#. The script below simply prints its first two arguments:
#!/bin/sh echo $1 echo $2
The example below shows how the programmer can define variables. The variable $string is assigned the value '' argument 1: '' and then concatenated to the command-line parameter $1. Copy the text into a file and call it hello2.sh:
#!/bin/sh string="the argument 1 is: " echo $string$1 #concatenate $string with $1
The script prints out its first command-line argument.
Try and find out what these variables do (precede with echo ):
$? Exit status or return code of last command $# Number of arguments $@ Argument 1 through to n with Input Field Separator $* "$1" $2" ... $n $! Process id of last background process $$ Process id of shell running this script $- The current shell flags
Write a shell script in the file myscript.sh that takes 2 or more arguments (call them ff1, ff2, ... ffn). Follow these guidelines:
If the file ff1 exists (test by -e), an error message is printed to STDERR.
If some files in ff2, ..., ffn are not regular files, then print an error message to STDERR at the end, displaying each missing file at once. For example, if files ff1 and ff2 are missing, print ``ERROR: missing files ff1, ff2''.
Otherwise, concatenate files ff2, ..., ffn using cat, and store the result in ff1
Run some test cases to be sure the solution is correct. Say we have files ff2:
If you attend all the labs
you will understand more about UNIX!
Then, ./myscript.sh ff1 ff2 ff3 should output the following into ff1:
File created on March 17, 3:23 PM by myscript.sh Author: uxx06m on robin ---------------------------------------------- If you attend all the labs you will understand more about UNIX! -----------------------------------------------
This section shows different test operations that can be done within IF statements in shell scripts.
“num1”, “num2”, “string” and “FILE” are numbers strings and files respectively:
string1 = string2 string 1 is equal to string 2 string1 !=string2 string 1 is not equal to string 2 -n string string is not zero length -z string string is zero length -d FILE FILE is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -r FILE FILE exists and is readable -s FILE FILE exists and has length greater than zero -w FILE FILE exists and is writable -x FILE FILE exists and is executable num1 -eq num2 num1 equal to num2 num1 -ne num2 num1 not equal to num2 num1 -lt num2 num1 less than num2 num1 -le num2 num1 less than or equal to num2 num1 -gt num2 num1 greater than num2 num1 -ge num2 num1 greater than or equal to num2