0% found this document useful (0 votes)
104 views33 pages

Amisha Linux File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views33 pages

Amisha Linux File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Linux-Lab

Practical File

Bachelor of Science and Information Technology


Session: - 2021

Submitted To: - Submitted By: -


Dr. Monisha Awasthi Amisha Rawat
Associate Professor (UIM) Roll No: 05

Page 1 of 33
INDEX
S. TITLE Page Remarks/Signature
No. No.

1. Design, develop and implement a shell 06


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

2. Design, develop and implement a shell 08


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.

3. Design, develop and implement a shell 10


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.

4. Design, develop and implement a shell 12


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

Page 2 of 33
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 16


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.

7. Design, Develop and implement a shell 21


script 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 23


script that display all the links to a file
specified as the first argument to the script.

Page 3 of 33
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

9. Design, Develop and implement a shell 25


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

10. Design, Develop and implement a shell 27


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.

11. Design, Develop and implement a shell 29


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

Page 4 of 33
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.

12. Design, Develop and Implement a shell 32


script that delete all lines containing a
specific word in one or more file supplied
as argument to it.

Page 5 of 33
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

Page 6 of 33
OUTPUT

Page 7 of 33
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

Page 8 of 33
echo permission of file $f2 is $p2

fi

OUTPUT

Page 9 of 33
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.

Page 10 of 33
OUTPUT

Page 11 of 33
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

Page 12 of 33
OUTPUT

Page 13 of 33
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"

Page 14 of 33
OUTPUT

Page 15 of 33
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." ;;

Page 16 of 33
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

Page 17 of 33
OUTPUT

Page 18 of 33
Page 19 of 33
Page 20 of 33
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

Page 21 of 33
OUTPUT

Page 22 of 33
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

Page 23 of 33
OUTPUT

Page 24 of 33
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 name of user"

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

Page 25 of 33
fi

done

OUTPUT

Page 26 of 33
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

Page 27 of 33
fi

done

echo “File Folded ”

OUTPUT

Page 28 of 33
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"

Page 29 of 33
stty echo

exit

else

echo "password mismatch"

fi

done

else

echo "password mismatch"

stty echo

fi

Page 30 of 33
OUTPUT

Page 31 of 33
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

Page 32 of 33
OUTPUT

Page 33 of 33

You might also like