Bash脚本从amazonapi输出创建主机文件

Bash脚本从amazonapi输出创建主机文件,bash,amazon-web-services,hosts,Bash,Amazon Web Services,Hosts,我是个脚本新手。我的要求是从AWSAPI的输出创建一个/etc/hosts文件,如下所示 Hostname PrivateIP abc x.y.x.z cde a.b.c.c 等等 我有下面的脚本 #!/bin/bash export AWS_ACCESS_KEY=abc export AWS_SECRET_KEY=cde export EC2_URL=https://url.com source ~/.bashrc ec2-describe-instances --filter "gro

我是个脚本新手。我的要求是从AWSAPI的输出创建一个/etc/hosts文件,如下所示

Hostname PrivateIP

abc x.y.x.z

cde a.b.c.c
等等

我有下面的脚本

#!/bin/bash
export AWS_ACCESS_KEY=abc
export AWS_SECRET_KEY=cde
export EC2_URL=https://url.com
source ~/.bashrc
ec2-describe-instances --filter "group-name=mygroup" > /tmp/info
name=$(cat /tmp/info| grep 'Hostname' | cut -f 5)
ip=$(cat /tmp/info| grep 'INSTANCE' | cut -f 18)
echo -e $name"\t"$ip
目前,输出在一行中。 我肯定我需要一个for循环。但不知道怎么做。
请给出建议。

如果要在name变量上使用for循环,请尝试以下操作:

IFS=$'\n'
name=(`cat /tmp/info| grep 'Hostname' | cut -f 5`)
unset IFS

# For loop based on name
for i in "${name[@]}"
do
   # process each name ($i)
done

我记不起ec2 descripe instances命令的确切格式,但这里有一个简单的方法来处理多行输出,这些输出被格式化为由选项卡分隔的字段:

input=/tmp/info
while read field1 field2 field3 ...; do        # fieldx variables map to each field in the output
    # process the fields you need
done < "$input"

应该发布更多关于/tmp/info的信息,以获得更具体的答案。