2015-10-01 102 views
1

如何從我的describe-images變量的製表符分隔輸出中獲得像ARR[ImageID]=CreationDate這樣的數組?從ec2描述的圖像陣列

IMAGES=$(aws ec2 describe-images --output text --query 'Images[].[ImageId,CreationDate]'); 

輸出類似於:

ami-11xxxxx 2015-03-06:12:00:00 
ami-12xxxxx 2015-03-06:12:00:00 
ami-13xxxxx 2015-03-06:12:00:00 

回答

1

這個怎麼樣?

IMAGES=$(aws ec2 describe-images --output text --query 'Images[].[ImageId,CreationDate]' |awk '{print $1 "=" $2}'); 
1
#!/bin/bash 

#your data 
IMAGES=" 
ami-11xxxxx 2015-03-06:12:00:11 
ami-12xxxxx 2015-03-06:12:00:12 
ami-13xxxxx 2015-03-06:12:00:13 
" 

#declare associative memory 
typeset -A ARR 

index="" 
for s in ${IMAGES} 
do 
    if [ -z ${index} ]; then 
    index=$s 
    else 
    ARR[${index}]=$s 
    index="" 
    fi 
done 

#test 
echo ${ARR[ami-11xxxxx]} 
echo ${ARR[ami-12xxxxx]} 

結果:

2015-03-06:12:00:11 
2015-03-06:12:00:12