OS Lab Report # 04 & 05 by Kamran Jalil 19pwcse1751
OS Lab Report # 04 & 05 by Kamran Jalil 19pwcse1751
Spring 2021
Submitted to:
Engr. Mian Ibad Ali Shah
03 May, 2021.
Assignment Problems on UNIX SHELL programming
1. Run all the programs given in the Lab Notes, and observe the output for each
program.
Done
Example:
echo Please Enter source file name:
read source
echo Enter the target file name:
read target
cp $source $target
echo file $source is copied into the $target
Example:
echo the parameters passed are: $1, $2, $3, $4, $5
echo the name of the script is: $0
echo the number of parameters passed are: $#
sum=`expr $1 + $2 + $3 + $4 + $5`
echo the sum is: $sum
Example:
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo sum is $sum
Example:
echo Enter a:
read a
echo Enter b:
read b
if [ $a == $b ]
then
echo a is equal to b
elif [ $a -gt $b ]
then
echo a is greater than b
elif [ $a -lt $b ]
then
echo a is less than b
else
echo None of the condition met
fi
2. Write a shell script that takes a keyword as a command line argument and lists the
filenames containing the keyword.
Code:
gedit labtasks
3. Write a shell script that takes a command line argument and reports whether it is a
directory, or a file or a link.
Code:
echo enter a name of the File or directory
read name
test -f $name
$?=$a
test -d $name
$?=$b
if [$a == 0 -a $b == 1]
then
echo $name is a file
elif [$a == 1 -a $b == 0]
then
echo $name is a Directory
else
echo It is a link
fi
4. Write a script to find the number of sub directories in a given directory.
Code:
echo enter the name of directory
read ndr
cd $ndr
ls > drl
sort drl
echo number of subdirectories are: ` wc -l drl`
5. Write a menu driven program that has the following options.
a. Search a given file is in the directory or not.
b. Display the names of the users logged in.
Code:
echo Enter One of the following
echo 1. Search a given file in Current Directory
echo 2. Display the names of users logged in
read text
case $text in
1) echo Enter a File Name
read name
find . -name $name -print;;
2) who;;
*********************************