Unix Shell Scripts
Unix Shell Scripts
else
echo " not a directory "
fi
done
Page |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.
2b.sh
echo " enter the pathname"
read p
i=1
j=1
len=`echo $p | wc -c`
while [ $i -le $len ]
do
x=`echo $p | cut -d ' ' -f $j`
namelength=`echo $x | wc -c`
mkdir -p $x
cd $x
pwd
j=`expr $j + 1`
i=`expr $i + $namelength`
echo $j
done
Page |3
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
echo " argument not entered sorry try again "
else
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
Page |4
else
echo "invalid passwd"
echo "re-enter passwd to conformation"
stty -echo
read pass2
stty echo
fi
done
if [ $pass1 = $pass2 ]
then
echo "terminal is locked "
echo " enter passwd to unlock"
stty -echo
read pass3
val=1
read pass3
done
if [ $pass3 = $pass2 ]
then
val=0
else
clear
echo "invalid passswd"
echo "enter passwd for conformation "
stty -echo
read pass3
fi
done
stty echo
fi
stty echo
echo "terminal unlocked "
Page |9
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
echo "argument not entered"
else
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
set -- `ls -l $file`
lcnt=$2
if [ $lcnt -eq 1 ]
then
echo "no other links"
exit 0
else
set -- `ls -i $file`
inode=$1
find "$dirx" -xdev -inum $inode -print
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
5aa.sh
if [ $# -eq 0 ]
then
echo "argument not entered"
else
set -- `ls -l $1`
echo " creation date and time is $5 $6 $7 $8"
fi
P a g e | 13
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/*/"
else
cal | sed "s/$3/**/"
fi
P a g e | 14
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
if [ $le -le 10 ]
then
else
echo $str
fi
P a g e | 16
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
hournow=`date | cut -c 12-13`
echo $hournow
echo $LOGNAME
case $hournow in
[0-1][0-1]|0[1-9])echo "good morning";;
1[0-1])echo "good mprning";;
1[2-6])echo "good noon";;
1[7-9])echo "good evening";;
2[0-1])echo "good evening";;
*)echo "good night";;
esac
P a g e | 17
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
hrs=`echo "$userinfo" | tr -s " " | cut -c 20-21`
min=`echo "$userinfo" | tr -s " " | cut -c 23-24`
echo "login hour" $hrs
echo "login min " $min
hrnow=`date | tr -s " " | cut -c 12-13` #get current time(hrs)
echo "current hour " $hrnow
minnow=`date | tr -s " " | cut -c 15-16` #get Cur time(min)
echo "current min " $minnow
if [ $minnow -le $min ]
then
minnow=`expr $minnow + 60`
P a g e | 19
hrnow=`expr $hrnow - 1`
fi
hour=`expr $hrnow - $hrs`
minutes=`expr $minnow - $min`
echo "$name is working since $hour hours and $minutes minutes"
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
sed -n "$2,$3 p" $1
P a g e | 22
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
if (arr[2] == 11)
print "nov"
if (arr[2] == 12)
print "dec"
print arr[1]
print arr[3]
}
}
P a g e | 26
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 }