I have written a shell script with a switch case and takes in options 1-5 and performs operations on the variables accordingly.
My code works fine though theres a small change I would like to make in my code.
When the code Enters the default case argument,after that it still prompts for "Enter two numbers: " and then prints "Invalid Option "which is inappropriate.Ideally the code should stop after invalid option.
I've just learnt shell scripting so I'm not that good at it.
I have copy pasted my source-code below:
#! /bin/bash
echo "1. Add
2.Substract
3.Divide
4.Multiply
5.Quit"
read -p "Enter an option:" option
read -p "Enter two numbes: " first second
case $option in
1)income=$[ first + second ];;
2)income=$[ first - second ];;
3)income=$[ first / second ];;
4)income=$[ first * second ];;
*)echo "Invalid Option";
exit 1;;
esac
echo "Income=$income"
exit 0
I tried using if,else ...but that doesn't serve the purpose. *Alternative approach:*
#! /bin/bash
echo "1. Add
2.Substract
3.Divide
4.Multiply
5.Quit"
read -p "Enter an option:" option
if [ option > 5 ]
then
echo "Invalid Option"
exit 1
else
read -p "Enter two numbes: " first second
fi
case $option in
1)income=$[ first + second ];;
2)income=$[ first - second ];;
3)income=$[ first / second ];;
4)income=$[ first * second ];;
*)echo "Invalid Option";
exit 1;;
esac
echo "Income=$income"
exit 0