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

Unix Lab Manual MCA1

This document contains 9 scripts with different functions: 1. A script that recursively finds the maximum file size in a directory and its sub-directories. 2. A script that displays a greeting ("Good Morning", "Good Afternoon", etc.) based on the current time. 3. A script that displays file properties like permissions, size, owner for a given file name. 4. A script that displays the creation time of a file if it exists, or shows an error if not. 5. A script that lists files with names at least 10 characters long.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Unix Lab Manual MCA1

This document contains 9 scripts with different functions: 1. A script that recursively finds the maximum file size in a directory and its sub-directories. 2. A script that displays a greeting ("Good Morning", "Good Afternoon", etc.) based on the current time. 3. A script that displays file properties like permissions, size, owner for a given file name. 4. A script that displays the creation time of a file if it exists, or shows an error if not. 5. A script that lists files with names at least 10 characters long.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

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

1|Page
3. Create a script file called file-properties that read 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

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

2|Page
5. Write a shell script to list all the files in a directory whose filename is at least 10 characters (use expr
command to check the length)
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

6. 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`

3|Page
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

7. A shell script that accept the file name, 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

4|Page
8. Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form if
day, month, and year. The script should check the validity of the argument and in case of error, display a
suitable message.
BEGIN{
printf("\n PROGRAM STARTED...")
FS="-"
}
{
printf("\n ENTERED DATE IS %d %s %d %s %d",$1,"-",$2,"-",$3)
if($1>1 && $1 <=31)
printf("\n VALID DATE")
else
{
printf("\n INVALID DATE")
}
if($1==1)mon="Jan"
else if($1==2)mon="Feb"
else if($1==3)mon="Mar"
else if($1==4)mon="Apr"
else if($1==5)mon="May"
else if($1==6)mon="June"
else if($1==7)mon="July"
else if($1==8)mon="Aug"
else if($1==9)mon="Sep"
else if($1==10)mon="Oct"
else if($1=11)mon="Nov"
else mon="Dec"
printf("\n Date in new format is %d %s %s %s %d\n",$2,"/",mon,"/",$3)
}
END{
printf("\n END OF PROGRAM...")
}

5|Page
9. Write an awk script to delete duplicated line from a text file. The order of the original lines must
remain unchanged.

BEGIN{
printf("\n Before removing duplicate lines\n")
system("cat dina")
printf("\n After removing duplicate lines\n")
}

{
line[++count]=$0

}
END{

for (i=1;i<=count;i++)
{
flag=1
for(j=1;j<i;j++)
if(line[i]==line[j])
flag=0
if(flag==1)
printf("\n")line[i]>>"dina"
}
system("cat dina")
}

6|Page

You might also like