1

I want to create simple witch case in which I can execute functions based on user prompts:

echo Would you like us to perform the option: "(Y|N)"

read inPut

case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo 'Starting.....'
donwload_source_code


# depending on the scenario, execute the other option
# or leave as default
"N") echo 'Stopping execution'
exit
esac

But when I execute the script I get error:

Would you like us to perform the option: (Y|N)
n
run.sh: line 27: syntax error near unexpected token `)'
run.sh: line 27: `"N") echo 'Stopping execution''
EMP-SOF-LT099:Genesis Plamen$ 

Dow you know how I can fix this issue?

1
  • You need to break case statements with ;; Commented Dec 6, 2016 at 8:38

2 Answers 2

2

Multiple issues.

  1. Add a ;; at end of each case construct
  2. The exit command is misplaced within the switch-case construct without the ;; present . It should be at the end of case or above.
  3. read has a own option to print the message for user prompt, can avoid a unnecessary echo.

Error-free script

#!/bin/bash

read -p "Would you like us to perform the option: \"(Y|N)\" " inPut

case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo 'Starting.....'
donwload_source_code
;;


# depending on the scenario, execute the other option
# or leave as default
"N") echo 'Stopping execution'
exit
;;

esac
Sign up to request clarification or add additional context in comments.

Comments

2

add ;;

#!/bin/bash
echo Would you like us to perform the option: "(Y|N)"

read inPut

case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo 'Starting.....'
donwload_source_code


# depending on the scenario, execute the other option
# or leave as default
;;
"N") echo 'Stopping execution'
exit
;;
esac

2 Comments

Thanks. I tested the code but the function is not called.
for the bash doesnot know the donwload_source_code. You may have to define it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.