Operating System
BCSE303P Operating Systems Lab
Laboratory Exercise 2
Shell Scripting
In Unix, the shell is a program that interprets commands
and acts as an intermediary between the user and the
inner workings of the operating system.
Shell scripting refers to writing computer programs
designed to be run by a Unix shell, which is a command-
line interpreter.
These scripts, typically plain text files containing a list of
commands, are interpreted and executed by the shell.
Why Shell Scripting?
Shell scripting is primarily used to automate repetitive
system tasks, such as backing up files, monitoring
system resources, and managing user accounts.
By turning a series of commands into a script, system
administrators can save time, increase accuracy, and
simplify complex tasks.
It is flexible, powerful, and provides widespread support
to operating systems
Shell Scripting – Structure of Program
Shebang Line (Optional)
This is the first line, starting with #! (shebang) followed
by the path to the interpreter that should be used to
execute the script.
For example: #!/bin/bash specifies the bash interpreter.
If omitted, the default shell on the system is used.
Shell Scripting – Structure of Program
Comments
Comments, indicated by #, are lines ignored by the
interpreter and used to explain the code's functionality.
They are helpful for documenting your script and making
it easier to understand.
Shell Scripting – Sample Program
echo "What is your name?"
read PERSON
echo "Hello, $PERSON“
Output
Shell Scripting – Structure of Program
Variables:
Variables store data that can be used throughout the script.
They are assigned values using the = operator.
A variable name could contain any alphabet (a-z, A-Z), any
digits (0-9), and an underscore ( _ ). However, a variable name
must start with an alphabet or underscore.
Shell scripts support various variable types, including strings,
numbers, and arrays.
Shell Scripting – Variables
Accessing Variables: Variable data could be accessed by
appending the variable name with ‘$’
#!/bin/bash
a=“Good"
b=“Morning"
echo "$a $b"
Shell Scripting – Structure of Program
Unsetting Variables: Variable data could be accessed by appending the
variable name with ‘$’
#!/bin/bash
a=“CSE"
b=23
echo $a $a
unset a
echo $a $a
Shell Scripting – Structure of Program
Control Flow Statements:
These statements control the execution flow of the script based on
conditions.
Common control flow statements include:
if...then...else for conditional execution
for loops for repetitive tasks based on a counter
while loops for repetitive tasks based on a condition
case statements for multi-way branching
Shell Scripting – if…then…else
if [ expression1 ]
Syntax
then
if [ expression ] statement1
statement2
if [ expression ] then elif [ expression2 ]
then statement1 then
statement3
statement else statement4
else
fi statement2
statement5
fi fi
if..elif..else..fi statement (Else If ladder)
if-else statement
Shell Scripting – Nested if
if [ expression1 ]
Syntax if..then..else..if..then..fi..fi..(Nested if)
then
statement1
statement2
else
if [ expression2 ]
then
statement3
fi
fi
Shell Scripting – if…then…else – Example 1
Write a shell script that compares three numbers and
displays the largest using if-then-else statements
Shell Scripting – if…then…else – Example 1
1. Shebang Line: #!/bin/bash specifies the bash interpreter.
2. User Input: Three read commands prompt the user to enter three numbers and
store them in num1, num2, and num3 variables.
3. Nested if-then-else: This block determines the largest number:
1. The first if condition checks if num1 is greater than or equal to both num2 and num3 (-ge
for greater than or equal). If true, num1 is the largest and stored in the largest variable.
2. The second elif condition checks if num2 is greater than or equal to both num1 and num3.
If true, num2 is the largest.
3. The final else block applies if neither of the above conditions is true, meaning num3 must be
the largest.
4. Display Output: Finally, echo displays a message indicating the largest number
stored in the largest variable.
Shell Scripting – Example
Shell Scripting – case
A case statement in bash scripts is used when a decision has
to be made against multiple choices.
It is useful when an expression has the possibility to have
multiple values.
Shell Scripting – case
Syntax:
Shell Scripting – case
Example:
Shell Scripting – For Loop
The for loop operates on lists of items. It repeats a set of commands for every
item in a list.
Syntax:
#/bin/bash Example
for <var> in <value1 value2 ... valuen> #!/bin/bash
do for i in 1 2 3 4 5 do
<command 1> echo "Welcome $i times“
<command 2> done
<etc>
done
Shell Scripting – For Loop
The for loop operates on lists of items. It repeats a set of
commands for every item in a list.
Syntax: Example:
Shell Scripting – For Loop
An Example: Demonstrating a for loop that prints
numbers from 1 to 5
#!/bin/bash
#Loop iterates 5 times
for (( i=1; i<=5; i++ )); do
echo "Number: $i“
done
Shell Scripting – Nested for loop
Demonstrating a nested for loop that creates a simple
multiplication table:
Shell Scripting – for loop – Example 2
1. Shebang Line: #!/bin/bash specifies the bash interpreter.
2. for Loop: for (( i=1; i<=5; i++ )) defines the loop structure:
1. i=1 initializes the counter variable i to 1.
2. i<=5 sets the loop condition to continue as long as i is less than or equal to 5.
3. i++ increments i by 1 after each iteration.
3. Loop Body: echo "Number: $i" is indented within the loop
and gets executed for each iteration. It simply prints
"Number:" followed by the current value of i.
Shell Scripting – Nested for loop – Example
3
1. Table Size: size=5 defines the size of the multiplication table (number of rows and
columns). Adjust this value as needed.
2. Outer Loop (Rows): for (( i=1; i<=$size; i++ )) iterates for each row of the table
(multiplicands).
3. Inner Loop (Columns): for (( j=1; j<=$size; j++ )) iterates for each column within
the current row (multipliers).
4. Multiplication: product=$(( i * j )) calculates the product of the current row (i) and
column (j) values.
5. Print Product: echo -n "$product " prints the calculated product followed by a
space without a newline (to keep all products in a row).
6. Newline: echo adds a newline character after each row for proper formatting.
Shell Scripting – while do
A while loop is a statement that iterates over a block of code till
the condition specified is evaluated to false.
Syntax: Example:
while [ condition ];
do
# statements
# commands
done
Relative Operators
Lab Assignment
1. Write a shell script using case to create a simple arithmetic
calculator.