Sie sind auf Seite 1von 32

Q.

1 Write a Shell Script that takes a search string and filename


from the terminal & displays the results.
Ans. read a
read b
word=`grep $a $b`
if test `echo $word | wc -c` -eq 1
then
echo "Pattern not found"
else
grep $a $b
fi
Output
sh ss1
Enter the string to be searched
Ashs
Enter the file name
ss2
Pattern not found

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 2
then
echo "Invalid no. of arguments"
else
word=`grep $1 $2`
if test `$word|wc -c` -eq 1
then
echo "Pattern not found"
else
grep $1 $2
echo Pattern Found
fi
fi
Output
sh ss2 Contents ss12
echo "\n" Contents are same Second file is being deleted
echo"\n"
Contents are different
Pattern Found

Q.3 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. if test $# -ne 3
then
echo "Invalid no. of arguments"
else
grep $1 $2 |cat>$3
if test -s $3
then
echo "Pattern found"
cat $3
else
echo Pattern not found
fi
Output:
sh ss3 echo ss12 output
Pattern found
echo "\n" Contents are same Second file is being deleted
echo"\n" Contents are different
echo "\n" File does not exist
cat output
echo "\n" Contents are same Second file is being deleted
echo"\n" Contents are different
echo "\n" File does not exist

Q.4 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.
if [ -d $1 ]
then
echo Its a directory
elif [ -f $1 ]
then
echo file exist
if [ -r $1 ]
then
echo file has read permission
echo the contents are...
cat $1
else File dont have read permission
fi
if [ -w $1 ]
then
echo "\n"File have write permission
cat>>$1
else
echo you do not have write permission
fi
if [ -x $1 ]
then
echo "\n"You have execute permission
else
echo you do not have Execute permission
fi
else
echo Nothing exist by this name
if

Output:
$ sh ss4 free
file exist
file has read permission
the contents are...
Yaman is studying in BSC 5th Sem
File have write permission
He is a student
You have execute permission

Q.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
Ans.
echo Enter a choice
echo 1. Todays Date
echo 2. Process of user
echo 3. List of files
echo 4. Quit to unix
read ch
case $ch in
1)date
;;
2)ps
;;
3)ls
;;
4)Exit
;;
*)echo invalid Choice
;;
esac
Output:
sh SS5
Enter a choice.
1. Today Date
2. Process of user
3. List of files
4. Quit to unix
1
Thu Nov 20 10:52:32 IST 2015

sh SS5
Enter a choice.
1. Today Date
2. Process of user
3. List of files
4. Quit to unix
2PID TTY
6001 pts/0
6203 pts/0
6204 pts/0

TIME CMD
00:00:00 bash
00:00:00 sh
00:00:00 ps

sh SS5
Enter a choice.
1. Today Date
2. Process of user
3. List of files
4. Quit to unix
3
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.txt
sh SS5
Enter a choice.
1. Today Date
2. Process of user
3. List of files
4. Quit to unix
4

Q.6 Write a Shell Script that computes the factorial of a given


number.
Ans.
echo "Enter the no. to compute it's factorial"
read
num i=1
fact=1
while test $i -le $num
dofact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "Factorial of:$num is:$fact"
Output:
sh ss6
Enter the no. to compute it's factorial
6
Factorial of: 6 is: 720

Q. 7 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 Shiva
echo "\n" Today is $a
echo Your tasks for today is as follows
case $a in
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 ss7
Welcome Shiva
Today is Thursday
Your tasks for today is as follows
Get your Unix file checked

Q. 8 Write a Shell Script that changes the extension of a group of


files from txt to doc.
Ans.
echo Before
ls *.txt
for i in `ls *.txt`
do
mv $i `echo $i|cut -f1 -d"."`.doc
done
echo After
ls *.doc
Output:
$ sh ss8
Before
d.txt f.txt r.txt
After
d.doc f.doc r.doc

Q. 9 Write a Shell Script that accepts both filename and a set of patterns
as positional parameters to a script.
Ans.
fn=$1
shift
for i in $*
do
grep $i
$fn
done
Output:
Yaman@Yaman-PC:~/File$ sh SS9 ss8 txt doc
ls *.txt
for i in `ls *.txt`
mv $i `echo $i|cut -f1 -d"."`.doc
ls *.doc
Yaman@Yaman-PC:~/File$

Q.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 name
read file
a=`date|cut -b 1-11,25-28`
echo $a|tee -a $file
clear echo "\n"
$file sucessfully created
echo "\n""Content of file is :"`cat $file`
Output:
sh ss10
Enter the file name
ss2
Thu Nov 20 2015
ss2
sucessfully created
Content of file is :if test $# -ne 2 then echo "Invalid no. of arguments"
else word=`grep $1$2`
if test `echo $word|wc -c` -eq 1
then
echo "Pattern not found"
else
grep $1$2
fi
fi
Thu Nov 20 2015

Q.11 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=y
while [ $char ="y" ]
do
ls
df t
sleep 30
who
echo"Want to continue\(y/n\)?"
read char
done
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.txt
Filesystem
1K-blocks
Used
Available Use% Mounted
on /dev/sda6 4845056
2476544 2124328
54%
/varrun
452316
96
452220
1% /var/run
Yaman pts/0
2015-11-20 11:25 (:0.0)
Want to continue\(y/n\)?

Q.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 ]
then
if diff $1$2
then
cat $1
echo "\n"
cat $2
echo "\n" Contents are same Second file is being deleted
rm $2
else 15
echo"\n" Contents are different
fi
else
echo "\n" File does not exist
fi
Output:
$ sh ss12 df rt
Yaman
Yaman
Contents are same Second file is being deleted
$

Q.13 If a number is input through the keyboard, WASS to calculate sum


of its digits.
Ans.
echo Enter a no.
read num
sum=0
while true
do
if test `expr $num % 10` -gt 0
then
temp=`expr $num % 10`
sum=`expr $sum + $temp`
num=`expr $num / 10`
else
echo $sum
exit
fi
Output:
sh ss13
Enter a no.
2345
14

Q.14 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 nclear
while [ $n -ge 0 ]
do
echo $n
sleep 1
n=`expr $n - 1`
done
echo Count down timer stopped
Output:
sh ss14
Enter the Countdown time
3
3
2
1
0
Count down timer stopped

Q.15 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=$1
mt=`expr $km \* 1000`
echo "1.) km to dm"
echo "2 ) km to cm"
echo Enter your choice
read num
case $num in
1)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
esac
Output:
sh ss15 5
5 kms in meters is 5000
1.) km to dm
2 ) km to cm
Enter your choice
1
5 in meters is- 5000 and in decimeters is 50000

Q.16 Write a Shell Script using for loop, which displays the message
"Welcome to the UNIX System".
Ans.
for var in $*
do
echo "Welcome to Unix System"
shift 1
done
Output:
sh ss16
Welcome to Unix System
Q.17 Write a Shell Script to change the filename of all files in a
directory from lower-case to upper-case.
Ans.
for i in *
do
mv $i `echo $i|tr "[:lower:]" "[:upper:]"`
done
Output:
sh SS17
mv: `COLD' and `COLD' are the same file
mv: `E.TXT' and `E.TXT' are the same file
mv: `F1' and `F1' are the same file
mv: `GOLD' and `GOLD' are the same file
mv: `Q.TXT' and `Q.TXT' are the same file
mv: `SOLD' and `SOLD' are the same file
mv: `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.TXT

Q.18 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 programs.
Ans.
echo Before "\n"
ls l
mkdir oldfiles cprograms
for var in `ls`
do
if test $var = *old
then
echo "\n" File $var is moved to old files directorymv
$var old files
fi
if test $var = *.c
then
echo"\n" File $var is moved to cprograms directory mv
$var cprograms
fi
done
cd oldfiles
echo "\n" Files in oldfiles
ls l
cd ..
echo "\n" After"\n"
ls l
Output:
sh SS18
Before
total 144
-rwxrwxrwx 1 yaman yaman 66 2015-11-20 10:07 COLD
-rwxrwxrwx 1 yaman yaman 0 2015-11-20 10:07 E.TXTrwxrwxrwx 1 yaman yaman 7 2015-11-20 10:07 F1
-rwxrwxrwx 1 yaman yaman 54 2015-11-20 10:07 GOLD

Files in oldfiles
total 0
After total 152
-rwxrwxrwx 1 yaman yaman 66 2015-11-20 10:07 COLD
drwxr-xr-x 2 yaman yaman 4096 2015-11-20 12:04 cprograms
-rwxrwxrwx 1 yaman yaman 0 2015-11-20 10:07 E.TXT
-rwxrwxrwx 1 yaman yaman 7 2015-11-20 10:07 F1
-rwxrwxrwx 1 yaman yaman 54 2015-11-20 10:07 GOLD
Q.19 Write a shell script to give the result of the student. Take marks of
the five subjects , student name, roll no , percentage and show a message
whether a student gets division as per the following rules:
70% and above ---- distinction
60%-70% ------first division
40%-59% -----second division
Less than 40% ----- fail
Ans.
for i in `ls $dirnm`
do
f=`echo $1/$i`
if [ -f $f ]
then
if [ `cat $f|head -1` = $2 ]
then
cat $f
if [ `cat $f|wc -l` -lt 50 ]
then
echo $i is small sized
fi
if [ `cat $f|wc -l` -ge 50 -a `cat $f|wc -l` -lt 100 ]
then
echo $i is a medium sized
fi
if [ `cat $f|wc -l` -gt 100 ]

then
echo $i is large sized
fi
fi
fi
done
Output:
sh ss19
newdir AMITY
AMITY
Amity University
file1 is small sized
Q.20: 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 $1
mkdir
tmp$1
for i in *
do
if [ -f $i ]
then
tmp=`ls -l $i|cut -f5 -d" "`
if [ $tmp -gt 1000 ]
then
ln $i tmp$1/$i
fi
fi
done
ls -lS tmp$1

echo Total number of such files is : `ls tmp$1|wc -w` rm


-r tmp$1
Output:
sh SS20
total 4
-rw-r--r-- 2 yaman yaman 1392 2015 -11-20 11:02 unix output
Total number of such files is : 1
Q.21 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`
do
if test -f $var
then
a=`echo $$`
mv $var $var.$a
fi
done
echo "\n" File name changed:"\n"
ls l
Output:
sh SS21.7600.a
File name changed:

Q.22 WAP to calculate and print the first m Fibonacci numbers.


Ans.
echo Enter the series length
read numx=0
y=1
if test $num -eq 1
then echo $x
else if test $num -eq 2
then echo "$x\n$y"
else
echo "$x\n$y"
i=3
while test $i -le $num
do
temp=`expr $y + $x`
x=$y
y=$temp
echo $yi=`expr $i + 1`
done
fi
fi
Output:
sh SS22
Enter the series length
6
0
1
1
2
3

Q.23 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 ]
then
echo "Directory : mydir exist"
else
echo Do not existmkdir mydir
fi
for a in $*
do
echo $a
then
echo "File does not exist "
else
echo file d
touch $a
mv $a mydir
fi
done
Output:
sh SS23 SS22
Directory : mydir exist
SS22
File does not exists

Q.24 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 ]
then
echo Enter even number of parameters
else
i=0
for k in $*
do
i=`expr $i + 1`
if [ $i -eq 1 ]
then
temp1=$k
fi
if
[ $i -eq 2 ]
then
temp2=$k
i=0
cp $temp1$temp2
fi
done
fi
cat
Output:
$ cat>txt
File Yaman lives in Delhi
$ cat>doc
Yaman is a student of BSC
$ cat>tree
$ cat>wee

His roll no- is 27


$ sh SS24 txt doc tree wee
His roll no- is 27 Shivam is here Yaman lives in Delhi Aditya is a
student of BSC
Q.25 WASS to identify all zero-byte files in the current directory and
delete them. Before proceeding with deletion, the shell script should get
a confirmation from the user.
Ans.
for i in *
do
if [ -e $i -a -f $i ]
then
if [ -s $i ]
then
echo
else
rm -i $i
fi
fi
done
Output:
sh SS25
rm: remove regular empty file `E.TXT'? y
rm: remove regular empty file `Q.TXT'? n
rm: remove regular empty file `W.TXT'? n

Q.26 WASS to compute the GCD and LCM of two numbers.


Ans.
echo Enter First number
read n1
echo Enter Second number
read n2
if [ $n1 -lt $n2 ]
then
i=$n1
else
i=$n2
fi
flag=0while [ $flag -eq 0 ]
do
if [ `expr $n1 % $i` -eq 0 -a `expr $n2 % $i` -eq 0 ]
then
echo GCD of $n1 and $n2 is $i
flag=1
fi
i=`expr $i - 1`
done
Output:
sh SS26
Enter First number
4
Enter Second number
8
GCD of 4 and 8 is 4
Q.27 Two numbers are entered through the keyboard. WAP to find the
value of one number raised to the power of another.
Ans.
read b
echo Enter power

read p
powr=$p
result=1
while [ $p -ge 1 ]
do
result=`expr $result \* $b`
p=`expr $p - 1`
done
echo $b raised to the power $powr is $result
Output :
sh SS27
Enter a number
4
Enter power
2
4 raised to the power 2 is 16

Q.28 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 first
read passw
i=0
while [ $i -lt 3 ]
do
echo Enter password
read p
if [ $p = $passw ]
then
echo Correct Password
i=3
else
echo Wrong password
i=`expr $i + 1`
fi
done
Output:
sh SS28
Set the password first
Yaman Singhania
Enter password
Yaman Singhania
Correct Password

Q.29 WASS that repeatedly asks the user repeatedly for the Name of
the Institution until the user gives the correct answer.
Ans.
flag=0
while [ $flag -eq 0 ]
do
echo Enter name of your institute in capital letters
read inst
if [ $inst = "AMITY" ]
then
echo Correct Answer flag=1
else
echo Enter Again
fi
done
Output:
sh SS29
Enter name of your institute in capital letters
AMITY
Correct Answer

Q. 30 WAP to generate all combinations of 1, 2 and 3 using for loop.


Ans.
for i in 1 2 3
do
for j in 1 2 3
do
for k in 1 2 3
do
echo $i$j$k
done
done
done
Output : sh SS30
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313

321
322
323
331
332
333

Das könnte Ihnen auch gefallen