Vous êtes sur la page 1sur 25

UNIX LABORATORY

LAB MANUAL

(MCA- 13MCA17)

AS PER VTU SYLLUBUS (New Scheme)


For MCA II Semester of VTU

Name of the Candidate ____________________________


Reg No. ____________________________
Subject code ____________________________

Prepared by

SUDARSANAM P, Assistant Professor

DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS

BMS Institute of Technology,


Yelehanka, Bangalore- 560064.
1. a. Write a shell script that takes a valid directory name as an argument and recursively
descend all the subdirectories, finds the maximum length of any file in that hierarchy
and writes this maximum value to the standard output.

clear
if [ $# -ne 1 ]
then
echo “Invalid number of arguments passed”
exit
fi
if [ ! -d $1 ]
then
echo “Directory does not exist”
ms=`ls -lR $1 | grep ‘^-‘ | tr –s “ “ | cut -d “ “ -f5,9 | sort -n | tail -1`
echo “The max file size is $ms”

OUTPUT

$sh 1a.sh mca


The max file size is 18 c

Department of Master of Computer Applications


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

if [ $# -ne 1 ]
then
echo “Invalid number of arguments”
else
mkdir -p $1
fi

OUTPUT

$sh 1b.sh kishore/kumar/manna/day


$ls -R kishore
kishore:
kumar
kishore/kumar:
manna
kishore/kumar/manna:
day
kishore/kumar/manna/day:

$sh 2b.sh
Invalid number of arguments

Department of Master of Computer Applications


2. a. Write a shell script that accepts two file names as arguments, checks if the
permissions for these files are identical and if the permissions are identical, output
common permissions and otherwise output each file name followed by its
permissions.

clear
if [ $# -ne 2 ]
then
echo “Invalid number of arguments”
else
ls -l $1 | cut -d “ “ -f1 > file1
ls -l $2 | cut -d “ “ -f1 > file2
if cmp file1 file2
then
echo -e”\n\nBoth files have same permissions\n”
cat file1
else
echo -e “\n\nFiles have different permissions\n”
echo “The permissions of file $1”
cat file1
echo “The permissions of file $2”
cat file2
fi
fi

OUPUT

$sh 2a.sh filea fileb


Both files have same permissions
-rw-rw-r--
$chmod 644 fileb
$sh 1b.sh filea fileb
file1 file2 differ: byte 6, line1
Files have different permissions
The permissions of filea
-rw-rw-r--
The permissions of fileb
-rw-r--r--

Department of Master of Computer Applications


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

clear
if [ $# -eq 0 ]
then
echo “No command line arguments passed”
exit
fi
while [ $1 ]
do
cat /etc/passwd | cut -d “:”-f1 | grep “^$1” > temp
ck=`cat temp`
if [ “$ck” ! = “$1” ]
then
echo “Error:$1 is an invalid log-in name”
else
echo “Home Directory for $1 is:”
cat /etc/passwd | grep “$1” | cut -d “:” -f6
fi
shift
done

OUTPUT

$sh 2b.sh mca05 mca02 mca10


Home Directory for mca05 is:/home/mca05
Home Directory for mca02 is:/home/mca02
Home Directory for mca10 is:/home/mca10

Department of Master of Computer Applications


3. a. Create a script file called file-properties that reads a file name entered and outputs it
properties.

clear
echo -e “Enter the filename:\c”
read fn
if [ -f $fn ]
then
echo “File permissions are”
echo `ls -l $fn | cut -d “ “ -f1`
echo “Number of links to the file”
echo `ls -l $fn | cut -d “ “ -f2`
echo “File size”
echo `ls -l $fn | cut -d “ “ -f5`
echo “Last modified date”
echo `ls -l $fn | cut -d “ “ -f6`
else
echo “File not found”
fi

OUTPUT

$sh 3a.sh
Enter the filename:test
File permissions are
-rw-rw-r--
Number of links to the file
3
File size
48
Last modified date
Oct 9

3. b. Write shell script to implement terminal locking (similar to the lock command). It
should prompt the user for a password. After accepting the password entered by the
user, it must prompt again for the matching password as confirmation and if match
occurs, it must lock the keyword until a matching password is entered again by the

Department of Master of Computer Applications


user, Note that the script must be written to disregard BREAK, control-D. No time
limit need be implemented for the lock duration.

clear
trap “ “ 2 20
stty -echo
echo -e “Enter the password:\c”
read key1
echo -e “Re-enter the password:\c”
read key2
if [ “$key1” = “$key2” ]
then
until [ “$key3” = “$key2” ]
do
echo -e “Enter correct password to unlock:\c”
read key3
done
else
echo “Mismatched password”
fi
stty echo

OUTPUT

$sh 3b.sh
Enter the password:
Re-enter the password:
Enter correct password to unlock:
Enter correct password to unlock:

4. a. Write a shell script that accept one or more filenames as argument and convert all of
them to uppercase, provided they exist in current directory.

clear
if [ $# -eq 0 ]
then

Department of Master of Computer Applications


echo “Invalid number of arguments”
exit
fi
for fn in “$@”
do
if [ -f $fn ]
then
echo $fn| tr ‘[a-z]’ ‘[A-Z]’
else
echo “File not found”
fi
done

OUTPUT

$sh 4a.sh test


TEST

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

if [ “$2” != “ “ ]

Department of Master of Computer Applications


then
cwd=`pwd`
cd $2
l=`ls -lR $ | tr -s “ “ | cut -d “ “ -f2`
cd $cwd
else
l=`ls -lR $1 | tr -s “ “ | cut -d “ “ -f2`
fi
echo “Number of links to the file $1 :$l”

OUTPUT

$sh 4b.sh test


Number of links to the file test :3

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

clear
if [ $# -ne 1 ]
then
echo “Invalid number of arguments”
exit
fi
if [ -e $1 ]

Department of Master of Computer Applications


then
echo “File $1 is created on :`ls -l | tr -s “ “ | cut -d “ “ -f6,7,8`”
else
echo “File not found”
fi

OUTPUT

$sh 5a.sh test


File test is created on :Oct 9 11:59

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

#!bin/bash
a=`date +%e`
if [ $a -lt 10 ]
tehn
cal|sed “s/ $a/ */”
else
cal | sed “s/$a/**/”
fi

Department of Master of Computer Applications


OUTPUT

$sh 5b.sh

April 2008
M T W T F S S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 ** 19 20 21 22 23
24 25 26 27 28 29 30
31

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

clear
if [ $# -eq 0 -o $# -gt 1 ]
then
echo "invalid files "
exit
else if [ -d mydir ]
then
cd mydir
rm *
cd ..

Department of Master of Computer Applications


rmdir mydir
fi
mkdir ~/mydir
find . -name $1 > findfiles 2> err.lst
if [ -s findfiles ]
then
echo " search sucessfull"
else
echo " searchunsucessfull"
fi
if [ -s err.lst ]
then
echo " give correct pattern"
exit
fi
for i in `cat findfiles`
do
echo -e "contents"
cat $i
cp $i ~/mydir/
done
fi
out put:
$sh 6a.sh
invalid files
$sh 6a.sh 6a.txt
search sucessfull

contents
1 Sudarsanam

2 Anil kumar

3 Lakshmi

4 Vasuki

5 Prhvi

6 Charan

7 VTU

8 Beguam

Department of Master of Computer Applications


Department of Master of Computer Applications
6. b. 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 lenght)

clear

ls >listfiles
echo -e "the list of files greater than a\n"
for i in `cat listfiles`
do
len=`expr length "$i"`
if [ $len -gt 9 ]
then
echo $i
fi

done

OUTPUT

$sh 6b.sh
the list of files greater than 9
example1.sh
example2.sh
example3.sh
example4.sh

Department of Master of Computer Applications


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.

clear
h=`who | head -1 | tr -s “ “ | cut -d “ “ -f4 | cut -d “:” -f1`
if [ $h -lt 12 ]
then
echo “Good Morning”
elif [ $h -ge 12 -a $h -lt 17 ]
then
echo “Good Afternoon”
else
echo “Good Evening”
fi

OUTPUT

$sh 7a.sh

Good Afternoon

Department of Master of Computer Applications


7. b. Write a shell script that accept a list of filenames as its argument, count and
report occurrence of each word that is present in the first argument file on other argument
files.
clear
if [ $# -lt 2 ]
then
echo " invalid arguments"
exit
fi
for word in `cat $1`
do
for file in $*
do
if [ $file != "$1" ]
then
echo -e "count :$word in $file"
echo -e `grep -iow "$word" $file|wc -w`
fi
done
done

OUTPUT
cat f1 cat f2
suda suda
mca suda
deepthi mca
charan mca

$sh 7b.sh f1 f2
count Word=suda in f2
2
count Word=mca in f2
2
count Word=charan in f2
0

Department of Master of Computer Applications


8a. Write a shell script that determine the period for which a specified user is working
on system and display appropriate message.

echo -e “Enter the username\t:\c”


read usr
tm=`who | grep $usr | 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”`
h=`expr $shr - $uhr`
m=`expr $smin - $umin`
echo “Username :$usr”
echo “Login period :$h:$m”

OUTPUT

$sh 8a.sh
Enter the Username : mca05
Username : mca05
Login period :00:17

Department of Master of Computer Applications


8. b. Write a shell script that reports the logging in of a specified user within one minute
after he/she log in. The script automatically terminate if specified user does not log
in
during a specified period of time.

echo -e “Enter the Username :\c”


read usr
umin=`who | grep $usr | tr -s “ “ | head -1 | cut -d “ “ -f4 | cut -d “:” -f2`
smin=`date “+%M”`
m=`expr $smin - $umin`
if [ $m -lt 1 ]
then
echo “Username :$usr has logged in”
else
echo “ $usr has not logged in specified time”
fi

OUTPUT

$sh 8b.sh
Enter the username :mca05
Username :mca05 has logged in

Department of Master of Computer Applications


9. a. Write 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 [ $# -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
Invalid number of arguments
cat 9a.txt
1 Sudarsanam
2 Anil kumar
3 Lakshmi
4 Vasuki
5 Prhvi
6 Charan
7 VTU
8 Beguam

OUTPUT
$sh 9a.sh

$sh 9a.sh test 4 7


4 Vasuki
5 Prhvi
6 Charan
7 VTU

Department of Master of Computer Applications


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

clear
echo -e " Enter the filename :\c"
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 $e + 40`
lgth=`expr $lgth - 40`
done
echo $ln | cut -c $s-
else
echo $ln
fi
done
echo "File Folded "

OUTPUT
cat 9b.txt
WriteashellscriptthatfoldslonglinesintocolumnThusanylinethatexceeds40
$sh 9b.sh
Enter the filename:9b.txt
Writeashellscriptthatfoldslonglinesintoc\
olumnThusanylinethatexceeds40
File folded

Department of Master of Computer Applications


10. a. Write an awk script that accepts date argument in the form of dd-mm-yy and
displays it in the form if month, day and year. The script should check the
validity of the argument and in the case of error, display a suitable message.

BEGIN {
FS="-";
printf " Day Month Year\n";
}
{
printf " \n The date is %d-%d-%d\n ", $1,$2,$3;
if (( $1 >=1 && $1 <=31) && ($2 >=1 && $2 <=12))
{
printf "The date is valid\t:";
if($2==1)mon="Jan";
else if($2==2) mon="Feb";
else if($2==3) mon="Mar";
else if($2==4) mon="Apr";
else if($2==5) mon="May";
else if($2==6) mon="Jun";
else if($2==7) mon="Jul";
else if($2==8) mon="Aug";
else if($2==9) mon="Sep";
else if($2==10) mon="Oct";
else if($2==11) mon="Nov";
else if($2==12) mon="Dec";
printf "Date is %s-%d-%d", mon,$1,$3;
}
else
printf " date is invalid\n"
system ("exit");
}

output
cat 10a.txt
21-10-2014
12-12-13
32-12-65

$awk -f 10a.awk 10a.txt


Day Month Year

The date is 21-10-2014


The date is valid :Date is Oct-21-2014
The date is 12-12-13
The date is valid :Date is Dec-12-13
The date is 32-12-65
date is invalid

Department of Master of Computer Applications


10. b. Write an awk script to delete duplicated line from a text file. The
order of the original lines must remain unchanged.

BEGIN {
printf " Program starts\n" ;
}
{
if ( data[$0]++ == 0 )
line[++count]=$0;
}
END {
for (i=1; i<= count; i++)
printf "\n" line[i];
printf " program ends";
}

output:
cat 11b.txt
Ajit
Anil
Lakshmi
Ajit
Anil
Prinka
$awk -f 11b.awk 11b.txt
Ajit
Anil
Lakshmi
Prinka

Department of Master of Computer Applications


11a. Write an awk script to 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

BEGIN {
printf " Total number of books each category\n"
}
{
b[$1]+=$2
}
END{
for(item in b)
{
printf "%s %s %d \n", item , "=", b[item];
total+=b[item];
}
printf "%s %s %s \n", "total books", "=",total;
}
cat 11a.dat
Electrical 34
Mechanical 67
Electrical 80
Computer 43
Mechanical 65
Civil 25
$awk -f 11a.awk 11a.dat
Civil 25
Mechanical 132
Electrical 114
total Books = 314

Department of Master of Computer Applications


11. b. Write an awk script to compute gross salary of an employee accordingly to rule
given below.
If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic
If basic salary is >=10000 then HRA=20% of basic & DA=50% of basic.

BEGIN {
printf " ENo ENAME DOJ EDEPT Esalary";
}
{
if($5 >= 1000)
{
hra=$5*0.20;
da=$5*0.50;
}
else
{
hra=$5*0.15;
da=$5*0.45;
}
gs=$5+hra+da;
printf " %s %s %s %s %d %d \n ",$1,$2,$3,$4,$5,gs
}

output
cat 11b.dat
e101 lakshmi 11/8/14 MCA 15000
e102 akhil 12/9/14 ECE 8000
$awk -f 11b.awk 11b.dat
ENo ENAME DOJ EDEPT Esalary
e101 lakshmi 11/8/14 MCA 15000 25500
e102 akhil 12/9/14 ECE 8000 13600

or
BEGIN { printf “Etner the Bsic Pay:Rs.”
getline bp < “/dev/tty”
if(bp<10000)
{
hra=.15*bp
da=.45*bp
}
else
{
hra=.2*bp
da=.5*bp
}
gs=bp+hra+da
printf “Gross Salary = Rs. %.2f\n”,gs
}

Department of Master of Computer Applications


OUTPUT

$awk -f 11b.awk
Enter the Basic Pay:Rs.10000
Gross Salary = Rs.17000

Department of Master of Computer Applications

Vous aimerez peut-être aussi