You are on page 1of 20

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

UNIX LAB MANUAL


Instructions for using UNIX
Step 1: start the linux operating system Step 2: click on applications go to system tools click on terminal Step 3: type vi program_name.sh Step 4: type the program to save it and execute type esc : wq Step 5: on the terminal type sh program_name.sh

Program1a
Write a non-recursive shell script which accepts any number of arguments and prints them in the reverse order (For example, if the script is named args, then executing args A B C should produce C B A on the standard output).

if [ $# -eq 0 ] then echo "No argument" exit fi for i in $* do y=$i" "$y done echo $y

Output
$ sh 1a.sh 1 2 3 3 21
Lecturer: Syed Khutubuddin Ahmed Page 1

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program1b
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.
if [ $# -ne 2 ] then echo "No arguments" elif [ ! -e $1 -o ! -e $2 ] then echo "FIle does not exist" else p1=`ls -l $1|cut -c2-10` p2=`ls -l $2|cut -c2-10` if [ $p1 == $p2 ] then echo "File permissions are equal & is $p1" else echo "File permissions are not equal" echo "1st file $p1" echo "2nd file $p2" fi fi

Output
$ sh 1b.sh a b File permissions are equal & is rwxrwxrwx

Program2a
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. if [ $# -ne 1 ] then echo "No argument" exit fi if [ ! -e $1 ] then echo "The given directory does not exist" exit
Lecturer: Syed Khutubuddin Ahmed Page 2

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

fi echo "The file with maximum length $1 dir is" ls -lR $1|tr -s " "|cut -d " " -f 5,10|sort -n|tail -n 1

Output
$ sh 2a.sh sharan The file with maximum length sharan dir is 252

Program2b
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 2b.sh a/b/c/d $ls R a a b a/b c a/b/c

Lecturer: Syed Khutubuddin Ahmed

Page 3

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

d a/b/c/d

Program3a
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 argument 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 login name" else echo "Home Directory for $1 is" echo `cat /etc/passwd | grep "^$1" | cut -d ":" -f6` fi shift done

Output
$ 3a.sh student Home Directory for student is /home/student

Lecturer: Syed Khutubuddin Ahmed

Page 4

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program3b
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 user, Note that the script must be written to disregard BREAK, control-D. No time limit need be implemented for the lock duration.
stty echo while true do clear echo "Enter the paasword" read pass1 echo "Re enter the password" read pass2 if [ $pass1 = $pass2 ] then echo "Terminal locked" echo "To unlock enter the password" pass1="" until [ "$pass1" = "$pass2" ] do read pass1 done echo "Terminal unlocked" stty echo exit else echo "Password mismatch retype it" fi done

Output
$ sh 3b.sh Enter the password Re enter the password Terminal locked To unlock enter the password Terminal unlocked

Lecturer: Syed Khutubuddin Ahmed

Page 5

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program4a
Create a script file called file-properties that reads a file name entered and outputs it properties.
echo "Enter a file name" read file if [ -f $file ] then set - -`ls -l $file` echo "file permission : $1" echo "number of links : $2" echo "User name : $3" echo "Owner name : $4" echo "Block size : $5" echo "Date of modification : $6 : $7" echo "Time of modification : $8" echo "Name of file : $9" else echo "File does not exist" fi

Output
$ sh 4a.sh Enter a file name Sharan File permissions : -rw-rw-r Number of links :1 User name :santosh Owner name: santosh Block size 24 Date of modification Nov 28 Time of modification 12:49 Name of file : sharan

Program4b
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.
for f in $* do Lecturer: Syed Khutubuddin Ahmed Page 6

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

if [ -e $f ] then cat $f|tr "[a-z]" "[A-Z]">tmp mv tmp $f else echo "File $f does not exist" fi done

Output
$ sh 4b.sh Cat >sharan Hi how are you $ sh 4b.sh sharan Cat sharan HI HOW ARE YOU

Program5a
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" != " " ] then cwd=`pwd` cd $2 link=`ls -l $1 | tr -s " " | cut -d " " -f2` cd $cwd else link=`ls -l $1 | tr -s " " | cut -d " " -f2` fi echo "Number of linkes of file $1: $link"

Lecturer: Syed Khutubuddin Ahmed

Page 7

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Output
$ sh 5a.sh kns Number of links of file kns : 4 1

Program5b
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.
if [ $# -eq 0 ] then echo "No argument" exit fi if [ -f $1 ] then time=`ls -l $1|cut -c 33-59` echo "File $1 has created on $time" else echo "File $1 does not exist" fi

Output
$ sh 5b.sh sharan File sharan has created on 2010-12-13 14:49 sharan

Lecturer: Syed Khutubuddin Ahmed

Page 8

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program6a
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.
set `date` y=$3 if [ $y -le 9 ] then cal |sed "s/$3/*/" else cal |sed "s/$3/**/" fi

Output
$ sh 6a.sh December 2010 su mo tu we th 1 5 6 * 8 12 13 14 15 19 20 21 22 26 2* 28 29

fr sat 2 3 4 9 10 11 16 1* 18 23 24 25 30 31

Program6b
Write a shell script to find smallest of three numbers that are read from keyboard.
echo "Enter the Three Numbers" read a b c if [ $a -le $b -a $a -le $c ] then echo "$a is Smallest" elif [ $b -le $c -a $b -le $a ] then echo "$b is Smallest" else echo "$c is Smallest" fi

Lecturer: Syed Khutubuddin Ahmed

Page 9

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Output
$ sh 6b.sh Enter the three numbers 2 3 1 is smallest

Program7a
Write 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 a String" read str c=`expr "$str" : '.*'` if [ $c -lt 10 ] then echo "The String has Less than 10 charecter" else echo "The String has $c charecters" fi

Output
$ sh 7a.sh Enter a string Hi how are you The string has 14 charecters

Program7b
Write a shell script to compute the sum of number passed to it as argument on command line and display the result.
sum=0 for i in $* do sum=`expr $sum + $i` done echo "Sum of Number is : $sum"

Lecturer: Syed Khutubuddin Ahmed

Page 10

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Output
$ sh 7b.sh 1 2 3 Sum of Number is : 6

Program8a
Write a shell script that compute gross salary of an employee, accordingly to rule given below. If basic salary is < 15000 then HRA=10% of basic 7 DA=90% of basic. If basic salary is >=15000 then HRA=500 of basic & DA=98% of basic.
Echo enter Basic salary read basic if [ $basic -lt 15000 ] then hra=`expr 10 \* $basic / 100` da=`expr 90 \* $basic / 100` else hra=`expr 50 \* $basic / 100` da=`expr 98 \* $basic / 100` fi gs=`expr $basic + $hra + $da` echo "Gross salary : Rs. $gs"

Output
$ sh 8a.sh Enter Basic salary 10000 Gross salary : Rs . 20000

Lecturer: Syed Khutubuddin Ahmed

Page 11

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program8b
Write a shell script that delete all lines containing a specific word in one or more file supplied as argument to it.
if [ $# -eq 0 ] then echo "Please enter one or more filenames as argument" exit fi echo "Enter the word to be searched in files" read word for file in $* do sed "/$word/d" $file | tee tmp mv tmp $file done

Output
$ sh 8b.sh kns Enter the word to be searched in a file are What about you

Program9a
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.
echo "Greetings of the day" echo `date` h=`date |cut -c 12-13` if [ $h -ge 0 -a $h -lt 12 ] then echo "Good Morning" elif [ $h -ge 12 -a $h -lt 18 ] then echo "Good Afternoon" elif [ $h -ge 18 -a $h -lt 20 ] then echo "Good Evening" else Lecturer: Syed Khutubuddin Ahmed Page 12

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

echo "Good Night" fi

Output
$ sh 9a.sh Greeting of the day The Dec 7 03:01:28 EST 2010 Good Morning

Program9b
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.
if [ $# -ne 2 ] then echo "Error :invalid no of arguments " exit fi str=`cat $1 | tr '\n' ' '` for a in $str do echo "word=$a , count=`grep -c "$a" $2` " done

Output
$ sh 9b.sh a b word=Sharanakumar, count=0 word=Shridharmurthy,count=1 word=Santoshkumar count=1 word=Shivaprasad count=0

Program10a
Write a shell script that determine the period for which a specified user is working on system.
echo -e "enter the user name :\c" read usr tuser=`who | tr -s " " | head -1 | cut -d " " -f1` if [ "$tuser" = "$usr" ] Lecturer: Syed Khutubuddin Ahmed Page 13

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

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

Output
$ sh 10.sh Enter the user name :santosh User name :santosh Login period :-19:41

Program10b
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.
a=`date +%M` b=`expr $a + 1` while [ $b -gt `date +%M` ] do who | grep $1 if [ $? -eq 0 ] then echo "$1 has logged in 1 minute" exit fi done echo "$1 has not looged in within 1 minute" Lecturer: Syed Khutubuddin Ahmed Page 14

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Output
$ sh 10b.sh santosh Santosh tty1 2010-12-06 21:30 (:0)

Program11a
Write a shell script that accepts two integers as its argument and compute the value of first number raised to the power of second number.
if [ $# -ne 2 ] then echo "Invalid number of arguments" exit fi pwr=`echo $1^$2 |bc` echo "$1 raised to $2 is : $pwr"

Output
$ sh 11a.sh 3 3 raised to 3 is :27

Program11b
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.
if [ $# -ne 3 ] then echo "Pass minimum three 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 "Invaid Input" exit Lecturer: Syed Khutubuddin Ahmed Page 15

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

fi sed -n "$2,$3p" $1

Output
$ sh 11b.sh a 2 5 Sharan Shri Santosh Shiva

Program12a
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.
{ if(length($0)<40) printf"%s",$0 else { x=$0 while(length(x)>40) { printf"%s\n",substr(x,1,40) x=substr(x,41,length(x)-40) } printf"%s\n",x } }

Output
$ awk f 12a.sh file C is the structure oriented language C++ Is the object oriented language

Lecturer: Syed Khutubuddin Ahmed

Page 16

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Program12b
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 the case of error, display a suitable message.
BEGIN { FS="-" f=1; printf("enter date with (mm-dd-yy) format:"); getline < "/dev/tty" if(((($3%4!=0) && ($1==2) && ($2>28)) || (($3%4==0) && ($1==2) && ($2>29))) || ((($1==1) || ($1==3) || ($1==5) || ($1==7) || ($1==8) || ($1==10) || ($1==12)) && ($2>31)) || ((($1==4) || ($1==6) || ($1==9) || ($1==11)) && ($2>30)) || ($1<1) || ($2<1) || ($3<1) || ($1>12)) f=0; if(f==0) printf("you have entered an invalid date\n"); else printf("\n \tThe date= %d\nThe month=%d\nThe year is=%d\n is valid date\n",$2,$1,$3); }

Output
$ awk -f 12b.sh Enter date with(mm-dd-yy)format :09-21-2010 The date =21 The month =9 The year is =2010 Is valid date

Program13a
Write an awk script to delete duplicated line from a text file. The order of the original Lines must remain unchanged.
BEGIN {i=1} { flag=1; for(j=1;j<i && flag ; j++) { if(x[j]==$0) Lecturer: Syed Khutubuddin Ahmed Page 17

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

flag=0; } if (flag) { x[j]=$0; printf("\n%s",x[i]); i++; } }

Output
$ awk f 13a.sh a Sharanakumar Shridhar Santosh Shiva

Program13b
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-1 67 Electrical-1 Mechanical Civil 80 65 198 Computer Science Civil-1 43 198

Computer Science-1 64

BEGIN{ printf "enter the mechanical books:" getline mc <"/dev/tty" printf "enter the electrical books:" getline ec <"/dev/tty" printf "enter the computer books:" getline cs <"/dev/tty" printf "enter the civil books:" getline ci <"/dev/tty" } { Lecturer: Syed Khutubuddin Ahmed Page 18

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

if(NR==1) tot1=$2-mc if(NR==2) tot2=$2-ec if(NR==3) tot3=$2-cs if(NR==4) tot4=$2-ci } { if(NR==4) { printf "total mechanical books present:%d\n",tot1 printf "total electrical books present:%d\n",tot2 printf "total computer books present:%d\n",tot3 printf "total civil books present:%d\n",tot4 } }

Program14
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"enter the basic salary: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 } Lecturer: Syed Khutubuddin Ahmed Page 19

KNS INSTITUTE OF TECHNOLOGY

DEPT OF MCA

Output
$ awk -f 14.sh Enter the basic salary :Rs 25000 Gross salary =Rs. 42500.00

Lecturer: Syed Khutubuddin Ahmed

Page 20

You might also like