You are on page 1of 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 "1.Rename the file"

echo "2.to print the working directory"

echo "3.list of users logged in"

echo "4.exit"

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 exp1st.sh

OPTION ARE

1.Rename the file

2.to print the working directory

3.list of users logged in

4.exit

enter your choice

enter the filename to rename

w.sh

enter the newname

l.sh

admin@DESKTOP-IO0TGBV ~/BCA

$ sh exp1st.sh

OPTION ARE
1.Rename the file

2.to print the working directory

3.list of users logged in

4.exit

enter your choice

present working directory:

/home/admin/BCA

admin@DESKTOP-IO0TGBV ~/BCA

$ sh exp1st.sh

OPTION ARE

1.Rename the file

2.to print the working directory

3.list of users logged in

4.exit

enter your choice

list of your logged in

admin@DESKTOP-IO0TGBV ~/BCA

$ sh exp1st.sh

OPTION ARE

1.Rename the file

2.to print the working directory


3.list of users logged in

4.exit

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 pg2.sh SHC.sh SHC2.sh sacred College.sh x.sh

---------

SHC.sh is a file

contents of the file is

echo "Good morning "

file is executable

---------

SHC2.sh is a file

contents of the file is

echo "welcome "

file is not executable

-------

content of directory are:

total 0

---------

College.sh is a file

contents of the file is

echo good morning

file is executable

---------

x.sh 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 "1.appending a file"

echo "2.list file or directory with specified pattern"

echo "3.assign execute permission to users and group"

echo "4.exit"

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 prm3.sh

1.appending a file
2.list file or directory with specified pattern

3.assign execute permission to users and group

4.exit

enter your choice

enter the source file

shc.sh

enter designation file

a.sh

echo "welcome to SHC"

echo "welcome to SHC"

1.appending a file

2.list file or directory with specified pattern

3.assign execute permission to users and group

4.exit

enter your choice

enter the pattern

ex*

file or directories with ex* are:

exam1.sh example02 example10 example4 exm2.sh exp1.sh exp2.sh exp4.sh exp5.sh

1.appending a file

2.list file or directory with specified pattern

3.assign execute permission to users and group

4.exit

enter your choice


3

enter a filename

a.sh

before assigning permission

-rw-rwxr--+ 1 lab1 None 46 Feb 25 01:28 a.sh

chmod: invalid mode: `ug'

Try 'chmod --help' for more information.

after assigning permission

-rwxrwxr--+ 1 lab1 None 46 Feb 25 01:28 a.sh

1.appending a file

2.list file or directory with specified pattern

3.assign execute permission to users and group

4.exit

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 exp4.sh

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 pg4.sh -d

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

wrong number of arguments

SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh A.sh

$ sh pg4.sh -d A.sh

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

the file A.sh deleted

SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh

$ sh pg4.sh B.sh

invalid options
SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh

$ shpg4.sh -d X.sh

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

File X.sh not found

$ sh pg4.sh -c college.sh SHC.sh

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

file SHC.sh already exists

do you want to overwrite<y/n>

file college.sh has been copied to SHC.sh

SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh

$ sh pg4.sh -c D.sh SHC.sh

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

File D.sh file does not exists

SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh

$ sh pg4.sh -c SHC.sh college.sh

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

file college.sh already exists

do you want to overwrite<y/n>

coping operation has been aborted

SHC.sh college.sh exp1.sh exp2.sh pgm3.sh exp4.sh exp5.sh

You might also like