In: Computer Science
Using Bash script,
1. Print the multiplication table upto 10 rows. Ask the user to enter a number. say user enters 10, your output should be :
[srivatss@athena shell]> ./mult.sh
I will be printing the multiplication table
Please enter a number
10
1 x 10 = 10
2 x 10 = 20
3 x 10 = 30
4 x 10 = 40
5 x 10 = 50
6 x 10 = 60
7 x 10 = 70
8 x 10 = 80
9 x 10 = 90
10 x 10 = 100
[ 5 points ]
2. Create a Simple Calculator [ 20 points , 5 points for each operator]
Ask the user for the first operand, second operand and the operator. You output the result to the user based on the operator. Here is the sample output
[srivatss@athena shell]> ./calc.sh
Welcome to my Simple calculator
Please enter the first operand
2
Please enter the second operand
3
Please enter the operator
+
2 + 3 = 5
[srivatss@athena shell]> ./calc.sh
Welcome to my calculator
Please enter the first operand
4
Please enter the second operand
2
Please enter the operator
-
4 - 2 = 2
[srivatss@athena shell]> ./calc.sh
Welcome to my calculator
Please enter the first operand
4
Please enter the second operand
5
Please enter the operator
*
4 * 5 = 20
[srivatss@athena shell]> ./calc.sh
Welcome to my calculator
Please enter the first operand
6
Please enter the second operand
3
Please enter the operator
/
6 / 3 = 2
3. Using RANDOM function and touch command , create a file with prefix =BatchJob followed by the random number.
For instance, I ran my script, it created a file with random number 10598.
[srivatss@athena shell]> ./cmds.sh
BatchJob10598
[ 5 points ]
If you have any doubts, please give me comment...
mult.sh
echo "I will be printing the multiplication table"
echo "Please enter a number"
read n
i=1
while [ $i -le $n ]
do
res=`expr $i \* $n`
echo "$i x $n = $res"
i=`expr $i + 1`
done
2)
echo "Welcome to my calculator"
echo "Please enter the first operand"
read n1
echo "Please enter the second operand"
read n2
echo "Please enter the operator"
read op
case "$op" in
'+')
result=`expr $n1 + $n2`
echo "$n1 + $n2 = $result"
;;
'-')
result=`expr $n1 - $n2`
echo "$n1 - $n2 = $result"
;;
'*')
result=`expr $n1 \* $n2`
echo "$n1 * $n2 = $result"
;;
'/')
result=`expr $n1 / $n2`
echo "$n1 / $n2 = $result"
;;
*)
echo "Invalid operator"
esac
3)
#!/bin/bash
RANDOM=$$
echo "BatchJob$RANDOM"