1. Write a Menuoperation using while and casearithmetic Driven Program to do all the .

choice= yecho "Enter two read a numbers" b while [$choice = "y" ] do echo "1) addition: " echo "2)sub: " echo "3)multiply: " echo "4)division: " echo "5)exit: " echo "Enter ur choice: " read ch case $ch in 1)c=`expr $a + $b` echo "the addition is: " 2)d=`expr$c;; $a $b` echo "the subtraction is: " $d;; 3)e=`expr $a \* $b` echo "the multiplication is: " $e;; 4)f=`expr $a \/ $b` echo "the division is: " $f;; 5) exit;; esac echo "do you want to continue y:n" read choice done

1

Output : [admin@localhost ~]$ Prog1.sh Enter two numbers 20 20 1) addition: 2)sub : )multipl 3 y: 4)divisio n: 5)exit : nter E ur choice: 1 the addition is: 40 you want to continue do y:n y 1) addition: 2)sub : )multipl 3 y: 4)divisio n: 5)exit : nter E ur choice: 2 the subtraction is: 0o you want to continue d y:n y 1) addition: 2)sub : )multipl 3 y: 4)divisio n: 5)exit : nter E ur choice: 3 the multiplication is: 400you want to continue do y:n y 1) addition: 2)sub : )multipl 3 y: 4)divisio n: 5)exit : nter E ur choice: 4 the division is: 1o you want to continue d y:n

sh

y 1) addition: 2)sub : )multipl 3 y: 4)divisio n: 5)exit : nter E ur choice: 5 [admin@localhost ~] $

3

2. Write a menu driven program to do factorial & fibonacci using FOR, While &CASE. choice= y hile [ $choice = "y" w ]o d echo "1) factorial: " cho "2) fibonacci: e " cho "3) exit: e " cho "enter the number: e "ead r ncho "enter ur choice: e "ead r ch case $ch in 1)fact= 1cho "The factorial of the number " $n e "is:" [ $n -gt 1 while ]o d fact=`expr $fact \* $n` n=`expr $n 1` done echo "factorial is:" $f act ;; 2) a=0 b=1 echo "Fibonacci series is:" -e $a "\n" echo $b for((i=3; i<=$n; i+ +)) do c=`expr $a + $b` $c echo a=$b b=$c done ;; 3) exit;; esac echo "do you want to continue? Y/n" read done choice

OUTPUT [ ~]$ sh : admin@localhost Prog2.sh 1) factorial: 2) fibonacci: 3) exit: enter the number: 1 enter ur choice: 1 The factorial of the number 1 is: factorial is: 1o you want to continue? d Y/n y 1) factorial: 2) fibonacci: 3) exit: enter the number: 6 enter ur choice: 1 The factorial of the number 6 is: factorial is: 720 you want to continue? do Y/n y 1) factorial: 2) fibonacci: 3) exit: enter the number: 7 enter ur choice: 2 Fibonacci series is: 0 1 1 2 3 5 8

do you want to continue? Y/n y

5

1) 2) factorial: fibonacci: 3) exit: enter number: 0 enter choice: 3

the ur

3. Write a shell script to check whether the stringor not. is palindrome

echo "Enter the number" read a=0 s rem= 0 t=$ a hile [ $a -gt 0 w ]o d rem=`expr $a \% 10` s=`expr $s \* 10 + $rem` a=`expr $a \/ done 10` if [ $s -eq $t ] the ncho e " string is palindrome." els echo "string is nor palindrome." f i

7

OUTPUT : [admin@localhost Prog3.sh Enter the number 65 6tring s is palindrome.

~]$

sh

4. Write a shell script to reverse a given string of numbers. echo "Enter the number" read aev= r 0em= r 0 hile [ $a -gt 0 w ]o d rem=`expr $a \% 10` rev=`expr $rev \* 10 + $rem` a=`expr $a \/ done 10` echo "The reverse of digit is" echo $rev

9

OUTPUT : [admin@localhost ~]$ Prog4.sh Enter the number 123 4 he reverse of digit T is 432 1

sh

5. Write a program to find the greatest among parameter through positional three numbers s. if [ $# -ne 3] the n echo "You didn't enter three arguments" els e echo "You enter the right numbers" $2 -a $1 -gt $3 if [ $1 -gt ] the ncho "Greatest nuber is: " e $1 [ $2 -gt $3 elif ]he t ncho "Greatest number is:" e $2 els echo "Greatest number is:" $3 f i f i

11

OUTPUT : [admin@localhost ~]$ sh Prog5.sh You didn't enter three arguments [admin@localhost ~]$ sh Prog5.sh 4 5 6 ou enter the right Y numbers number is: Greatest 6

6. Write a shell script to delete all zero size files from a directory. echo "Enter the directory name in which you want to delete zero sized files:" read path cd $path "You are in the directory" echo $path = file `ls` i in for $file do if [ -s “$i” ] the n echo "File” $i “has data" els e echo "File” $i “has no rm -i data" $i echo "File has been deleted" f done i

13

OUTPUT : Enter the directory name in which you want to delete zero sized files: / home/admin/MC directory You are in the A MCA has no File1 data File has been deleted has no File2 data File has been deleted has File3 data File4 has data

7. Write a program in C to print the pyramid. #include<stdio.h >oid v main() { int n,c=1; int i,j,k,t,s; printf("Enter the number:"); scanf ("%d",&n); s=n; for(i=1;i<=n;i+ +) { for(k=s;k>=1;k-) printf(" "); for(j=i;j>=1;j-) printf("* "); s=s-1; printf("\n") ; } }

15

OUTPUT : [admin@localhost Prog7.c [admin@localhost ./Prog7.o number Enter the 6 * * * * * * * * * * * * * * * * * * *

~]$

gcc ~]$

-o

Prog7.o

8. Write a program to searchfile name shouldgrep in a given use pattern and a pattern using be entered by file, r. Stu.ls t 1001|Neha|G.M|SALES|12/12/21| 90000 1002|Anu|D.M| PRODUC |03/12/50| 13000 1003|Deepak|D.G.M|MKT|05/03/30| 85000 1004|Savita|G.M|PERS|12/12/12| 40000 1005|Shipra|M.D|GOVT|13/20/30| 45000 Prog8.s h echo "Enter filename" read acho "Enter e pattern to be searched" read b grep "$b" $a

17

OUTPUT [ ~]$ sh : admin@localhost Prog8.sh Enter filename stu.ls t nter E pattern to be searched G.M 1001|Neha|G.M|SALES|12/12/21| 90000 1003|Deepak|D.G.M|MKT|05/03/30| 85000 1004|Savita|G.M|PERS|12/12/12| 40000

9. Write a program to search afile nameusing egrep in a givenuse pattern and pattern should be entered by file, r. pat.ls t ALE S S OVT G [ Prog9.s h echo "Enter file name" read acho "Enter the pattern to e search" read pat [ -s if $pat ] the ngrep -f $pat e $a els echo "File" $i " has no data:Nothing to search" f i

19

OUTPUT : [admin@localhost ~]$ sh Prog9.sh file Enter name stu.ls t nter the pattern to E search pat.ls t 001|Neha|G.M|SALES|12/12/21| 1 90000 1005|Shipra|M.D|GOVT|13/20/30| 45000

10.Write a menu driven program to search and replace a patter n using sed command . echo "Enter the file name" read file "What do u want to echo do?" echo "1. For pattern searching" echo "2. For pattern replacing" echo "Enter ur choice:" read ch [ $ch -eq if 1] the ncho "Enter the pattern to e search:" read pattern sed -n "/"$pattern"/p" $file [ $ch -eq elif 2] the ncho "Enter the pattern to e replace:" read pattern1 echo "Enter the pattern with which to be replaced:" read pattern2 sed "s/"$pattern1"/"$pattern2"/g" $file els echo "wrong option" f i

21

OUTPUT : [admin@localhost ~]$ sh Prog10.sh Enter the file name stu.ls t hat do u want to W do? For 1. pattern searching 2. For pattern replacing Enter ur choice: 1 Enter the pattern to search: SALE S001|Neha |G.M |SALES |12/12/21| 1 90000 [admin@localhost ~]$ sh Prog10.sh [admin@localhost ~]$ sh Prog10.sh Enter the file name stu.ls t hat do u want to W do? For 1. pattern searching 2. For pattern replacing Enter ur choice: 2 Enter the pattern to replace: SALE S nter the pattern with which to be E replaced: SA L001|Neha |G.M |SAL |12/12/21| 1 90000 1002|Anu |D.M |PRODUC|03/12/50| 13000 1003|Deepak|D.G.M|MKT |05/03/30| 85000 1004|Savita|G.M |PERS |12/12/12| 40000 1005|Shipra|M.D |GOVT |13/20/30| 45000

11.Write a program to show the functionality of shell functio n. copy() { echo "enter the file which is to be copied:" read file1 "enter the file name for echo copy:" read file2 cp $file1 $file2 "the file has been echo copied"

} move( ) {

echo "enter the file which is to be moved:" read file1 "enter the name for the echo move:" read file2 mv $file1 $file2 "the file is echo moved" } remove( ) { echo "enter the file which is to be removed:" read file1 rm -i $file1 "the file has been echo removed" } list( ) { echo "enter the file or directory whose detail you want" read file1 -l ls $file1 "the detail of file or echo directory" } choice= y hile [ $choice = "y" w ]o d echo "1.for copy the file from source to destination:" moving the echo "2.for files:" "3.for removing the echo files:" "4.for listing all the echo files:" "enter echo your choice:"

23

read case $ch ch in 1) copy ;; 2) move ;; 3) remove ;; 4) list ;; 5) exit ;; esac echo "do you want to continue? y/n" read choice done

OUTPUT : [admin@localhost ~]$ sh Prog11.sh 1.for copy the file from source to destination: 2.for moving the files: removing the 3.for files: listing all the 4.for files: enter your choice: 1 enter the file which is to be copied: aa.tx t nter the file name for e copy: anu1.tx t p: cannot stat `aa.txt': No such file or c directory has been the file copied want to continue? do you y/n y 1.for copy the file from source to destination: 2.for moving the files: removing the 3.for files: listing all the 4.for files: enter your choice: 2 enter the file which is to be moved: anu1.tx t nter the name for the e move: anu2.tx t v: cannot stat `anu1.txt': No such file or m directory the file is moved want to continue? do you y/n y 1.for copy the file from source to destination: 2.for moving the files: removing the 3.for files: listing all the 4.for files: enter your choice: 4 enter the file or directory whose detail you want anu2.tx ts: cannot access anu2.txt: No such file or l directory the detail of file or directorywant to continue? do you y/n n

25

12.Write a program to show the file permisssio n. echo "the file name is" read af [ ! -f i $a ] the ncho "file doesn't e exist" els ef [ ! -s i $a ] the ncho "file exists but of zero e size" f if [ ! -r $a ] the ncho e "file is not readable." els echo "file is readable." f if [ ! -w $a ] the ncho "file is not e writable" els echo "file is writable" f if [ ! -x $a ] the ncho e "file is not executable" els echo "file is executable" f i f i

OUTPUT : [admin@localhost Prog12.sh the file name is stu.ls t ile f is readable. is file writable is file not executable

~]$

sh

27

13.Write a script to copy a file without overwritin g. if [ $# -ne 2 ] ; then echo "Usage: $0 source destination" ; elif [ ! exit $2 ] ; -d then echo "directory $2 doesn't exist" els e file=$ 1 [ -f $2/$file ] ; if then $1 cp $2 els eopies=1 c while true ; do [ ! -f $2/$file.$copies ] ; if then cp $1 $2/$1.$copies $1 copied to echo "File $1.copies" brea kls e eopies=`expr $copies + c 1` f i one d f i f i

Output : [admin@localhost safe1 directory safe1 exist [admin@localhost safe [admin@localhost safe File script.sh script.sh.copies

~]$ sh cpback.sh script.sh doesn't ~]$ mkdir ~]$ sh cpback.sh script.sh copied to

29

14. Write a program to do data entry in a existing fil e. #!/bin/s h while echo "Designation code: \c" ; do read desig "$desig" case in [0-9][0-9]) if grep "^$desig" desig.lst >/dev/null ; then echo "Code exists" ; continue fi ; ; ) echo "Invalid code" ; * continue ;; esac while echo "Description : \c" ; do desc read case "$desc" in a-zA-Z]*) echo "Can contain only alphabets and spaces" ; *[!\ continue "Description not entered" ; continue "") echo ;; ;; echo "$desig|$desc" >> newlist ; *) break esac done echo "\nWish to (y/n): \c" read answer"$answer" case in [yY]*) continue ;; *) break ;; esac done continue?

Output : [admin@localhost ~]$ sh dentry1.sh Designation code: 01 Code exists Designation code: 07 Description : security officer Wish to continue? (y/n) : nadmin@localhost ~] [ $

31