Beruflich Dokumente
Kultur Dokumente
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
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. 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.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
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
321
322
323
331
332
333