I'm trying to loop through the contents of 3 files using a while loop in bash. They contain a list of names, aws accounts and aws account numbers.
But the loop isn't correct and it keeps repeating the first name from the list and the first aws environment from the list.
This is the output I see:
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
These are the files I'm trying to read from:
aws_users_all="source_files/aws_users_all.txt"
Sample output:
aadatiya
abigailcharles
tdunphy
broberts
Next file:
aws_env_list="source_files/aws_environments_all.txt"
Sample output:
company-lab
company-stage
company-nonprod
company-prod
Last file: aws_account_numbers="source_files/aws_account_numbers.txt" Sample output:
123456789191
987654321211
456721231213
123213512321
And this is the code with the incorrect loop:
ofile=source_files/aws_access_keys/company-aws-access-keys-all-accounts.csv
while IFS= read -r aws_user_name
do
while IFS= read -r aws_key
do
while IFS= read -r aws_account_num
do
user_lives_here=$(aws iam get-user --user-name "$aws_user_name" --profile="$aws_key" 2> /dev/null | jq -r '.User.UserName')
if [[ -z "$user_lives_here" ]]; then
printf "AWS user name: %s does not exist in AWS account: %s\\n(%s)" "$aws_user_name" "$aws_key" "$aws_account_num"
else
echo "$aws_user_name,$user_access_key1,$key1_date_created,$key1_last_used,$key1AgeDays,$aws_key,$aws_account_num" >> $ofile
fi
done < "$aws_account_numbers"
done < "$aws_env_list"
done < "$aws_users_all"
If I take out one level (the account numbers level) the script behaves as expected and produces this output:
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-bill
AWS user name: aadatiya does not exist in AWS account: company-stage
AWS user name: aadatiya does not exist in AWS account: company-dlab
AWS user name: aadatiya does not exist in AWS account: company-nonprod
AWS user name: aadatiya does not exist in AWS account: company-prod
AWS user name: aadatiya does not exist in AWS account: company-govcloud-admin-nonprod
AWS user name: abigailcharles does not exist in AWS account: company-lab
AWS user name: abigailcharles does not exist in AWS account: company-bill
AWS user name: abigailcharles does not exist in AWS account: company-stage
AWS user name: abigailcharles does not exist in AWS account: company-dlab
AWS user name: abigailcharles does not exist in AWS account: company-nonprod
AWS user name: abigailcharles does not exist in AWS account: company-prod
AWS user name: abigailcharles does not exist in AWS account: company-govcloud-admin-nonprod
I just commented out this level and it works:
#while IFS= read -r aws_account_num
#do
#done
How can I do this correctly so that I loop through each name, aws account and aws account number so that each entry shows once?