PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] How to convert piped/awk output to string/variable

 November 19, 2022     bash, scripting, shell     No comments   

Issue

I'm trying to create a bash function that automatically updates a cli tool. So far I've managed to get this:

update_cli_tool () {
    # the following will automatically be redirected to .../releases/tag/vX.X.X
    # there I get the location from the header, and remove it to get the full url
    latest_release_url=$(curl -i https://github.com/.../releases/latest | grep location: | awk -F 'location: ' '{print $2}')
    # to get the version, I get the 8th element from the url .../releases/tag/vX.X.X
    latest_release_version=$(echo "$latest_release_url" | awk -F '/' '{print 8}')
    
    # this is where it breaks
    # the first part just replaces the "tag" with "download" in the url
    full_url="${latest_release_url/tag/download}/.../${latest_release_version}.zip"
    echo "$full_url"  # or curl $full_url, also fails
}

Expected output: https://github.com/.../download/vX.X.X/vX.X.X.zip

Actual output: -.zip-.../.../releases/download/vX.X.X

When I just echo "latest_release_url: $latest_release_url" (same for version), it prints it correctly, but not when I use the above mentioned flow. When I hardcode the ..._url and ..._version, the full_url works fine. So my guess is I have to somehow capture the output and convert it to a string? Or perhaps concatenate it another way?

Note: I've also used ..._url=`curl -i ...` (with backticks instead of $(...)), but this gave me the same results.


Solution

The curl output will use \r\n line endings. The stray carriage return in the url variable is tripping you up. Observe it with printf '%q\n' "$latest_release_url"

Try this:

latest_release_url=$(
    curl --silent -i https://github.com/.../releases/latest \
    | awk -v RS='\r\n' '$1 == "location:" {print $2}'
)

Then the rest of the script should look right.



Answered By - glenn jackman
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing