2017-04-25 251 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