Practical OS
Practical OS
Practical OS
Practical no 1
1a. Write a shell script that takes a valid directory name as an argument and
recursively descend all the sub-directories, finds the maximum length of any file
in that hierarchy and write this maximum value to the standard output.
1a.sh
for i in $* do
if [ -d $i ]
then
echo " large file size is "
echo `ls -Rl $1 | grep "^-" | tr -s ' ' | cut -d ' ' -f 5,9 | sort -n | tail -1`
else
echo " not a directory "
fi
done
P a g e |2
1b.Write a shell script that accepts a path name and creates all the components in
that path name as directories. For example, if the script is named mpc, then the
command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d.
1b.sh
done
P a g e |3
Practical no 2
2a.Write a shell script that accepts two filenames as arguments, checks if the
permissions for these files are identical and if the permissions are identical,
output common permissions and otherwise output each filename followed by
its permissions.
2a.sh
if [ $# -eq 0 ]
then
ls -l $1>f1
x=`cut -c 2-10 f1` echo
$x
ls -l $2>f2
y=`cut -c 2-10 f2`
echo $y
If [ $x = $y ] then
echo " permission of both files are same " echo
$x
else
echo "permission are not same " echo $x
echo $y
fi
fi
P a g e |4
2b.Write a shell script which accepts valid log-in names as arguments and
prints their corresponding home directories, if no arguments are specified, print
a suitable error message.
2b.sh
y=$#
i=1
if [ $y -eq 0 ] then
echo "argument are entered "
else
while [ $i -le $y ] do
loginname=$1
grep $loginname /etc/passwd > s if [ $?
-eq 0 ]
then
echo " loginname:$loginname" echo
"home directory"
cut -d ":" -f 6 s
fi
shift
i=`expr $1 + 1`
done
fi
P a g e |5
Practical no 3
3a.Create a script file called file-properties that reads a filename entered and
outputs it Properties.
3a.sh
fi
ls -l $file
P a g e |6
3b.sh
clear
else
echo "invalid passwd"
echo "re-enter passwd to conformation" stty -echo
read pass2 stty
echo
fi
done
read pass3
done
clear
echo "invalid passswd"
echo "enter passwd for conformation " stty -echo
read pass3
fi
done
stty echo
fi
stty echo
echo "terminal unlocked "
P a g e |9
Practical no 4
4a. Write a shell script that accept one or more file names as argument and
convert all of them to uppercase, provided they exist in current directory.
4a.sh
y=$#
if [ $y -le 0 ]
then
for file in $* do
echo $file
n=`echo -n "$file" | tr "[a-z]" "[A-Z]"` mv
"$file" "$n"
echo $n
done
fi
P a g e | 10
4b. Write a shell script that displays all the links to a file specified as the first
argument to the script. The second argument, which is optional, can be used to
specify in which the search is to begin. I f this second argument is not present,
the search is to begin in current working directory. In either case, the starting
directory as well as all its subdirectories at all levels must be searched. The
script need not include any error checking.
4b.sh
file=$1
if [ $# -eq 1 ] then
dirx="."
else
dirx="$2"
fi
fi
P a g e | 11
4b.sh
file=$1
set -- `ls -l $file`
lcnt=$2
if [ $lcnt -eq 1 ] then
echo "no other links" exit
else
set -- `ls -i $file`
inode=$1
find "." -xdev -inum $inode -print
fi
P a g e | 12
Practical no 5
5a. Write a shell script that accepts as filename as argument and display its
creation time if file exist and if it does not send output error message.
5a.sh
if [ $# -eq 0 ]
then
ls -l $1>t5
x=`cut -c 23-38 t5`
echo $x
fi
5aa.sh
if [ $# -eq 0 ]
then
5b. Write a shell script to display the calendar for current month with current
date replaced by *or** depending on whether the date has one digit or two
digits.
5b.sh
set `date`
echo $3
if [ $3 -le 9 ] then
n=`cal | tail -n +3 | grep -n "$3" | cut -d ":" -f1 | head -n1` n=`expr $n + 2`
cal | sed "$n s/$3/*/" cal |
fi
P a g e | 14
Practical no 6
6a. Write a shell script to find a file/s that matches a pattern given as command
line argument in the home directory, display the contents of the file and copy
the file into the directory ~/mydir.
6a.sh
if [ $# -eq 0 ] then
echo " no arguments " exit
fi
for i in $* do
ls $* cat
$*
cp -f $* /home/anju/mydir # ~/ =/home/anju
done
P a g e | 15
6b. Write a shell script to list all the files in a directory whose filename is atleast
10 characters.(use expr command to check the length
$le -le 10 ]
then
echo $str fi
P a g e | 16
Practical no 7
7a. Write a shell script that gets executed displays the message either “Good
Morning” or “Good Afternoon” or “Good Evening” depending upon time at
which the user logs in.
7a.sh
esac
P a g e | 17
7b. Write a shell script that accept a list of filenames as its argument, count and
report occurrence of e a c h word that is present in the first argument file on
other argument files.
7b.sh
if [ $# -lt 2 ]
then
echo "usage:wordcount wordfile file1 file2 ................... "
exit
fi
done
done
P a g e | 18
Practical no 8
8a. Write a shell script that determine the period for which as specified use ris
working on system and display appropriate message.
8a.sh
echo "Enter Login name of the user" read
name
userinfo=`who | grep -w "$name"` # |grep"pts"` echo $userinfo
if [ $? -ne 0 ] then
echo "$name is not Logged in" exit
fi
hrnow=`expr $hrnow - 1`
fi
8b. Write a shell script that reports the logging in of as specified user within
one minute after he/she login. The script automatically terminate if specified
user does not login during as specified period of time.
8b.sh
echo -n "enter the login name "
read lname
period=0
echo -n "enter the unit of time (min):" read
min
until who | grep -w "$lname"> /dev/null do
sleep 4
period=`expr $period + 1` if [
$period -gt $min ] then
echo "$lname has not logged in since $min minutes" exit
fi
done
Practical no 9
9a.Write a shell script that accept the filename, starting and ending line number
as an argument and display all the lines between the given line number.
9a.sh
if [ $# -ne 3 ] then
echo "invalid number of arguments" exit
fi
c=`cat $1 | wc -l`
if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ] then
echo " invalid input" exit
fi
9b. Write a shell script that folds long lines into 40 columns. Thus any line that
exceeds 40 characters must be broken after 40th , a “\” is to be appended as the
indication of folding and the processing is to be continued with the residue. The
input is to be supplied through a text file created by the user.
9b.sh
echo "enter the file name" read fn
for ln in `cat $fn` do
lgth=`echo $ln | wc -c`
lgth=`expr $lgth - 1` s=1;e=40
if [ $lgth -gt 40 ]
then
while [ $lgth -gt 40 ] do
echo "`echo $ln | cut -c $s-$e`\\" s=`expr $e +
1`
e=`expr $s + 40` lgth=`expr
$lgth - 40` done
echo $ln | cut -c $s- else
P a g e | 23
echo $ln fi
done
echo "file folded"
P a g e | 24
Practical no 10
10a. Write an awkscript that accepts date argument in the form of dd-mm-
yy and display it in the form month, day and year. The script should check the
validity of the argument and in the case of error, display a suitable message.
10a.awk
10b. Write an awkscript to delete duplicated line from a text file. The order of
the original lines must remain unchanged.
10b.awk
BEGIN {
print "removing duplicate lines"
}
{
line [++no]=$0
}
END {
for(i=1;i<=no;i++)
{
flag=1
for(j=1;j<i;j++)
if(line[i]==line[j]) flag=0
if(flag==1)
print line[i]>>"out10b.txt"
}
}
P a g e | 27
Practical no 11
11a. Write an awkscript of find out total number of books sold in each
discipline as well as total book sold using associate array down table as given
below.
Electrical 34
Mechanical 67
Electrical 80
Computer Science 43
Mechanical 65
Civil 98
Computer Science 64
11a.awk
BEGIN {print"TOTAL NUMBER OF BOOOKS SOLD IN EACH
CATAGORY"}
{books [ $1 ]+=$2}
END { for(item in books)
{ printf("\t%-17s %1s %-5d\n", item, "=",books[item]) total+=books[item]
}
printf("%-17s %1s %-5s\n", "total books sold","=", total)
}
P a g e | 28
BEGIN { FS=":"
print "\n\t\tsalary statments of employee for the month of
feb"
print
"sl.no","\t","name","\t\t","designation","\t","basic","\t","da","\t"
,"hra","\t","gross"}
{slno++;basic_tot+=$5;
if($5>=10000)
{da=0.45*$5;da_tot+=da;
hra=0.15*$5;hra_tot+=hra;} else {
da=0.50*$5;da_tot+=da;
hra=0.20*$5;hra_tot+=hra;}
sal_tot+=$5 + da +hra
printf "%2d\t%-15s %-12s %8d %8.2f %8.2f
%8.2f\n",slno,$2,$3,$5,da,hra,$5+da+hra}
END {print"\n\t basic salary paid is rs" basic_tot print"\n\t
total da paid in rs" da_tot
print"\n\t total hra paid is" hra_tot print"\n\t total
salary paid" sal_tot }
P a g e | 29
Practical
FACTORIAL OF A NUMBER
echo "Enter a number"
read num
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i))
}
echo $fact
FIND THE GREATEST OF THREE NUMBERS
echo "Enter three Integers:"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is Greatest"
elif [ $b -gt $c -a $b -gt $a ]
then
echo "$b is Greatest"
else
echo "$c is Greatest!"
fi
P a g e | 30
Practical