提问人:Welo 提问时间:8/31/2023 最后编辑:Welo 更新时间:8/31/2023 访问量:139
卷曲一个包含多个命令的 bash 变量?[复制]
Curling a bash variable containing multiple commands inside? [duplicate]
问:
这个问题在这里已经有答案了:
Bash:执行变量的内容,包括管道 [重复] (2 个答案)
如何从变量运行脚本命令? (3 个答案)
运行存储在变量中的命令(包括管道和重定向) (3 个答案)
3个月前关闭。
我正在bash中使用从YAML文件值填充的变量运行for循环。变量本身不仅包含要 curl 的 url,还包含 |jq 命令来解析数据、排序和获取特定二进制文件的最新版本。
echo $version
https://api.github.com/repos/hashicorp/vault/tags | jq -r '.[].name' | sort -Vr | head -n 1 | cut -c 2-)
当我手动卷曲整个字符串时,它可以工作,并给我版本号。
curl -s https://api.github.com/repos/hashicorp/vault/tags | jq -r '.[].name' | sort -Vr | head -n 1 | cut -c 2-
1.14.2
但是,当使用变量将其传递给 curl 时:
curl $version
Warning: Invalid character is found in given range. A specified range MUST
Warning: have only digits in 'start'-'stop'. The server's response to this
Warning: request is uncertain.
curl 8.2.1 (x86_64-alpine-linux-musl) libcurl/8.2.1 OpenSSL/3.0.10 zlib/1.2.13 brotli/1.0.9 nghttp2/1.51.0
Release-Date: 2023-07-26
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp ws wss
Features: alt-svc AsynchDNS brotli HSTS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL threadsafe TLS-SRP UnixSockets
到目前为止,我尝试在地址周围使用双引号,使用 \ 作为退出字符,并在开头和结尾使用另一组双引号。这无济于事。我也试过使用$version |$parse变量来拆分 curl 部分和解析部分,但结果curl: (23) Failure writing output to destination.
编辑:使用“$version”不起作用,导致 .我也尝试在变量内的 URL 周围使用双引号。curl: (3) URL rejected: Malformed input to a URL function
答: 暂无答案
评论
eval
parseVersionFromCmd() { "$@" https://api.github.com/repos/hashicorp/vault/tags | jq -r '.[].name' | sort -Vr | head -n 1 | cut -c 2-; }
,那么运行将是做你想做的事的一种方式——看看在这种情况下我们如何使用函数而不是字符串parseVersionFromCmd curl
curl -s https://api.github.com/repos/hashicorp/vault/tags | jq -r '.[].name' | sort -Vr | head -n 1 | cut -c 2-
["curl", "-s", "https://api.github.com/repos/hashicorp/vault/tags"]
jq
sort