Professional Documents
Culture Documents
REVA University
B19MC4090
CONTENTS
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.
commands
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
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 ]
OUTPUT:
Sh primenumber.sh
Enter lower limit
1
Enter upper limit
5
Prime numbers between 1 and 5 are
2
3
OUTPUT:
Sh palindrome.sh
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
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`
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
OUTPUT:
Enter the first number
15
Enter the second number
35
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
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
PART-B
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.
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.
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
clear
read str
len=`expr $len - 1`
do
echo -n $ch
len=`expr $len - 1`
done
OUTPUT
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
clear
hours=`date|cut -c12-13`
if [ $hours -le 12 ]
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
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
B9) Write a shell script to find file properties for entered file name
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 ]
OUTPUT:
----------Menu driven program --------