Professional Documents
Culture Documents
Linux Lab File 4th Sem
Linux Lab File 4th Sem
Linux Lab File 4th Sem
Practical File
1|P age
INDEX
S. No. TITLE Page No. Remarks/
Signature
2|P age
5. Design and implement a shell script that 14
computes the gross salary of a employee
according to the following rules:
3|P age
subdirectories at all levels must be searched.
The script need not include any error checking
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
read p
i=1
j=1
do
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
exit
fi
read f1
read f2
if [ $p1 = $p2 ]
then
echo $p1
else
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
read a b c
i=$a
then
i=$b
fi
then i=$c
fi
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 `ls -Rl $1 | grep "^-" | tr -s ' ' | cut -d' ' -f 5,8 | sort -n
| tail -1`
else
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
Solution: -
#!/bin/bash
read bs
then
then
hra=500
fi
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."
read -r k
case $k in
ls -l ;;
read -r ofile
read -r nfile
16 | P a g e
3 ) echo "Enter the file name to remove."
read -r rfile
read -r ofile
read -r nfile
mv "$ofile" "$nfile" && echo "The file $ofile name renamed to $nfile." ||
echo "You cann't Rename the file. " ;;
read -r ofile
read -r lfile
ln "$ofile" "$lfile " && echo "Create the linking file Sccessfully." || echo
"You cann't Linking the file.";;
*)
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
read str
strlen=${#str}
read s1
read 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
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
read user
period=0
while[true]
do
var='who|grep -w "$user"
len='echo "$var"|wc-c'
if[$len -gt 1]
then
exit
else
sleep 1
tm='expr $tm+1'
fi
then
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
read fn
do
lgth=`expr $lgth - 1`
s=1;e=5
if [ $lgth -gt 40 ]
then
do
s=`expr $e + 1`
e=`expr $e + 40`
done
echo $ln
26 | P a g e
fi
done
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
read pass1
read pass2
if [ "$pass1" = "$pass2" ]
then
trap ``/1 2 3 9 15 18
while true
do
read pass3
if [ $pass1 = $pass3 ]
28 | P a g e
stty echo
exit
else
fi
done
else
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
read y
for i in $*
do grep -v "$y""$i">temp
if[$? -ne 0]
then
else
cp temp $i
rm temp
fi
done
fi
done
31 | P a g e
OUTPUT
32 | P a g e