Pages

Monday, 25 February 2019

LIST OF SHELL PROGRAM

/* 1)program to illustrate if  */
#!/bin/bash
echo " enter a value"
read a
echo " enter b value"
read b
if [ $a -ne $b ]
then
    echo "a is not equal to b"
fi

/* 2)program to illustrate if else */
#!/bin/bash
echo "Enter a value"
read a
echo "Enter b value"
read b
if [ $a -eq $b ]
then
 echo "a is equal to b"
else
 echo "a is not equal to b"
fi

/* 3)program to illustrate nested if else */
#!/bin/bash
echo "Enter a value"
read a
echo "Enter b value"
read b
echo "Enter c value"
read c
if [ $a -gt $b  -a $a -gt $c ]
then
     echo "a is big"
elif [ $b -gt $c ]
then
     echo "b is big"
else
     echo "c is big"                       
fi

/* 4)program to find Odd or Even numbers */
#!/bin/bash
echo " enter any no"
read n
x=`expr $n % 2`
if [ $x -eq 0 ]
then
    echo "$n is even"
else
    echo "$n is odd"
fi

/* 5)program to illustrate switch case */
#!/bin/bash
echo "enter 1st value"
read x
echo  "enter 2nd value"
read y
echo "enter 1 adding"
echo "enter 2 sub"
echo "enter 3 mul"
echo "enter 4 quo"
echo "enter 5 rem"
echo "enter 6 exit"
echo "enter * invalid number"
echo "enter u r choice"
read s
case $s in
1)p=`expr $x + $y`
echo "sum =$p"
;;

2)p=`expr $x - $y`
echo "sub=$p"
;;

3)p=`expr $x \* $y`
echo "mul=$p"
;;

4)p=`expr $x / $y`
echo "quo =$p"
;;

5)p=`expr $x % $y`
echo "rem=$p"
;;

6)exit
;;

*)echo "wrong choice"
;;
esac

/* 6) program to find factorial using while*/
#!/bin/bash
echo "enter any  number"
read n
fact=1
while [ $n -ne 0 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "factorial is : $fact"

/* 6.1)program to find factorial using for*/
#!/bin/bash
fact=1
echo "enter a number"
read n
for (( i=$n; i>=1; i-- ))
do
fact=`expr $fact \* $i`
done
echo "the factorial of $n is $fact"

/* 7)program to illustrate multiplication using while  */
#!/bin/bash
echo "multiplication table"
echo "which table u want"
read n
iter=1
while [ $iter -le 10 ]
do
res=`expr $n \* $iter`
echo $n"*"$iter "=" $res
iter=`expr $iter + 1`
done

/* 7.1)program to illustrate multiplication using for  */
#!/bin/bash
echo "multiplication table"
echo "which table u want"
read n
iter=1
for((  ; $iter < 10 ; ))
do
res=`expr $n \* $iter`
echo $n"*"$iter "=" $res
iter=`expr $iter + 1`
done

/* 8)program to find perfect number using while */
#!/bin/bash
echo enter any number
read n
sum=0
i=1
while [ $i -lt $n ]
do
    x=`expr $n % $i`
    if [ $x -eq 0 ]
    then
    sum=`expr $sum + $i`
    fi
i=`expr $i + 1`
done
if [ $sum -eq $n ]
then
echo number is perfect
else
echo number is not a perfect
fi

/* 8.1)program to find perfect number using for */
#!/bin/bash
echo enter any number
read n
sum=0
i=1
for ((  ;  $i <  $n ; ))
do
    x=`expr $n % $i`
    if [ $x -eq 0 ]
    then
    sum=`expr $sum + $i`
    fi
i=`expr $i + 1`
done
if [ $sum -eq $n ]
then
echo number is perfect
else
echo number is not a perfect
fi

/* 9)program to find prime number using while  */
#!/bin/bash
echo "enter any number"
read n
c=0
i=1
while [ $i -le $n ]
do
 x=`expr $n % $i`
 y=`expr $n % $n`
 if [ $x -eq 0 -a $y -eq 0 ]
 then
 c=`expr $c + 1`
 fi
 i=`expr $i + 1`
done
if [ $c -eq 2 ]
then
echo "$n is a prime number"
else
echo "$n is a not a prime number"
fi

/* 9.1)program to find prime number using for  */
#!/bin/bash
echo "enter any number"
read n
c=0
for (( i=1; i<=$n; i++ ))
do
 x=`expr $n % $i`
 y=`expr $n % $n`
 if [ $x -eq 0 -a $y -eq 0 ]
 then
 c=`expr $c + 1`
 fi
done
if [ $c -eq 2 ]
then
echo "$n is a prime number"
else
echo "$n is a not a prime number"
fi

/* 10)program to reverse the given number using while */
#!/bin/bash
echo "enter any number:"
read n
s=$n
rev=0
while [ $n -gt 0 ]
do
rem=`expr $n % 10`
n=`expr $n / 10`
rev=`expr $rev \* 10 + $rem`
done
echo "$s reverse number  is $rev"

output:

/* 10.1)program to reverse the given number using for */
#!/bin/bash
echo "enter any number:"
read n
s=$n
rev=0
for ((  ;  $n > 0 ; ))
do
rem=`expr $n % 10`
n=`expr $n / 10`
rev=`expr $rev \* 10 + $rem`
done
echo "$s reverse number  is $rev"

output:


/* 11)program to illustrate sequentional for  */
for i in $(seq 1 2 10)
do
echo $i
done

/* 12)program to illustrate sequentional for  */
for i in $(seq 10)
do
echo $i
done

/* 13)program to illustrate hi and bye  */

hi()
{
 echo "welcome to unix"
}
bye()
{
date
ls -l
banner  ram is a boy
cowsay sita is a girl
}

output:


No comments:

Post a Comment