0% found this document useful (0 votes)
42 views19 pages

(Os) Lab Prgms

The document contains 32 shell script programs with examples covering a variety of tasks including: 1) Getting input from the user and displaying output 2) Performing arithmetic operations 3) Checking conditions and branching program flow 4) Using loops like while and for to iterate through operations The scripts cover basics like input/output, calculations, and control flow as well as more advanced topics like matrices, permutations, prime numbers and more.
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)
42 views19 pages

(Os) Lab Prgms

The document contains 32 shell script programs with examples covering a variety of tasks including: 1) Getting input from the user and displaying output 2) Performing arithmetic operations 3) Checking conditions and branching program flow 4) Using loops like while and for to iterate through operations The scripts cover basics like input/output, calculations, and control flow as well as more advanced topics like matrices, permutations, prime numbers and more.
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/ 19

1)Shell script to print student name, student number, Student class.

echo stname

read stname

echo stnumber

read stnumber

echo stclass

read stclass

echo $stname "-" $stnumber "-" $stclass

# Input from user

echo "Enter the number -"

read n

2)Shell script to print multiplication table using while loop.


# initializing i with 1

i=1

# Looping i, i should be less than # or equal to 10

while [ $i -le 10 ]

do

res=`expr $i \* $n`

# printing on console

echo "$n*$i=$res"

# incrementing i by one

((++i))

done
3)Shell script to print multiplication table using for loop.
echo "enter number"

read n

i=1

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

do

res=`expr $n \* $i`

echo "$n * $i = $res"

done

4)Shell script to find the given number is even (or) odd numbers.
echo "enter the number"

read n

b=$((n%2))

if [ $b -eq 0 ]

then

echo "$n is even"

else

echo "$n is odd"

fi

5)Shell script to find sum of all command line arguments


echo "entered arguments are"

read $*

sum=0

for i in $*

do

sum=`expr $sum + $i |bc`


done

echo "sum of all command line arguments is $sum"

6)Shell script to perform basic Arithmetic operations.


echo "enter the values a"

read a

echo "enter the value of b"

read b

c=`expr $a + $b`

echo "addition of two numbers is" $c

d=`expr $a - $b`

echo "subtraction of two numbers is" $d

e=`expr $a \* $b`

echo "multiplication of two numbers is"$e

f=`expr $a / $b`

echo "division of two numbers is " $f

7)Shell script to print greatest and smallest of three numbers.


echo "plz enter the three numbers"

read x

read y

read z

if [ $x -ge $y ] && [ $x -ge $z ]

then

echo "$x is the largest number"

elif [ $y -ge $x ] && [ $y -ge $z ]

then

echo "$y is the largest number"

else
echo "$z is the largest number”

fi

if [ $x -lt $y ] && [ $x -lt $z ]

then

echo "$x is the smallest number"

elif [ $y -lt $x ] && [ $y -lt $z ]

then

echo "$y is the smallest number"

else

echo "$z is the smallest number"

8)Shell script to swap two given numbers.


echo "enter a"

echo "enter b"

read a

read b

temp=$a

a=$b

b=$temp

echo "after swapping numbers are: a=$a b=$b"

9)Shell script to find the length of the string.


str="supriya "

n=${#str}

echo "length of the string is:$n"

10)Shell Script to give Automatic numbering for the Arguments in a file


vishwa vishwani course

BS-MS
BBA

MBA

PGDM

11)Shell script to perform menu driven program using case.


echo "enter the choice"

echo "1.who"

echo "2.pwd"

echo "3.ls"

echo "4.date"

echo "5.cal"

echo "6.exit"

read ch

case $ch in

1)who;;

2)pwd;;

3)ls;;

4)date;;

5)cal;;

6)exit;;

*)echo "invalid option"

esac #use to evaluate and execute several diff stmts

12)Shell script to check whether the given number is prime or not for loop.
echo "Enter a number :"

read n

for (( i=2; i<=$n/2; i++ ))

do

num=$((n%i))
if [ $num -eq 0 ]

then

echo " $n is not a prime number. "

exit

fi

done

echo " $n is a prime number. "

13)shell script to find prime number using while loop


echo "enter a number"

read n

d=2

r=1

while [[ $d -lt $n && $r -ne 0 ]]

do

r=`expr $n % $d`

d=`expr $d+1`

done

if [[ $r -eq 0 ]]

then

echo "$n is not a prime num"

else

echo "$n is a prime"

fi

14)Shell script to check whether the given number is Palindrome or not


echo "Enter a Number: "

read n

num=$n

while ((num>0))
do

ans=$((ans*10))

rem=$((num%10))

ans=$((ans+rem))

num=$((num/10))

done

if (($n == $ans))

then

echo "It's a Palindrome Number"

else

echo "It's not a Palindrome Number"

fi

15)shell script to print reverse of a number


read -p " enter a number: " number

temp=$number

while [ $temp -ne 0 ]

do

reverse=$reverse$((temp%10))

temp=$((temp/10))

done

echo " reveres of $number is $reverse "

16)shell script to print GCD of two numbers


echo Enter two number:

read m

read n

temp=`expr $m \* $n`

while [ $m != $n ]

do

if [ $m -gt $n ]
then

m=`expr $m - $n`

else

n=`expr $n - $m`

fi

done

echo gcd= $n

17) shell script program to find whether the given number is Armstrong or
not
echo "Enter a Number"

read c

x=$c

sum=0

r=0

n=0

while [[ $x -gt o ]]

do

r=`expr $x % 10`

n=`expr $r \* $r \* $r`

sum=`expr $sum + $n`

x=`expr $x / 10`

done

if [[ sum -eq $c ]]

then

echo "It is an Armstrong Number"

else

echo "It is not an Armstrong Number"

fi
18)shell script to print the average of n numbers
echo "Enter the limit"

read n

i=0

sum=0

while [ $i -lt $n ]

do

echo "Enter the number"

read a

sum=`expr $sum + $a`

i=`expr $i + 1`

done

sum=`expr $sum / $n |bc`

echo "The average of n number"$sum

echo $(( $x "+" $y ))

19)write a shell script for fibonacci series


i=1

a=0

b=1

echo "enter the range"

read n

echo "$a"

echo "$b"

n=`expr $n - 2`

while [ $i -le $n ]

do

c=`expr $a + $b|bc`

echo $c

a=$b
b=$c

i=`expr $i + 1`

done

20)shell script to print perfect numbers


echo "enter a number"

read a

sum=0

echo "factor of "$a" is"

for((i=1;i<a;i++))

do

x=`expr $a % $i|bc`

if [ $x -eq 0 ]

then

echo "$i"

sum=`expr $sum + $i|bc`

fi

done

echo "sum of factors of "$a" is $sum"

if [ $sum -eq $a ]

then

echo "it is a perfect number"

else

echo "it is not a perfect number"

fi

21)write a shell script to find prime numbers up to n numbers


echo "enter the limit"

read n

echo "the prime numbers are"


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

do

c=0

for (( j=2 ; j<=i ; j++ ))

do

x=`expr $i%$j | bc`

if [ $x -eq 0 ]

then

c=`expr $c+1 | bc`

fi

done

if [ $c -eq 1 ]

then

echo "$i"

fi

done

22)write shell script to find pattern of asterisk


i=0

echo "enter the number of lines"

read n

while [ $i -le $n ]

do

j=1

while [ $j -le $i ]

do

echo -n "*"

j=`expr $j + 1`

done

echo " "


i=`expr $i + 1`

done

23)shell script to print sum of digits of a given number


echo "enter the number"

read n

sum=0

while [ $n -gt 0 ]

do

rem=`expr $n % 10`

sum=`expr $sum + $rem`

n=`expr $n \/ 10`

done

echo "the sum of digits of number is $sum"

24)shell script to find whether the given number is divisible by 11 or not


echo "enter number to be checked"

read n

c=$((n % 11))

if [ $c -eq 0 ]

then

echo "the number is divdisible by 11"

else

echo "the number is not divdisible by 11"

fi

25)Shell script to print perfect numbers between 1 to 100.


i=1

echo "prefect numbers from 1 - 100 are"

n=1
while [ $n -lt 100 ]

do

x=$n

sum=0

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

do

r=`expr $x % $i`

if [ $r -eq 0 ]

then

sum=`expr $sum + $i`

fi

done

if [ $sum -eq $n ]

then

echo "$n"

fi

n=`expr $n + 1`

done

26)shell script to find whether the given number is +ve , -ve , or zero
echo "enter a number"

read n

if [ $n -eq 0 ]

then

echo "the number is zero"

elif [ $n -le 0 ]

then

echo "the number is negitive"

elif [ $n -ge 0 ]

then
echo "the number is positive"

else

echo "it is not a number"

fi

27)shell script to find n P r (permutation) for the given values of n and r


echo Enter the values of n and r:

read n

read r

f=1

i=1

while [ $i -le $n ]

do

f=`expr $f \* $i |bc`

i=`expr $i + 1 |bc`

done

s=`expr $n - $r |bc`

s1=1

j=1

while [ $j -le $s ]

do

s1=`expr $s1 \* $j |bc`

j=`expr $j + 1 |bc`

done

d=`expr $f / $s1 |bc`

echo $n P $r = $d

28)shell script to print a simple 3*3 matrix


i=1

j=1

while [ $i -lt 10 ]
do

j=1

while [ $j -lt 4 ]

do

echo -n $i ""

j=`expr $j + 1`

i=`expr $i + 3`

done

if

test $i -lt 12

then

i=`expr $i - 8`

fi

echo ""

done

29)shell script to print unit matrix of order 3*3

i=1

j=1

k=1

l=0

while [ $i -le 3 ]

do

j=1

while [ $j -le 3 ]

do

if [ $i -eq $j ]

then
echo -n $k " "

else

echo -n $l " "

fi

j=`expr $j + 1`

done

i=`expr $i + 1`

echo " "

done

30)shell script to print fair price for automobiles


echo "1)Car - Rs.10 per kilometer"

echo "2)Bus - Rs.8 per kilometer"

echo "3)Train - Rs.5 per kilometer"

echo "4)Bike - Rs.6 per kilometer"

echo "Choose the option"

read n

case $n in

1) echo "CAR"

echo "enter the travelled kilometers"

read x

z=`expr $x*10 | bc`

echo "car fair -" $z;;

2) echo "BUS"

echo "enter the travelled kilometers"

read a

b=`expr $a*8 | bc`

echo "bus fair -" $b;;

3) echo "TRAIN"

echo "enter the travelled kilometers"


read c

d=`expr $c*5 | bc`

echo "train fair -" $d;;

4) echo "BIKE"

echo "enter the travelled kilometers"

read u

v=`expr $u*6 | bc`

echo "bike fair -" $v;;

*) echo "invalid option";;

esac

31)shell script to print student result (pass or fail)


echo "enter the stname"

read name

echo "enter the strollnumber"

read rollnumber

echo "enter the three subjects marks"

read s1

read s2

read s3

t=`expr $s1+$s2+$s3 |bc`

echo "total = $t"

avg=`expr $t\/3 |bc`

echo "average of three subjects is $avg"

if [ $avg -ge 70 ] && [ $avg -lt 99 ]

then

echo "distinciton"

elif [ $avg -ge 60 ] && [ $avg -lt 70 ]

then
echo "First class"

elif [ $avg -ge 50 ] && [ $avg -lt 60 ]

then

echo "Second class"

elif [ $avg -ge 40 ] $$ [ $avg -lt 50 ]

then

echo "Third class"

else

echo "Fail"

fi

32)shell script to find the area of geometrical figures


echo "Enter your choice"

echo "1.Area of circle"

echo "2.Area of rectangle"

echo "3.Area of square"

echo "4.Area of parallelogram"

read ch

case $ch in

1)echo "Enter radius"

read r

a=`echo 3.14 \* $r \* $r |bc`

echo "Area of circle"$a;;

2)echo "Enter length and breadth"

read l

read b

c=`expr $l \* $b |bc`

echo "Area of rectangle"$c;;

3)echo "Enter side of the square"

read s
d=`expr $s \* $s |bc`

echo "Area of square"$d;;

4)echo "Enter breadth and height"

read t

read h

e=`expr $t \* $h |bc`

echo "Area of parallelogram"$e;;

*)echo "Invalid option";;

esac

You might also like