Download - Unix Lab File
-
7/27/2019 Unix Lab File
1/37
1. Write a Shell Script which creates the following menu and prompts for choicefrom user and runs the chosen command.
a. Today's date
b. Process of user
c. List of files
d. Quit to UNIX
echo Enter your Choiceecho 1. Dateecho 2. Process of Userecho 3. List of Files
echo 4. Exitread chcase "$ch" in1)date ;;2)PS ;;3)LS ;;4)exit ;;*) echo invalid Choiceesac
-
7/27/2019 Unix Lab File
2/37
2. Write a Shell Script which will redirect the output of the date commandwithout the time into a file.
echo Enter the file nameread filea= `date|cut -b1-11, 25-28`echo $ a|tee -a $fileecho "\n" $file successfully created
echo "\n" "content of file is:" `cat $file`
-
7/27/2019 Unix Lab File
3/37
3. 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.
if [ -d $1 ] ; thenecho "It is a Directory"fiif [ -f $1 ] ; thenecho "It is a File"fi
if [ ! -r $1 ] ; thenecho "File is not readable"elseecho "File is Readable"fi
if [ -w $1 ] ; thenecho "File is Writable"elseecho "File is not Writable"
fi
if [ -x $1 ] ; thenecho "File is Executable"elseecho "File Is not Executable"fi
-
7/27/2019 Unix Lab File
4/37
4. Write a Shell Script that computes the factorial of a given number
echo "Enter any no for Factorial"read noa=$noi=1;while test $no -ge 1doi=`expr $i \* $no`no=`expr $no - 1`doneecho "Factorial of " $a " is "$i
-
7/27/2019 Unix Lab File
5/37
-
7/27/2019 Unix Lab File
6/37
5. Write a Shell Script that works like a calendar reminding the user ofcertain things depending on the day of the week.
a=`date +%A`echo "\n" Today is $aecho Your tasks for today is as followscase $a inMonday) echo Come late in class;;Tuesday) echo Just two lectures and then masti;;Wednesday) echo Boring Lectures;;Thursday) echo Yupiiieeee Foriegn Language Lecture
;;Friday) echo Last Working Day;;Saturday) echo Movie;;Sunday) echo Sleeping;;Esac
-
7/27/2019 Unix Lab File
7/37
6. Write a Shell Script which takes a command line argument of Kms and bydefault converts that number into meters. Also provide options to convertkm to dm and km to cm.
echo "Enter kms"read noa=$noi=1p=1000i=`expr $a \* $p`echo "In meters = " $iecho "Enter choice for operation 1 for dm or 2 for cm"read op
case $op in
1)c=`expr $a \* 10000`echo "In dm" $c;;
2)d=`expr $a \* 100000`echo "In cm" $d;;
esac
-
7/27/2019 Unix Lab File
8/37
7. Write a Shell Script that performs a count-down either from 10 (default) orfrom the value that is entered by the user.
a=10while test $a -ge 1doecho $asleep 5a=`expr $a - 1`done
-
7/27/2019 Unix Lab File
9/37
8. If a number is input through the keyboard, WASS to calculate sum of itsdigits.
echo "Enter the number ="read noa=$noe=0while test $a -ge 1dor=`expr $a \% 10`a=`expr $a \/ 10`e=`expr $e + $r`doneecho $e
-
7/27/2019 Unix Lab File
10/37
9. Write a Shell Script using for loop, which displays the message "Welcometo the UNIX System"
Echo displayin a messageFor((i=0;i
-
7/27/2019 Unix Lab File
11/37
10.WAP to calculate and print the first m Fibonacci numbers.
echo "Enter any no"read no
n1=-1n2=1i=1echo "The fibonacci series is"while test $i -le $nodon3=`expr $n1 + $n2`echo $n3n1=$n2n2=$n3i=`expr $i + 1`
done
-
7/27/2019 Unix Lab File
12/37
11.WASS to compute the GCD and LCM of two numbers.
echo "Enter the first number :"read a
echo "Enter the second number : "read b
if [ $a -gt $b ]thennum=$aden=$belsenum=$bden=$afi
r=`expr $num % $den`
while [ $r -ne 0 ]donum=$denden=$rr=`expr $num % $den`done
gcd=$denlcm=`expr $a \* $b / $gcd`
echo " The LCM of $a and $b is : $lcm"echo " The GCD of $a and $b is : $gcd"
-
7/27/2019 Unix Lab File
13/37
12.Two numbers are entered through the keyboard. WAP to find the value ofone number raised to the power of another.
Echo "Enter the no."read no
echo "Enter it's power"read pwi=1c=1while test $i -le $pwdoc=`expr $c \* $no`i=`expr $i + 1`doneecho "the power "$pw" of "$no" is "$c
-
7/27/2019 Unix Lab File
14/37
13.WAP to generate all combinations of 1, 2 and 3 using for loop.
for i in 1 2 3dofor j in 1 2 3
dofor k in 1 2 3doecho $i$j$kdonedonedone
-
7/27/2019 Unix Lab File
15/37
14.WASS that repeatedly asks the user repeatedly for the Name of theInstitution until the user gives the correct answer.
echo "Name of the Instituition"read str
if test $str = aiitthenecho "Name of instituition is aiit"elseecho "Wrong name"fi
-
7/27/2019 Unix Lab File
16/37
15.Write a Shell Script that changes the extension of a group of files from txtto doc
for file in *.txtdoleftname=`basename $file txt`mv $file ${leftname}docdone
-
7/27/2019 Unix Lab File
17/37
16. 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
if [ -f $1 -a -f $2 ]thenif diff $1 $2thencat $1echo "\n"cat $2echo contents are same, second file is being deletedrm $2elseecho contents are different
fielseecho "file does not exist"fi
-
7/27/2019 Unix Lab File
18/37
17.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.
char=ywhile [ $char="y" ]dolsdf -hsleep 2echo "want to continue ?"read chardone
-
7/27/2019 Unix Lab File
19/37
18.Write a Shell Script to change the filename of all files in a directory fromlower-case to upper-case.
for i in *domv $i `echo $i|tr "[:lower:]" "[:upper:]"`done
-
7/27/2019 Unix Lab File
20/37
19.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 and fileswhose names end in .c are moved to directory named cprograms.
for i in *.olddomv $i oldfilesdone
for i in *.cdomv $i cprogramsdone
Ques: Write a Shell Script which searches all files in the given directory (to be taken as
-
7/27/2019 Unix Lab File
21/37
command line argument) for the file having the title (to be taken as command line
argument), as the first line in the file and display the contents of the searched file and
then, print the size, where
File is small-sized if total no. of lines is
-
7/27/2019 Unix Lab File
22/37
-
7/27/2019 Unix Lab File
23/37
Ques: 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
Code:if [ ! -d $1 ]thenecho "'$1' is not a directory..."
elif [ `expr $#` -eq 0 ]thenecho "ERROR...! (no arguements passed)"
elseecho ":::::FILES (exceeding 1000):::::"
echo " "cd $1ls -l|grep [1-9][0-9][0-9][0-9].|cut -d " " -f 5,9|sort -g -r
echo "----- ------------"echo "TOTAL= `ls -l|grep -c [1-9][0-9][0-9][0-9]. "
fi
-
7/27/2019 Unix Lab File
24/37
-
7/27/2019 Unix Lab File
25/37
Ques: Write a Shell Script 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...
Code:
echo "Enter Directory- "read DIR
if [ ! -d $DIR ]thenecho "$DIR is not a directory..."
elsecd $DIR
echo ":::::CONTENTS OF $DIR:::::"
ls -l
for i in *doif [ -f $i ]thenmv $i $i.`echo $$`fidone
echo ":::::CONTENTS OF $DIR (new):::::"ls -l
fi
-
7/27/2019 Unix Lab File
26/37
-
7/27/2019 Unix Lab File
27/37
Ques: Write a Shell Script 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.
Code:
for i in $*doif [ -e $i ]then
if [ -f $i ]
thenecho "'$i' exists...(file)"elif [ -d $i ]thenecho "'$i' exists...(directory)"fi
elseecho "'$i' doesnot exists..."echo File created: `date| cut -c 1-10,20-28` > $iecho "'$i' created..."
if [ ! -e MYDIR ]thenecho 'MYDIR' doesnot exist...mkdir MYDIRecho Created 'MYDIR'...fimv $i /home/aman/MYDIR/$iecho "'$i' moved to MYDIR..."fi
done
-
7/27/2019 Unix Lab File
28/37
-
7/27/2019 Unix Lab File
29/37
Ques: Write 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
Code:if [ `expr $# % 2` -ne 0 ]thenecho "ERROR...! (odd number of files passed)"
elif [ `expr $#` -eq 0 ]thenecho "ERROR...! (no files passed)"
elsecount=0
for i in $*docount=`expr $count + 1`
if [ $count -eq 1 ]thenfile1=$ifi
if [ $count -eq 2 ]thenfile2=$iecho $file1 copied to $file2cp $file1 $file2count=0fi
donefi
-
7/27/2019 Unix Lab File
30/37
-
7/27/2019 Unix Lab File
31/37
Ques: Write a shell script 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
Code:echo ALL FILESls
for i in *doif [ -f $i -a ! -s $i ]thenrm -i $ifi
done
echo "ALL FILES (empty files removed)"l
s
-
7/27/2019 Unix Lab File
32/37
Ques: Write a shell script 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
Code:
count=1
while [ $count -le 3 ]doecho Enter Password-read pass
if [ $pass == "ak727" ]thenecho CORRECT PASSWORD...exit
elseif [ `expr 3 - $count` == 0 ]thenecho "WRONG PASSWORD (No more attempt available)..."exitfi
echo "WRONG PASSWORD (`expr 3 - $count` attemp(s) left)..."count=`expr $count + 1`fi
done
-
7/27/2019 Unix Lab File
33/37
-
7/27/2019 Unix Lab File
34/37
Ques: Write a Shell Script that takes a search string and filename from the terminal &
displays the results
Code:
echo "Enter file- "read FILE
echo "Enter Pattern (to search)- "read ch
if [ ! -f $FILE ]thenecho "'$FILE' is not a file..."
elseif [ `grep -c $ch $FILE` -gt 0 ]then
echo "Pattern '$ch' is found in '$FILE' '`grep -c $ch $FILE` time(s) ' "fifi
-
7/27/2019 Unix Lab File
35/37
Ques: 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
Code:
if [ `expr $#` -eq 0 ]then
echo "ERROR...! (no arguments passed)"
elif [ `expr $#` -eq 1 ]thenecho "ERROR...! (file no passed)"
else
if [ ! -f $2 ]thenecho "'$2' is not a file..."
elseif [ `grep -c $1 $2` -gt 0 ]thenecho "Pattern '$1' is found in '$2' '`grep -c $1 $2` time(s)' "fifi
fi
-
7/27/2019 Unix Lab File
36/37
Ques: 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 resultis to be stored. Write a Shell Script that accepts both filename and a set of patterns as
positional parameters to a script...
Code:
if [ `expr $#` -gt 3 ]thenecho "ERROR...! (extra arguments passed)"
elif [ `expr $#` -lt 3 ]then
echo "ERROR...! (Required argument(s) not passed)"
elseif [ ! -f $2 ]thenecho "'$2' is not a file..."if [ ! -f $3 ]thenecho "'$3' is also not a file..."fi
elif [ ! -f $3 ]thenecho "'$3' is not a file..."
elseif [ `grep -c $1 $2` -gt 0 ]thenecho "Pattern '$1' is found in '$2' '`grep -c $1 $2` time(s)'" > $3elseecho "Pattern '$1' is not found in '$2'" > $3fifi
fi
-
7/27/2019 Unix Lab File
37/37