[英]How to simultaneously access two arrays indexes for a curl request in Bash for loop?
In Bash i am trying to query country currencies from http://free.currencyconverterapi.com/api/v5/convert?q=USD_GBP and update my OpenEdge API automatically without Hard coding the symbols. 在Bash中,我尝试从http://free.currencyconverterapi.com/api/v5/convert?q=USD_GBP查询国家/地区货币,并自动更新我的OpenEdge API,而无需对符号进行硬编码。 meaning I have to curl and save currency symbols from my API in an array and curl and save the values from the currency converter API.
这意味着我必须将我的API中的货币符号卷曲并保存在数组中,并卷曲并保存货币转换器API中的值。 after that i want to run two for loops simultaneously or access the indexes of two arrays at the same time in order to run the API call that requires the
之后,我想同时运行两个for循环或同时访问两个数组的索引,以运行需要
url/rest/USD/12
kind of input. 一种输入。
In bash I am calling a curl that returns 在bash中,我称卷发返回
ZAR USD EUR BWP CHF GBP
from my API in one line. 从我的API一行。 Then i save that result in a variable called currency
然后我将结果保存在一个名为currency的变量中
jsonEnd="_ZAR.val"
symbolEnd="_ZAR"
I then run 然后我跑
values=()
for j in ${currency[*]};
do
${values[j]}=$(curl -X GET -H "Content-type: application/json" http://free.currencyconverterapi.com/api/v5/convert?q=${currency[j]}$symbolEnd&compact=y | jq '.${currency[j]}$jsonEnd')
done
To get the currency values into an array where '_ZAR.val' is a from a json result, tried to point at "val" with jq 要将货币值放入json结果中“ _ZAR.val”为a的数组中,请尝试使用jq指向“ val”
{
"USD_ZAR": {
"val": 14.23065
} }
at last i am trying to run a POST Curl which needs the relative currency symbol found above like USD with a value to update. 最后,我试图运行一个POST Curl,它需要上面发现的相对货币符号(例如USD)和一个要更新的值。 in this form
以这种形式
curl -X PUT -H "Content-Type: application/json" -H "Authorization: $context" http://192.168.xxx.xxx:8080/rest/exchangerates/USD/12
i tried this 我尝试了这个
for i in ${values[@]}
do
curl -X PUT -H "Content-Type: application/json" -H "Authorization: $context" http://192.168.xxx.xxx:8080/rest/exchangerates/${currency[i]}/${values[i]}
done
i can't get it right, the errors include 我做对了,错误包括
curl: (6) Could not resolve host: USD_ZAR
etc I am new to bash 等我刚接触bash
Both ${currency[*]}
and ${currency[*]}
will expand to the values inside the array. ${currency[*]}
和${currency[*]}
都将扩展为数组内的值 。 So when you write ... 所以当你写...
for j in "${currency[@]}"; do
echo "${currency[j]}"
done
... you do not access something like ${currency[0]}
but ${currency[USD]}
which does not exist. ...您不会访问诸如
${currency[0]}
类的东西,但不会访问不存在的${currency[USD]}
。
Also, to assign variables you cannot write ${array[i]}=value
but have to use array[$i]=value
. 另外,要分配变量,您不能编写
${array[i]}=value
而必须使用array[$i]=value
。
You may also want to switch to an associative array (also known as dictionary or map ) where you can use strings as indices. 您可能还希望切换到可以使用字符串作为索引的关联数组(也称为字典或map )。 Here is a rough skeleton:
这是一个粗略的骨架:
currencies=(ZAR USD EUR BWP CHF GBP)
declare -A values
for c in "${currencies[@]}"; do
values["$c"]="$(curl ..."$c"... | jq ..."$c"... )"
done
c=EUR
echo "The value of $c is ${values[$c]}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.