Wednesday, 17 April 2019

Write shell script, using case statement to perform basic math operation as follows : + Addition - Substraction / Division x Multiplication.


Write shell script, using case statement to perform basic math operation
 as follows :
 + Addition
 - Substraction
 / Division
 x Multiplication.

script:-

if [ $# -eq 3 ]
  then
       case $2 in
         +)   let z=$1+$3;;
         -)   let z=$1-$3;;
         /)   let z=$1/$3;;
         x|X) let z=$1*$3;;
         *)   echo "Warning: $2 invalid operator, only +, -, x, / operator"\
                     " allowed."
              exit;;
       esac
       echo "Answer is $z"
else
       echo "Usage: $0 value1 operator value2"
       echo "         Where, value1 and value2 are numeric values"
       echo "                operator: +, -, /, x (multiplication)"
                       fi   

Write a shell script to print sum of digit of any number


Write a shell script to print sum of digit of any number
echo -n "Enter number : "
read n

# store single digit
sd=0

# store number of digit
sum=0

# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder
    n=$(( $n / 10 ))  # get next digit
    sum=$(( $sum + $sd )) # calculate sum of digit
done
echo  "Sum of all digit  is $sum"

Ouput:
[root@localhost shell_script]# bash sumofdigit.sh
Enter number : 123
Sum of all digit  is 6

Write a shell script to print sum of all even numbers from 1 to 10.


Write a shell script to print sum of all even numbers from 1 to 10.

Script:
echo enter limit

read n
i=2
while [ $i -lt $n ]
do
sum=$((sum+i))
i=$((i+2))
done
echo $sum.

Output
[root@localhost shell_script]# bash sumofevenno.sh
enter limit
10
20.

Write a shell script to calculate factorial of a given number

Write a shell script to calculate factorial of a given number

Script:
echo "Enter a number"
read num
fact=1
while [ $num -gt 1 ]
do
  fact=$((fact * num))  #fact = fact * num
  num=$((num - 1))      #num = num - 1
done
echo $fact

Output:
[root@localhost shell_script]# bash factorial.sh
Enter a number
3
6

Write a shell script to print table of a given number


Write a shell script to print table of a given number

Script:
echo "Enter a Number"
read n
i=0
while [ $i -le 10 ]
do
          echo " $n x $i = `expr $n \* $i`"
          i=`expr $i + 1`
done

Output:
[root@localhost shell_script]# bash  multiplicationtable.sh
Enter a Number
2
 2 x 0 = 0
 2 x 1 = 2
 2 x 2 = 4
 2 x 3 = 6
 2 x 4 = 8
 2 x 5 = 10
 2 x 6 = 12
 2 x 7 = 14
 2 x 8 = 16
 2 x 9 = 18
2 x 10 = 20

Write a shell script to print all even and odd number from 1 to 10.


Write a shell script to print all even and odd number from 1 to 10.

Script:

echo enter n value as range to calculate odd and even numbers.
read n
i=1
while [ $i -le $n ]
do
if [ `expr $i % 2` -eq 0 ]
then
echo even=$i
else
echo odd=$i
fi
i=`expr $i + 1`
done

Output:
[root@localhost shell_script]# bash oddeven.sh
enter n value as range to calculate odd and even numbers.
10
odd=1
even=2
odd=3
even=4
odd=5
even=6
odd=7
even=8
odd=9
even=10

Write a shell script to calculate profit or loss.


Write a shell script to calculate profit or loss.
echo "input nthe cost price of an item"
read cop
echo "input the selling price of the item"
read sop
if test $cop -eq $sop
then
echo "no profit or no gain"

fi
if test $cop -gt $sop
then
s=`echo $cop - $sop |bc`
echo " u obtained loss of rs:$s"
else
s=`echo $sop - $cop |bc`
echo "u obtained profit of rs:$s"
fi

Output:
[root@localhost shell_script]# bash profitloss.sh
input nthe cost price of an item
100
input the selling price of the item
120
u obtained profit of rs:20