0% found this document useful (0 votes)
1 views10 pages

OS Code

The document outlines a series of shell scripting programs designed for an Operating System Lab, authored by Mejbah Ahammad. It includes 20 distinct programs covering various topics such as basic arithmetic, string manipulation, and file handling. Each program is accompanied by its corresponding code and functionality description.

Uploaded by

ahanafshovon22
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)
1 views10 pages

OS Code

The document outlines a series of shell scripting programs designed for an Operating System Lab, authored by Mejbah Ahammad. It includes 20 distinct programs covering various topics such as basic arithmetic, string manipulation, and file handling. Each program is accompanied by its corresponding code and functionality description.

Uploaded by

ahanafshovon22
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

Operating System Lab Practice

Shell Scripting Programs


Mejbah Ahammad
May 11, 2026

Contents
1 Program 1: Hello World 3

2 Program 2: Addition of Two Numbers 3

3 Program 3: Even or Odd Number 3

4 Program 4: Factorial of a Number 3

5 Program 5: Fibonacci Series 4

6 Program 6: Prime Number Check 4

7 Program 7: Largest of Three Numbers 5

8 Program 8: Palindrome Number 5

9 Program 9: Reverse a Number 6

10 Program 10: Sum of Digits 6

11 Program 11: Multiplication Table 7

12 Program 12: Armstrong Number 7

13 Program 13: Swap Two Numbers 8

14 Program 14: Check Leap Year 8

15 Program 15: Simple Calculator 8

16 Program 16: Count Characters in a String 9

17 Program 17: File Existence Check 9

18 Program 18: Count Lines, Words and Characters 10

1
19 Program 19: Display Current Date and Time 10

20 Program 20: Student Grade Calculation 10

2
1 Program 1: Hello World
Code

1 # !/ bin / bash
2

3 echo " Hello World "

2 Program 2: Addition of Two Numbers


Code

1 # !/ bin / bash
2

3 echo " Enter first number : "


4 read a
5

6 echo " Enter second number : "


7 read b
8

9 sum = $ (( a + b ) )
10

11 echo " Sum = $sum "

3 Program 3: Even or Odd Number


Code

1 # !/ bin / bash
2

3 echo " Enter a number : "


4 read n
5

6 if [ $ (( n %2) ) - eq 0 ]
7 then
8 echo " Even Number "
9 else
10 echo " Odd Number "
11 fi

4 Program 4: Factorial of a Number


Code

3
1 # !/ bin / bash
2

3 echo " Enter a number : "


4 read n
5

6 fact =1
7

8 for (( i =1; i <= n ; i ++ ) )


9 do
10 fact = $ (( fact * i ) )
11 done
12

13 echo " Factorial = $fact "

5 Program 5: Fibonacci Series


Code

1 # !/ bin / bash
2

3 echo " Enter limit : "


4 read n
5

6 a =0
7 b =1
8

9 echo " Fibonacci Series : "


10

11 for (( i =0; i < n ; i ++ ) )


12 do
13 echo -n " $a "
14

15 fn = $ (( a + b ) )
16 a = $b
17 b = $fn
18 done

6 Program 6: Prime Number Check


Code

1 # !/ bin / bash
2

3 echo " Enter a number : "


4 read n
5

6 flag =0
7

4
8 for (( i =2; i <= n /2; i ++ ) )
9 do
10 if [ $ (( n % i ) ) - eq 0 ]
11 then
12 flag =1
13 break
14 fi
15 done
16

17 if [ $flag - eq 0 ]
18 then
19 echo " Prime Number "
20 else
21 echo " Not Prime Number "
22 fi

7 Program 7: Largest of Three Numbers


Code

1 # !/ bin / bash
2

3 echo " Enter three numbers : "


4 read a b c
5

6 if [ $a - ge $b ] && [ $a - ge $c ]
7 then
8 echo " $a is largest "
9 elif [ $b - ge $a ] && [ $b - ge $c ]
10 then
11 echo " $b is largest "
12 else
13 echo " $c is largest "
14 fi

8 Program 8: Palindrome Number


Code

1 # !/ bin / bash
2

3 echo " Enter a number : "


4 read n
5

6 rev =0
7 temp = $n
8

9 while [ $temp - gt 0 ]

5
10 do
11 rem = $ (( temp %10) )
12 rev = $ (( rev *10+ rem ) )
13 temp = $ (( temp /10) )
14 done
15

16 if [ $n - eq $rev ]
17 then
18 echo " Palindrome "
19 else
20 echo " Not Palindrome "
21 fi

9 Program 9: Reverse a Number


Code

1 # !/ bin / bash
2

3 echo " Enter number : "


4 read n
5

6 rev =0
7

8 while [ $n - gt 0 ]
9 do
10 rem = $ (( n %10) )
11 rev = $ (( rev *10+ rem ) )
12 n = $ (( n /10) )
13 done
14

15 echo " Reverse = $rev "

10 Program 10: Sum of Digits


Code

1 # !/ bin / bash
2

3 echo " Enter number : "


4 read n
5

6 sum =0
7

8 while [ $n - gt 0 ]
9 do
10 rem = $ (( n %10) )
11 sum = $ (( sum + rem ) )

6
12 n = $ (( n /10) )
13 done
14

15 echo " Sum of digits = $sum "

11 Program 11: Multiplication Table


Code

1 # !/ bin / bash
2

3 echo " Enter a number : "


4 read n
5

6 for (( i =1; i <=10; i ++ ) )


7 do
8 echo " $n x $i = $ (( n * i ) ) "
9 done

12 Program 12: Armstrong Number


Code

1 # !/ bin / bash
2

3 echo " Enter number : "


4 read n
5

6 temp = $n
7 sum =0
8

9 while [ $temp - gt 0 ]
10 do
11 rem = $ (( temp %10) )
12 sum = $ (( sum + rem * rem * rem ) )
13 temp = $ (( temp /10) )
14 done
15

16 if [ $n - eq $sum ]
17 then
18 echo " Armstrong Number "
19 else
20 echo " Not Armstrong Number "
21 fi

7
13 Program 13: Swap Two Numbers
Code

1 # !/ bin / bash
2

3 echo " Enter first number : "


4 read a
5

6 echo " Enter second number : "


7 read b
8

9 temp = $a
10 a = $b
11 b = $temp
12

13 echo " After swapping : "


14 echo " a = $a "
15 echo " b = $b "

14 Program 14: Check Leap Year


Code

1 # !/ bin / bash
2

3 echo " Enter year : "


4 read year
5

6 if [ $ (( year %400) ) - eq 0 ] ||
7 { [ $ (( year %4) ) - eq 0 ] && [ $ (( year %100) ) - ne 0 ]; }
8 then
9 echo " Leap Year "
10 else
11 echo " Not Leap Year "
12 fi

15 Program 15: Simple Calculator


Code

1 # !/ bin / bash
2

3 echo " Enter two numbers : "


4 read a b
5

6 echo " 1. Addition "

8
7 echo " 2. Subtraction "
8 echo " 3. Multiplication "
9 echo " 4. Division "
10

11 echo " Choose option : "


12 read ch
13

14 case $ch in
15 1) echo " Result = $ (( a + b ) ) " ;;
16 2) echo " Result = $ (( a - b ) ) " ;;
17 3) echo " Result = $ (( a * b ) ) " ;;
18 4) echo " Result = $ (( a / b ) ) " ;;
19 *) echo " Invalid Choice " ;;
20 esac

16 Program 16: Count Characters in a String


Code

1 # !/ bin / bash
2

3 echo " Enter a string : "


4 read str
5

6 len = $ { # str }
7

8 echo " Length = $len "

17 Program 17: File Existence Check


Code

1 # !/ bin / bash
2

3 echo " Enter filename : "


4 read file
5

6 if [ -f $file ]
7 then
8 echo " File exists "
9 else
10 echo " File does not exist "
11 fi

9
18 Program 18: Count Lines, Words and Characters
Code
1 # !/ bin / bash
2

3 echo " Enter filename : "


4 read file
5

6 wc $file

19 Program 19: Display Current Date and Time


Code
1 # !/ bin / bash
2

3 echo " Current Date : "


4 date
5

6 echo " Current Calendar : "


7 cal

20 Program 20: Student Grade Calculation


Code
1 # !/ bin / bash
2

3 echo " Enter marks : "


4 read marks
5

6 if [ $marks - ge 80 ]
7 then
8 echo " Grade A + "
9 elif [ $marks - ge 70 ]
10 then
11 echo " Grade A "
12 elif [ $marks - ge 60 ]
13 then
14 echo " Grade A - "
15 elif [ $marks - ge 50 ]
16 then
17 echo " Grade B "
18 else
19 echo " Fail "
20 fi

10

You might also like