39564101-unix-file-by-me
TRANSCRIPT
-
8/7/2019 39564101-unix-file-by-me
1/35
INDEXS.No. PROGRAM NAME Page
No.Sign.
1. Write a Shell Script that takes a search string and filename from theterminal & displays the results.
4
2. Write a Shell Script that takes pattern and filename as command linearguments and displays the results appropriately i.e. patternfound/pattern not found.
5
3. Write a Shell Script that accepts only three arguments from thecommand line. The first argument is the pattern string, the secondargument is the filename in which the pattern is to be searches and thethird argument is the filename in which the result is to be stored.
6
4. Write a Shell Script that accepts a filename as a command line argumentand finds out if its a regular file or a directory. If its a regular file, thenperforms various tests to see if it is readable, writeable, executable etc.
7
5. Write a Shell Script which creates the following menu and prompts forchoice from user and runs the chosen command.
Today's dateProcess of userList of filesQuit to UNIX
8
6. Write a Shell Script that computes the factorial of a given number. 10
7. Write a Shell Script that works like a calendar reminding the user of
certain things depending on the day of the week.
11
8. Write a Shell Script that changes the extension of a group of files fromtxt to doc.
12
9. Write a Shell Script that accepts both filename and a set of patterns aspositional parameters to a script.
13
10. Write a Shell Script which will redirect the Output of the date commandwithout the time into a file.
14
11. Write a Shell Script (using while loop) to execute endlessly (untilterminated by user) a loop which displays contents of current directory,disk space status, sleep for 30 seconds and display the users currentlylogged in on the screen.
15
12. Write a Shell Script that receives two filenames as arguments. It shouldcheck whether content of the two files is same or not. If they are same,second file should be deleted.
16
13. If a number is input through the keyboard, WASS to calculate sum of itsdigits.
17
14. Write a Shell Script that performs a count-down either from 10 (default)or from the value that is entered by the user.
18
1
-
8/7/2019 39564101-unix-file-by-me
2/35
15. Write a Shell Script which takes a command line argument of Kms andby default converts that number into meters. Also provide options toconvert km to dm and km to cm.
19
16. Write a Shell Script using for loop, which displays the message"Welcome to the UNIX System"
20
17. Write a Shell Script to change the filename of all files in a directory fromlower-case to upper-case.
21
18. Write a Shell Script that examines each file in the current directory. Fileswhose names end in old are moved to a directory named old files andfiles whose names end in .c are moved to directory named cprograms.
22
19. Write a Shell Script which searches all files in the given directory (to betaken as command line argument) for the file having the title (to betaken as command line argument), as the first line in the file.
a)Display the contents of the searched file.b)In the end, print the file is ###, where### is small-sized if total no. of lines is
-
8/7/2019 39564101-unix-file-by-me
3/35
28. WASS that prompts the user for the password. The user has maximum of3 attempts. If the user enters the correct password, the message CorrectPassword is displayed else the message Wrong Password.
33
29. WASS that repeatedly asks the user repeatedly for the Name of theInstitution until the user gives the correct answer.
34
30. WAP to generate all combinations of 1, 2 and 3 using for loop. 35
1. Write a Shell Script that takes a search string and filename from the terminal & displays
the results.
3
-
8/7/2019 39564101-unix-file-by-me
4/35
Ans :
read aread bword=`grep $a $b`
if test `echo $word | wc -c` -eq 1thenecho "Pattern not found"elsegrep $a $bfi
Output :
sh ss1enter the string to be searched.ashsenter the file namess2Pattern not found2. 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 :
4
-
8/7/2019 39564101-unix-file-by-me
5/35
if test $# -ne 2thenecho "Invalid no. of arguments"else
word=`grep $1 $2`if test `$word|wc -c` -eq 1thenecho "Pattern not found"elsegrep $1 $2echo Pattern Foundfifi
Output :
sh ss2 Contents ss12echo "\n" Contents are same Second file is being deletedecho"\n" Contents are differentPattern Found3. Write a Shell Script that accepts only three arguments from the command line. The first
argument is the pattern string, the second argument is the filename in which the pattern is
to be searches and the third argument is the filename in which the result is to be stored.
Ans :
5
-
8/7/2019 39564101-unix-file-by-me
6/35
if test $# -ne 3thenecho "Invalid no. of arguments"else
grep $1 $2 |cat>$3if test -s $3thenecho "Pattern found"cat $3elseecho Pattern not foundfi
Output :
sh ss3 echo ss12 outputPattern foundecho "\n" Contents are same Second file is being deletedecho"\n" Contents are differentecho "\n" File does not exist
cat outputecho "\n" Contents are same Second file is being deletedecho"\n" Contents are differentecho "\n" File does not exist4. Write a Shell Script that accepts a filename as a command line argument and finds out if
its a regular file or a directory. If its a regular file, then performs various tests to see if it is
readable, writeable, executable etc.
Ans :
6
-
8/7/2019 39564101-unix-file-by-me
7/35
if [ -d $1 ]thenecho Its a directoryelif [ -f $1 ]then
echo file existif [ -r $1 ]thenecho file has read permissionecho the contents are......cat $1else File dont have read permissionfiif [ -w $1 ]thenecho "\n"File have write permission
cat>>$1elseecho you do not have write permissionfiif [ -x $1 ]thenecho "\n"You have execute permissionelseecho you do not have Execute permissionfielseecho Nothing exist by this namefiOutput :
$ sh ss4 freefile existfile has read permissionthe contents are......Aditya is studying in BCA 5th Year Enrollment NO. A1004807003He stays in Model TownFile have write permissionHe is a studentYou have execute permission$5. Write a Shell Script which creates the following menu and prompts for choice from user
and runs the chosen command.
Today's date
Process of user
List of files
Quit to UNIX
7
-
8/7/2019 39564101-unix-file-by-me
8/35
Ans :
echo Enter a choice.echo 1. Today Dateecho 2. Process of user
echo 3. List of filesecho 4. Quit to unixread chcase $ch in1)date;;2)ps;;3)ls;;4)exit
;;*)echo invalid Choice;;esac
Output :
sh SS5Enter a choice.1. Today Date2. Process of user3. List of files4. Quit to unix
8
-
8/7/2019 39564101-unix-file-by-me
9/35
1Thu Nov 20 10:52:32 IST 2008
sh SS5Enter a choice.
1. Today Date2. Process of user3. List of files4. Quit to unix2PID TTY TIME CMD6001 pts/0 00:00:00 bash6203 pts/0 00:00:00 sh6204 pts/0 00:00:00 ps
sh SS5Enter a choice.1. Today Date2. Process of user3. List of files4. Quit to unix3cold gold sold ss11 ss14 SS17 ss2 ss22 SS25 SS28 SS30 ss6 SS9e.txt output ss1 ss12 ss15 ss18 SS20 ss23 SS26 SS29 ss4 ss7 tr.cf1 q.txt ss10 ss13 ss16 SS19 ss21 SS24 SS27 ss3 SS5 ss8 w.txt
sh SS5Enter a choice.1. Today Date2. Process of user3. List of files4. Quit to unix4
6. Write a Shell Script that computes the factorial of a given number.
Ans :
echo "Enter the no. to compute it's factorial"read numi=1fact=1while test $i -le $num
9
-
8/7/2019 39564101-unix-file-by-me
10/35
dofact=`expr $fact \* $i`i=`expr $i + 1`doneecho "Factorial of:$num is:$fact"
Output :
sh ss6Enter the no. to compute it's factorial6Factorial of: 6 is: 7207. Write a Shell Script that works like a calendar reminding the user of certain things
depending on the day of the week.
Ans :
a=`date +%A`echo "\n" Welcome Shivaecho "\n" Today is $aecho Your tasks for today is as followscase $a in
10
-
8/7/2019 39564101-unix-file-by-me
11/35
Monday)echo Complete Unix Assignments;;Tuesday)echo Take print outs of Unix programs;;Wednesday)echo Get your Unix practical file properly arranged
;;Thursday)echo Get your Unix file checked;;Friday)echo Get advance bookings for a movie;;Saturday)echo Go out for movie;;Sunday)Sunday is a fun day;;esac
Output :
sh ss7Welcome ShivaToday is ThursdayYour tasks for today is as followsGet your Unix file checked8. Write a Shell Script that changes the extension of a group of files from txt to doc.
Ans :
echo Beforels *.txtfor i in `ls *.txt`domv $i `echo $i|cut -f1 -d"."`.docdoneecho After
11
-
8/7/2019 39564101-unix-file-by-me
12/35
ls *.doc
Output :
$ sh ss8Befored.txt f.txt r.txtAfterd.doc e?q?w.doc f.doc q?w.doc r.doc w.doc9. Write a Shell Script that accepts both filename and a set of patterns as positional
parameters to a script.
Ans :
fn=$1shiftfor i in $*dogrep $i $fndone
12
-
8/7/2019 39564101-unix-file-by-me
13/35
Output :
aditya@ADITYA-PC:~/File$ sh SS9 ss8 txt docls *.txtfor i in `ls *.txt`mv $i `echo $i|cut -f1 -d"."`.docls *.docaditya@ADITYA-PC:~/File$10. Write a Shell Script which will redirect the output of the date command without the
time into a file.
Ans :
echo Enter the file nameread filea=`date|cut -b 1-11,25-28`echo $a|tee -a $fileclearecho "\n"$file sucessfully createdecho "\n""Content of file is :"`cat $file`
13
-
8/7/2019 39564101-unix-file-by-me
14/35
Output :
sh ss10Enter the file namess2Thu Nov 20 2008ss2 sucessfully createdContent of file is :if test $# -ne 2 then echo "Invalid no. of arguments" else word=`grep $1 $2` iftest `echo $word|wc -c` -eq 1 then echo "Pattern not found" else grep $1 $2 fi fi Thu Nov 20200811. Write a Shell Script (using while loop) to execute endlessly (until terminated by user) a
loop which displays contents of current directory, disk space status, sleep for 30 seconds
and display the users currently logged in on the screen.
Ans :
char=ywhile [ $char ="y" ]dolsdf -tsleep 30whoecho"Want to continue\(y/n\)?"
14
-
8/7/2019 39564101-unix-file-by-me
15/35
read chardone
Output :
sh ss11
cold gold sold ss11 ss14 SS17 ss2 ss22 SS25 SS28 SS30 ss6 SS9e.txt output ss1 ss12 ss15 ss18 SS20 ss23 SS26 SS29 ss4 ss7 tr.cf1 q.txt ss10 ss13 ss16 SS19 ss21 SS24 SS27 ss3 SS5 ss8 w.txtFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda6 4845056 2476544 2124328 54% /varrun 452316 96 452220 1% /var/runvarlock 452316 0 452316 0% /var/lockudev 452316 56 452260 1% /devdevshm 452316 12 452304 1% /dev/shmlrm 452316 39760 412556 9% /lib/modules/2.6.24-19-generic/volatilegvfs-fuse-daemon 4845056 2476544 2124328 54% /home/aditya/.gvfsaditya tty7 2008-11-20 11:20 (:0)aditya pts/0 2008-11-20 11:25 (:0.0)Want to continue\(y/n\)?12. Write a Shell Script that receives two filenames as arguments. It should check whether
content of the two files is same or not. If they are same, second file should be deleted.
Ans :
if [ -f $1 -a -f $2 ]thenif diff $1 $2thencat $1echo "\n"cat $2echo "\n" Contents are same Second file is being deletedrm $2else
15
-
8/7/2019 39564101-unix-file-by-me
16/35
echo"\n" Contents are differentfielseecho "\n" File does not existfi
Output :
$ sh ss12 df rtAditya SadhAditya SadhContents are same Second file is being deleted$13. If a number is input through the keyboard, WASS to calculate sum of its digits.
Ans :
echo Enter a no.read numsum=0while truedoif test `expr $num % 10` -gt 0thentemp=`expr $num % 10`sum=`expr $sum + $temp`num=`expr $num / 10`elseecho $sum
16
-
8/7/2019 39564101-unix-file-by-me
17/35
exitfi
Output :
sh ss13Enter a no.23451414. Write a Shell Script that performs a count-down either from 10 (default) or from the
value that is entered by the user.
Ans :
echo "Enter the Countdown time."read nclearwhile [ $n -ge 0 ]doecho $nsleep 1n=`expr $n - 1`doneecho Count down timer stopped
17
-
8/7/2019 39564101-unix-file-by-me
18/35
Output :
sh ss14Enter the Countdown time.3
3210Count down timer stopped15. Write a Shell Script which takes a command line argument of Kms and by default
converts that number into meters. Also provide options to convert km to dm and km to cm.
Ans :
km=$1mt=`expr $km \* 1000`echo "1.) km to dm"echo "2 ) km to cm"echo Enter your choiceread numcase $num in1)dm=`expr $km \* 10000`echo $km in meters is :$mt and in decimeters is : $dm;;2)cm=`expr $km \* 100000`echo $km in meters is :$mt and in centimeters is : $cm;;
18
-
8/7/2019 39564101-unix-file-by-me
19/35
esac
Output :
sh ss15 55 kms in meters is 50001.) km to dm2 ) km to cmEnter your choice15 in meters is- 5000 and in decimeters is 5000016. Write a Shell Script using for loop, which displays the message "Welcome to the UNIX
System".
Ans :
for var in $*doecho "Welcome to Unix System"shift 1done
19
-
8/7/2019 39564101-unix-file-by-me
20/35
Output :
sh ss16Welcome to Unix System17. Write a Shell Script to change the filename of all files in a directory from lower-case to
upper-case.
Ans :
for i in *domv $i `echo $i|tr "[:lower:]" "[:upper:]"`done
20
-
8/7/2019 39564101-unix-file-by-me
21/35
Output :
sh SS17mv: `COLD' and `COLD' are the same filemv: `E.TXT' and `E.TXT' are the same file
mv: `F1' and `F1' are the same filemv: `GOLD' and `GOLD' are the same filemv: `Q.TXT' and `Q.TXT' are the same filemv: `SOLD' and `SOLD' are the same filemv: `SS1' and `SS1' are the same file
COLD GOLD SOLD SS11 SS14 SS17 SS2 SS22 SS25 SS28 SS30 SS6 SS9E.TXT OUTPUT SS1 SS12 SS15 SS18 SS20 SS23 SS26 SS29 SS4 SS7 TR.CF1 Q.TXT SS10 SS13 SS16 SS19 SS21 SS24 SS27 SS3 SS5 SS8 W.TXT18. Write a Shell Script that examines each file in the current directory. Files whose names
end in old are moved to a directory named old files and files whose names end in .c are
moved to directory named cprograms.
Ans :
echo Before "\n"ls -lmkdir oldfiles cprogramsfor var in `ls`doif test $var = *oldthenecho "\n" File $var is moved to old files directorymv $var old filesfiif test $var = *.cthenecho"\n" File $var is moved to cprograms directorymv $var cprogramsfi
21
-
8/7/2019 39564101-unix-file-by-me
22/35
donecd oldfilesecho "\n" Files in oldfilesls -lcd ..
echo "\n" After"\n"ls -l
Output :
sh SS18Before
total 144-rwxrwxrwx 1 aditya aditya 66 2008-11-20 10:07 COLD-rwxrwxrwx 1 aditya aditya 0 2008-11-20 10:07 E.TXT-rwxrwxrwx 1 aditya aditya 7 2008-11-20 10:07 F1-rwxrwxrwx 1 aditya aditya 54 2008-11-20 10:07 GOLD
Files in oldfilestotal 0
Aftertotal 152-rwxrwxrwx 1 aditya sadh 66 2008-11-20 10:07 COLDdrwxr-xr-x 2 aditya aditya 4096 2008-11-20 12:04 cprograms-rwxrwxrwx 1 aditya aditya 0 2008-11-20 10:07 E.TXT-rwxrwxrwx 1 aditya aditya 7 2008-11-20 10:07 F1-rwxrwxrwx 1 aditya aditya 54 2008-11-20 10:07 GOLD
22
-
8/7/2019 39564101-unix-file-by-me
23/35
19. Write a Shell Script which searches all files in the given directory (to be taken as
command line argument) for the file having the title (to be taken as command line
argument), as the first line in the file.
a) Display the contents of the searched file.
b) In the end, printthe the file is ###, where
### is small-sized if total no. of lines is
-
8/7/2019 39564101-unix-file-by-me
24/35
thenecho $i is a medium sizedfiif [ `cat $f|wc -l` -gt 100 ]then
echo $i is large sizedfififidone
Output :
sh ss19 newdir AMITYAMITYAmity Universityfile1 is small sized20. Write a shell script which reports names and sizes of all files in a directory (directory
would be supplied as an argument to the shell script) whose size is exceeding 1000 bytes.
The filenames should be printed in descending order of their sizes. The total number of
such files should also be reported.
Ans :
cd $1mkdirtmp$1for i in *doif [ -f $i ]thentmp=`ls -l $i|cut -f5 -d" "`if [ $tmp -gt 1000 ]thenln $i tmp$1/$ififidonels -lS tmp$1echo Total number of such files is : `ls tmp$1|wc -w`rm -r tmp$1
24
-
8/7/2019 39564101-unix-file-by-me
25/35
Output :
sh SS20total 4-rw-r--r-- 2 aditya aditya 1392 2008-11-20 11:02 unix outputTotal number of such files is : 121. WASS for renaming each file in the directory such that it will have the current shell
PID as an extension. The shell script should ensure that the directories do not get renamed.
Ans :
for var in `ls`doif test -f $varthena=`echo $$`mv $var $var.$afidoneecho "\n" File name changed:"\n"ls -l
25
-
8/7/2019 39564101-unix-file-by-me
26/35
Output :
sh SS21.7600.aFile name changed:
total 152-rwxrwxrwx aditya aditya 66 2008-11-20 10:07 COLD.7600.a.adrwxr-xr-x 2 aditya aditya 4096 2008-11-20 12:04 cprograms-rwxrwxrwx 1 aditya aditya 0 2008-11-20 10:07 E.TXT.7600.a.a
-rwxrwxrwx 1 aditya aditya 7 2008-11-20 10:07 F1.7600.a.a-rwxrwxrwx 1 aditya aditya 54 2008-11-20 10:07 GOLD.7600.a.adrwxr-xr-x 2 aditya aditya 4096 2008-11-20 12:04 oldfiles22. WAP to calculate and print the first m Fibonacci numbers.
Ans :
echo Enter the series lengthread numx=0y=1if test $num -eq 1then echo $xelse if test $num -eq 2then echo "$x\n$y"elseecho "$x\n$y"i=3while test $i -le $numdotemp=`expr $y + $x`x=$yy=$tempecho $yi=`expr $i + 1`donefifi
26
-
8/7/2019 39564101-unix-file-by-me
27/35
Output :
sh SS22Enter the series length6011
2323. WASS that will receive any number of filenames as arguments. The shell script should
check whether such files already exist. If they do, then it should be reported. The files that
do not exist should be created in a sub-directory called mydir. The shell script should first
check whether the sub-directory mydir exists in the current directory. If it doesnt exist,
then it should be created. If mydir already exists, then it should be reported along with the
number of files that are currently present in mydir.
Ans :
if [ -e mydir ]thenecho "Directory : mydir exist"elseecho Do not existmkdir mydirfifor a in $*doecho $athenecho "File does not exist "elseecho file dtouch $amv $a mydirfidone
27
-
8/7/2019 39564101-unix-file-by-me
28/35
Output :
sh SS23 SS22Directory : mydir existSS22
File does not exists24. A shell script receives even number of filenames. Suppose four filenames are supplied,then the first file should get copied into second file, the third file should get copied into
fourth and so on. If odd number of filenames is supplied then no copying should take place
and an error message should be displayed.
Ans :
if [ `expr $# % 2` -ne 0 ]thenecho Enter even number of parameterselsei=0for k in $*doi=`expr $i + 1`if [ $i -eq 1 ]thentemp1=$kfiif [ $i -eq 2 ]thentemp2=$ki=0cp $temp1 $temp2fidoneficat
28
-
8/7/2019 39564101-unix-file-by-me
29/35
Output :
$ cat>txtFile Aditya lives in Delhi$ cat>docAditya is a student of BCA$ cat>treeAditya is a cool dude$ cat>weeHis roll no- is A1004807024$ sh SS24 txt doc tree weeHis roll no- is A1004807024 Shivani is a cool dude Aditya lives id Delhi Aditya is a student ofBCA
25. WASS to identify all zero-byte files in the current directory and delete them. Beforeproceeding with deletion, the shell script should get a conformation from the user.
Ans :
for i in *doif [ -e $i -a -f $i ]thenif [ -s $i ]thenechoelserm -i $ififidone
29
-
8/7/2019 39564101-unix-file-by-me
30/35
Output :
sh SS25
rm: remove regular empty file `E.TXT'? yrm: remove regular empty file `Q.TXT'? nrm: remove regular empty file `W.TXT'? n26. WASS to compute the GCD and LCM of two numbers.
Ans :
echo Enter First numberread n1echo Enter Second numberread n2if [ $n1 -lt $n2 ]theni=$n1elsei=$n2fiflag=0while [ $flag -eq 0 ]doif [ `expr $n1 % $i` -eq 0 -a `expr $n2 % $i` -eq 0 ]thenecho GCD of $n1 and $n2 is $iflag=1fii=`expr $i - 1`done
30
-
8/7/2019 39564101-unix-file-by-me
31/35
Output :
sh SS26Enter First number4Enter Second number8GCD of 4 and 8 is 427. Two numbers are entered through the keyboard. WAP to find the value of one number
raised to the power of another.
Ans :
read becho Enter powerread ppowr=$presult=1while [ $p -ge 1 ]doresult=`expr $result \* $b`p=`expr $p - 1`doneecho $b raised to the power $powr is $result
31
-
8/7/2019 39564101-unix-file-by-me
32/35
Output :
sh SS27Enter a number4Enter power24 raised to the power 2 is 1628. WASS that prompts the user for the password. The user has maximum of 3 attempts. If
the user enters the correct password, the message Correct Password is displayed else the
message Wrong Password.
Ans :
echo Set the password firstread passwi=0while [ $i -lt 3 ]doecho Enter passwordread pif [ $p = $passw ]thenecho Correct Passwordi=3elseecho Wrong passwordi=`expr $i + 1`fidone
32
-
8/7/2019 39564101-unix-file-by-me
33/35
Output :
sh SS28Set the password firstAdityaEnter passwordadityaCorrect Password29. WASS that repeatedly asks the user repeatedly for the Name of the Institution until
the user gives the correct answer.
Ans :
flag=0while [ $flag -eq 0 ]doecho Enter name of your institute in capital lettersread instif [ $inst = "AMITY" ]thenecho Correct Answerflag=1elseecho Enter Againfidone
33
-
8/7/2019 39564101-unix-file-by-me
34/35
Output :
sh SS29Enter name of your institute in capital lettersAMITYCorrect Answer30. WAP to generate all combinations of 1, 2 and 3 using for loop.
Ans :
for i in 1 2 3
dofor j in 1 2 3dofor k in 1 2 3doecho $i$j$kdonedonedone
Output :
sh SS30111112113121122123131132133211212213221222223231232
34
-
8/7/2019 39564101-unix-file-by-me
35/35
233311312313321
322323331332333