我正在处理一些清单物料,并且试图将所有AWS区域保存在一个阵列上,然后在另一个下方显示元素以将其用作输入菜单。
下一个命令给了我正确的输出,但是当我使用FOR进入数组时,数组长度仅为1,导致结果为:
aws ec2 describe-regions --output text|awk -F\t '{print $3}'| sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'
eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1 ap-southeast-1 ap-southeast-2 eu-central-1 us-east-1 us-east-2 us-west-1 us-west-2
这是我归档阵列的方式:
# Get regions
declare -a regions=$(aws ec2 describe-regions --output text | awk -F\t '{print $3}' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /')
echo -e "\nPlease, select the region you would like to query: "
# Print Regions
len=${#regions[@]}
last=$((len+1))
for (( i=0; i<$len; i++ )); do
echo -e "$i.${regions[$i]}\n" ;
done
echo -e "$last All of them (this could take a while...O_o)\n"
read region_opt
if [${region_opt}!=${last}] then
region=(${regions[$region_opt]})
我想要在输出中显示的内容类似
- eu-north-1
- ap-south-1
- eu-west-3 ....
答案 0 :(得分:0)
您在数组值周围缺少括号,例如
declare -a ARRAY=(value1 value2 ... valueN)
(参考:https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html,https://www.gnu.org/software/bash/manual/bash.html)
以下形式也适用,在GNU's Bash reference manual,Bash guide for beginners和Advanced bash-scripting guide中以第一个(不带declare -a
)为例:>
ARRAY=(value1 value2 ... valueN)
declare ARRAY=(value1 value2 ... valueN)
答案 1 :(得分:0)
$()
是命令替换,只是将任何标准输出转换为字符串并将其分配给变量
如果您说对了,结果是
eu-north-1 ap-south-1 eu-west-3...
然后从数组中取出数组,使其以句法形式显示,然后告诉Bash对其进行评估,
regions=($regions)
扩展后,它将是有效的数组语法
regions=(eu-north-1 ap-south-1 eu-west-3)
然后将其用""
括起来并作为Bash eval
参数
$ eval "regions=($regions)"
$ echo ${regions[0]}
eu-north-1
因此,我确信您将能够自己完成并解决它...