0% found this document useful (0 votes)
118 views

Scripting (Tkakabin)

The document provides instructions for creating and using basic shell scripts. It discusses creating a simple shell script called sample.sh that echoes text and runs the cal command. It then covers making the script executable and running it directly without needing to use bash. The document also demonstrates using conditional logic like if/else statements and while loops in shell scripts to check conditions and repeat execution. Basic shell scripting allows automating commands and routines by placing them in a file and running the file as a program.

Uploaded by

Tipia Kakabin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Scripting (Tkakabin)

The document provides instructions for creating and using basic shell scripts. It discusses creating a simple shell script called sample.sh that echoes text and runs the cal command. It then covers making the script executable and running it directly without needing to use bash. The document also demonstrates using conditional logic like if/else statements and while loops in shell scripts to check conditions and repeat execution. Basic shell scripting allows automating commands and routines by placing them in a file and running the file as a program.

Uploaded by

Tipia Kakabin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

9.

3 Basic Shell Scripting


Shell scripting allows you to take a complex sequence of commands, place them into a file and
then run the file as a program. This saves you the time of having to repeatedly type a long
sequence of commands that you routinely use.

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:

echo "Hello there! Here is the calendar for this month:"


cal
jharvard@appliance (-) :vi sample.sh
echo "Hello there! Here is the calendar for this month:"
cal
~
~

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.

Add the following to the bottom of the sample.sh file:

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.

Now execute the sample.sh 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

Your screen should look like the following:

jharvard@appliance (-) :sample.sh


-bash: sample.sh: command not found
sysadmin@localhost:~$

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

Begin by placing the following in drive.sh:


This column describes the code (don't enter into
Enter this column into drive.sh
the file)
#!/bin/bash
echo "Please enter your age" # print a prompt
read age # read user input and place in the $age variable
if test $age -lt 16
# test $age -lt 16 returns "true" if $age is
numerically less then 16
then
echo "You are not old enough to
drive." # executes when test is true
else
echo "You can drive!" # executes when test is false
fi # This ends the if statement

Then make the file executable and run it:

cat drive.sh
chmod a+x drive.sh
./drive.sh

Your screen should look like the following:

jharvard@appliance (-) :cat drive.sh


#!/bin/bash
echo "Please enter your age"
read age
if test $age -lt 16
then
echo "You are not old enough to drive."
else
echo "You can drive!"
fi
jharvard@appliance (-) :chmod a+x drive.sh
jharvard@appliance (-) :./drive.sh
Please enter your age
14
You are not old enough to drive.
sysadmin@localhost:~$
Verbally, you could read the if statement as "If $age is less than 16, then echo 'You are not old
enough to drive', else echo 'You can drive!'". The fi ends the if statement.

Note

$age must be an integer value. If not, the program will crash.

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 ]

Then run the program again:

cat drive.sh
./drive.sh

Your screen should look like the following:

jharvard@appliance (-) :cat drive.sh


#!/bin/bash
echo "Please enter your age"
read age
if [ $age -lt 16 ]
then
echo "You are not old enough to drive."
else
echo "You can drive!"
fi
jharvard@appliance (-) :./drive.sh
Please enter your age
21
You can drive!
sysadmin@localhost:~$

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.

create a file called check.sh:

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

Then run the following commands:


cat check.sh
chmod a+x check.sh
./check.sh

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:

jharvard@appliance (-) :cat 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
jharvard@appliance (-) :chmod a+x check.sh
jharvard@appliance (-) :./check.sh
Enter a username to check:
root
root is on this system
jharvard@appliance (-) :./check.sh
Enter a username to check:
bobby
bobby does not exist
sysadmin@localhost:~$
9.4.4 Step 4
Another common conditional statement is called the while loop. This statement is used to
execute code repeatedly as long as a conditional check returns "true". Begin by creating a file
called num.sh:

vi num.sh
jharvard@appliance (-) :vi num.sh

Next, place the following in a file named num.sh:

This column describes the code (don't enter


Enter this column into num.sh
into the file)
#!/bin/bash
echo "Please enter a number greater
than 100"
read num

while [ $num -le 100 ]


# Execute code from "do" to "done if test
condition is true
do
echo "$num is NOT greater than 100."
echo "Please enter a number greater
than 100"
read num
done # This ends the while statement
echo "Finally, $num is greater than
100"

Then make the file executable and run it:

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:

jharvard@appliance (-) :cat num.sh


#!/bin/bash
echo "Please enter a number greater than 100"
read num
while [ $num -le 100 ]
do
echo "$num is NOT greater than 100."
echo "Please enter a number greater than 100."
read num
done
echo "Finally, $num is greater than 100"
jharvard@appliance (-) :chmod a+x num.sh
jharvard@appliance (-) :./num.sh
Please enter a number greater than 100
25
25 is NOT greater than 100.
Please enter a number greater than 100.
99
99 is NOT greater than 100.
Please enter a number greater than 100.
101
Finally, 101 is greater than 100
sysadmin@localhost:~$

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:

for name in /etc/passwd /etc/hosts /etc/group


do
wc $name
done

Your screen should look like the following:

jharvard@appliance (-) :for name in /etc/passwd /etc/hosts /etc/group


> do
> wc $name
> done
24 30 1001 /etc/passwd
7 15 161 /etc/hosts
46 46 561 /etc/group
sysadmin@localhost:~$

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:~$

You might also like