Linux-Lab
Practical File
Bachelor of Science and Information Technology
Session: - 2021
Submitted To: - Submitted By: -
Dr. Monisha Awasthi Harsh Raj
Assistant Professor (UIM) Roll No: 16
1|P age
INDEX
S. No. TITLE Page No. Remarks/
Signature
1. Design, develop and implement a shell script 06
that accepts a path name and creates all the
components in the path name as directories
(ex: a/b/c/d should create a directory a, a/b,
a/b/c, a/b/c/d.)
2. Design, develop and implement a shell script 08
that accepts two file names as arguments,
checks if the permissions for these files are
identical and if the permissions are identical,
output common permissions and otherwise
output each file name followed by its
permissions.
3. Design, develop and implement a shell script 10
to find out biggest number from given three
nos. Numbers are supplied as command line
arguments. Print error if sufficient arguments
are not supplied.
4. Design, develop and implement a shell script 12
that takes a valid directory name as an
argument and recursively descend all the
subdirectories find its maximum length of any
file in that hierarchy and writes this maximum
value to the second output.
2|P age
5. Design and implement a shell script that 14
computes the gross salary of a employee
according to the following rules:
I) If basic salary is <1500 then HRA=10% of
the basic and DA=90% of the basic
ii) If the basic salary is>=1500 then
HRA=500/- and DA=98% of the basic
The basic salary is entered interactively
through the key board.
6. Design, Develop and implement an interactive 16
file –handling shell program. Let it offer the
user the choice of copying removing,
renaming, or linking files. Once the user has
made a choice, have the same program ask the
user for the necessary information, such as the
file name, new name and so on.
7. Design, Develop and implement a shell script 21
to perform the following string operations:
I)To extract a sub-string from a given string.
II)To find the length of a given string.
8. Design, Develop and implement a shell script 23
that display all the links to a file specified as
the first argument to the script. The second
argument, which is optional, can be used to
specify in which the search is to begin in
current working directory, In either case, the
starting directory as well as all its
3|P age
subdirectories at all levels must be searched.
The script need not include any error checking
9. Design, Develop and implement a shell script 24
that reports the logging in of a specified user
within one minute after he/she logs in. The
script automatically terminates if the specified
user does not login during a specified period
of time
10. Design, Develop and implement a shell script 26
that folds long lines into 40 columns. Thus,
any line that exceeds 40 characters must be
broken after 40th; a\ is to be appended as the
indication of folding and the processing is to
be continued with the residue. The input is to
be through a text file created by the user.
11. Design, Develop and implement a shell script 28
to implement terminal locking (similar to the
lock command) .it should prompt the user for
the password .after accepting the password
entered by the user it must prompt again for
the matching password as confirmation and if
match occurs it must lock the keyword until a
matching password is entered again by the
user ,note that the script must be written to
disregard BREAK, control-D. No time limit
need be implemented for the lock duration.
4|P age
12. Design, Develop and Implement a shell script 31
that delete all lines containing a specific word
in one or more file supplied as argument to it.
5|P age
1. Design, develop and implement a shell script that accepts a path name
and creates all the components in the path name as directories (ex:
a/b/c/d should create a directory a, a/b, a/b/c, a/b/c/d.)
Solution: -
#!/bin/sh
echo "enter the pathname"
read p
i=1
j=1
len=`echo $p|wc -c`
while [ $i -le $len ]
do
x=`echo $p | cut -d / -f $j`
namelength=`echo $x|wc -c`
mkdir $x
cd $x
pwd
j=`expr $j + 1`
i=`expr $i + $namelength`
echo $g
done
6|P age
OUTPUT
7|P age
2. Design, develop and implement a shell script that accepts two file
names as arguments, checks if the permissions for these files are
identical and if the permissions are identical, output common
permissions and otherwise output each file name followed by its
permissions.
Solution: -
#!/bin/bash
if [ $# -ne 2 ]
then
echo "pass 2 argument"
exit
fi
echo enter file name
read f1
echo enter the second file name
read f2
p1=`ls -l $f1 | cut -c 2-10`
p2=`ls -l $f2 | cut -c 2-10`
if [ $p1 = $p2 ]
then
echo permissions are same
echo $p1
else
echo permissions are different
echo permission of file $f1 is $p1
8|P age
echo permission of file $f2 is $p2
fi
OUTPUT
9|P age
3. Design, develop and implement a shell script to find out biggest
number from given three nos. Numbers are supplied as command line
arguments. Print error if sufficient arguments are not supplied.
Solution: -
#!/bin/bash
echo Enter three numbers:
read a b c
i=$a
if[$b -gt $i]
then
i=$b
fi
if[$c -gt $i]
then i=$c
fi
echo Largest Number of $a $b and $c is $i.
10 | P a g e
OUTPUT
11 | P a g e
4. Design, develop and implement a shell script that takes a valid
directory name as an argument and recursively descend all the
subdirectories find its maximum length of any file in that hierarchy
and writes this maximum value to the standard output.
Solution: -
#!/bin/bash
for i in $*
do
if [ -d $i ]
then
echo "large filename size is"
echo `ls -Rl $1 | grep "^-" | tr -s ' ' | cut -d' ' -f 5,8 | sort -n
| tail -1`
else
echo "not directory"
fi
done
12 | P a g e
OUTPUT
13 | P a g e
5. Design and implement a shell script that computes the gross salary of
an employee according to the following rules:
i) If basic salary is <1500 then HRA=10% of the basic and
DA=90% of the basic
ii) If the basic salary is>=1500 then HRA=500/- and DA=98% of
the basic
The basic salary is entered interactively through the key board.
Solution: -
#!/bin/bash
echo " Enter the Basic salary"
read bs
if [ $bs -lt 1500 ]
then
hra=`echo $bs \*10 /100 | bc`
da=`echo $bs \* 90 / 100 | bc`
elif [ $bs -ge 1500 ]
then
hra=500
da=`echo $bs \* 98 /100 | bc`
fi
gs=`echo $bs + $hra + $da | bc`
echo "Gross salary=$gs"
14 | P a g e
OUTPUT
15 | P a g e
6. Design, Develop and implement an interactive file –handling shell
program. Let it offer the user the choice of copying removing,
renaming, or linking files. Once the user has made a choice, have the
same program ask the user for the necessary information, such as the
file name, new name and so on.
Solution: -
#!/bin/bash
echo "*******MENU*********"
echo "
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files."
echo "enter your choice"
read -r k
case $k in
1 ) echo "The list of file names."
ls -l ;;
2 ) echo "Enter the old filename."
read -r ofile
echo "Enter the new file name."
read -r nfile
cp "$ofile" "$nfile" && echo "Copied sucessfully." || echo "Copied is not
possible." ;;
16 | P a g e
3 ) echo "Enter the file name to remove."
read -r rfile
rm -f "$rfile" && echo "Successfully removed." ;;
4 ) echo "Enter the old file name."
read -r ofile
echo "Enter the new file name."
read -r nfile
mv "$ofile" "$nfile" && echo "The file $ofile name renamed to $nfile." ||
echo "You cann't Rename the file. " ;;
5 ) echo "Enter the original filename."
read -r ofile
echo "Enter the new filename to link a file."
read -r lfile
ln "$ofile" "$lfile " && echo "Create the linking file Sccessfully." || echo
"You cann't Linking the file.";;
*)
echo "Invalid option."
echo " Enter correct choice."
esac
17 | P a g e
OUTPUT
18 | P a g e
19 | P a g e
20 | P a g e
7. Design, Develop and implement a shell script to perform the following
string operations:
a). To extract a sub-string from a given string.
b). To find the length of a given string.
Solution: -
#!/bin/sh
echo "enter the string"
read str
strlen=${#str}
echo "The length of the given string '$str' is:$strlen "
echo "enter the string possibion in main str"
read s1
echo "ending position"
read f1
echo $str | cut -c$s1-$f1
21 | P a g e
OUTPUT
22 | P a g e
8. Design, Develop and implement a shell script that display all the links
to a file specified as the first argument to the script. The second
argument, which is optional, can be used to specify in which the search
is to begin in current working directory, in either case, the starting
directory as well as all its subdirectories at all levels must be searched.
The script need not include any error checking.
Solution: -
#!/bin/sh
if [ $# -eq 1 ]
then pwd>tm
cat tm
else
tm=$2
echo "$tm"
fi
t1=`ls -aliR | grep "$1" | cut -c 1-8 `
ls -alir $tm | grep "$t1" |cut -c 65- > t2
echo "the links are"
cat t2
OUTPUT
23 | P a g e
9. Design, Develop and implement a shell script that reports the logging
in of a specified user within one minute after he/she logs in. The script
automatically terminates if the specified user does not login during a
specified period of time.
Solution: -
#!/bin/sh
echo"Enter the username:"
read user
period=0
while[true]
do
var='who|grep -w "$user"
len='echo "$var"|wc-c'
if[$len -gt 1]
then
echo"$user logged in $tm seconds."
exit
else
sleep 1
tm='expr $tm+1'
fi
if[$tm -eq 61]
then
echo"$user didn't login within 1 minute"
exit
24 | P a g e
fi
done
OUTPUT
25 | P a g e
10. Design, Develop and implement a shell script that folds long lines into
40 columns. Thus, any line that exceeds 40 characters must be broken
after 40th; a\ is to be appended as the indication of folding and the
processing is to be continued with the residue. The input is to be
through a text file created by the user.
Solution: -
#!/bin/sh
echo “ Enter the filename :\c”
read fn
for ln in `cat $fn`
do
lgth=`echo $ln | wc -c`
lgth=`expr $lgth - 1`
s=1;e=5
if [ $lgth -gt 40 ]
then
while [ $lgth -gt 40 ]
do
echo "`echo $ln | cut -c $s-$e`\\"
s=`expr $e + 1`
e=`expr $e + 40`
lgth=`expr $lgth - 40`
done
echo $ln | cut -c $selse
echo $ln
26 | P a g e
fi
done
echo “File Folded ”
OUTPUT
27 | P a g e
11. Design, Develop and implement a shell script to implement terminal
locking (similar to the lock command) .it should prompt the user for
the password .after accepting the password entered by the user it must
prompt again for the matching password as confirmation and if match
occurs it must lock the keyword until a matching password is entered
again by the user ,note that the script must be written to disregard
BREAK, control-D. No time limit need be implemented for the lock
duration.
Solution: -
#!/bin/sh
clear
stty -echo
echo "enter password to lock the terminal"
read pass1
echo " Re-enter password"
read pass2
if [ "$pass1" = "$pass2" ]
then
echo "system is locked"
echo "enter password to unlock"
trap ``/1 2 3 9 15 18
while true
do
read pass3
if [ $pass1 = $pass3 ]
then echo "system unlocked"
28 | P a g e
stty echo
exit
else
echo "password mismatch"
fi
done
else
echo "password mismatch"
stty echo
fi
29 | P a g e
OUTPUT
30 | P a g e
12. Design, Develop and Implement a shell script that delete all lines
containing a specific word in one or more file supplied as argument to
it.
Solution: -
#!/bin/sh
if[$# -eq 0]
then
echo"no arguments"
else
echo"Enter a deleting word or char"
read y
for i in $*
do grep -v "$y""$i">temp
if[$? -ne 0]
then
echo"Pattern not found."
else
cp temp $i
rm temp
fi
done
fi
done
31 | P a g e
OUTPUT
32 | P a g e