0% found this document useful (0 votes)
41 views

Subject: Unix Lab Date: ROLL NO:07211A0568

The document contains 21 code snippets demonstrating various UNIX/Linux shell scripting concepts like file operations, string manipulation, conditional statements, loops, functions, arithmetic operations etc. The scripts perform tasks like copying/appending files, calculating factorial, reversing numbers, finding area and perimeter, checking leap years, comparing files, calculating percentage, performing arithmetic operations using switch case, finding prime numbers, replacing strings, listing files, checking file permissions and types, finding word frequency, multiplication tables and more.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Subject: Unix Lab Date: ROLL NO:07211A0568

The document contains 21 code snippets demonstrating various UNIX/Linux shell scripting concepts like file operations, string manipulation, conditional statements, loops, functions, arithmetic operations etc. The scripts perform tasks like copying/appending files, calculating factorial, reversing numbers, finding area and perimeter, checking leap years, comparing files, calculating percentage, performing arithmetic operations using switch case, finding prime numbers, replacing strings, listing files, checking file permissions and types, finding word frequency, multiplication tables and more.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

SUBJECT : UNIX LAB DATE:

ROLL NO:07211A0568 PAGE NO :


1.
# ! / bin/ksh
# To copy from one file to another file and if the file exists to append the contents into the file
read f1 f2
if [ ! –s $file ]]
then
cat f1>f2
else
cat f1>>f2
fi

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

(( perimeter= length + breadth))


echo $perimeter

((carea= 3.14 * radius * radius))


echo $carea

(( cperimeter= 3.14 * 2* radius))


echo $cperimeter

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

s= $( cmp $fname1 $fname2)


if(( s = =0))
then
rm $fname
echo $fname2
else
echo two files are not equal
fi
Output:
sha1.cpp
sha2.cpp
two files are not equal

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

SUBJECT : UNIX LAB DATE:


ROLL NO:07211A0568 PAGE NO :
79 179
83 181
87 187

# ! / 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 “

SUBJECT : UNIX LAB DATE:


ROLL NO:07211A0568 PAGE NO :
fi
echo “ number of links is “
ls –l $i| cut -c 12
echo “ no of lines is “
cat $i| wc –l
done

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

SUBJECT : UNIX LAB DATE:


ROLL NO:07211A0568 PAGE NO :
Output:
file4
file4 is a regular file

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

SUBJECT : UNIX LAB DATE:


ROLL NO:07211A0568 PAGE NO :
17.
# ! / bin/ksh
read dname
filelist= $(ls –l $dname )
for f in $ ( cat filelist )
do
cat $f
done
Output:
mydir
f1 f2 f3 f4
unix is unix
cpp is cpp
WELCOME TO UNIX

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

SUBJECT : UNIX LAB DATE:


ROLL NO:07211A0568 PAGE NO :
20.
# ! / bin/ksh
# to find whether a given number is even or add
read a
if ( a%2 = = 0 )
echo $a is even
else
echo $a is odd
fi
Output:
4
4 is even

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

You might also like