0

I'm using a script to get the latest version of Prometheus. I'm able to do that:

v=$(curl -v --silent https://github.com/prometheus/node_exporter/releases/latest 2>&1 | grep "tag" | cut -d "v" -f2)

echo "latest version is $v"

latest version is 1.3.1

but on the next step for download with 'wget' and use a variable:

wget https://github.com/prometheus/node_exporter/releases/download/v$v/node_exporter-$v.linux-amd64.tar.gz

it fails:

https://github.com/prometheus/node_exporter/releases/download/v1.3.1%0D/node_exporter-1.3.1%0D.linux-amd64.tar.gz

I've tried to use wget with '' or use {} for variable like {$v}, but I got same result. Can someone help me?

user11563595
  • 45
  • 1
  • 9
  • 1
    As the bash tag you used instructs - "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting here." – Ed Morton Sep 11 '22 at 16:21
  • An other case of getting a superfluous `\r` character in a `curl` request – Fravadona Sep 11 '22 at 16:49
  • The version output is using CRLF line endings, which cause all sorts of confusion. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Sep 11 '22 at 17:25

2 Answers2

1

Add | tr -d '\r' to your curl command to strip the carriage returns as follows:

#!/bin/bash

v=$(curl -v --silent https://github.com/prometheus/node_exporter/releases/latest 2>&1 | grep "tag" | cut -d "v" -f2 | tr -d '\r')
echo "latest version is $v"

wget https://github.com/prometheus/node_exporter/releases/download/v$v/node_exporter-$v.linux-amd64.tar.gz
m19v
  • 1,800
  • 4
  • 11
  • 26
1

I'm using a script to get the latest version of Prometheus.

Instead of trying to parse the HTML-source with unsuitable tools, why not use Github's API together with the JSON-parser to get the exact download-url you want:

$ xidel -s "https://api.github.com/repos/prometheus/node_exporter/releases/latest" \
  -e '$json/(assets)()[contains(name,"linux-amd64")]/browser_download_url'
https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
Reino
  • 3,203
  • 1
  • 13
  • 21