You are on page 1of 24

Operating Systems and Linux Lab Manual

REVA University

SCHOOL OF APPLIED SCIENCE

BSc – Bio Informatics

Operating Systems and Linux


Lab Manual

B19MC4090

School of Applied Science – BSc Computer Science Page 1


Operating Systems and Linux Lab Manual
REVA University

CONTENTS

SR.NO Problem Statement  – Part A PAGENO

Write a menu driven program to calculate (i) Simple interest (ii) Compound 3
A1 interest

Write a shell script To print all prime numbers between m and n (m<n). 4
A2
Write a shell script to Reverse a given number and check whether it is 5
A3 palindrome or not.

Write a shell script to find maximum and minimum of given set 6


A4
Write a shell script to count the number of vowels in a given string. 7
A5
A6 Write a shell script to check whether a given string is a palindrome or not. 7
Write a shell script, which will accept two positive integers and find GCD and LCM 8
A7 of these numbers.
Write a script to take two numbers as argument and output their sum using bc. 9
A8 Include error checking to test whether two arguments were entered and they are valid
or not.
A9 Write a Shell script to display all the file permissions. 11
Write a shell script that creates a file and compresses it using: 12
A10 a) Compress b) decompress.

SR.NO Problem Statement  – Part B PAGE NO

B1 Execution of various file/directory handling commands. 13


B2 Write a Shell script to check whether the given number is even or odd. 15
B3 Write a Shell script to reverse a string 15
B4 16
Write a Shell script to create a simple calculator that performs basic
arithmetic operations
B5 Write a Shell script to wish according to day and time 17
B6 write a shell script to search a pattern using grep and fgrep command 18
B7 19
Write a shell script to find factorial of a given number
B8 Write a shell script that accepts two file names as arguments, checks if the 19
permissions for these files are identical, if yes output the common
permissions otherwise output each file name followed by its permissions
B9 Write a shell script to find file properties for entered file name 20
B10 21
Menu driven shell script to demonstrate the use of tail , cmp, uniq and rm

School of Applied Science – BSc Computer Science Page 2


Operating Systems and Linux Lab Manual
REVA University

commands

A1. Write a menu driven program to calculate (i) Simple interest


(ii) Compound interest

while [ 1 ]
do
echo "menu"
echo "1. simple interest"
echo "2. compound interest"
echo "3. exit"
echo "enter the choice"
read choice
case $choice in
1) echo "enter the value of p,t and r"
read p
read t
read r
si=`echo "scale=2;( $p * $t * $r ) / 100" | bc`
echo "simple interest = $si" ;;
2) echo "enter the value of p, t, r and n"
read p
read t
read r
read n
power=`expr $n \* $t`
echo $power
ci=`echo "scale=2; ( $p * ( 1 + $r / $n ) ) ^ $power" | bc`
echo "compound interest = $ci" ;;
3) exit;;
*) echo "invalid choice";;
esac

School of Applied Science – BSc Computer Science Page 3


Operating Systems and Linux Lab Manual
REVA University
done

OUTPUT:

Menu
1. Simple interest
2. Compound interest
3. Exit
Enter the choice
1
Enter the value of p, t and r
20000
2
1
Simple interest=400.00
Menu
1. Simple interest
2. Compound interest
3. Exit
Enter the choice
2
Enter the value of p, t, r and n
50000
5
1
1
5
Compound interest = 1000000000000000000000.00

A2. Write a shell script to print all prime numbers between m and n (m<n).

clear
echo "Enter lower limit"
read m
echo "Enter the upper limit"
read n
flag=0
echo "Prime number between $m and $n are"
m=`expr $m + 1`
while [ $m -lt $n ]
do
if [ $m -eq 1 ]

School of Applied Science – BSc Computer Science Page 4


Operating Systems and Linux Lab Manual
REVA University
then
m=`expr $m + 1`
fi
i=2
while [ $i -le `expr $m / 2` ]
do
if [ `expr $m % $i` -eq 0 ]
then
flag=1
break
else
flag=0
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done

OUTPUT:
Sh primenumber.sh
Enter lower limit
1
Enter upper limit
5
Prime numbers between 1 and 5 are
2
3

A3. Reverse a given number and check whether it is palindrome or not.

echo "enter number to find whether its palindrome or not "


read num

School of Applied Science – BSc Computer Science Page 5


Operating Systems and Linux Lab Manual
REVA University
orgnum=$num
rev=0
while [ $num -ne 0 ]
do
rem=`expr $num % 10`
rev=`expr $rev \* 10 + $rem`
num=`expr $num / 10`
done
if [ $orgnum -eq $rev ]
then
echo $orgnum "is a palindrome"
else
echo $orgnum "is a not palindrome"
fi

OUTPUT:
Sh palindrome.sh

Enter a number to find whether its palindrome or not


121
Number is palindrome

A4. Shell script to find maximum and minimum of given set

echo "enter the name of the file that contains number in it"
read filename
min=`cat $filename | sort -n | head -1`
echo "minimum value = $min"
max=`cat $filename | sort -n | tail -1`
echo "maximum value = $max"

OUTPUT:

Sh minmax.sh

Enter the name of the file that contains number in it


Number.txt
School of Applied Science – BSc Computer Science Page 6
Operating Systems and Linux Lab Manual
REVA University
Minimum value=2
Maximum value=195

A5.write a shell script to count the number of vowels in a given string

clear
echo "Entre a string to find the number of Vowels "
read st
len=`expr $st | wc -c`
len=`expr $len - 1`
count=0
while [ $len -gt 0 ]
do
ch=`expr $st | cut -c $len`
case $ch in
([aeiou,AEIOU]) count=`expr $count + 1` ;;
esac
len=`expr $len - 1`
done
echo "Number of vowels in the give string is $count"

OUTPUT:
Sh.vowels.sh
Entre a string to find the number of Vowels
reva
Number of vowels in the give string is 2

A6. Write a shell script to check whether a given string is a palindrome or not.

clear
echo "Enter the string to find whether its palindrome or not"
read str
len=`echo $str | wc -c`

School of Applied Science – BSc Computer Science Page 7


Operating Systems and Linux Lab Manual
REVA University
len=`expr $len - 1`
i=1
while [ $i -le $len ]
do
ch=`echo $str | cut -c $i`
ch1=`echo $str | cut -c $len`
if [ $ch != $ch1 ]
then
echo "String is not palindrome"
exit
fi
len=`expr $len - 1`
i=`expr $i + 1`
done
echo "String is a palindrome"
OUTPUT:
Sh stringpalindrome.sh

Enter the string to find whether its palindrome or not


madam
String is a palindrome
Sh stringpalindrome.sh
Enter the string to find whether its palindrome or not
reva
String is not palindrome

A7. Write a shell script, which will accept two positive integers and find GCD and LCM of these
numbers.
echo "Enter the first number :"
read a
echo "Enter the second number : "
read b
if [ $a -gt $b ]
then
num=$a

School of Applied Science – BSc Computer Science Page 8


Operating Systems and Linux Lab Manual
REVA University
den=$b
else
num=$b
den=$a
fi
r=`expr $num % $den`
while [ $r -ne 0 ]
do
num=$den
den=$r
r=`expr $num % $den`
done
gcd=$den
lcm=`expr $a \* $b / $gcd`
echo " The LCM of $a and $b is : $lcm"
echo " The GCD of $a and $b is : $gcd"

OUTPUT:
Enter the first number
15
Enter the second number
35

The GCD of 15 and 35 is: 5


The LCM of 15 and 35:105

A8. Write a script to take two numbers as argument and output their sum using bc. Include error
checking to test whether two arguments were entered and they are valid or not.

if [ $# -eq 2 ]
then
n1=$1
n2=$2
len1=`expr "$n1" : ".*"`
len2=`expr "$n2" : ".*"`
flag=0
i=0

School of Applied Science – BSc Computer Science Page 9


Operating Systems and Linux Lab Manual
REVA University
while [ $i -lt $len1 ]
do
char=`expr "$n1" : ".\{$i\}\(.\)"`
case $char in
[!0-9.])
flag=1
esac
i=`expr $i + 1`
done
i=0
while [ $i -lt $len2 ]
do
char=`expr "$n2" : ".\{$i\}\(.\)"`
case $char in
[!0-9.])
flag=1
esac
i=`expr $i + 1`
done
if [ flag -eq 0 ]
then
echo "\nAddition is :: \c"
echo "$n1+$n2" | bc
echo "\n"
else
echo "Both numbers should be Numerical/Decimal"
fi
else
echo "You have to pass two numerical arguments"
fi
OUTPUT:
You have to pass two numerical arguments
10
20
Addition is
30

School of Applied Science – BSc Computer Science Page 10


Operating Systems and Linux Lab Manual
REVA University

A9. Write a Shell script to display all the file permissions.

echo " The files and their permissions are"


echo "File name Read Write Execute"
for file in *
do

if [ -f $file ];
then
if [ -w $file ] ;
then
W="Yes"
else
W="No"
fi

if [ -r $file ] ;
then
R="Yes"
else
R="No"
fi

if [ -x $file ];
then
X="Yes"
else
X="No"
fi
printf "%-20s %6s %6s %6s\n" $file, $R, $W, $X
fi
done
OUTPUT:
The files and their permissions are
File name Read Write Execute
Calc.sh yes yes no
Emp, yes, yes, no
Employee.sh, yes, yes, no
School of Applied Science – BSc Computer Science Page 11
Operating Systems and Linux Lab Manual
REVA University
Factorial.sh, yes, yes, no
Fibonacci.sh, yes, yes, no

A10. Write a shell script that creates a file and compresses it using:
a) Compress b) decompress.

clear
echo "enter the file name"
read fname
echo "size of file name before compression is"
wc -c $fname
gzip $fname
echo "size of file name after compression"
wc -c $fname.gz

#unzip the file


gunzip $fname.gz
echo "after decompression $fname"
wc -c $fname
echo $fname
output:
enter the file name
prime.sh
size of $file name before compression is
103
Prime.gz
size of file name after compression
87
Prime.gz
After decompression prime.gz
103
School of Applied Science – BSc Computer Science Page 12
Operating Systems and Linux Lab Manual
REVA University
Prime.sh

PART-B

B1) Execution of various file/directory handling commands.


1) pwd COMMAND:
pwd - Print Working Directory. pwd command prints the full filename of
the current working directory.
SYNTAX:
The Syntax is
pwd [options]

2) cd COMMAND:
cd command is used to change the directory.

SYNTAX:
The Syntax is
cd [directory | ~ | ./ | ../ | - ]

3) ls COMMAND:
ls command lists the files and directories under current working directory.
SYNTAX:
The Syntax is
ls [OPTIONS]... [FILE]
OPTIONS:
-l Lists all the files, directories and their mode, Number of links,
owner of the file, file size, Modified date and time and filename.
-t Lists in order of last modification time.
-a Lists all entries including hidden files.
-d Lists directory files instead of contents.
-p Puts slash at the end of each directories.
-u List in order of last access time.
-i Display inode information.

4) rm COMMAND:
rm linux command is used to remove/delete the file from the directory.
SYNTAX:
The Syntax is
rm [options..] [file | directory]
OPTIONS:
-f Remove all files in a directory without prompting the user.
-i Interactive. With this option, rm prompts for confirmation before
removing any files.

School of Applied Science – BSc Computer Science Page 13


Operating Systems and Linux Lab Manual
REVA University
5) mv COMMAND:
mv command which is short for move. It is used to move/rename file from one directory to another.
mv command is different from cp command as it completely removes the file from the source and
moves to the directory specified, where cp command just copies the content from one file to another.
SYNTAX:
The Syntax is
mv [-f] [-i] oldname newname
OPTIONS:
-f This will not prompt before overwriting (equivalent to --
reply=yes). mv -f will move the file(s) without prompting even
if it is writing over an existing target.
-i Prompts before overwriting another file.

6) cat COMMAND:
cat linux command concatenates files and print it on the standard output.
SYNTAX:
The Syntax is
cat [OPTIONS] [FILE]...
OPTIONS:
-A Show all.
-b Omits line numbers for blank space in the output.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.

7) cp COMMAND:
cp command copy files from one location to another. If the destination is an existing file, then the file
is overwritten; if the destination is an existing directory, the file is copied into the directory (the
directory is not overwritten).
SYNTAX:
The Syntax is
cp [OPTIONS]... SOURCE DEST

8) echo COMMAND:
echo command prints the given input string to standard output.
SYNTAX:
The Syntax is
echo [options..] [string]

9)mkdir COMMAND:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories
OPTIONS:
-m Set the access mode for the new directories.
-p Create intervening parent directories if they don't exist.
-v Print help message for each directory created.

10) rmdir COMMAND:

School of Applied Science – BSc Computer Science Page 14


Operating Systems and Linux Lab Manual
REVA University
rmdir command is used to delete/remove a directory and its subdirectories.
SYNTAX:
The Syntax is
rmdir [options..] Directory
OPTIONS:
-p Allow users to remove the directory dir name and its parent
directories which become empty.

B2) Write a Shell script to check whether the given number is even or odd.
clear
echo "enter a number"
read n
if [ `expr $n % 2` -eq 0 ]; then
echo " $n is even"
else
echo "$n is odd"
fi
OUTPUT

enter a number
4
4 is even
enter a number
5
5 is odd

B3) Write a Shell script to reverse a string

clear

echo "enter the string"

read str

len=`expr $str | wc -c`

len=`expr $len - 1`

School of Applied Science – BSc Computer Science Page 15


Operating Systems and Linux Lab Manual
REVA University
while [ $len -gt 0 ]

do

ch=`echo $str | cut -c $len`

echo -n $ch

len=`expr $len - 1`

done

OUTPUT

enter the string


Reva
aveR

B4) Write a Shell script to create a simple calculator that performs basic arithmetic
operations
clear
sum=0
i="y"
echo " Enter FIRST no."
read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in

School of Applied Science – BSc Computer Science Page 16


Operating Systems and Linux Lab Manual
REVA University
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 - $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
OUTPUT

Enter FIRST no.


4
Enter second no
6
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
1
Sum = 10
B5) Write a Shell script to wish according to day and time

clear
hours=`date|cut -c12-13`
if [ $hours -le 12 ]

School of Applied Science – BSc Computer Science Page 17


Operating Systems and Linux Lab Manual
REVA University
then
echo "Good Morning"
else
if [ $hours -le 16 ]
then
echo "Good Afternoon"
elif [ $hours -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi

OUTPUT

Good evening
B6) write a shell script to search a pattern using grep and fgrep command

clear
echo "enter the file name"
read fname1
echo "enter the contents of $fname1 and press ctrl+d"
cat >$fname1
echo "enter the file name 2"
read fname2
echo "enter the contents of $fname2 and press ctrl+w"
cat >$fname2
echo "enter the pattern"
read ptrn
#echo $ptrn $fname1 $fname2
grep -i $ptrn $fname1 $fname2
fgrep -i $ptrn $fname1 $fname2

School of Applied Science – BSc Computer Science Page 18


Operating Systems and Linux Lab Manual
REVA University
OUTPUT

Enter file name 1


Raj.txt
Enter the contents of raj.txt and press ctrl+d
Hi I am raj studying bca
Enter the file name 2
Sai.txt
Enter the contents of sai.txt and press ctrl+w
Hi I am sai studying bca
Enter the pattern
Hi
Raj.txt:Hi I am raj studying in bca
Sai.txt: I am sai studying in bca

B7) Write a shell script to find factorial of a given number

clear
echo "enter a number"
read n
i=1
if [ $n -lt 0 ]; then
echo " you cannot find factorial for a negative number"
else
if [ $n -eq 0 ]; then
echo "factorial of zero is 1"
else
fact=1
while [ $i -le $n ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "the factorial of $n is $fact"
fi
fi

OUTPUT

enter a number
5
the factorial of 5 is 120
School of Applied Science – BSc Computer Science Page 19
Operating Systems and Linux Lab Manual
REVA University

B8) Write a shell script that accepts two file names as arguments, checks if the permissions for
these files are identical, if yes output the common permissions otherwise output each file name
followed by its permissions

if [ $# -eq 0 ] ; then
echo “No arguments”
exit
elif [ $# -lt 2 ] ; then
echo “Only one arguments”
exit
else
f1=`ls -l $1|cut -c ‘2-10’`
f2=`ls -l $2|cut -c ‘2-10’`
if [ "$f1" == "$f2" ] ; then
echo “File permission are identical”
else
echo “File permission are not identical”
echo “The permission of first file is f1:$f1″
echo “The permission of the second file is f2:$f2″
fi
fi
OUTPUT

sh 1b.sh 1a.sh bhu.sh


File permission are not identical
The permission of first file is f1:rw-rw-r–
The permission of the second file is f2:rwxrw-r–

B9) Write a shell script to find file properties for entered file name

echo “Enter a file name”


read file
if [ ! -e $file ] ; then
echo “File does not exit”

School of Applied Science – BSc Computer Science Page 20


Operating Systems and Linux Lab Manual
REVA University
exit
else
ftype=`ls -l $file|cut -c 1`
fper=`ls -l $file|cut -c 2-10`
fowner=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f3`
fsize=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f5`
fdate=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f6`
ftime=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f7`
fname=`ls -l $file|tr -s ‘ ‘|cut -d ” ” -f8`
fi
echo “The file type is : $ftype”
echo “The file permission is : $fper”
echo “The file owner is : $fowner”
echo “The file size is : $fsize”
echo “The file date is : $fdate”
echo “The file time is : $ftime”
echo “The file name is : $fname”

Output

sh 4a.sh
Enter a file name
2.sh
The file type is : -
The file permission is : rw-rw-r–
The file owner is : root
The file size is : 104
The file date is : 2009-04-30
The file time is : 02:31
The file name is : 2.sh

B10) Write a menu driven shell script to demonstrate the use of tail , cmp, uniq and rm commands

clear
opt=y
while [ $opt = y ]

School of Applied Science – BSc Computer Science Page 21


Operating Systems and Linux Lab Manual
REVA University
do
clear
echo "----------Menu driven program --------"
echo "1. Usage of tail command "
echo "2. Usage of cmp command"
echo "3. Usage of uniq command"
echo "4. Usage of rm -r command"
echo "enter your choice"
read ch
case $ch in
1) clear
echo "================================"
echo "tail displays lines from bottom of the file"
echo
printf "enter number of lines to display"
read n
echo
echo last $n lines of file temp "
echo "------------------------"
echo
tail -$n temp;;
2) clear
echo "================================"
echo "cmp compares two files and gives the point of
difference"
echo
echo temp1 and temp2 are compared
echo "------------------------"
echo
cmp temp1 temp2;;
3) clear
echo "================================"
echo "uniq eliminates adjacent duplicate lines"
School of Applied Science – BSc Computer Science Page 22
Operating Systems and Linux Lab Manual
REVA University
echo "======================================="
echo
echo unique lines of temp1
echo
echo "------------------------"
cat temp1 | sort | uniq;;
4) clear
echo "================================"
echo " rm -r deletes the directory along wih contents"
echo "============================================="
echo
echo "enter the subdirectory name"
read subdir
mkdir $subdir
cd $subdir
touch file1 file2
echo current directory is `pwd`
cd ..
if rm -r $subdir
then
echo $subdir removed succesffully
fi;;
*) echo "invalid choice"
esac
echo "do you like to continue ?"
read opt
done
echo
tail -$n temp;;

OUTPUT:
----------Menu driven program --------

1. Usage of tail command "

School of Applied Science – BSc Computer Science Page 23


Operating Systems and Linux Lab Manual
REVA University
2. Usage of cmp command"
3. Usage of uniq command"
4. Usage of rm -r command"
enter your choice
1
tail displays lines from bottom of the file
enter number of lines to display
10
Last 10 lines from bottom of the file

School of Applied Science – BSc Computer Science Page 24

You might also like