Home > OS >  How to convert piped/awk output to string/variable
How to convert piped/awk output to string/variable

Time:10-18

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.

CodePudding user response:

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.

  • Related