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.

1

2 Answers 2

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")"
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Good answer; I would just avoid using cat when printf 'header = "Authorization: Bearer %s"\n' "$JWT" >> "$curlctl" would work just as well.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.