You are on page 1of 34

UNIX

LAB FILE

SUBMITTED TO:

SUBMITTED BY:

NEETU GUPTA

KAPIL GARG
B.Sc.-IT
A1004913054
2013-2016

INDEX
S.No
.
1.

Particulars
Write a Shell Script that takes a search string and
filename from the terminal & displays the results.

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.

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.

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.
Write a Shell Script which creates the following menu
and prompts for choice from user and runs the chosen
command.
1. Today's date
2. Process of user
3. List of files
4. Quit to UNIX
Write a Shell Script that computes the factorial of a
given number.
Write a Shell Script that works like a calendar
reminding the user of certain things depending on the
day of the week.
Write a Shell Script that changes the extension of a
group of files from txt to doc.

5.

6.
7.
8.
9.

Write a Shell Script that accepts both filename and a


set of patterns as positional parameters to a script.

10.

Write a Shell Script which will redirect the output of


the date command without the time into a file.

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.
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.

12.

Date

Sign.

13.

If a number is input through the keyboard, WASS to


calculate sum of its digits.

14.

Write a Shell Script that performs a count-down


either from 10 (default) or from the value that is
entered by the user.

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.
Write a Shell Script using for loop, which displays the
message "Welcome to the UNIX System".

16.
17.

Write a Shell Script to change the filename of all files


in a directory from lower-case to upper-case.

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.
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
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.
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.

19.

20.

21.

22.

WAP to calculate and print the first m Fibonacci


numbers.

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.

24.

25.

26.
27.

28.

29.

30.

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.
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.

WASS to compute the GCD and LCM of two


numbers.
Two numbers are entered through the keyboard.
WAP to find the value of one number raised to the
power of another.
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.
WASS that repeatedly asks the user repeatedly for the
Name of the Institution until the user gives the
correct answer.
WAP to generate all combinations of 1, 2 and 3 using
for loop.

1. Write a Shell Script that takes a search string and filename from the terminal & displays the results.
Ans.
echo enter a string
read str
echo enter filename in which string is to be searched
read file
a=`grep $str $file| wc -c`
if [ $a -gt 0 ]
then
echo pattern Found
else
echo pattern Not Found
fi

Output:

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 $# -ne2
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:

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
fi

Output:

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
echo 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 dont have execute permission
fi
else
echo Nothing exist by this name
fi

Output:

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:

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


Ans.
echo Enter the no. to compute its factorial
read num
i=1
fact=1
while test $i -le $num
do
fact=expr $fact \*$i
i=expr $i + 1
done
echo factorial of :$num is :$fact

Output:

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 Khagendra
echo \n Today is $a
echo Your task for today is as follows
case $a in
Monday)echo Complete Unix Assignment
;;
Tuesday)echo Study for unix
;;
Wednesday)echo Get books
;;
Thursday)echo Rest day
;;
Friday)echo Study for exams
;;
Saturday)echo Go out and enjoy
;;
Sunday)Sunday is a fun day
;;
esac
Output:

8. Write a Shell Script that changes the extension of a group of files from txt to doc
Ans.
clear
echo "Enter first file name: "
read f1
if [ -f $f1 ]
then
echo "Enter new extention: "
read e
cp ${f1} ${f1}.$e
# i=basename $f1
# b= $i txt
# for file in *.txt
# i=`basename file txt`
# mv $file $i doc
echo $f1"."$e" file is created"else
echo "File not Exist "
fi
Output:

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:

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 successfully created
echo \n Content of the file is : cat $file

Output:

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.
while :
do
echo show current directory
ls
echo show current disk space
df
echo show current logged user
who am i
echo sleep for 30 seconds
sleep 30
done

Output:

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 $f2]
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
echo \n Contents are different
fi
else
echo \n File does not exist
fi

Output:

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
temp=`expr $sum + $temp
temp=`expr $num / 10
else
echo $sum
exit
fi
done

Output:

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 n
clear
while[ $n -ge 0]
do
echo $n
sleep 1
n= `expr $n -1
done
echo Countdown timer stop

Output:

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
a=`expr $km \* 1000`
echo
echo $km km is equal to $a meter
echo Enter your choice
echo 1 Convert into decimeter
echo 2 Convert into centimeter
echo
read b
case $b in
1) dm=`expr $km \* 10000`
echo $km km is equal to $dm dms
;;
2) cm=`expr $km \* 100000`
echo $km km is equal to $cm cms
;;
esac
Output:

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:

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:

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:

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 subdirectory 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:

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:

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:

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:

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:

You might also like