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

Practical Programs

This document contains 5 shell scripts with explanations and code to perform tasks like accepting integers and counting positives/negatives/zeros, accepting student details and calculating grade, copying contents of one file to another, creating a menu-driven script for listing, renaming files and directories, and accepting multiple filenames from command line and displaying file contents or number of files in a directory.

Uploaded by

afzal ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Practical Programs

This document contains 5 shell scripts with explanations and code to perform tasks like accepting integers and counting positives/negatives/zeros, accepting student details and calculating grade, copying contents of one file to another, creating a menu-driven script for listing, renaming files and directories, and accepting multiple filenames from command line and displaying file contents or number of files in a directory.

Uploaded by

afzal ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operating System Practical Manual: Shell Scripts.

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.

#!/bin/bash
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"

Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 1
Contact: [email protected], [email protected].
Operating System Practical Manual: Shell Scripts.

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).

#!/bin/bash
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 -gt 35 -a $m2 -gt 35 -a $m3 -gt 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
else
echo Grade=FAIL
fi
else
echo Grade=FAIL
fi

Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 2
Contact: [email protected], [email protected].
Operating System Practical Manual: Shell Scripts.

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.

#!/bin/bash
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, Dept. of Computer Science, FMKMC College, Madikeri. Page 3
Contact: [email protected], [email protected].
Operating System Practical Manual: Shell Scripts.

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.

#!/bin/bash
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, Dept. of Computer Science, FMKMC College, Madikeri. Page 4
Contact: [email protected], [email protected].
Operating System Practical Manual: Shell Scripts.

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.

#!/bin/bash
for i in $*
do
if [ -f $i ]
then
echo Contents of $i is:
cat $i
if [ -x $i ]
then
echo The file $i has execute permission
else
echo The file $i has no execute permission
fi
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, Dept. of Computer Science, FMKMC College, Madikeri. Page 5
Contact: [email protected], [email protected].

You might also like