Lab Assessment
HIT172/2020/Asif_Karim/CDU/NT/AU
Shell Scripting
A shell is a command-line interpreter; it is basically a real programming
language, complete with variables, control structures, and so forth. No
matter how complicated a script gets, it is still just a list of commands
executed sequentially with the help of several programming constructs. A
shell script is a computer program designed to be run by the Unix/Linux
shell. To create and execute a shell script, follow the steps below:
Step 1
Create a file called ‘script1.sh’ in the desktop and open it with gedit (right
click > open with gedit), as shown in the image below:
Step 2
Open the file and at the very top add the line #!/bin/sh, though it’s a
comment, its important and signals to the system that the script should
always be run with bash (the default Bourne Shell) rather than another shell
(such as C Shell and Korn Shell). /bin/sh is the executable at bin (Binary)
directory representing the system shell (bash in this case).
Then type out the following small script:
echo “What is your name?”
read PERSON
echo “Hello, $PERSON”
HIT172/2020/Asif_Karim/CDU/NT/AU
Explanation: echo outputs the message on screen, while read accepts a
value and stores it in the variable called PERSON. Variables are placeholders
in the RAM for storing different values needed for the execution of the
program. The second echo shows the accepted name from the user. Note
that to use the variable PERSON, we have prefixed it with the ‘$’ sign.
Step 3
Execution: To execute the script, first change its permission and prefix the
filename with ‘./’ and press Enter, as shown in the following screenshot. You
have to set the permission only once. Ensure that you are in the Desktop.
The executed program should produce the following output:
_____________________________________________________________
We can set variables inside the script as well as shown in the following
screenshot (with output):
HIT172/2020/Asif_Karim/CDU/NT/AU
Carefully note that there should NOT be any whitespace on either side of ‘=’
while setting the variable in the code, check how Institution has been set.
Mathematical Operations
Execute the following program to see how simple math operations are done.
Note that, such basic operations are usually enclosed within 2 first brackets
and prefixed with ‘$’ before assigning the result to any variable; and multiple
math operations can be carried out in one single statement, with the
appropriate used of brackets as shown in the following screenshot:
Once you execute the program, following output should be produced:
_____________________________________________________________
Executing a series of Linux commands
Scripting is most useful when you can combine any number of valid
commands with different programming constructs. At this exercise we will
only see how we can execute multiple commands. Later on we will use
different programming structures such as loops and decisions with Linux
commands to gain more flexibility.
As you can see from the following screenshot that six (6) commands have
been used together to automate a task of creating a text file with a specific
text in a specific location.
First Line: Ensure the CD command to change the present working directory
to Desktop is done in accordance to you OWN system. For you it may be cd
/home/YOURNAME/Desktop
HIT172/2020/Asif_Karim/CDU/NT/AU
The script navigates to Desktop, creates a Folder named TestFolder, creates
a file named TestFile.txt inside that folder, changes permission, and writes
some text using the redirection operator ‘>’ to that file.
_____________________________________________________________
BASH Conditional Programming (HIT 172 Week 8)
if …. some condition ….
then
….. some statement ….
fi
----------------------------------------------
if …. some condition ….
then
….. some statement ….
else
….. some statement ….
fi
----------------------------------------------
if – then syntax
if – then – else syntax
HIT172/2020/Asif_Karim/CDU/NT/AU
if …. some condition ….
then
….. some statement ….
elif …. some condition ….
then
….. some statement ….
else
….. some statement ….
fi
----------------------------------------------
Numeric Comparison
---------
if – then – elseif - else syntax
HIT172/2020/Asif_Karim/CDU/NT/AU
Example:
BASH Conditional Programming - Loops (HIT 172 Week 9)
In computer programming, a loop is a sequence of instruction(s) that is continually
repeated until a certain condition is reached. Loops can greatly reduce the code length and
makes the program efficient and readable. There are three types of Loops in Bash:
• For Loop
• Until Loop
• While Loop
In this course we will only concentrate on While Loops. However, the other loops are just
close variations of it.
While Loop: The ‘While Loop’ consists of a block of code and a condition. The condition is
evaluated, and if the condition is true, then the code within the block is executed. This
repeats until the condition becomes false.
The body of the code (for true condition) is enclosed between the keyword do and done,
however, this can be differently written in other programming languages.
HIT172/2020/Asif_Karim/CDU/NT/AU
Following are two while loops to illustrate the concept (in one single program)
HIT172/2020/Asif_Karim/CDU/NT/AU
Multi Condition If-Else
An IF Statement may have ANY number of conditions associated with it. We
can combine ‘AND’ and ‘OR’ operator to write complex instructions. For an
‘OR’ operator, any of the two conditions on either side has to be TRUE to get a
TRUE outcome from overall if statement. For the ‘AND’ operator, both the
conditions need to be TRUE to get a TRUE outcome from overall if statement.
Note that these ‘AND’ and ‘OR’ operators can be combined as needed.
================