0

Depending on certain conditions I want to use JWT else I want to provide path to certs. Thus in my shell script this is the code:

if /* some condition */
  authorization='-H "'Authorization': 'Bearer' ${JWT}"'
else
  authorization="--cert "${ADMIN_CERT_PATH}" --key "${ADMIN_KEY_PATH}""

Now the curl request should be: curl -H "Authorization: Bearer 348129" for if condition curl --cert /Users/.../admin_cert --key /Users/../admin_key .. for else path

In order to get that output I need to use the following format in my shell script for if condition

response_code="$(curl -s -o /dev/null -w "%{http_code}" "$authorization" "$status_url")"

and following format for else code:

response_code="$(curl -s -o /dev/null -w "%{http_code}" $authorization "$status_url")"

Note: I need $authorization variable quoted in first case and unquoted in the else case. I do not want to write 2 different curl commands instead reuse the authorization variable. Thus, i need to modify the way I have declared my authorization variable such that I can write any one of the curl commands only once which works for both if and else cases.

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • 3
    Quotes don't nest, and even if they did storing them *in* variables doesn't work (they're treated as data rather than shell syntax). Use an array instead; see [Why does shell ignore quotes in arguments passed to it through variables?](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables) P.s. do *not* use `eval`. – Gordon Davisson Dec 11 '18 at 05:00

2 Answers2

2

curl supports a way to pass command line parameters in a file that I have used before when I have complex parameters. The idea is to place the complex command-line parameters into a simple text file and instruct curl to read parameters from it using --config parameter.

In this case the shell script would look something like the following.

#!/bin/sh

## "safely" create a temporary configuration file
curlctl=$(mktemp -q -t $(basename "$0"))
if test $? -ne 0
then
    echo "$0: failed to create temporary file, exiting."
    exit 75  # EX_TEMPFAIL
fi
trap 'rm "$curlctl"' 0

## write parameters used in all cases
cat>>"$curlctl" <<EOF
output = /dev/null
silent
write-out = %{http_code}
EOF

## append conditional parameters
if test "$some" = 'condition'
then
    printf 'header = "Authorization: Bearer %s"\n' "$JWT" >> "$curlctl"
else
    echo "cert = $ADMIN_CERT_PATH" >> "$curlctl"
    echo "key = $ADMIN_KEY_PATH" >> "$curlctl"
fi

# uncomment to see what the config file looks like
# cat "$curlctl" | sed 's/^/curl config:   /'

response_code=$(curl --config "$curlctl" http://httpbin.org/get)
echo "response code: $response_code"

The first few lines set up a temporary file that is deleted when the shell script exits. If you are already using trap then your cleanup will probably be more complex.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • Good answer; I would just avoid using `cat` when `printf 'header = "Authorization: Bearer %s"\n' "$JWT" >> "$curlctl"` would work just as well. – chepner Dec 11 '18 at 12:44
  • @chepner - I usually fall back to using heredocs to avoid all of the trapfalls and headaches associated with shell quoting but `printf` would work as well. Then again, the requirement of using hard tabs for `<<-EOF` is almost as onerous. – D.Shawley Dec 11 '18 at 12:49
2

When you are using a shell that supports arrays, you can avoid the need for a temporary configuration file.

curl_opts=(-s -o /dev/null -w "%{http_code}")
if /* some condition */
  curl_opts+=(-H "Authorization: Bearer $JWT")
else
  curl_opts+=(--cert "$ADMIN_CERT_PATH" --key "$ADMIN_KEY_PATH")
fi

...

response_code="$(curl "${curl_opts[@]}" "$status_url")"
chepner
  • 497,756
  • 71
  • 530
  • 681