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. 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. 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
url/rest/USD/12
kind of input.
In bash I am calling a curl that returns
ZAR USD EUR BWP CHF GBP
from my API in one line. Then i save that result in a variable called 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
{
"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. 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
Both ${currency[*]}
and ${currency[*]}
will expand to the values inside the array. 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.
Also, to assign variables you cannot write ${array[i]}=value
but have to use 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. 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]}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.