Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3K views

Practical File For Operating System

The document contains code snippets for various shell script programs to perform tasks like displaying multiplication tables, finding factorials, checking if a number is prime, merging files etc. It also includes sample outputs for each code snippet.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Practical File For Operating System

The document contains code snippets for various shell script programs to perform tasks like displaying multiplication tables, finding factorials, checking if a number is prime, merging files etc. It also includes sample outputs for each code snippet.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

qwertyuiopasdfghjklzxcvbnmqwertyui

opasdfghjklzxcvbnmqwertyuiopasdfgh
DEEN DAYAL UPADHYAYA COLLEGE

jklzxcvbnmqwertyuiopasdfghjklzxcvb
BSC(MATHEMATICAL SCIENCE)

nmqwertyuiopasdfghjklzxcvbnmqwer
tyuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklzx
OPERATING SYSTEM

cvbnmqwertyuiopasdfghjklzxcvbnmq
LAB PRACTICALS
BY-

wertyuiopasdfghjklzxcvbnmqwertyuio
Himanshu Rawat(18MTS5919)

pasdfghjklzxcvbnmqwertyuiopasdfghj
klzxcvbnmqwertyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmrty
uiopasdfghjklzxcvbnmqwertyuiopasdf
ghjklzxcvbnmqwertyuiopasdfghjklzxc
1) write a shell script to display the multiplication
table any number

Ans )
echo -n "enter your number" ;read n
count=1

while [ $count -le 10 ]


do
res=`expr $n \* $count`
echo "$n*$count = $res"
count=`expr $count + 1`
done
Output:-

2) write a shell script to find the factorial of given


number
Ans:

echo -n "Enter n" ; read n


fact=1
while [ $n -ge 1 ]
do
(( fact=fact*n ))
(( n=n-1 ))
done
echo -n "Your factorial ans is $fact"

Output:-

3) write a shell script to find the sum of digits of a


given number
Ans )

echo -n "Enter n" ; read n


sum=0 ; num=$n
while [ $n -gt 0 ]

do
((rem=$n%10))
((sum=sum+rem))

((n=n/10))
done
echo “The sum of digits of $num is $sum”

Output:-

4)write a shell script to find the reverse of digits of


given number.
Ans )
echo -n "Enter a number:" ; read n
reverse=0
num=$n
while [ $n -gt 0 ]
do
((rem=$n%10))
((reverse=$reverse*10+$rem))
((n=$n/10))
done
echo "The reverse 0f $num is $reverse"

Output:-
5) write a shell script to find the power of a given
number
Ans )
echo -n "Input no" ; read no
echo -n "Input power" ; read power

ans=1
for ((counter=1;counter<=power;counter++))
do
((ans=no*ans))

done
echo "Your ans is $ans"

Output:-

6) write a shell script to check whether the number is


Armstrong or not.
Ans )
echo "enter your number"; read n
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=`expr $n % $c`
i=`expr $r \* $r \* $r`
s=`expr $s + $i`
n=`expr $n / $c`
done
if [ $s -eq $t ]
then
echo "This is a armstrong number"
else
echo "This is not a armstrong number"
fi

Output:-
7) write a shell script to perform the task of basic
calculator.
Ans )
echo "enter any two number:"
read a
read b
echo "enter choice :"
echo "1. Addition "
echo "2. Subtraction "
echo "3. Multiplication "
echo "4. Division "
read ch
case $ch in
1) res=`echo $a +$b | bc`;;
2) res=`echo $a - $b | bc`;;
3) res=`echo $a \* $b | bc`;;
4) res=`echo "scale=2; $a / $b " | bc`;;
esac
echo "Result : $res"
Output:-

8) program to show the right angled triangle of special


character “*”
Ans )
echo -n "Enter the rows for a right angled triangle:" ;
read h
for ((i=1;i<=h;i++))
do
for ((j=1;j<=i;j++))
do
echo -ne "* "
done
echo
done
Output:-

9) write a shell script to check if the number entered


at the command line is prime or not.
Ans )
echo -n "Enter Number : " ; read N
f=0
for ((i=2;i<=N/2;i++))
do
((a=N%i))
if [ $a -eq 0 ]
then
f=1
fi
done
if [ $f -eq 0 ]
then
echo "The $N is a prime number"
else
echo “the number is not prime”
fi
Output:-

10) write a shell script to accept a login name. if


not a valid name display message ‘entered login name is
invalid’
Ans;
echo -n "Enter login name" ; read login
if [ $login == "xyz" ]
then
echo -n "You may enter"
else
echo -n "Entered login name is invalid"
fi

Output:-

11)write a shell script to display date in the mm/dd/yy


Ans)
Set `date`
echo $2 $3 $6

Output:-

12) write a shell script to display on the screen


sorted output of “who” command along with the total
number of user.
Ans) echo “sorted list of total number of user:”
who –q < whout.txt
cat whout.txt

Output:-

14) write a shell script to merge three file and


display the content page by page
Ans )

echo -n "Enter file1 :" ; read file1


echo -n "Enter file2 :" ; read file2
echo -n "Enter file3 :" ; read file3
if [ -f $file1 -a -f $file2 -a -f $file3 ]
then
sort $file1 > t1
sort $file2 > t2
sort $file3 > t3
echo -e "The sorted contents are as follows"
sort -m t1 t2 t3 > sm.txt

rm t1 t2 t3
else
echo "Files with these name do not exist"
fi
cat sm.txt | more | less
Output:-

15) write a shell script to check whether the file have


all the permission or not.
Ans )
echo -n "enter file name"
read file
[ -w $file ] && W="Write= yes" || W="Write=No"
[ -x $file ] && X="Execute= yes" || X="Execute= No"
[ -r $file ] && R="Read= yes" || R="Read= No"
echo "file permission"
echo "$W"
echo "$R"
echo "$X"

Output:-

16) write a shell script to modify “cal” command to


display calendar of the specified months.
Ans )

for m in $@
do
m1=`echo $m | tr " [A-Z] " "[ a-z] "`
case $m1 in
jan* ) m1=1 ;;
feb* ) m1=2 ;;
mar* ) m1=3 ;;
apr* ) m1=4 ;;
may* ) m1=5 ;;
jun* ) m1=6 ;;
jul* ) m1=7 ;;

aug* ) m1=8 ;;
sep* ) m1=9 ;;
oct* ) m1=10 ;;
nov* ) m1=11 ;;
dec* ) m1=12 ;;
* ) echo -e "You have Entered Invalid Month\n"

esac
set `date`; y=$6
cal $m1 $y

done

Output:-
17) write a shell script to modify “cal” command to
display calendars of the specified range of months.

Ans )
mon1=`echo $* | tr -s "." | cut -d"." -f1`
mon2=`echo $* | tr -s "." | cut -d"." -f2`

m1=`echo $mon1 | tr ' [a-z] ' ' [A-Z] '`


case $m1 in
JAN* ) m1=1;;
FEB* ) m1=2;;
MAR* ) m1=3;;
APR* ) m1=4;;
MAY* ) m1=5;;
JUN* ) m1=6;;
JUL* ) m1=7;;
AUG* ) m1=8;;
SEP* ) m1=9;;
OCT* ) m1=10;;
NOV* ) m1=11;;
DEC* ) m1=12;;
* ) echo -e "wrong month\n"
esac

m2=`echo $mon2 | tr ' [a-z] ' ' [A-Z] '`


case $m2 in
JAN) m2=1;;

FEB) m2=2;;
MAR) m2=3;;
APR) m2=4;;

MAY) m2=5;;
JUN) m2=6;;
JUL) m2=7;;
AUG) m2=8;;
SEP) m2=9;;
OCT) m2=10;;

NOV) m2=11;;
DEC) m2=12;;
* ) echo -e "wrong month\n"
esac

if [ $m1 -le $m2 ]


then

set `date`; y=$6

for((i=m1;i<=m2;i++))

do
cal $i $y
done
else
echo "invalid range of months"
fi

Output:-

18) write a shell script to find the gcd of two number.


Ans )

echo "Enter a" ; read a


echo "Enter b" ; read b

m=$a
if [ $b -lt $m ]

then
m=$b
fi

while [ $m -ne 0 ]
do
((x=$a%$m))
((y=$b%$m))
if [ $x -eq 0 -a $y -eq 0 ]
then
echo "GCD of $a and $b is $m"
break
fi

((m=$m-1))
done

Output:-

You might also like