Professional Documents
Culture Documents
1
15. Write a Shell Script which takes a command line argument of Kms and 19
by default converts that number into meters. Also provide options to
convert km to dm and km to cm.
16. Write a Shell Script using for loop, which displays the message 20
"Welcome to the UNIX System"
17. Write a Shell Script to change the filename of all files in a directory from 21
lower-case to upper-case.
18. Write a Shell Script that examines each file in the current directory. Files 22
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.
19. Write a Shell Script which searches all files in the given directory (to be 24
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, print the file is ###, where
### is small-sized if total no. of lines is <50
### is medium-sized if total no. of lines between 50&100
### is large-sized.
20. Write a shell script which reports names and sizes of all files in a 25
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.
21. WASS for renaming each file in the directory such that it will have the 26
current shell PID as an extension. The shell script should ensure that the
directories do not get renamed.
22. WAP to calculate and print the first m Fibonacci numbers. 27
23. WASS that will receive any number of filenames as arguments. The 28
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 doesn’t 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.
24. A shell script receives even number of filenames. Suppose four 29
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.
25. WASS to identify all zero-byte files in the current directory and delete 30
them. Before proceeding with deletion, the shell script should get a
conformation from the user.
26. WASS to compute the GCD and LCM of two numbers. 31
27. Two numbers are entered through the keyboard. WAP to find the value 32
of one number raised to the power of another.
2
28. WASS that prompts the user for the password. The user has maximum of 33
3 attempts. If the user enters the correct password, the message “Correct
Password” is displayed else the message “Wrong Password”.
29. WASS that repeatedly asks the user repeatedly for the “Name of the 34
Institution” until the user gives the correct answer.
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
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
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 :
4
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 :
Ans :
5
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 :
cat output
echo "\n" Contents are same Second file is being deleted
echo"\n" Contents are different
echo "\n" File does not exist
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 :
6
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
fi
Output :
$ sh ss4 free
file exist
file has read permission
the contents are......
Aditya is studying in BCA 5th Year Enrollment NO. A1004807003
He stays in Model Town
File have write permission
He is a student
You 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
Ans :
Output :
sh SS5
Enter a choice.
1. Todayś Date
2. Process of user
3. List of files
4. Quit to unix
8
1
Thu Nov 20 10:52:32 IST 2008
sh SS5
Enter a choice.
1. Todayś Date
2. Process of user
3. List of files
4. Quit to unix
2
PID TTY TIME CMD
6001 pts/0 00:00:00 bash
6203 pts/0 00:00:00 sh
6204 pts/0 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 SS9
e.txt output ss1 ss12 ss15 ss18 SS20 ss23 SS26 SS29 ss4 ss7 tr.c
f1 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
Ans :
9
do
fact=`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
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
10
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
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
11
ls *.doc
Output :
$ sh ss8
Before
d.txt f.txt r.txt
After
d.doc e?q?w.doc f.doc q?w.doc r.doc w.doc
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
12
Output :
Ans :
13
Output :
sh ss10
Enter the file name
ss2
Thu Nov 20 2008
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
2008
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\)?"
14
read char
done
Output :
sh ss11
cold gold sold ss11 ss14 SS17 ss2 ss22 SS25 SS28 SS30 ss6 SS9
e.txt output ss1 ss12 ss15 ss18 SS20 ss23 SS26 SS29 ss4 ss7 tr.c
f1 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
varlock 452316 0 452316 0% /var/lock
udev 452316 56 452260 1% /dev
devshm 452316 12 452304 1% /dev/shm
lrm 452316 39760 412556 9% /lib/modules/2.6.24-19-
generic/volatile
gvfs-fuse-daemon 4845056 2476544 2124328 54% /home/aditya/.gvfs
aditya 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 ]
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
Aditya Sadh
Aditya Sadh
Contents are same Second file is being deleted
$
13. If a number is input through the keyboard, WASS to calculate sum of its digits.
Ans :
16
exit
fi
Output :
sh ss13
Enter a no.
2345
14
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 :
17
Output :
sh ss14
Enter the Countdown time.
3
3
2
1
0
Count down timer stopped
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
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
19
Output :
sh ss16
Welcome to Unix System
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
20
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 SS9
E.TXT OUTPUT SS1 SS12 SS15 SS18 SS20 SS23 SS26 SS29 SS4 SS7 TR.C
F1 Q.TXT SS10 SS13 SS16 SS19 SS21 SS24 SS27 SS3 SS5 SS8 W.TXT
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 cprograms.
Ans :
21
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 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 oldfiles
total 0
After
total 152
-rwxrwxrwx 1 aditya sadh 66 2008-11-20 10:07 COLD
drwxr-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
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 <50
### is medium-sized if total no. of lines between 50&100
### is large-sized.
Ans :
23
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 :
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
24
Output :
sh SS20
total 4
-rw-r--r-- 2 aditya aditya 1392 2008-11-20 11:02 unix output
Total number of such files is : 1
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 :
25
Output :
sh SS21.7600.a
File name changed:
total 152
-rwxrwxrwx aditya aditya 66 2008-11-20 10:07 COLD.7600.a.a
drwxr-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.a
drwxr-xr-x 2 aditya aditya 4096 2008-11-20 12:04 oldfiles
22. WAP to calculate and print the first m Fibonacci numbers.
Ans :
26
Output :
sh SS22
Enter the series length
6
0
1
1
2
3
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 doesn’t 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 exist
mkdir 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
27
Output :
sh SS23 SS22
Directory : mydir exist
SS22
File does not exists
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
28
Output :
$ cat>txt
File Aditya lives in Delhi$ cat>doc
Aditya is a student of BCA$ cat>tree
Aditya is a cool dude$ cat>wee
His roll no- is A1004807024
$ sh SS24 txt doc tree wee
His roll no- is A1004807024 Shivani is a cool dude Aditya lives id Delhi Aditya is a student of
BCA
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 conformation 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
29
Output :
sh SS25
Ans :
30
Output :
sh SS26
Enter First number
4
Enter Second number
8
GCD of 4 and 8 is 4
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
31
Output :
sh SS27
Enter a number
4
Enter power
2
4 raised to the power 2 is 16
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 :
32
Output :
sh SS28
Set the password first
Aditya
Enter password
aditya
Correct Password
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
33
Output :
sh SS29
Enter name of your institute in capital letters
AMITY
Correct Answer
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
34
233
311
312
313
321
322
323
331
332
333
35