I want to use wget with variable

Viewed 48

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?

2 Answers

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

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
Related