Unix MSc-IT LAB
Unix MSc-IT LAB
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 writes this maximum value to the standard output.
clear
if [ -z "$*" ]; then
echo "No arguments given..."
elif [ -d $1 ];then
set -- `ls -lr $1|cut -d " " -f5,8|sort -n|tail -1`
echo "$2 is the largest file with size $1"
else
echo "invalid"
fi
2. Create a script file called file-properties that reads a file name entered and
outputs it properties.
clear
if [ $# -eq 0 ];then
echo "No arguments given..."
elif [ -e $1 ];then
set -- `ls -l $1`
echo "File Permission : $1"
echo "File Links : $2"
echo "Owner Name : $3"
echo "Group Name : $4"
echo "File Size : $5"
echo "Modification Date : $6"
echo "Modification Time : $7"
echo "File name : $8"
else
echo "Invalid file name..."
fi
3. Write a shell script that gets executed displace the message either “Good
Morning” or “Good Afternoon” or “Good Evening” depending upon time at which the
user logs-in.
clear
set -- `date "+%H"`
echo $1
if [ $1 -lt 12 ];then
echo "Good Morning...."
elif [ $1 -gt 12 -a $1 -lt 17 ];then
echo "Good Afternoon..."
elif [ $1 -gt 17 -a $1 -lt 20 ];then
echo "Good Evening..."
else
echo "Good Night..."
fi
4. Write a shell script that compute gross-salary for an employee, according to the
rule given below. If basic salary = 15000 then HRA=500 of basic and DA=98% of basic.
clear
echo "Enter the basic pay of employee"
read basic
if [ $basic -lt 15000 ];
then
hra=`expr $basic \* 10 / 100`
da=`expr $basic \* 90 / 100`
else
hra=500
da=`expr $basic \* 98 / 100`
fi
gross=`expr $basic + $hra + $da`
echo "The gross salary is $gross"
5. Write a shell script to find smallest of three numbers that are read from
keyboard.
clear
echo "Enter 3 values ? "
read a b c
if [ $a -le $b -a $a -le $c ];then
echo "$a is the smallest"
elif [ $b -le $c ];
then
echo "$b is the smallest"
else
echo "$c is the smallest"
fi
6. A shell script using expr command to read in a string and display a suitable
message if it does not have at least 10 characters.
echo “Enter the string”
read str
if [ -z str ]; then
echo “null character”
else
len=`expr “$str” : ‘.*’`
if [ $len –ge 10 ]; then
echo “$str has $len character”
else
echo “$str has less than 10 character”
fi
fi
7. Write a shell script that accepts as file-name as argument and display its creation
time if file exist and if it does not send output error message.
clear
if [ $# -eq 0 ]; then
echo "No arguments given.."
elif [ -e $1 ]; then
date=`ls -l $1|cut -d " " -f6,7`
echo "The creation time of file $1 is $date"
else
echo "Invalid file name"
fi
8. Write a shell script that accepts two file name as arguments, checks if the
permission for these files are identical and if the permissions are identical, output
common permissions otherwise output each file name followed by its permissions.
clear
echo "Enter file 1 "
read f1
echo "Enter file 2 "
read f2
p1=`ls -l $f1|cut -d " " -f1`
p2=`ls -l $f2|cut -d " " -f1`
if [ $p1 == $p2 ];then
echo "File permissions are same $p1 "
else
echo "File permissions are differnt "
echo "FILE NAME $f1 : FILE PERMISSIONS :$p1"
echo "FILE NAME $f2 : FILE PERMISSIONS :$p2"
fi
9. Write a shell script that determine the period for which a specified user is
working on system and display appropriate message.
echo "enter the user name "
read usr
tuser=`who | tr -s " " | head -1 | cut -d " " -f1`
if [ "$tuser" = "$usr" ]; then
tm=`who | tr -s " " | head -1 | cut -d " " -f4`
uhr=`echo $tm | cut -d ":" -f1`
umin=`echo $tm | cut -d ":" -f2`
shr=`date "+%H"`
smin=`date "+%M"`
if [ $smin -lt $umin ]; then
shr=`expr $shr - 1`
smin=`expr $smin + 60`
fi
h=`expr $shr - $uhr`
m=`expr $smin - $umin`
echo "user name : $usr"
echo "login period : $h : $m"
else
echo "Invalid User"
fi
10. Write a shell script that accept a filename, starting and ending line number as an
argument and display all the lines between the given line number.
clear
if [ $# -lt 3 ];then
echo "invalid arguments..."
else
i=1
exec < $1
while read rec
do
if [ $i -gt $2 -a $i -lt $3 ];then
echo $rec > /dev/tty
fi
i=`expr $i + 1`
done
fi
11. A shell script to display the calendar for the current month with current date
Replace by * or ** depending on whether the date has one digit or two digits.
a=`date +%e`
if [ $a -lt 10 ]
then
cal|sed “s/ $a/ */”
else
cal | sed “s/$a/**/”
fi
12. A shell script for non-recursive, which accepts any number of arguments and
prints them in the reverse order(for example, if script is named rags, then executing
rags A B C should produce C B A on the standard output).
clear
if [ $# -lt 1 ]; then
echo "No arguments given..."
exit
else
for i in $*
do
rev=$i" "$rev
done
echo "The reverse is : $rev"
fi