Subject: Unix Lab Date: ROLL NO:07211A0568
Subject: Unix Lab Date: ROLL NO:07211A0568
Output :
abc pqr
2.
# ! /bin /ksh
# To find factorial of a given number
f=1
s=1
read n
while(( s<=n))
do
((f=f*s))
((s=s+1))
done
echo $f
Output:
4
24
3.
# ! / bin/ksh
# To find the reverse of a given number
echo enter a no.
read n
s=0
r=0
b=0
i=1
while(($n>0))
do
((b=n%10))
((r=r*10+b))
SUBJECT : UNIX LAB DATE:
ROLL NO:07211A0568 PAGE NO :
((n=n/10))
done
echo $r
Output:
123
321
4.
# ! / bin/ksh
length= 5
breadth= 4
radius= 3
(( area= length*breadth))
echo $area
Output:
area = 20
perimeter = 9
carea = 28
cperimeter = 18
5.
# ! / bin/ksh
# the given year is a leap year or not
read year
while (( year!=0))
do
if(( year%4 = = 0))
then
echo $year is leap year
else
echo $year is not a leap year
fi
done
while (( year = = “ “))
SUBJECT : UNIX LAB DATE:
ROLL NO:07211A0568 PAGE NO :
do
year = 2008
if(( year%4 = = 0))
then
echo $year is a leap year
else
echo $year is not a leap year
fi
done
Output:
2012
2012 is a leap year
6.
# ! / bin/ksh
read fname1
read fname2
7.
# ! / bin/ksh
# students percentage and division
read percent
if((percent>=60))
then
echo first division
elif((percent>=50&&percent<=59))
echo second division
elif((percent>=40&&percent<=49))
echo third division
else
SUBJECT : UNIX LAB DATE:
ROLL NO:07211A0568 PAGE NO :
echo fail
fi
Output:
45
third division
8.
# ! / bin/ksh
# Arithmetic operation using switch case
select ch in add sub mod div mul quit
do
case $ch in
add) read a b
(( c= a+b))
echo “ sum is : “ $c
ii
sub) read p q
((d= p-q))
echo “ sub is : “ $d
ii
mod) read v w
(( g= v%w))
echo : modular division is : “ $g
ii
mul) read r s
((e= r*s))
echo “product is : “ $e
div) read t u
(( f= t/u))
echo “division is : “ $f
quit) exit ;;
esac
done
Output:
add
45
9
9.
# ! / bin/ksh
# To find prime from 1 to 200
LIMIT= 200
echo The Prime numbers are
for ((m= 1; m<= LIMIT ; m++))
SUBJECT : UNIX LAB DATE:
ROLL NO:07211A0568 PAGE NO :
do
p=0
for((i=2;i<=m/2; i++))
do
if ((m%i= = 0))
then
p=1
break
fi
done
if(p = = 0)
then
echo $m
fi
done
Output:
2 89 191
3 91 197
5 97 199
7 101
11 103
13 107
17 109
19 113
29 119
31 121
37 131
41 137
43 143
47 149
53 151
59 157
61 157
67 161
71 167
73 173
# ! / bin/ksh
# to replace cpp by unix in a given file
read f1
s= $( sed ` 1s| cpp| g’ $f1) 10.
echo $f1
Output:
f4
unix is unix
11.
# ! / bin/ksh
# displays the list of all files in the current directory
f= $(pwd | ls –l |grep “ ^.rwx” )
echo $f
Output:
f1 f2 f3 f4
12.
# ! / bin/ksh
# file type ,No of links,users permissions, No of lines
i=0
for i in $*
do
if [ -f $i ]
then
echo “normal file “
elif [ -d $i ]
then
echo “ directory file “
else
echo “ symbolic file “
fi
if [ -r $i –a –w $i -a –x $i ]
then
echo “ user has all permissions “
Output:
f1
directory file
user al l permissions
number of links is 2
number of line s are 3
13.
# ! / bin/ksh
# file exists and frequency of word
read fname
read word
while [ -e fname ]
do
str= $( grep –c $word $fname )
done
echo $str
Output:
file4
unix
2
14.
# ! / bin/ksh
# to find whether given file is regular ,directory file
read fname
if [ [ -f $fname ] ]
then
echo $fname is a regular file
elif[ -d $fname ]
echo $fname is a directory file
else
echo $fname is any other file
fi
15.
# ! / bin/ksh
# multiplication table
read n
i=1
while ( i <= 10 )
do
((product = n*i ))
echo $n * $i = $product
((i=i+1))
done
Output:
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
16.
# ! / bin/ksh
# display the list of files and contents of a directory
echo “ Enter the directory name “
read dir
f= $( ls $dir )
s= $( ls $dir | more $1 )
echo $f
echo $s
Output:
mydir
f1 f2 f3 f4
f5 f5 f7 f8
18.
# ! / bin/ksh
# to move a file to another location
read fname f2
mv $fname $f2
19.
# ! / bin/ksh
# to find the sum of 5digit numbers
s=0
read n
while ( n! = 10 )
do
(( p= n%10))
(( s= s+p ))
(( n= n/10))
done
echo $s
Output:
12345
15
21.
#!/bin/ksh
#to calculate gross salary
echo enter the salary
read salary
((HRA=(20*salary)/100))
((DA=(40*salary)/100))
((GS=$HRA+$DA+$salary))
echo $GS
Output:
1000
1600