Shell Lab 2008
Shell Lab 2008
Purpose
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. You will notice that some of the points covered here were described in the lecture. However, there are additional elements to these shell scripts which werent covered in the lecture and which are introduced in this lab.
Exercise 1:
The following script demonstrates: Automatic variable assignment into variables $1, $2, etc. The use of an if statement The use of read to read from stdin. Variable assignment using read
#!/bin/sh if [ $1 != prompt ] then echo the second argument was $2 else echo -n please enter at least two words: read input1 input2 junk echo the second word was $input2 fi
Create a directory called ShellLab in your home directory. Change into this directory and create a new file called script1.sh using Emacs. Copy and paste this script into it. Before running this script you should make the file executable using:
chmod u+x script1.sh
1. 2.
What does it do? What happens if you run this script with arguments that do not include the word prompt? 3. What happens if you run this script with the first argument as prompt followed by other arguments? 4. What happens if you run this script without any arguments? 5. Change the script so that the second occurrence of echo does not use the n flag. Run your script. What happens?
Notice the use of $1 and $2 to access the first and second arguments given on the command line. 7. Notice when read is used that it reads into variables input1 and input2 and, potentially, junk. Alter your script so that it prints out all three of these variables after using read. Put each of your echo statements on a separate line. In other words, your script should print out the values stored in input1, input2 and junk. Run your script with the argument prompt and then, when prompted, type in a long sentence. Notice the contents of each of the three variables. 6. 8. Change your script to remove the double quotes from around $1. Run the test cases 2-4 shown above again. You will notice that for case 4 you get an error message which says something like:
./script1.sh: test: argument expected
This is because without the use of double quotes: If you give at least one argument to the script (say you pass the script the string firstarg) then $1 has a value and so the test statement within the script will be: [ firstarg != prompt ] If you dont give at least one argument to the script then without double quotes the test statement within the script will be: [ != prompt ] this causes an error because test expects 3 arguments, two arguments to be compared and the comparison operator in the middle. If you dont give at least one argument to the script but do use double quotes then the test statement within the script will be: [ NULL != prompt ] in other words, test will compare against a NULL string and so the test statement will be happy! The point behind this is that it is good practice to put double quotes around user input that is being processed because you cant guarantee what the user input will be. Another example where double quotes will catch a problem is if the user enters an argument with a space, for example: ./script1.sh hello world hi
In this case the script would pass hello world as one argument to test, but test would then strip the from this and would perform the test: [ hello world != prompt ] resulting in test having 4 arguments! This, again, would give an error.
Exercise 2:
The following script is an extension of the script in exercise 1 but it includes some error checking. It demonstrates: The use of $# to access the number of arguments provided to the script on the command line The use of ne to test whether the number of arguments is NOT equal to 2 The use of exit to exit a scripts execution early
#!/bin/sh if [ "$#" -ne "2" ] then echo "you must provide 2 arguments" exit fi if [ "$1" != "prompt" ] then echo "the second argument was $2" else echo -n "please enter at least two words: " read input1 input2 junk echo "the second word was $input2" fi
Create a new file called script2.sh. Copy and paste this script into it. Make sure you have executable permissions on this script and run it as follows:
./script2.sh prompt
1. 2.
What happens? What happens if you run this script with 3 or more arguments, for example:
./script2.sh hello world how are you
3.
4.
Change your script so that it expects 2 or more arguments (you may wish to refer to the lecture notes). Run your script and check it with 1, 2 and 3 arguments.
Exercise 3:
The following script demonstrates: Explicit assignment of values to variables within the script The use of a for loop The use of set The use of expr to evaluate an expression (note the use of back primes!)
#!/bin/sh TEST_ARGS="apple pear pomegranate kiwi banana 1 2 3 4 5 6 7 8 9 0" set $TEST_ARGS counter="1" for i in $* do echo "argument number $counter is " $i counter=`expr $counter + 1` done
Create a new file called script3.sh and copy the script above into it. Run this script without any arguments. 1. 2. What does this script do? set $TEST_ARGS expands $TEST_ARGS as if each of the items were given on the command line. It therefore fills $1, $2, $3, etc with the corresponding item and also $# with the value of the total number of arguments. Notice the use of $* in the for loop to access each of these arguments in turn. 3. Comment out the line which assigns TEST_ARGS and the line which uses set (lines 3 and 4). Add in a line before the for loop which says:
echo $12
4.
Run the script with 12 arguments. Youll notice that the script is unable to access the 12th argument in this way. What does the script actually do in this case? 5. Alter your script to try printing out, for example, the 11th argument, the 10th argument, the 9th argument. What is the maximum number of arguments that the script can access using this method?
Exercise 4:
The following script is an adaptation of the script in exercise 3. It demonstrates: The use of a while loop The use of shift
#!/bin/sh TEST_ARGS="apple pear pomegranate kiwi banana 1 2 3 4 5 6 7 8 9 0" set $TEST_ARGS counter="1" while [ "$#" -gt 0 ] do echo "argument number $counter is " $1 shift counter=`expr $counter + 1` done
Create a new file called script3.sh and copy the script above into it. Run this script without any arguments. 1. You should notice that this script performs in the same way as the script used in exercise 3. 2. shift is a command which moves the command line arguments along by one, so after the first loop $1 is discarded and $2 to $n+1 are moved down to $1 and $n respectively, and so on Comment out the line which contains the shift command. Run your script without any arguments, as before. What happens? NOTE: you may want to use Cntrl-C to stop the execution of the script! 3. Remove the comment so that the shift command is operational again. Change your script so that the echo statement uses single quotes rather than double quotes. Run your script. What do you notice about the output?
Additional Material/References
If you want further practice, a very good tutorial which teaches shell scripting is Steve Bournes original Bash shell scripting tutorial can be found at: http://steve-parker.org/sh/sh.shtml