OS Lab Ex 1
OS Lab Ex 1
2 Shell Scripting
A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The
various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell
scripts include file manipulation, program execution, and printing text. A script which sets up the environment,
runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.
2.1 Arithmetic
Doing arithmetic operations in bash is not similar to other standard programming languages. One of the
limitations of bash is that it can’t handle floating point or double numbers like other scripting languages.
2.1.1 Let
let is a builtin function of Bash that allows us to do simple arithmetic. It follows the basic format:
let <arithmetic expression>
1
2 #!/bin/bash
3 # Basic arithmetic using let
4
5 let a=5+4
6 echo $a # 9
7
8 let "a = 5 + 4"
9 echo $a # 9
10
11 let a++
12 echo $a # 10
13
14 let "a = 4 * 5"
15 echo $a # 20
16
17 let "a = $1 + 30"
18 echo $a # 45
2.1.3 Input/Output
Output of the program is given below.
./let_example.sh 15
9
9
10
20
45
1 #!/bin/bash
2 # Basic arithmetic using expr
3
4 expr 5 + 4 # 9
5
6 expr "5 + 4" # 5 + 4
7
8 expr 5+4 # 5+4
9
10 expr 5 \* $1 # 60
11
12 expr 11 % 2 # 1
13
14 a=$( expr 10 − 3 )
15 echo $a # 7
2.1.6 Input/Output
Output of the program is given below.
./expr_example.sh 12
9
5+4
5+4
60
1
7
1 #!/bin/bash
2 # Basic arithmetic using double parentheses
3
4 a=$(( 4 + 5 ))
5 echo $a # 9
6
7 a=$((3+5))
8 echo $a # 8
9
10 b=$(( a + 3 ))
11 echo $b # 11
2.1.9 Input/Output
Output of the program is given below.
./expansion_example.sh
9
8
11
12
13
16
20
1 #!/bin/bash
2 # Floating−point arithmetic
3 echo "scale = 5; 123.456789/345.345345" | bc # .35748
4
5 echo ’scale=4;20+5/2’ | bc # 22.5000
6
7 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
8 #Bash Shell Script to convert Celsius to Fahrenheit
9 #!/bin/bash
10 read −p "Enter degree celsius temperature: " celsius
11 fahrenheit=‘echo "scale=4; $celsius*1.8 + 32" | bc‘
12 echo "$celsius degree celsius is equal to $fahrenheit degree fahrenheit"
13
14 #37 degree celsius is equal to 98.6 degree fahrenheit
For using the bc utility, we need to configure a scale parameter. Scale is the number of significant digits to the
right of the decimal point.
1 #!/bin/bash
2 # Show the length of a variable.
3
4 a=’Hello World’
5 echo ${#a} # 11
6
7 b=4953
8 echo ${#b} # 4
2.1.14 Input/Output
Output of the program is given below.
1. ./length_example.sh
2. 11
3. 4
1 #!/bin/bash
2 # Take input from user and calculate sum.
3
4 read −p "Enter first number: " num1
5 read −p "Enter second number: " num2
6
7 sum=$(( $num1 + $num2 ))
8
9 echo "Sum is: $sum"
2.1.16 Input/Output
Output of the program is given below.
2.1.17 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the area and circumference of a circle.
1 #Extract substring of length $LEN from $STRING starting after position $POS.
Note that first position is 0.
2 STRING="this is a string"
3 POS=1
4 LEN=3
5 echo ${STRING:$POS:$LEN} # his
1 #! /bin/bash
2
3 var="Welcome to the geekstuff"
4
5 echo ${var:15} # geekstuff
6 echo ${var:15:4} # geek
• if statement
• if-else statement
• if..elif..else..fi statement (Else If ladder)
• if..then..else..if..then..fi..fi..(Nested if)
• switch statement
2.3.2 Input/Output
Output of the program is given below.
a is not equal to b
1 #!/bin/bash
2 # Basic if statement
3 if [ $1 −gt 100 ]
4 then
5 echo Hey that\’s a large number.
6
7 fi
8 date
./if_example.sh 15
Fri 2 Jul 15:38:39 2021 ./if_example.sh 150
Hey that’s a large number.
Fri 2 Jul 15:38:39 2021
2.3.6 Input/Output
Output of the program is given below.
a is not equal to b
1
2 #!/bin/sh
3
4 a=10
5 b=20
6
7 if [ $a == $b ]
8 then
9 echo "a is equal to b"
10 elif [ $a −gt $b ]
11 then
12 echo "a is greater than b"
13 elif [ $a −lt $b ]
14 then
15 echo "a is less than b"
16 else
17 echo "None of the condition met"
18 fi
a is less than b
1 CARS="bmw"
2
3 #Pass the variable in string
4 case "$CARS" in
5 #case 1
6 "mercedes") echo "Headquarters − Affalterbach, Germany" ;;
7
8 #case 2
9 "audi") echo "Headquarters − Ingolstadt, Germany" ;;
10
11 #case 3
12 "bmw") echo "Headquarters − Chennai, Tamil Nadu, India" ;;
13 esac
2.3.10 Input/Output
Output of the program is given below.
2.3.11 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the largest among three numbers.
2. Write a Shell program to display student grades using switch case.
1 #!/bin/bash
2
3 VAR1="Linuxize"
4 VAR2="Linuxize"
5
6 if [ "$VAR1" = "$VAR2" ]; then
7 echo "Strings are equal."
8 else
9 echo "Strings are not equal."
10 fi
2.4.2 Input/Output
Output of the program is given below.
1 #!/bin/bash
2
3 read −p "Enter first string: " VAR1
4 read −p "Enter second string: " VAR2
5
6 if [[ "$VAR1" == "$VAR2" ]]; then
7 echo "Strings are equal."
8 else
9 echo "Strings are not equal."
10 fi
2.4.4 Input/Output
Output of the program is given below.
2.4.7 Input/Output
Output of the program is given below.
String is empty.
String is not empty.
1
2
3 #!/bin/bash
4
5 VAR="Arch Linux"
6
7 case $VAR in
8
9 "Arch Linux")
10 echo −n "Linuxize matched"
11 ;;
12
13 Fedora | CentOS)
14 echo −n "Red Hat"
15 ;;
16 esac
2.4.9 Input/Output
Output of the program is given below.
Linuxize matched.
5 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.