0% found this document useful (0 votes)
72 views12 pages

OS Lab Ex 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views12 pages

OS Lab Ex 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of

Computer Science and Engineering

Title: Shell Scripting- I

Operating System Lab


CSE 310

Green University of Bangladesh


1 Objective(s)
• To gather knowledge of shell program.

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>

2.1.2 Implementation in shell

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

© Dept. of Computer Science and Engineering, GUB


2.1.4 Expr
expr is similar to let except instead of saving the result to a variable it instead prints the answer. Unlike let you
don’t need to enclose the expression in quotes. You also must have spaces between the items of the expression.
It is also common to use expr within command substitution to save the output to a variable.
expr item1 operator item2

2.1.5 Implementation in shell

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

2.1.7 Double Parentheses


In the section on Variables we saw that we could save the output of a command easily to a variable. It turns
out that this mechanism is also able to do basic arithmetic for us if we tweak the syntax a little. We do so by
using double brackets like so:
$(( expression ))

2.1.8 Implementation in shell

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

© Dept. of Computer Science and Engineering, GUB


12
13 b=$(( $a + 4 ))
14 echo $b # 12
15
16 (( b++ ))
17 echo $b # 13
18
19 (( b += 3 ))
20 echo $b # 16
21
22 a=$(( 4 * 5 ))
23 echo $a # 20

2.1.9 Input/Output
Output of the program is given below.

./expansion_example.sh
9
8
11
12
13
16
20

2.1.10 Floating-point arithmetic


In Bash shell, we can only perform integer arithmetic. If we want to perform arithmetic involving a floating
point or fractional values, then we will need to use various other utilities, such as awk, bc, and similar.
Let’s see an example of using the utility called bc:

2.1.11 Implementation in shell

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.

2.1.12 Length of a Variable


This isn’t really arithmetic but it can be quite useful. If you want to find out the length of a variable (how
many characters) you can do the following:
$#variable

© Dept. of Computer Science and Engineering, GUB


2.1.13 Implementation in shell

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

2.1.15 Takes input from the user at run time

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.

Enter first number: 12


Enter second number: 15
Sum is: 27

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.

2. Write a Shell program to find the roots of a quadratic equation.


3. Write a Shell program to Find out the Area and Perimeter of Rectangle.
4. Write a Shell program to Find the Perimeter of a Circle, Rectangle and Triangle

© Dept. of Computer Science and Engineering, GUB


2.2 Basic String Operations
The shell allows some common string operations which can be very useful for script writing.
1 # 1234567890123456
2 STRING="this is a string"
3 echo ${#STRING} # 16

1 #Find the numerical position in $STRING of any single character in $SUBSTRING


that matches. Note that the ’expr’ command is used in this case.
2 STRING="this is a string"
3 SUBSTRING="hat"
4 expr index "$STRING" "$SUBSTRING" # 1 is the position of the first ’t’ in
$STRING

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

1 #If :$LEN is omitted, extract substring from $POS to end of line


2 STRING="this is a string"
3 echo ${STRING:1} # $STRING contents without leading character
4 echo ${STRING:12} # ring

1 #Replace first occurrence of substring with replacement


2 STRING="to be or not to be"
3 echo ${STRING[@]/be/eat} # to eat or not to be

1 #Replace all occurrences of substring


2 STRING="to be or not to be"
3 echo ${STRING[@]//be/eat} # to eat or not to eat

1 #Delete all occurrences of substring (replace with empty string)


2 STRING="to be or not to be"
3 echo ${STRING[@]// not/} # to be or to be

1 #Replace occurrence of substring if at the beginning of $STRING


2 STRING="to be or not to be"
3 echo ${STRING[@]/#to be/eat now} # eat now or not to be

1 #Replace occurrence of substring if at the end of $STRING


2 STRING="to be or not to be"
3 echo ${STRING[@]/%be/eat} # to be or not to eat

© Dept. of Computer Science and Engineering, GUB


2.2.1 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a shell Script to Concatenate Two Strings.

2.3 Conditional Statements


There are total 5 conditional statements which can be used in bash programming.

• 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.1 Implementation in shell

1 #Initializing two variables


2 a=10
3 b=20
4
5 #Check whether they are equal
6 if [ $a == $b ]
7 then
8 echo "a is equal to b"
9 fi
10
11 #Check whether they are not equal
12 if [ $a != $b ]
13 then
14 echo "a is not equal to b"
15 fi

2.3.2 Input/Output
Output of the program is given below.

a is not equal to b

2.3.3 Implementation in shell

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

© Dept. of Computer Science and Engineering, GUB


2.3.4 Input/Output
Output of the program is given below.

./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.5 Implementation in shell

1 #Initializing two variables


2 a=20
3 b=20
4
5 if [ $a == $b ]
6 then
7 #If they are equal then print this
8 echo "a is equal to b"
9 else
10 #else print this
11 echo "a is not equal to b"
12 fi

2.3.6 Input/Output
Output of the program is given below.

a is not equal to b

2.3.7 Implementation in shell

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

© Dept. of Computer Science and Engineering, GUB


2.3.8 Input/Output
Output of the program is given below.

a is less than b

2.3.9 Implementation in shell

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.

Headquarters - Chennai, Tamil Nadu, India.

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.

2.4 Compare Strings in Bash


2.4.1 Implementation in shell

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.

Strings are equal.

© Dept. of Computer Science and Engineering, GUB


2.4.3 Implementation in shell

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.

Enter first string: Linuxize


Enter second string: Ubuntu
Strings are not equal.

2.4.5 Implementation in shell

1 #There are multiple ways to check if a string contains a substring.


2
3 #One approach is to use surround the substring with asterisk symbols * which
means match all characters.
4 #!/bin/bash
5
6 VAR=’GNU/Linux is an operating system’
7 if [[ $VAR == *"Linux"* ]]; then
8 echo "It’s there."
9 fi
10
11 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
12 #!/bin/bash
13
14 strval="Microsoft Internet Explorer"
15
16 if [[ $strval == *Internet* ]];
17 then
18 echo "Partially Match"
19 else
20 echo "No Match"
21 fi
22
23 if [[ $strval == *internet* ]];
24 then
25 echo "Partially Match"
26 else
27 echo "No Match"
28 fi

2.4.6 Implementation in shell

© Dept. of Computer Science and Engineering, GUB


1 #Check if a String is Empty
2 #Quite often you will also need to check whether a variable is an empty string
or not. You can do this by using the −n and −z operators.
3
4 #!/bin/bash
5
6 VAR=’’
7 if [[ −z $VAR ]]; then
8 echo "String is empty."
9 fi
10
11 #!/bin/bash
12
13 VAR=’Linuxize’
14 if [[ −n $VAR ]]; then
15 echo "String is not empty."
16 fi

2.4.7 Input/Output
Output of the program is given below.

String is empty.
String is not empty.

2.4.8 Implementation in shell

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.

© Dept. of Computer Science and Engineering, GUB


3 Discussion & Conclusion
Based on the focused objective(s) to understand about the shell program, the additional lab exercise made me
more confident towards the fulfilment of the objectives(s).

4 Lab Exercise (Submit as a report)


• Write a Shell program to find the sum of odd and even numbers from a set of numbers.
• Write a Shell program to Check Triangle is Valid or Not

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.

© Dept. of Computer Science and Engineering, GUB

You might also like