1

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.

4
  • Is the usage of ARGS[*] vs ARGS[@] intentional? Commented Jun 21, 2020 at 19:46
  • Useless use of an array; a string is sufficient and not error prone and the backquote is used in the old-style command substitution, e.g. <pre>foo=command</pre> The foo=$(command) syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. Check mywiki.wooledge.org/BashFAQ/082 Commented Jun 21, 2020 at 19:47
  • 1
    Better use set -x to debug your script instead of echo. Commented Jun 21, 2020 at 19:49
  • @eyevan Neat, thanks. Seems like the cmd got some extra apostrophes somehow. Turned out none were needed anyway, great. Commented Jun 21, 2020 at 20:04

2 Answers 2

1

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

Comments

0
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.

3 Comments

While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation.
One reason not to post "code only" answers is because they fall into a category that can be flagged as VLQ and are liable to be put on a review queue for deletion, please consider this answer that explains it better. The other answer in this thread is mostly exempt of that, because it does include a brief explanation.
I have edited this and offered an explanation; that I greatly prefer seperate variables to arrays, for reasons of greater clarity and flexibility.

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.