2016-12-06 3 views
1

ich einfach Hexe Fall erstellt werden soll, in dem ich Funktionen basierend auf Benutzer ausführen kann aufgefordert werden:Fehler in Bash-Skript Schaltergehäuse

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 

Aber wenn ich das Skript ausführen, bekomme ich Fehler:

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 wissen Sie, wie ich dieses Problem beheben kann?

+0

Sie müssen case-Anweisungen brechen mit '' ;; – 123

Antwort

2

Mehrere Probleme.

  1. eine ;; am Ende jeder case hinzufügen innerhalb des switch-case Konstrukt ohne die vorliegende ;;
  2. exit Der Befehl wird konstruieren verlegt. Es sollte am Ende case oder höher sein.
  3. read hat eine eigene Option zum Drucken der Nachricht für die Benutzereingabeaufforderung, kann eine unnötige echo vermeiden.

fehlerfreie Skript

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

hinzufügen ;;

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

Danke. Ich habe den Code getestet, aber die Funktion wird nicht aufgerufen. –

+0

für die Bash kennt den donwload_source_code nicht. Sie müssen es möglicherweise definieren. –