8/17/2019 Unix LAB fILE (2)
1/32
Q.1 Write a Shell Script that takes a search string and filename
from the terminal & displays the results.
Ans. read aread b
word=`grep $a $b`if test `echo $word | wc -c` -eq 1thenecho "Pattern not found"elsegrep $a $bfi
Outputsh ss1Enter the string to be searchedAshsEnter the file naess!Pattern not found
8/17/2019 Unix LAB fILE (2)
2/32
Q.2 Write a Shell Script that takes pattern and filename as
command line arguments and displays the results appropriately i.e.
pattern found/pattern not found.
Ans.if test $ -ne !thenecho "#nalid no. of arguents"elseword=`grep $1 $!`if test `$word|wc -c` -eq 1then
echo "Pattern not found"elsegrep $1 $!echo %Pattern &ound'fifi
Output
sh ss! (ontents ss1!echo ")n" (ontents are sae *econd file is being deletedecho")n"(ontents are differentPattern &ound
8/17/2019 Unix LAB fILE (2)
3/32
Q.3 Write a Shell Script that accepts only three arguments from the
command line. he first argument is the pattern string! the second
argument is the filename in "hich the pattern is to #e searches and
the third argument is the filename in "hich the result is to #e stored.Ans. if test $ -ne +thenecho "#nalid no. of arguents"elsegrep $1 $! |cat,$+if test -s $+then
echo "Pattern found"cat $+elseecho Pattern not foundfiOutput$
sh ss+ echo ss1! outputPattern found
echo ")n" (ontents are sae *econd file is being deletedecho")n" (ontents are differentecho ")n" &ile does not eist
cat outputecho ")n" (ontents are sae *econd file is being deletedecho")n" (ontents are differentecho ")n" &ile does not eist
8/17/2019 Unix LAB fILE (2)
4/32
Q.% Write a Shell Script that accepts a filename as a command line
argument and finds out if its a regular file or a directory. f its a
regular file! then performs 'arious tests to see if it is reada#le!
"ritea#le! e(ecuta#le etc.Ans.if -d $1 /thenecho #ts a director0elif -f $1 /thenecho file eist
if -r $1 /thenecho file has read perissionecho the contents are...cat $1else &ile dont hae read perissionfiif -w $1 /
thenecho ")n"&ile hae write perissioncat,,$1elseecho 0ou do not hae write perissionfiif - $1 /thenecho ")n"ou hae eecute perission
elseecho 0ou do not hae Eecute perissionfielseecho 2othing eist b0 this naeif
8/17/2019 Unix LAB fILE (2)
5/32
Output$
$ sh ss3 freefile eist
file has read perissionthe contents are...aan is stud0ing in 4*( 5th *e&ile hae write perission6e is a studentou hae eecute perission
8/17/2019 Unix LAB fILE (2)
6/32
Q.) Write a Shell Script "hich creates the follo"ing menu and
prompts for choice from user and runs the chosen command.
oday*s date
+rocess of user,ist of files
Quit to -
Ans.echo Enter a choiceecho 1. 7oda0s 8ateecho !. Process of userecho +. 9ist of files
echo 3. :uit to uniread chcase $ch in1;date
8/17/2019 Unix LAB fILE (2)
7/32
sh **5Enter a choice.1. 7oda0> 8ate!. Process of user
+. 9ist of files3. :uit to uni!P#8 77 7#E (8B??1 ptsC? ??@??@?? bashB!?+ ptsC? ??@??@?? shB!?3 ptsC? ??@??@?? ps
sh **5
Enter a choice.1. 7oda0> 8ate!. Process of user+. 9ist of files3. :uit to uni+cold gold sold ss11 ss13 **1D ss! ss!! **!5 **! **+? ssB **Fe.ttoutput ss1 ss1! ss15 ss1 **!? ss!+ **!B **!F ss3 ssD tr.cf1 q.tt ss1?
ss1+ ss1B **1F ss!1 **!3 **!D ss+ **5 ss w.tt
sh **5Enter a choice.1. 7oda0> 8ate!. Process of user+. 9ist of files3. :uit to uni3
8/17/2019 Unix LAB fILE (2)
8/32
:.B Write a Shell Script that computes the factorial of a gi'ennum#er.
Ans.
echo "Enter the no. to copute itGs factorial"readnu i=1fact=1while test $i -le $nudofact=`epr $fact ) $i`i=`epr $i H 1`done
echo "&actorial of@$nu is@$fact"
Iutput@sh ssBEnter the no. to copute itGs factorialB&actorial of@ B is@ D!?
8/17/2019 Unix LAB fILE (2)
9/32
Q. 0 Write a Shell Script that "orks like a calendar reminding the
user of certain things depending on the day of the "eek.
Ans.
a=`date HJA`echo ")n" Kelcoe *hiaecho ")n" 7oda0 is $aecho our tasLs for toda0 is as followscase $a inonda0;echo (oplete Mni Assignents
8/17/2019 Unix LAB fILE (2)
10/32
Q. 8 Write a Shell Script that changes the e(tension of a group of
files from t(t to doc.
Ans.
echo 4eforels .ttfor i in `ls .tt`do $i `echo $i|cut -f1 -d"."`.docdoneecho After ls .doc
Output$
$ sh ss4efored.tt f.tt r.ttAfterd.doc f.doc r.doc
8/17/2019 Unix LAB fILE (2)
11/32
:. F Krite a *hell *cript that accepts both filenae and a set of patternsas positional paraeters to a script.Ans.
fn=$1shiftfor i in $dogrep $i$fndone
Iutput@aanOaan-P(@C&ile$ sh **F ss tt docls .ttfor i in `ls .tt` $i `echo $i|cut -f1 -d"."`.docls .docaanOaan-P(@C&ile$
8/17/2019 Unix LAB fILE (2)
12/32
:.1? Krite a *hell *cript which will redirect the output of the datecoand without the tie into a file.Ans.
echo Enter the file naeread filea=`date|cut -b 1-11Q!5-!`echo $a|tee -a $fileclear echo ")n"$file sucessfull0 createdecho ")n""(ontent of file is @"`cat $file`
Iutput@sh ss1?Enter the file naess!7hu 2o !? !?15ss!sucessfull0 created(ontent of file is @if test $ -ne ! then echo "#nalid no. of arguents"
else word=`grep $1$!`if test `echo $word|wc -c` -eq 1thenecho "Pattern not found"elsegrep $1$!fifi7hu 2o !? !?15
8/17/2019 Unix LAB fILE (2)
13/32
:.11 Krite a *hell *cript Rusing while loop; to eecute endlessl0 Runtilterinated b0 user; a loop which displa0s contents of current director0QdisL space statusQ sleep for +? seconds and displa0 the users currentl0
logged in on the screen.Ans.char=0while $char ="0" /dolsdf Stsleep +?
whoecho"Kant to continue)R0Cn);T"read char done
Iutput@sh ss11cold gold sold ss11 ss13 **1D ss! ss!! **!5 **! **+? ssB **Fe.tt
output ss1 ss1! ss15 ss1 **!? ss!+ **!B **!F ss3 ssD tr.cf1 q.tt ss1?ss1+ ss1B **1F ss!1 **!3 **!D ss+ **5 ss w.tt&iles0ste 1U-blocLs Msed Aailable MseJ ountedon CdeCsdaB 335?5B !3DB533 !1!3+! 53JCarrun 35!+1B FB 35!!!? 1J CarCrunaan ptsC? !?15-11-!? 11@!5 R@?.?;Kant to continue)R0Cn);T
8/17/2019 Unix LAB fILE (2)
14/32
:.1! Krite a *hell *cript that receies two filenaes as arguents. #tshould checL whether content of the two files is sae or not. #f the0 aresaeQ second file should be deleted.
Ans.if -f $1 -a -f $! /thenif diff $1$!thencat $1echo ")n"cat $!
echo ")n" (ontents are sae *econd file is being deletedr $!else 15echo")n" (ontents are differentfielseecho ")n" &ile does not eistfi
Iutput@$ sh ss1! df rtaanaan(ontents are sae *econd file is being deleted$
8/17/2019 Unix LAB fILE (2)
15/32
:.1+ #f a nuber is input through the Le0boardQ KA** to calculate suof its digits.Ans.
echo Enter a no.read nusu=?while truedoif test `epr $nu J 1?` -gt ?thentep=`epr $nu J 1?`
su=`epr $su H $tep`nu=`epr $nu C 1?`elseecho $sueitfi
Iutput@
sh ss1+Enter a no.!+3513
8/17/2019 Unix LAB fILE (2)
16/32
:.13 Krite a *hell *cript that perfors a count-down either fro 1?Rdefault; or fro the alue that is entered b0 the user.Ans.
echo "Enter the (ountdown tie."read nclearwhile $n -ge ? /doecho $nsleep 1n=`epr $n - 1`done
echo (ount down tier stopped
Iutput@sh ss13Enter the (ountdown tie+
+
!1?(ount down tier stopped
8/17/2019 Unix LAB fILE (2)
17/32
:.15 Krite a *hell *cript which taLes a coand line arguent of Usand b0 default conerts that nuber into eters. Also proide options toconert L to d and L to c.
Ans.L=$1t=`epr $L ) 1???`echo "1.; L to d"echo "! ; L to c"echo Enter 0our choiceread nucase $nu in
1;d=`epr $L ) 1????`echo $L in eters is @$t and in decieters is @ $d
8/17/2019 Unix LAB fILE (2)
18/32
:.1B Krite a *hell *cript using for loopQ which displa0s the essage"Kelcoe to the M2#V *0ste".Ans.
for ar in $doecho "Kelcoe to Mni *0ste"shift 1done
Iutput@sh ss1B
Kelcoe to Mni *0ste
:.1D Krite a *hell *cript to change the filenae of all files in adirector0 fro lower-case to upper-case.Ans.for i in do
$i `echo $i|tr "@lower@/" "@upper@/"`doneIutput@sh **1D@ `(I98G and `(I98G are the sae file@ `E.7V7G and `E.7V7G are the sae file@ `&1G and `&1G are the sae file@ `NI98G and `NI98G are the sae file@ `:.7V7G and `:.7V7G are the sae file
@ `*I98G and `*I98G are the sae file@ `**1G and `**1G are the sae file(I98 NI98 *I98 **11 **13 **1D **! **!! **!5 **! **+? **B**FE.7V7 IM7PM7 **1 **1! **15 **1 **!? **!+ **!B **!F **3**D 7W.(&1 :.7V7 **1? **1+ **1B **1F **!1 **!3 **!D **+ **5** K.7V7
8/17/2019 Unix LAB fILE (2)
19/32
:.1 Krite a *hell *cript that eaines each file in the current director0.&iles whose naes end in old are oed to a director0 naed old filesand files whose naes end in .c are oed to director0 naed progras.
Ans.echo 4efore ")n"ls SlLdir oldfiles cprograsfor ar in `ls`doif test $ar = oldthen
echo ")n" &ile $ar is oed to old files director0$ar old filesfiif test $ar = .cthenecho")n" &ile $ar is oed to cprogras director0 $ar cprograsfi
donecd oldfilesecho ")n" &iles in oldfilesls Slcd ..echo ")n" After")n"ls SlIutput@sh **1
4eforetotal 133-rwrwrw 1 0aan 0aan BB !?15-11-!? 1?@?D (I98-rwrwrw 1 0aan 0aan ? !?15-11-!? 1?@?D E.7V7-rwrwrw 1 0aan 0aan D !?15-11-!? 1?@?D &1-rwrwrw 1 0aan 0aan 53 !?15-11-!? 1?@?D NI98
8/17/2019 Unix LAB fILE (2)
20/32
&iles in oldfilestotal ?After total 15!
-rwrwrw 1 0aan 0aan BB !?15-11-!? 1?@?D (I98drwr-r- ! 0aan 0aan 3?FB !?15-11-!? 1!@?3 cprogras-rwrwrw 1 0aan 0aan ? !?15-11-!? 1?@?D E.7V7-rwrwrw 1 0aan 0aan D !?15-11-!? 1?@?D &1-rwrwrw 1 0aan 0aan 53 !?15-11-!? 1?@?D NI98
:.1F Krite a shell script to gie the result of the student. 7aLe arLs ofthe fie subXects Q student naeQ roll no Q percentage and show a essage
whether a student gets diision as per the following rules@D?J and aboe ---- distinctionB?J-D?J ------first diision3?J-5FJ -----second diision 9ess than 3?J ----- failAns.for i in `ls $dirn`do
f=`echo $1C$i`if -f $f /thenif `cat $f|head -1` = $! /thencat $f if `cat $f|wc -l` -lt 5? /thenecho $i is sall siYed
fiif `cat $f|wc -l` -ge 5? -a `cat $f|wc -l` -lt 1?? /thenecho $i is a ediu siYedfiif `cat $f|wc -l` -gt 1?? /
8/17/2019 Unix LAB fILE (2)
21/32
thenecho $i is large siYedfifi
fidone
Iutput@sh ss1Fnewdir A#7A#7Ait0 Mniersit0
file1 is sall siYed
:.!?@ Krite a shell script which reports naes and siYes of all files in adirector0 Rdirector0 would be supplied as an arguent to the shell script;whose siYe is eceeding 1??? b0tes. 7he filenaes should be printed indescending order of their siYes. 7he total nuber of such files shouldalso be reported.Ans.
cd $1Ldirtp$1for i in doif -f $i /thentp=`ls -l $i|cut -f5 -d" "`if $tp -gt 1??? /
thenln $i tp$1C$ififidonels -l* tp$1
8/17/2019 Unix LAB fILE (2)
22/32
echo 7otal nuber of such files is @ `ls tp$1|wc -w` r-r tp$1
Iutput@
sh **!?total 3-rw-r--r-- ! 0aan 0aan 1+F! !?15 -11-!? 11@?! uni output7otal nuber of such files is @ 1
:.!1 KA** for renaing each file in the director0 such that it will haethe current shell P#8 as an etension. 7he shell script should ensure thatthe directories do not get renaed.
Ans.for ar in `ls`doif test -f $ar thena=`echo $$` $ar $ar.$afi
doneecho ")n" &ile nae changed@")n"ls Sl
Iutput@sh **!1.DB??.a&ile nae changed@
8/17/2019 Unix LAB fILE (2)
23/32
:.!! KAP to calculate and print the first &ibonacci nubers.Ans.echo Enter the series lengthread nu=?
0=1if test $nu -eq 1then echo $else if test $nu -eq !then echo "$)n$0"elseecho "$)n$0"i=+
while test $i -le $nudotep=`epr $0 H $`=$00=$tepecho $0i=`epr $i H 1`donefi
fi
Iutput@sh **!!Enter the series lengthB?11
!+
8/17/2019 Unix LAB fILE (2)
24/32
:.!+ KA** that will receie an0 nuber of filenaes as arguents.7he shell script should checL whether such files alread0 eist. #f the0 doQthen it should be reported. 7he files that do not eist should be created ina sub-director0 called 0dir. 7he shell script should first checL whether
the sub-director0 0dir eists in the current director0. #f it doesnZt eistQthen it should be created. #f 0dir alread0 eistsQ then it should bereported along with the nuber of files that are currentl0 present in0dir.Ans.if -e 0dir /thenecho "8irector0 @ 0dir eist"
elseecho 8o not eistLdir 0dirfifor a in $doecho $athenecho "&ile does not eist "
elseecho %file dtouch $a $a 0dirfidone
Iutput@sh **!+ **!!
8irector0 @ 0dir eist**!!&ile does not eists
8/17/2019 Unix LAB fILE (2)
25/32
:.!3 A shell script receies een nuber of filenaes. *uppose fourfilenaes are suppliedQ then the first file should get copied into secondfileQ the third file should get copied into fourth and so on. #f odd nuberof filenaes is supplied then no cop0ing should taLe place and an error
essage should be displa0ed.Ans.if `epr $ J !` -ne ? /thenecho Enter een nuber of paraeterselsei=?for L in $
doi=`epr $i H 1`if $i -eq 1 /thentep1=$L fiif $i -eq ! /
thentep!=$Li=?cp $tep1$tep!fidoneficatIutput@
$ cat,tt&ile aan lies in 8elhi$ cat,docaan is a student of 4*($ cat,tree$ cat,wee
8/17/2019 Unix LAB fILE (2)
26/32
6is roll no- is !D$ sh **!3 tt doc tree wee6is roll no- is !D *hia is here aan lies in 8elhi Adit0a is astudent of 4*(
:.!5 KA** to identif0 all Yero-b0te files in the current director0 anddelete the. 4efore proceeding with deletionQ the shell script should geta confiration fro the user.Ans.for i in doif -e $i -a -f $i /
thenif -s $i /thenechoelser -i $ififi
doneIutput@sh **!5r@ reoe regular ept0 file `E.7V7GT 0r@ reoe regular ept0 file `:.7V7GT nr@ reoe regular ept0 file `K.7V7GT n
8/17/2019 Unix LAB fILE (2)
27/32
:.!B KA** to copute the N(8 and 9( of two nubers.Ans.echo Enter &irst nuberread n1
echo Enter *econd nuberread n!if $n1 -lt $n! /theni=$n1elsei=$n!fi
flag=?while $flag -eq ? /doif `epr $n1 J $i` -eq ? -a `epr $n! J $i` -eq ? /thenecho N(8 of $n1 and $n! is $iflag=1fii=`epr $i - 1`
done
Iutput@sh **!BEnter &irst nuber3Enter *econd nuberN(8 of 3 and is 3
:.!D 7wo nubers are entered through the Le0board. KAP to find thealue of one nuber raised to the power of another.Ans.read becho Enter power
8/17/2019 Unix LAB fILE (2)
28/32
8/17/2019 Unix LAB fILE (2)
29/32
:.! KA** that propts the user for the password. 7he user hasaiu of + attepts. #f the user enters the correct passwordQ theessage %(orrect Password' is displa0ed else the essage %KrongPassword'.
Ans.echo *et the password firstread passwi=?while $i -lt + /doecho Enter passwordread p
if $p = $passw /thenecho (orrect Passwordi=+elseecho Krong passwordi=`epr $i H 1`fi
done
Iutput@sh **!*et the password firstaan *inghaniaEnter passwordaan *inghania(orrect Password
8/17/2019 Unix LAB fILE (2)
30/32
:.!F KA** that repeatedl0 asLs the user repeatedl0 for the %2ae ofthe #nstitution' until the user gies the correct answer.Ans.flag=?
while $flag -eq ? /doecho Enter nae of 0our institute in capital lettersread instif $inst = "A#7" /thenecho (orrect Answer flag=1else
echo Enter Againfidone
Iutput@sh **!FEnter nae of 0our institute in capital lettersA#7
(orrect Answer
8/17/2019 Unix LAB fILE (2)
31/32
:. +? KAP to generate all cobinations of 1Q ! and + using for loop.Ans.for i in 1 ! +do
for X in 1 ! +dofor L in 1 ! +doecho $i$X$Ldonedonedone
Iutput @ sh **+?11111!11+1!11!!1!+
1+11+!1++!11!1!!1+!!1!!!!!+
!+1!+!!+++11+1!+1+
8/17/2019 Unix LAB fILE (2)
32/32
+!1+!!+!+++1
++!+++