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


Write a shell script to find a year is leap year or not.

Write a shell script to find a year is leap year or not.


echo "Enter Year:"
read y

year=$y

y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
          echo "$year is Leap Year!"
else
          echo "$year is not a Leap Year!"
fi

Output:

Enter Year:
2019
2019 is not a Leap Year!

Write shell script to find out biggest number from given three numbers.


Write shell script to find out biggest number from given three numbers. 

echo "enter first number"
read first
echo "enter second number"
read sec
echo "enter third number"
read third
if [ $first -gt $sec ] ; then
if [ $first -gt $third ] ; then
echo -e " $first is greatest number "
else
echo -e " $third is greatest number "
fi
else
if [ $sec -gt $third ] ; then
echo -e " $sec is greatest number "
else
echo -e " $third is greatest number "
fi
fi

Output
# bash bignum.sh
enter first number
23
enter second number
34
enter third number
12
34 is greatest number

Monday, 1 April 2019

Write a shell script to search an element in an array.

  Write a shell script to search an element in an array.
Script:-
echo "enter total no. of values"
read n;
flag=0
echo "enter array:"
for((i=0;i<n;i++))
do
read arr[$i]
done
echo "enter array:"
echo "enter number you wanna search"
read n1
for((i=0;i<n;i++))
do
a=$((arr[$i]))
echo $a
if [ $a -eq $n1 ] ;then
flag=1
break
fi
done
if [ $flag -eq 0 ] ;then
echo "number not found"
else
echo "number is found at $(($i+1)) location"

fi

Output:
enter total no. of values
2
enter array:
23
43
enter number you wanna search
23
23
number is found at 1 location