The document contains 11 programs written in the Bash shell scripting language. Program 1 concatenates the contents of two files into a third file. Program 2 calculates the sum, average, and product of 4 input numbers. Program 3 swaps the values of two variables without using a third variable.
1. Program 1
WAP to concatenate the contents of 2 files ?
cat > first
hai this is first file
^d
cat > second
hai this is Second file
^d
program
-------
vi prog1
echo contents of first file
cat first
echo contents of second file
cat second
echo contents of third file
cat first second > third
cat third
esc : x
output
------
sh prog1
Program 2
WAP to Find SUm,AVG,PRODUCT of 4 numbers ?
vi prog2
clear
echo enter A,B,C,D values
read a
read b
read c
read d
sum= `Expr $a + $b + $c + $d`
Avg= `Expr $sum /4`
2. Product= `Expr $a * $b * $c *d`
echo Sum is $sum
echo Avg is $Avg
echo Product is $ Product
esc:x
output
------
sh prog2
Program 3
WAP to change two values with out using another variable ?
vi prog3
clear
echo enter A,B values
read a
read b
echo actual values are A="$a" and B="$b"
a=`Expr $a + $b`
b=`Expr $a - $b`
c=`Expr $a - $b`
echo swapped values are "A"=$a and "B"=$b
esc:x
output
-----
sh prog3
Program 4
WAP to find largest of 3 numbers
vi prog4
clear
read a
read b
read c
3. if [$a -gt $b]
then
if [$a -gt $c]
then echo $a is Bigger
else
echo $c is Bigger
fi
else
if [$b -gt $c]
then
echo $b is Bigger
else
echo $c is Bigger
fi
fi
output
------
sh prog4
Program 5
WAp to find the Factorial of a given number
vi prog5
clear
echo enter the number to get Factorial
read n
fact=1
i=$n
while [$n -ge 1]
do
fact =`Expr $fact * $n`
n=`Expr $n-1`
done
echo Factorial of $i is $fact
output
4. ------
sh prog5
Program 6
WAP to find the Sum of INDIVIDUAL digits of given numbers
vi prog6
clear
echo enter any number having atleast 2 digits
read n
i=$n
r=0
sum=0
while test $n -gt 0
do
r=`Expr $n %10`
sum=`Expr $sum + $r`
n=`Expr $n/10`
done
echo sum of individual digits of $i is $sum
output
-------
sh prog6
Program 7
WAP to find the reverse of a given number
vi prog7
clear
echo enter a number
read n
m=$n
x=0
while test $n -gt 0
do
r=`Expr $n %10`
x=`Expr $n *10 + $r`
5. n=`Expr $n/10`
done
echo reverse of given number is $x
output
------
sh prog7
Program 8
WAP to generate fibnacci series
vi prog8
clear
echo enter any number
read n
fo=0
f1=1
echo $fo
echo $f1
i=3
while test $i le $n
do
f2=`Expr $fo + $f1`
echo $f2
f0=$f1
f1=$f2
i=`Expr $i+1`
done
output
------
sh prog8
Program 9
WAP to check whether a given number is PALLENDROME or NOT
vi prog9
echo enter any number
read n
x=0
6. m=$n
while test $n -gt 0
do
r=`Expr $n%10`
x=`Expr $x *10 +r`
n=`Expr $n/10`
done
if test $x -eq $n
then
echo given number is PALLENDROME
else
echo given number is NOT a PALLENDROME
fi
output
------
sh prog9
Program 10
Write a menu Driven Prog
vi prog10
clear
echo "1.Contents of a FILE "
echo
echo "2.Present Working Directory"
echo
echo "3.EXIT"
echo
echo "Enter your Choice"
read ch
case $ch in
1)echo enter file name
read filename
cat $filename
;;
2)echo present working directory
pwd
;;
3)echo program EXIT
;;
7. *)echo Incorrect choice
;;
esac
output
-------
sh prog10
Program 11
WAP to count the no.of UPPER case letters , no.of lowercase letters, no.of digits
and no.of DIGITS and no.of Special characters in a given string
vi prog11
clear
echo enter a string
read s
len=`echo $s |wc-c`
h=`echo $len -1`
echo length is $h
u=0
l=0
d=0
sc=0
while test $n -gt 0
do
a=`echo $s |cat-c $n`
case $a in
[a-z])
l=`Expr $l+1`
;;
[A-Z])
u=`Expr $u+1`
;;
[0-9])
d=`Expr $d+1`
;;
*)
sc=`Expr $sc+1`
8. ;;
esac
n=`Expr $n-1`
done
echo no.of uppercase letters are $u
echo no.of lowercase letters are $l
echo no.of digits are $d
echo no.of special characters are $sc