Lets say I have the following file named "test"
which contains:
[a1b2c3]=test1.xyz
[d4e5f6]=test2.xyz
[g7h8i9]=test3.xyz
...
Which is about 30 lines long. I would want to assign
a1b2c3
d4e5f6
g7h8i9
into an array named array how would I do it?
with bash 3.1 this would be my attempt
declare -a array
while read -r line;
do
array+=("$(echo "$line")")
done < test
But it seems that I need to upgrade my bash to 4 or 5 for declarative arrays. So the question would be, how do I separate specifically get the values inside the brackets per line to be assigned as different elements in an array array
This array values would be used in a curl script which should produce a file with the name of the values associated with it.
for i in "${array[@]}"
do
curl -X GET "https://api.cloudflare.com/client/v4/zones/$i/dns_records?" \
-H "X-Auth-Email: $X-Auth-Email" \
-H "X-Auth-Key: X-Auth-Key" \
-H "Content-Type: application/json" \
-o "${array[@]}.txt"
done
so the expectation is that curl -X GET "https://api.cloudflare.com/client/v4/zones/a1b2c3/dns_records? would be run and an output file named test1.xyz.txt would be produced containing the dns records of the said link. Which goes for the next line and test2.xyz.txt and so on and so forth.
bashyou can use associative arrays (declare -A array). And then, if you dare usingeval:eval "declare -A array=($(<test))". After which you can access the keys with${!array[@]}and the values with${array[@]}.${array[@]}but if I try testing getting an element with${array[3]}it comes out empty.Aindeclare -A array? Did you pay attention to the!in${!array[@]}?