2017-04-25 6 views
1

"어떤 형식을 사용 하시겠습니까?"라는 질문을하고 대답이 xml 또는 json인지 여부에 따라 적절한 출력을 인쇄하십시오. 그러나 xml 또는 json이 아니면 코드가 완료됩니다.IF ELSE 문

코드 :

[[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?> 

을 사용자가 입력 할 수

  • 해당 번호
  • 도움을 그만두 게에 대한
  • q에 대한 XML
  • h에 대한
  • 또는 j, js, json JSON과 x, xml ...
  • 잘못된 (잘못 입력) 답변이 처리하고 다시 요청하고 있습니다 ..
+0

그건 내가 쓰려고하는 코드를 표현하는 정말 좋은 방법이고, 확실히 유용 할 것입니다.고마워요 – Aaron

+0

@ 애런 yvw - enjoy :) – jm666

0

올바른 구문은

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

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 

는 내가 그것을 실행하려고하면이 오류가 조건은 ; 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

옵션 및 인수는 단일 문자열이 아닌 배열에 저장해야합니다. 또한 case 문은 여기에서 더 간단합니다.

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