OS Lab Ex 2
OS Lab Ex 2
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 Loop
Most languages have the concept of loops: If we want to repeat a task twenty times, we don’t want to have
to type in the code twenty times, with maybe a slight change each time. As a result, we have for, until and
while loops in the Bourne shell. This is somewhat fewer features than other languages, but nobody claimed
that shell programming has the power of C.
1
2
3 #!/bin/bash
4 for i in 1 2 3 4 5
5 do
6 echo "Welcome $i times"
7 done
8
9 −−−−−−−−−−−−−−−−−−−−−−−−−−
10
11 #!/bin/bash
12 for i in {1..5}
13 do
14 echo "Welcome $i times"
15 done
16
17 −−−−−−−−−−−−−−−−−−−−−−−−−−
18
19 #!/bin/bash
20 for i in {0..10..2}
21 do
22 echo "Welcome $i times"
23 done
24
25 −−−−−−−−−−−−−−−−−−−−−−−−−−
26
27 #!/bin/bash
28 for i in $(seq 1 2 20)
29 do
30 echo "Welcome $i times"
31 done
32
33 −−−−−−−−−−−−−−−−−−−−−−−−−−
34
35 #!/bin/bash
36 for (( c=1; c<=5; c++ ))
37 do
2.1.2 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the sum of odd and even numbers from a set of numbers.
2. Write a Shell program to find the smallest number from a set of numbers.
3. Write a Shell program to find the sum of all numbers between 50 and 100, which are divisible by 3 and
not divisible by 5.
4. Write a Shell program to find the second highest number from a set of numbers.
5. Write a Shell program to find the factorial of a number using for loop.
6. Write a Shell program to generate Fibonacci series.
1
2
3 #!/bin/bash
4 # set n to 1
5 n=1
6
7 # continue until $n equals 5
8 while [ $n −le 5 ]
9 do
10 echo "Welcome $n times."
11 n=$(( n+1 )) # increments $n
12 done
13 −−−−−−−−−−−−−−−−−−−−−−−−−−
14
15 #!/bin/bash
16 n=1
17 while (( $n <= 5 ))
18 do
19 echo "Welcome $n times."
20 n=$(( n+1 ))
21 done
22 −−−−−−−−−−−−−−−−−−−−−−−−−−
23
24 #!/bin/sh
25 INPUT_STRING=hello
26 while [ "$INPUT_STRING" != "bye" ]
27 do
28 echo "Please type something in (bye to quit)"
29 read INPUT_STRING
30 echo "You typed: $INPUT_STRING"
31 done
1
2
3 #!/bin/bash
4
5 counter=0
6
7 until [ $counter −gt 5 ]
8 do
9 echo Counter: $counter
10 ((counter++))
11 done
2.1.7 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the sum of odd digits and even digits from a number.
2.2 Functions
Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which
can then be called upon to perform their individual tasks when needed.
Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part
of modern object-oriented programming principles.
1
2 #!/bin/sh
3
4 # Define your function here
5 Hello () {
6 echo "Hello World"
7 }
8
9 # Invoke your function
10 Hello
2.2.2 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the largest number between two numbers using function.
2. Write a Shell program to find the sum of the numbers passed as parameters.
1 #! /bin/bash
2 # To declare static Array
3 arr=(prakhar ankit 1 rishabh manish abhinav)
4
5 # To print all elements of array
6 echo ${arr[@]} # prakhar ankit 1 rishabh manish abhinav
7 echo ${arr[*]} # prakhar ankit 1 rishabh manish abhinav
8 echo ${arr[@]:0} # prakhar ankit 1 rishabh manish abhinav
9 echo ${arr[*]:0} # prakhar ankit 1 rishabh manish abhinav
10
11 # To print first element
12 echo ${arr[0]} # prakhar
13 echo ${arr} # prakhar
14
15 # To print particular element
16 echo ${arr[3]} # rishabh
17 echo ${arr[1]} # ankit
18
19 # To print elements from a particular index
20 echo ${arr[@]:0} # prakhar ankit 1 rishabh manish abhinav
21 echo ${arr[@]:1} # ankit 1 rishabh manish abhinav
22 echo ${arr[@]:2} # 1 rishabh manish abhinav
23 echo ${arr[0]:1} # rakhar
24
25 # To print elements in range
26 echo ${arr[@]:1:4} # ankit 1 rishabh manish
27 echo ${arr[@]:2:3} # 1 rishabh manish
28 echo ${arr[0]:1:3} # rak
29
30 # Length of Particular element
31 echo ${#arr[0]} # 7
32 echo ${#arr} # 7
33
34 # Size of an Array
35 echo ${#arr[@]} # 6
36 echo ${#arr[*]} # 6
37
38 # Search in Array
39 echo ${arr[@]/*[aA]*/} # 1
40
41 # Replacing Substring Temporary
42 echo ${arr[@]//a/A} # prAkhAr Ankit 1 rishAbh mAnish AbhinAv
43 echo ${arr[@]} # prakhar ankit 1 rishabh manish abhinav
44 echo ${arr[0]//r/R} # pRakhaR
2.3.2 Input/Output
Output of the program is given below.
1
2 # !/bin/bash
3 #By Using while−loop
4 # To declare static Array
5 arr=(1 12 31 4 5)
6 i=0
7
8 # Loop upto size of array
9 # starting from index, i=0
10 while [ $i −lt ${#arr[@]} ]
11 do
12 # To print index, ith
13 # element
14 echo ${arr[$i]}
15
16 # Increment the i = i + 1
17 i=‘expr $i + 1‘
18 done
2.3.4 Input/Output
Output of the program is given below.
1
12
31
4
5
1
2 # !/bin/bash
3 #By Using for−loop
4 # To declare static Array
5 arr=(1 2 3 4 5)
6
7 # loops iterate through a
8 # set of values until the
9 # list (arr) is exhausted
10 for i in "${arr[@]}"
11 do
12 # access each element
13 # as $i
14 echo $i
15 done
2.3.6 Input/Output
Output of the program is given below.
1
2
3
4
5
2.3.7 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Shell program to find the sum of odd and even numbers.
Output:
5
6
4
8
Sample Input:
Enter the number: 148541547854
Output:
1 = 2 times
4 = 4 times
8 = 2 times
5 = 3 times
7 = 1 times
• Write a Shell program to find the 2nd highest and 3rd highest numbers from a set of numbers and sum of
them using array.
Sample Input:
Enter the number of elements: 5
Enter the number: 10
Enter the number: 21
Enter the number: 30
Enter the number: 17
Enter the number: 5
Output:
The sum of first and last element is: (21+17) = 38
• Write a Shell program to find the factorial of two different numbers and sum of the numbers using function.
Sample Input:
Factorial of 5 is 120
Factorial of 6 is 720
Output:
120 + 720 = 840
• Write a Shell program to find total number of alphabets, digits or special characters in a string.
Sample Input:
Today is 12 November.
Output:
Alphabets = 15
Digits = 2
Special characters = 4
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.