0

I have a bash script as below:

curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}'

which is working fine, when i run run it, i able to get the cdn url as:

https://cdn.some-domain.com/some-result/

when i put it as variable :

myvariable=$(curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}')

and i echo it like this:

echo "CDN URL:  '$myvariable'"

i get blank result. CDN URL:

any idea what could be wrong? thanks

1
  • 1
    A common problem is that some HTTP APIs output DOS line endings, though I don't see how you could get exactly the output you claim because of that. (But then your diagnostics don't seem particularly exact.) Commented Mar 23, 2021 at 7:23

2 Answers 2

2

If your curl command produces a trailing DOS carriage return, that will botch the output, though not exactly like you describe. Still, maybe try this.

myvariable=$(curl -s "$url" | awk -F[\",] '/https:\/\/cdn/{ sub(/\r/, ""); url=$2} END { print url }')

Notice also how I refactored the grep and the tail (and now also tr -d '\r') into the Awk command. Tangentially, see useless use of grep.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right, the ^M character cause it become invalid url for CURL, thanks.
0

The result could be blank if there's only one item after awk's split. You might try grep -o to only return the matched string:

myvariable=$(curl -s "$url" | grep -oP 'https://cdn.*?[",].*' | tail -n 1 | awk -F[\",] '{print $2}')
echo "$myvariable"

1 Comment

Not my downvote, but your regex seems frightfully wrong for what you are suggesting. Also, switching from proper modern $(...) command-substitution syntax to obsolescent backticks seems like a move in the wrong direction. Finally, you should quote your shell variable.

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.