You are on page 1of 6

Field Marshal K M Cariappa College

Department of Computer Science

Operating System Concepts


(For Second Semester B.Com – Vocational Course) Practical
2019 – 20
Manual
Shell Script – 1
Write a shell script to accept 'N' integers and count +ves, -ves and zeros separately. Also find the sum of +ves, and -ves.

pc=0
nc=0
zc=0
psum=0
nsum=0
i=0
n=0
num=0
echo "\n Enter the limit: "
read n
while [ $i -lt $n ]
do
echo "\n Enter the number: "
read num
if [ $num -eq 0 ]
then
zc=`expr $zc + 1`
elif [ $num -lt 0 ]
then
nc=`expr $nc + 1`
nsum=`expr $nsum + $num`
elif [ $num -gt 0 ]
then
pc=`expr $pc + 1`
psum=`expr $psum + $num`
fi
i=`expr $i + 1`
done
echo "\n Positive Count : $pc"
echo "\n Positive Sum : $psum"
echo "\n Negative Count : $nc"
echo "\n Negative Sum : $nsum"
echo "\n Count of Zeroes : $zc"

Shell Script – 1
a shell script to accept 'N' integers and count +ves, -ves and zeros separately. Also find the sum of +ves, and -ves.
Shell Script – 2

Write a shell script to accept student name and marks in 3 subjects. Find the total marks and grade (depending on the
total marks).

name=$1
m1=$2
m2=$3
m3=$4
total=0
avg=0
echo name=$1
echo m1=$2
echo m2=$3
echo m3=$4
total=`expr $m1 + $m2 + $m3`
echo Total=$total
avg=`expr $total \/ 3`
echo Average=$avg%
if [ $m1 -ge 35 -a $m2 -ge 35 -a $m3 -ge 35 ]
then
if [ $avg -ge 70 ]
then
echo Grade=DISTINCTION
elif [ $avg -ge 60 -a $avg -lt 70 ]
then
echo Grade=FIRST CLASS
elif [ $avg -ge 50 -a $avg -lt 60 ]
then
echo Grade=SECOND CLASS
elif [ $avg -ge 35 -a $avg -lt 50 ]
then
echo Grade=PASS
fi
else
echo Grade=FAIL
fi

Prepared by: Afzal, Lecturer, Department of Computer Science, FMKMC College, Madikeri.
3
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com
Shell Script – 3
Write a shell script program to copy the content of one file1 to file2 and display the content of both the files.

echo enter the source filename:


read f1
if [ -f $f1 ]
then
echo enter destination filename:
read f2
if [ -f $f2 ]
then
cp -i $f1 $f2
echo file copied successfully. \\n\\n
echo Contents of source and destination files:
cat $f1 $f2
fi
fi

Prepared by: Afzal, Lecturer, Department of Computer Science, FMKMC College, Madikeri.
4
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com
Shell Script – 4
Write a menu driven shell script for the following.
a) To list files and directories.
b) Renaming a file (check for the existence of the source file).
c) To display the current working directory.
d) To list the users logged in.
e) Exit.

i=1
while [ $i -eq 1 ]
do
echo 1: To list files and directories.
echo 2: To Rename a File.
echo 3: To display Current Working Directory.
echo 4: To display the list of Users logged in.
echo 5: To Quit.
echo Enter the Choice:
read ch
case $ch in
1) ls
;;
2) echo Enter the Filename to be renamed:
read f2
if [ -f $f2 ]
then
echo Enter a new Filename:
read new
mv $f2 $new
echo File Renamed.\\n\\n
ls
else
echo File does not exist!!
fi
;;
3) echo The Current Working Directory is:
pwd
;;
4) echo The list of Users logged in:
who
;;
5) echo Exiting......!
exit
;;
esac
done

Prepared by: Afzal, Lecturer, Department of Computer Science, FMKMC College, Madikeri.
5
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com
Shell Script – 5
Write a shell script to accept many filenames through command line. Do the following for each file name:
a) If it is an ordinary file display its content.
b) If it is a directory, display the number of files in it.
c) If the file/directory does not exist, display a Message.

for i in $*
do
if [ -f $i ]
then
echo Contents of $i is:
cat $i
elif [ -d $i ]
then
echo $i is a directory. The number of files in $i is:
cd $i
ls | wc -l
else
echo File or Directory does not exist!
fi
done

Prepared by: Afzal, Lecturer, Department of Computer Science, FMKMC College, Madikeri.
6
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com

You might also like