views:

87

answers:

3

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
+1  A: 

You should look at the select builtin, it will help reduce the complexity of what you want by quite a lot. You can replace the echo/case with it.

In this case I think your problem is that you have your cases as 1.) rather than 1). Your user would need to type the number with the period for the former to match.

Additionally, you need to have your shebang line as #!/bin/bash, it will not work with the space you have there.

Daenyth
@Daenyth -- Thank you ok I'll try select,and edit the periods :) ... +1. The shebang line works fine with the space as well.
Pavitar
A: 

When you have a menu like this, you can use an infinity loop.

#! /bin/bash


while true
do
cat <<-EOF
  1. Add
  2.Substract
  3.Divide
  4.Multiply
  5.Quit
EOF
    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 ));;
    5) exit;;
    *) echo "Invalid Option, try again";
    esac

    echo "Income=$income"
done
ghostdog74
@ghostdog74 -- Is it that,the usage of $[..] should be avoided? or is it wrong? I'm asking this because I have used $[..] and it works fine.please explain.
Pavitar
no, its not wrong. just that i am used to C style that i forgot we can use square brackets as well.
ghostdog74
@ ghostdog74 -- ok thank you.. :)
Pavitar
This answer still doesn't solve my problem.After I hit quit it shouldn't ask for the two number again,instead just print an Error message and break out of the program..
Pavitar
then put `read -p "Enter two numbes: " first second` after the `case/esac`
ghostdog74
A: 

You don't need to use if else. Just take the input in the switch case.

#!/bin/bash

echo "
      1.Add
      2.Subtract
      3.Divide
      4.Multiply
      5.Quit"

read -p "Enter an option:" option

case $option in

1)
    read -p "Enter two numbers " first second
    income=$[ first + second ];;

2)
    read -p "Enter two numbers " first second
    income=$[ first - second ];;


3)
    read -p "Enter two numbers " first second
    income=$[ first / second ];;

4)
    read -p "Enter two functions " first second
    income=$[ first * second ];;

*)echo "Invalid Option";
    exit 1;;

esac

echo "Income = $income"

exit 0

Or you can use function to avoid repetition of read statement.

#!/bin/bash

function GetInput()
{
    read -p "Enter two numbers : " first second
}



echo "
      1.Add
      2.Substract
      3.Divide
      4.Multiply
      5.Quit"

read -p "Enter an option:" option

case $option in

1)
    GetInput
    income=$[ first + second ];;

2)
    GetInput
    income=$[ first - second ];;


3)
    GetInput
    income=$[ first / second ];;

4)
    GetInput
    income=$[ first * second ];;

*)echo "Invalid Option";
    exit 1;;

esac

echo "Income = $income"

exit 0
Searock