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

Teddybugs
  • 1,232
  • 1
  • 12
  • 37
  • 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.) – tripleee Mar 23 '21 at 07:23

2 Answers2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
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"
  • 3
    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](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). – tripleee Mar 23 '21 at 07:25