2016-12-06 57 views
1

我要創造,我可以根據用戶執行一些功能簡單的女巫情況下提示:錯誤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 

但是,當我執行該腳本,我得到錯誤:

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$ 

陶氏你知道我該如何解決這個問題?

+0

你需要打破case語句用'';;感謝 – 123

回答

2

多個問題。

  1. 在每個case的末尾添加;;構造
  2. exit命令在switch-case構建體內放錯地方,而不;;本。它應該在case或更高的末尾。
  3. read有自己的選項來打印用戶提示信息,可以避免不必要的echo

無錯腳本

#!/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 
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 
+0

。我測試了代碼,但函數沒有被調用。 –

+0

爲bash不知道donwload_source_code。您可能需要定義它。 –