2017-04-25 142 views
1

我要問這個問題:「你想用什麼格式?」,那麼,這取決於答案是否xmljson,打印適當的輸出。但是,如果它不是xmljson,則代碼結束。的if else語句

代碼:

#!/bin/bash 
read -r -e -p "What format do you want to use? json/xml?: " format 

if [[ "${format,,}" == "json" ]]; then 
    curl=$curl"\"\Content-Type: application/json\"\ 
else [[ "${format,,}" == "xml" ]]; then 
    curl=$curl"\"\Content-Type: application/xml\"\ 
elif [[ "${format,,}" == "no" ]]; then 
    echo "Goodbye, you must specify a format to use Rest" 
else 
    echo "Unknown" 
    exit 0 
fi 
echo $curl 

我收到此錯誤,當我嘗試運行它:

[[email protected] groups]# ./test3 
What format do you want to use? json/xml?: xml 
./test3: line 8: syntax error near unexpected token `then' 
./test3: line 8: `elif [[ "${format,,}" == "no" ]]; then' 

回答

2

我會用時間長一點,但更人性化的變種:

err() { echo "[email protected]" >&2; return 1; } 

show_help() { 
cat - >&2 <<EOF 
You must specify a format to use Rest because bla bla... 
EOF 
} 

select_format() { 
    PS3="Select the format do you want to use?> " 
    select ans in json xml "quit program"; do 
     case "$REPLY" in 
      [0-9]*) answer=$ans;; 
      *) answer=$REPLY;; 
     esac 
     case "${answer,,}" in 
      '') ;; 
      j|js|json) echo '-H "Content-Type: application/json"' ; return 0 ;; 
      x|xml)  echo '-H "Content-Type: application/xml"' ; return 0 ;; 
      q|quit*) err "Bye..." ; return 1;; 
      h|help) show_help ;; 
      *) err "Unknown format $answer" ;; 
     esac 
    done 
    [[ "$answer" ]] || return 1 #handles EOF (ctrl-D) 
} 

curl_args=() 
curl_args+=($(select_format)) || exit 1 
echo curl "${curl_args[@]}" 

它顯示一個菜單:

1) json 
2) xml 
3) quit program 
Select the format do you want to use?> 

,並且用戶可以輸入:

  • 任一相應的數字
  • jjsjson對JSON和xxml的XML
  • h的幫助
  • q的退出...
  • 不正確的(錯誤輸入)的答案被處理,並再次要求.. 。
+0

這是一種很好的方式來表示我正在編寫的代碼,它一定會派上用場。謝謝 – Aaron

+0

@Aaron yvw - 享受:) – jm666

0

正確的語法是

if ... 
elif ... 
else ... 
fi 

else必須遵循的命令,而不是一個條件和; then

+0

嗨,感謝您的回覆,我遵循這種格式,但仍然沒有運氣的代碼。我編輯了我的問題,以便爲您提供更新。有什麼想法嗎? – Aaron

1
#!/bin/bash 
read -r -e -p "What format do you want to use? json/xml?: " format 

if [[ "${format,,}" == "json" ]]; then 
    curl=$curl"\"Content-Type: application/json\"" 
elif [[ "${format,,}" == "xml" ]]; then 
    curl=$curl"\"Content-Type: application/xml\"" 
elif [[ "${format,,}" == "no" ]]; then 
    echo "Goodbye, you must specify a format to use Rest" 
else 
    echo "Unknown" 
    exit 0 
fi 
echo $curl 

應該工作。如果沒有,則問題不在於if/else語句。

編輯:發現問題。這是不正確的使用引號。我試圖解決它,所以上面應該現在工作。

+0

我已經在我的問題中包含錯誤消息,如果這將有助於更多 – Aaron

+0

請參閱編輯。我認爲它應該工作 – Zuzzuc

+0

歡樂 - 它的工作。非常感謝您的耐心。 – Aaron

1

選項和參數應存儲在數組中,而不是一個單一的字符串。此外,這裏的陳述會更簡單。

#!/bin/bash 

curl_opts=() 
read -r -e -p "What format do you want to use? json/xml?: " format 

shopt -s nocasematch 
case $format in 
    json) curl_opts+=(-H "Content-Type: application/json") ;; 
    xml) curl_opts+=(-H "Content-Type: application/xml") ;; 
    no) printf 'Goodbye, you must specify a format to use Rest\n' >&2 
     exit 1 
     ;; 
    *) printf 'Unknown format %s, exiting\n' "$format" >&2 
     exit 1 
     ;; 
esac 

curl "${curl_opts[@]}" ... 
+0

這很有趣,我會研究,因爲我正在用多個IF語句構建一個腳本,這意味着這將更容易。 雖然這段代碼拋出一個錯誤 - 請參閱編輯 – Aaron

+0

我忘了一些';;'來終止每個單獨的案例;請閱讀'bash'手冊頁中的'case'語句以獲取更多信息。 – chepner