如何将一个字符串变量嵌套在另一个作为命令保存输出的变量中?
我想完成这段代码的作用:
xmrprice=$(curl -s 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true' |jq -r '.monero'|jq -r '.usd')
echo "The price of Monero is $xmrprice"
btcprice=$(curl -s 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true' |jq -r '.bitcoin'|jq -r '.usd')
echo "The price of BTC is $btcprice"
ltcprice=$(curl -s 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true' |jq -r '.litecoin'|jq -r '.usd')
echo "The price of Litecoin is $ltcprice"
但没有打电话curl
三遍。删除该-s
标志将表明curl 确实被调用了三次。我想从输出中分配字符串
curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true
到一个变量,然后从中提取xmrprice
、brcprice
和的值ltcprice
。就像这个 Python 代码一样:
#!/usr/bin/python3
#pip3 install requests
import requests
import json
result = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true", headers = {"accept":"application/json"})
print("Monero's and Bitcoin's prices as of now:")
print(str(result.json()["monero"]["usd"]))
print(str(result.json()["bitcoin"]["usd"]))
我有遇到这个关于嵌套变量的问题在 Bash 中,但它更多地涉及将多个命令的嵌套输出分配给变量。
我已经尝试过这一切:
response=$(curl -s https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true)
#xmrprice=(${response}|jq -r '.monero'|jq -r '.usd') #syntax error near unexpected token `|'
#xmrprice=("${response}"|jq -r '.monero'|jq -r '.usd') #syntax error near unexpected token `|'
#xmrprice=$("${response}"|jq -r '.monero'|jq -r '.usd') #{"error":"Missing parameter vs_currencies"}: command not found
#xmrprice=$("{response}"|jq -r '.monero'|jq -r '.usd') #: {response}: command not found
echo "The price of Monero is ${xmrprice}".
这些都不起作用。这些评论描述了我从 Bash 中得到的错误。
答案1
我可能会这样做:
#!/bin/sh
unset -v xmrprice btcprice ltcprice
url='https://api.coingecko.com/endpoint/path'
eval "$(
curl -s "$url" |
jq -r '
@sh "xmrprice=\(.monero.usd|tonumber)",
@sh "btcprice=\(.bitcoin.usd|tonumber)",
@sh "ltcprice=\(.litecoin.usd|tonumber)"'
)"
printf '%s is %s\n' \
'xmrprice' "$xmrprice" \
'btcprice' "$btcprice" \
'ltcprice' "$ltcprice"
这调用curl
一次并将生成的文档传递给单身的 jq
调用而无需将其存储在文件或变量中。该jq
表达式创建三个字符串。每个字符串都是一个变量赋值。运算@sh
符 injq
确保为 shell 正确引用字符串。
评估eval
变量分配,创建三个 shell 变量。
请注意,代码中使用的引号,无论是单引号还是双引号,都不是偶然的或不需要的(除了末尾参数中的纯字符串周围的单引号之外printf
,它们只是为了好看)。
如果您实际上对创建 shell 变量不感兴趣,则无需这样做,因为jq
可用于执行格式化输出:
#!/bin/sh
url='https://api.coingecko.com/endpoint/path'
curl -s "$url" |
jq -r '
"xmrprice is at \(.monero.usd|tonumber) USD",
"btcprice is at \(.bitcoin.usd|tonumber) USD",
"ltcprice is at \(.litecoin.usd|tonumber) USD"'
答案2
您已经接近您要寻找的东西了。你只是缺少一个echo
.
#!/usr/bin/env bash
response=$(curl -s 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cbitcoin-cash%2Clitecoin%2Cmonero&vs_currencies=usd&include_market_cap=true')
xmrprice=$(echo "${response}"|jq -r '.monero'|jq -r '.usd')
echo "${xmrprice}"
编辑:正如 Kusalananda 所指出的,您还缺少curl 命令中 URL 周围的引号。