【发布时间】:2019-04-14 19:08:33
【问题描述】:
我正在处理一些库存资料,我正在尝试将所有 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 ....
【问题讨论】:
-
当您编写
-F\t时,您是在告诉awk 拆分字母@987654324@ 上的输入,就像您刚刚编写-Ft一样,因为t不是元字符。您的意思是写-F'\t'将输入拆分为制表符吗?无论那个神秘的 sed 脚本在做什么,您都可以在 awk 中清晰易读地完成 - 如果您需要帮助,请发布一个单独的问题。 -
declare -a regions=( $(aws ec2 ...) )会将区域填充为 array -- 如果没有( $(...) ),您只会填充数组的第一个元素。