Scripting (Tkakabin)
Scripting (Tkakabin)
This lab will focus on how to create simple shell scripts. For the purpose of this lab, it is assumed
that you know how to use a text editor. Feel free to use the editor of your choice: vi, nano,
gedit or any other editor that you like.
9.3.1 Step 1
To create a simple shell script, you just need to create a text file and add commands. Create a file
called sample.sh and add the following lines:
9.3.2 Step 2
To make it clear that this is a BASH shell script, you need to include a special line at the top of
the file called a "shbang" (or "shebang"). This line starts with #! and then contains the path to the
BASH shell executable. Add the following line at the top of the sample.sh file:
#!/bin/bash
#!/bin/bash
echo "Hello there! Here is the calendar for this month:"
cal
~
~
9.3.3 Step 3
One way that you can run this program is by typing bash before the filename. Execute the
following:
bash sample.sh
jharvard@appliance (-) :bash sample.sh
Hello there! Here is the calendar for this month:
December 2018
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 9 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
sysadmin@localhost:~$
9.3.4 Step 4
You can avoid having to type bash in front of the filename by making the file "executable" for all
users. Run the following commands:
ls -l sample.sh
chmod a+x sample.sh
ls -l sample.sh
./sample.sh
jharvard@appliance (-) :ls -l sample.sh
-rw-rw-r-- 1 sysadmin sysadmin 73 Dec 9 14:46 sample.sh
jharvard@appliance (-) :chmod a+x sample.sh
jharvard@appliance (-) :./sample.sh
Hello there! Here is the calendar for this month:
December 2018
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 9 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
sysadmin@localhost:~$
The chmod command is used to change permissions on the file so that the file can be executed.
9.3.5 Step 5
A common feature used in scripting is "backquoting". With this technique, you can run a shell
command "within" another shell command. The outcome of the internal command will be
returned as an argument to the external command.
vi sample.sh
jharvard@appliance (-) :vi sample.sh
echo "Today is" `date +%A`
#!/bin/bash
echo "Hello there! Here is the calendar for this month:"
cal
echo "Today is" `date +%A`_
~
~
Exit insert mode by pressing Esc, then type :wq! and hit Enter to save and exit the file.
cat sample.sh
./sample.sh
jharvard@appliance (-) :cat sample.sh
#!/bin/bash
echo "Hello there! Here is the calendar for this month:"
cal
echo "Today is" `date +%A`
jharvard@appliance (-) :./sample.sh
Hello there! Here is the calendar for this month:
December 2018
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 9 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Today is Tuesday
sysadmin@localhost:~$
9.3.6 Step 6
You have been using ./ in front of the sample.sh filename to indicate that the file is in the
current directory. Execute the following to see how the shell would fail to find the file if you
don't use the ./:
sample.sh
9.3.7 Step 7
Recall that the $PATH variable is used to search for commands that you type. Execute the
following to see the $PATH variable for the sysadmin account:
echo $PATH
jharvard@appliance (-) :echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/b
in:/usr/games
sysadmin@localhost:~$
9.3.8 Step 8
Note that /home/sysadmin/bin is one of the directories in the $PATH variable. This is a great
place to put your shell scripts:
mkdir bin
mv sample.sh bin
sample.sh
jharvard@appliance (-) :mkdir bin
jharvard@appliance (-) :mv sample.sh bin
jharvard@appliance (-) :sample.sh
Hello there! Here is the calendar for this month:
December 2018
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 9 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Today is Tuesday
sysadmin@localhost:~$
9.4 Conditional and Repetitive Execution
Note that in this section examples that are more complex will be demonstrated. When doing so,
you will be using a technique to describe what is happening in the program. The technique will
look like the following:
Enter this column into drive.sh This column describes the code (don't enter into the file)
echo "Please enter your age" # print a prompt
read age # read user input and place in $age variable
When following the instructions provided, you are to enter the text from the left column into the
specified file (drive.sh in the example above). The right column is used to describe specific
lines in the program. The pound (hash) sign # character is used because in a shell script you can
place comments within your program by using a # character.
9.4.1 Step 1
Scripts that are more complex may make use of conditional execution. A conditional expression,
like the if statement, can make use of the outcome of a command called test. The test
statement compares two numbers (or two strings) for things like "equal to", "less than", etc.
Create the following file (drive.sh) and make it executable to see how the if and test
statements work.
vi drive.sh
jharvard@appliance (-) :vi drive.sh
cat drive.sh
chmod a+x drive.sh
./drive.sh
Note
9.4.2 Step 2
The test statement is automatically called when you place its arguments within square brackets [
] surrounded by spaces. Modify the if line of drive.sh so it looks like the following:
if [ $age -lt 16 ]
cat drive.sh
./drive.sh
To see a full list of test conditions, run the command man test.
Important
There must be spaces around the square brackets. [$age -lt 16] would fail, but [ $age -lt
16 ] would work.
9.4.3 Step 3
You can also use the outcome of other shell commands as they all return "success" or "failure".
For example, create and run the following program, which can be used to determine if a user
account is on this system.
vi check.sh
jharvard@appliance (-) :vi check.sh
Add the following to check.sh:
#!/bin/bash
echo "Enter a username to check: "
read name
if grep $name /etc/passwd > /dev/null
then
echo "$name is on this system"
else
echo "$name does not exist"
fi
When prompted for a username, give the value of "root". Execute the command again
(./check.sh) and provide the value of "bobby". Your screen should look like the following:
vi num.sh
jharvard@appliance (-) :vi num.sh
cat num.sh
chmod a+x num.sh
./num.sh
When prompted for a number, enter 25. When prompted again, enter 99. Finally, enter 101 when
prompted for a number the third time. Your screen should look like the following:
If the conditional check for the while statement ( [ $num -le 100 ] ) returns true, then the
statements between do and done are executed.
Once those statements have completed executing, the conditional check for the while statement
is checked again. If true again, then again the statements between do and done are executed.
This will continue repeating until the while condition returns false, or when the value is greater
than 100.
9.4.5 Step 5
Scripting code is part of the BASH shell, which means you can use these statements on the
command line just like you use them in a shell script. This can be useful for a statement like the
for statement, a statement that will assign a list of values one at a time to a variable. This allows
you to perform a set of operations on each value. For example, run the following on the
command line:
Note that the wc command was run three times: once for /etc/passwd, once for /etc/hosts and
once for /etc/group.
9.4.6 Step 6
Often the seq command is used in conjunction with the for statement. The seq command can
generate a list of integer values, for instance from 1 to 10. For example, run the following on the
command line to create 12 files named test1, test2, test3, etc. (up to test12):
ls
for num in `seq 1 12`
do
touch test$num
done
ls
jharvard@appliance (-) :ls
Desktop Downloads Pictures Templates check.sh num.sh
Documents Music Public Videos drive.sh
jharvard@appliance (-) :for num in `seq 1 12`
> do
> touch test$num
> done
jharvard@appliance (-) :ls
Desktop Music Templates drive.sh test10 test2 test5 test8 Documents
Pictures Videos num.sh test9 test3 test6 test9 Downloads Public
check.sh test1 test12 test4 test7
sysadmin@localhost:~$