I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id ${SharedFileSystem} | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "${EFS_SERVER_IPS_ARRAY[@]}"
do
echo "$element"
echo "$element $MOUNT_SOURCE" >> /etc/hosts
done
This works but looks ugly. I want to avoid the array variable and the for loop (basically I don't care about the first echo command).
Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:
long AWS command >> echo $element $MOUNT_SOURCE >> /etc/hosts
with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?
The output of the AWS command is like this:
10.10.10.10
10.22.22.22
Then, the added lines in /etc/hosts look like:
10.10.10.10 unique-id.efs.us-east-1.amazonaws.com
10.22.22.22 unique-id.efs.us-east-1.amazonaws.com
/etc/hosts? It seems more likely it is just adding the literal numbers0and1to it.${!name[@]}syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).