I want to use curl command to get strings at cloud and parse them into a dictionary.
my shell code :
#!/bin/bash
#
URL=https://raw.githubusercontent.com/Nova-He/python/master/base_images
declare -A dic
for x in $(curl -s $URL);do
dic+=([$(echo $x |cut -d/ -f1)]="$(echo $x |cut -d/ -f2)")
done
# print all key
echo ${!dic[*]}
# print all value
echo ${dic[*]}
using ./
run :
➜ ./get_ip_dic.sh
./get_ip_dic.sh: line 6: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
./get_ip_dic.sh: line 11: 10.114.12.26: syntax error: invalid arithmetic operator (error token is ".114.12.26")
but, using bash
run :
➜ bash get_ip_dic.sh
10.134.34.228 10.134.34.227 10.114.12.27 10.114.12.26 10.129.35.188
b5be4856d837 2b8b028e6eeb b5be4856d837 b5be4856d837 2b8b028e6eeb
After searching online, I know that both methods are run in the subshell,there isn't different. So,I don't know what happened,thanks in advance.