Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3238996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:55:06+00:00 2026-05-17T17:55:06+00:00

I have written a shell script with a switch case and takes in options

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:55:06+00:00Added an answer on May 17, 2026 at 5:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.