6-Shell Scripts Continuation-02-08-2024
6-Shell Scripts Continuation-02-08-2024
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.
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"
#!/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.
if [ expression ] statement1
statement2
if [ expression ] then elif [ expression2 ]
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
2. User Input: Three read commands prompt the user to enter three numbers and
store them in num1, num2, and num3 variables.
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.
Syntax:
#/bin/bash Example
<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
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.
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.