| Name: _____________________ | Class: CET 421 |
| SSN/ID: _____________________ | Section & Group: ____________ |
What is shell scripting?
Now that you're familiar with all the basic unix commands it's time to learn how to piece these together into scripts to save typing the same sequences of commands over and over again. Scripts allow us to string a number of commands together, save them in a file, and then run it whenever we want, or even set it to run automatically on a regular basis or at a particular time.
Note: It's important to have the exact syntax of the scripts right, including spaces as they are shown below. If you don't, your scripts may not run correctly.
A shell is an application program that interprets the commands that a user types and executes programs. There are several shell programs: Bourne shell (sh, bsh), C-Shell (csh), Korn-shell (ksh), T-CShell (tcsh), and Bash (Bourne-again sh). The most standard one is sh and this is the one that you will use in this lab to write shell scripts. For reference, you can check out these great notes on shell programming or also this reference on shell scripting.
A Simple Script: "hello_script.sh"
Now, let's try to write, save, and execute a simple script that does nothing useful but just prints a line of text. This is just to show you how to run a script.
We are going to use the emacs editor you learned about last week to create a new file called hello_script.sh where the script will be written. You might want to open last week's lab in another browser window so you can have the emacs commands handy while you work through this one.
Now, start the script by typing emacs hello_script.sh & at the prompt. Into the new file, type the following:
#!/bin/sh
######################################################################
#
# First shell script
#
######################################################################
clear
echo "Hello World, this is my first script"
Then, save the file and exit emacs. The first line tells it where the
shell is located and that this is a shell script. The next few lines
are comments as they start with the # symbol. Any lines
starting with # are ignored when the script is run and are
put in the program only to be read by anyone reading the script. The
first un-commented line of the script clears the screen and the next
line echos a line of text to the screen. These are both commands we've
seen in previous labs.
Now, before the script can be run, we must change the permissions on the file so that it's executable by typing chmod a+x hello_script.sh at the prompt.
Now you can run the script using ./hello_script.sh.
A more useful script: getinfo.sh
Once the hello_script is working okay, try creating a more complicated one by using emacs to create a file called getinfo.sh and save the following lines in the file:
#!/bin/sh
######################################################################
#
# Script that prints who currently is logged in & the current date & time
#
######################################################################
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0
Now change the permissions on the getinfo.sh script the same as you
did with hello_script and then run it. Look at what's displayed on the
screen and try to understand which lines of the script above caused
which bits of the output.
Note: if you use bash, you'll have to change the \c with echo -n to ensure the newline isn't entered.
Variables
In the getinfo.sh script, you may have noticed the $USER term. This is a variable as it has a $ before its name. Just like other programming languages, variables can store a piece of data which can be manipulated.
There are two types of variables: System Variables & User
Variables. System variables are created and maintained by Unix
itself and are defined in CAPITAL LETTERS. User variables are created
by users such as yourself and should be named using lowercase letters.
A script that adds two numbers passed as arguements
Now to demostrate using variables here is a script that takes in 2 numbers and prints the answer.
#!/usr/bin/csh
######################################################################
#
# Variables demo script
#
######################################################################
set sum = 0
set num1=$argv[1]
set num2=$argv[2]
@ sum = $num1 + $num2
echo "You entered $num1 and $num2"
echo " The sum of the two numbers is $sum"
Save this in a file called add.sh.
Notes:
Three variables are used in this program - sum, num1 & num2. These can be created and set to a value using set before them. The value of a shell variable can be referenced by placing a $ before the name of the variable.
The @ sign used before the sum variable indicates a mathamatical operation on it (here we are adding the two numbers). The line #!/usr/bin/csh is very important as it tells Unix to use the C shell (csh) to interpret the script. There are a few different types of shell for Unix (e.g., bash) and some use different syntax in scripts.
Also used in this example are $argv[1] and $argv[2], which represent the first two arguments passed when the program is run; so, to run this correctly, we need to pass two numbers to the script. This is done by entering them after the name of the script, separated by spaces, when you are running the script. E.g.,
% ./add.sh 5 7
This, for example, runs the script and passes 5 and
7 as the arguements. Use whatever numbers you want and see if
the total is correct.
Hand In: This lab handout with the answers filled in attached
to a listing of your final programs
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename>. You can also use a2ps as in: a2ps <filename>).