Erik Vesterlund
Erik Vesterlund

Reputation: 448

curl command works manually but not in script

I have a curl command in a script, when the script is run the command isn't able to fetch a resource (the command itself works, it's the response that's incorrect), but if I copy and paste the same command into the terminal I get the expected response.

After reading this my script looks like this:

jsess=`awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt`
ARGS=( -k -v -b $jsess $url7)
echo "curl ${ARGS[*]}"
curl "${ARGS[@]}"

and the last echo looks like this:

curl -k -v -b 'JSESSIONID=hexystuff' https://secretstuff.com

The last curl doesn't work, but copy-pasting that echo works. Any ideas what could be wrong? Thanks.

Upvotes: 1

Views: 1655

Answers (2)

petrus4
petrus4

Reputation: 614

args="-k -v -b"
jsess=$(awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt)
url7="https://secretstuff.com"

curl "${args}" "${jsess}" "${url7}"

The use of arrays is not my personal preference, and I believe the current situation demonstrates why. I believe that as much as possible, every individual item of data should be contained in its' own variable. This makes accessing said variables much simpler, and also greatly increases flexibility. I can choose exactly which pieces of information will go into a given command line.

Upvotes: 0

Philippe
Philippe

Reputation: 26727

The problem seems in the two single quotes, try this :

jsess="$(awk '/\sJSESSION/ { print $6"="$7 }' cookies.txt)"
ARGS=( -k -v -b "$jsess" "$url7")
echo "curl ${ARGS[*]}"
curl "${ARGS[@]}"

Upvotes: 1

Related Questions