1

I want to ask the question "What format do you want to use?", then, depending on whether the answer is xml or json, print the appropriate output. However, if it is not xml or json, then the code finishes.

Code:

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

I receive this error when I try to run it:

[root@osprey 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'
0

4 Answers 4

1

I would use a bit longer, but much more user-friendly variant:

err() { echo "$@" >&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[@]}"

It shows a menu:

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

and the user could enter:

  • either the corresponding number
  • or j, js, json for json and x, xml for the xml
  • h for the help
  • and q for the quit...
  • incorrect (mistyped) answers are handled and asking again...
Sign up to request clarification or add additional context in comments.

2 Comments

That's a really good way to represent the code i'm trying to write and it will definitely come in handy. Thank you
@Aaron yvw - enjoy :)
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

Should work. If it does not, the problem is not with the if/else statements.

Edit: Found the problem. It was incorrect use of quotation marks. I tried to fix it, so the above should work now.

2 Comments

I've included the error message in my question if that would help even more
No worries. For future troubles, see shellcheck before you post here.
1

Options and arguments should be stored in an array, not a single string. Further, a case statement would simpler here.

#!/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[@]}" ...

2 Comments

That's interesting, I will research that because I'm building a script with multiple IF statements meaning this will be easier. Although this code threw an error - see edit
I forgot a handful of ;; to terminate each separate case; read about the case statement in the bash man page for more information.
0

The correct syntax is

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

else must be followed by commands, not a condition and ; then.

1 Comment

Hi, thanks for the reply, I've followed that format but still no luck with the code. I've edited my question to give you an update. Any thoughts?