0% found this document useful (0 votes)
89 views17 pages

Shell Script Menu Operations Guide

The document describes a shell script that accepts command line arguments to either delete or copy files. It checks that the correct number of arguments are provided based on the option selected. For copying, it checks if the destination file already exists and prompts the user to overwrite. The output shows the script handling different test cases like insufficient arguments, non-existent source file, overwriting destination file etc.

Uploaded by

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

Shell Script Menu Operations Guide

The document describes a shell script that accepts command line arguments to either delete or copy files. It checks that the correct number of arguments are provided based on the option selected. For copying, it checks if the destination file already exists and prompts the user to overwrite. The output shows the script handling different test cases like insufficient arguments, non-existent source file, overwriting destination file etc.

Uploaded by

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

NAME : PREETHI DATE:15/12/2021

EXP NO : 1 CLASS:2 nd BCA

PROGRAM:Write a menu driven shell for following.

a)Rename the file(Check for the existence of the source file)

b)Display the current working directories

c)List the user logged in.

echo "OPTION ARE"

echo "[Link] the file"

echo "[Link] print the working directory"

echo "[Link] of users logged in"

echo "[Link]"

echo "enter your choice"

read ch

case "$ch" in

1)echo "enter the filename to rename"

read fname

if [ -f $fname ]; then

echo "enter the newname"

read nwname

mv $fname $nwname

else

echo "file doesnt exit"

fi

;;

2)echo "present working directory:"


pwd;;

3)echo "list of your logged in"

who;;

4)exit;;

esac

OUTPUT:

admin@DESKTOP-IO0TGBV ~/BCA

$ sh [Link]

OPTION ARE

[Link] the file

[Link] print the working directory

[Link] of users logged in

[Link]

enter your choice

enter the filename to rename

[Link]

enter the newname

[Link]

admin@DESKTOP-IO0TGBV ~/BCA

$ sh [Link]

OPTION ARE
[Link] the file

[Link] print the working directory

[Link] of users logged in

[Link]

enter your choice

present working directory:

/home/admin/BCA

admin@DESKTOP-IO0TGBV ~/BCA

$ sh [Link]

OPTION ARE

[Link] the file

[Link] print the working directory

[Link] of users logged in

[Link]

enter your choice

list of your logged in

admin@DESKTOP-IO0TGBV ~/BCA

$ sh [Link]

OPTION ARE

[Link] the file

[Link] print the working directory


[Link] of users logged in

[Link]

enter your choice

4
NAME : PREETHI DATE:2/1/2022

EXP NO : 2 CLASS:2 nd BCA

PROGRAM: Write a shell script to accept many files name through command
line. Do the following for each file name.

a)If it an ordinary file, display the content and check it has execute permission

b)If it is a directory display the number of file in it.

c)If the file/directories doesn’t exist display a message.

for arg in $*

do

if [ -f $arg ];then

echo "________________"

echo "$arg is a file"

echo "contentes of the file is:"

cat $arg

if [ -x $arg ];then

echo "file is executable"

else

echo "file is not executable"

fi

elif [ -d $arg ];then

echo"__________________"

echo "content of the directory are:"

ls -l $arg

else

echo"_____________"

echo"$arg is not a file or directory:"


fi

done

OUTPUT:

$ sh [Link] [Link] [Link] sacred [Link] [Link]

---------

[Link] is a file

contents of the file is

echo "Good morning "

file is executable

---------

[Link] is a file

contents of the file is

echo "welcome "

file is not executable

-------

content of directory are:

total 0

---------

[Link] is a file

contents of the file is

echo good morning

file is executable

---------

[Link] is not a file or directory


NAME : PREETHI DATE:11/1/2022

EXP NO : 3 CLASS:2 nd BCA

PROGRAM: Write a Menu driven shell script for the following.

a) Append the content of a file to another file(Display the message if the file
doesn’t exist in the directories)

b) List all the file names/directories names in the present working directory
which has the specified pattern.

c)Assign the execute permission to a specified file for the owner and group.

while [ : ]

do

echo "[Link] a file"

echo "[Link] file or directory with specified pattern"

echo "[Link] execute permission to users and group"

echo "[Link]"

echo "enter your choice"

read ch

case $ch in

1)echo "enter the source file"

read sname

if [ -f $sname ];then

echo "enter designation file"

read dname

if [ -f $dname ];then

cat $sname>>$dname

cat $dname
else

echo "designation file doesnt exist"

fi

else

echo "source file doesnt exist"

fi

;;

2)echo "enter the pattern"

read pattern

echo "file or directories with $pattern are:"

ls $pattern;;

3)echo "enter a filename"

read fname

echo "before assigning permission"

ls -l $fname

chmod ug + X $fname

echo "after assigning permission"

ls -l $fname

;;

4)exit;;

esac

done

OUTPUT:

lab1@lab1-PC ~/BCA

$ sh [Link]

[Link] a file
[Link] file or directory with specified pattern

[Link] execute permission to users and group

[Link]

enter your choice

enter the source file

[Link]

enter designation file

[Link]

echo "welcome to SHC"

echo "welcome to SHC"

[Link] a file

[Link] file or directory with specified pattern

[Link] execute permission to users and group

[Link]

enter your choice

enter the pattern

ex*

file or directories with ex* are:

[Link] example02 example10 example4 [Link] [Link] [Link] [Link] [Link]

[Link] a file

[Link] file or directory with specified pattern

[Link] execute permission to users and group

[Link]

enter your choice


3

enter a filename

[Link]

before assigning permission

-rw-rwxr--+ 1 lab1 None 46 Feb 25 01:28 [Link]

chmod: invalid mode: `ug'

Try 'chmod --help' for more information.

after assigning permission

-rwxrwxr--+ 1 lab1 None 46 Feb 25 01:28 [Link]

[Link] a file

[Link] file or directory with specified pattern

[Link] execute permission to users and group

[Link]

enter your choice


NAME : PREETHI DATE:17/1/2022

EXP NO :5 CLASS:2 nd BCA

PROGRAM: Write a shell script access many characters and count individuals
vowels, digits, spaces, special characters, and constants.

echo "Enter a string:"

read str

str=` expr "$str" | tr "a-z" "A-Z"`

n=`expr length "$str"`

echo $n

VC=0

dc=0

sp=0

sc=0

cc=0

i=1

while [ $i -le $n ];do

ch=`expr substr "$str" $i + 1`

case $ch in

[AEIOU])vc=` expr $vc + 1`

;;

[BCDFGHJKLMNPQRSTVWXYZ])cc=` expr $cc + 1`

;;

[0-9])dc=` expr $dc + 1`

;;

[" "])sp=` expr $sp + 1`

;;

*) sc =` expr $sc + 1`
esac

i=`expr $i + 1`

done

echo number of vowels: $vc

echo number of digits: $dc

echo number of spaces: $sp

echo number of special character: $sc

echo number of consonant: $cc

OUTPUT:

$ sh [Link]

Enter a string:

sacred heart12345@#

19

number of vowels: 4

number of digits: 5

number of spaces: 1

number of special character: 2

number of consonant: 7
NAME : PREETHI DATE:12/1/2022

EXP NO : 4 CLASS:2 nd BCA

PROGRAM: Write a shell script to accept your options for deleting(-d) or for
copying(-c) a file and filename(s) through command line arguments.

(Ex:For deletion:$sh filename –d file1; for copying:$sh filename –c file 2)

And check for the following:

a) Check whether the given arguments are sufficient for the selected option.

b) file to be copied or deleted must be present in the directories.

c) while copying,if the destination file already exists prompt for overwriting.

echo case $1 in

-d) if [ $# -ne 2 ]

then

echo ********output*********

echo "wrong number of arguments"

echo ************

exit

fi

file=$2

if [ ! -f $file ]

then

echo **********output************

echo "File "$file" not found"

exit

fi
rm $file

echo *********************output************

echo "the file "$file" deleted"

;;

-c) if [ $# -ne 3 ]

then

echo *********output************

echo "Wrong number of arguments"

echo **********

exit

fi

file=$2

if [ ! -f $file ]

then

echo ********output***********

echo "File "$file" file not exists"

echo *****************

exit

fi

option=y

file2=$3

if [ -f $file2 ]

then

echo **********output****************

echo "file "$file2" already exists"

echo "do you want to overwrite<y\n>"

read option
fi

case $option in

y|Y) cp $file $file2

echo "file" $file "has been copied to" $file2

;;

*)echo "copying operation has been aborted"

;;

esac

;;

*)echo "invaid option"

;;

Esac

OUTPUT:

$ sh [Link] -d

********OUTPUT********

wrong number of arguments

[Link] [Link] [Link] [Link] [Link] [Link] [Link] [Link]

$ sh [Link] -d [Link]

******OUTPUT*********

the file [Link] deleted

[Link] [Link] [Link] [Link] [Link] [Link] [Link]

$ sh [Link] [Link]

invalid options
[Link] [Link] [Link] [Link] [Link] [Link] [Link]

$ [Link] -d [Link]

*******OUTPUT********

File [Link] not found

$ sh [Link] -c [Link] [Link]

******OUTPUT*******

file [Link] already exists

do you want to overwrite<y/n>

file [Link] has been copied to [Link]

[Link] [Link] [Link] [Link] [Link] [Link] [Link]

$ sh [Link] -c [Link] [Link]

******OUTPUT*******

File [Link] file does not exists

[Link] [Link] [Link] [Link] [Link] [Link] [Link]

$ sh [Link] -c [Link] [Link]

******OUTPUT*******

file [Link] already exists

do you want to overwrite<y/n>

coping operation has been aborted

[Link] [Link] [Link] [Link] [Link] [Link] [Link]

You might also like