在while循环bash中通过多个值重复输出循环 [英] Duplicate output looping through multiple values in while loop bash

查看:75
本文介绍了在while循环bash中通过多个值重复输出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bash中的while循环遍历3个文件的内容.它们包含名称,aws帐户和aws帐号的列表.

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.

但是循环是不正确的,它会不断重复列表中的名字和列表中的第一个aws环境.

But the loop isn't correct and it keeps repeating the first name from the list and the first aws environment from the list.

这是我看到的输出:

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"

示例输出:

aadatiya
abigailcharles 
tdunphy
broberts

下一个文件:

aws_env_list="source_files/aws_environments_all.txt"

样本输出:

company-lab
company-stage
company-nonprod
company-prod

最后一个文件:aws_account_numbers ="source_files/aws_account_numbers.txt"样本输出:

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

如何正确执行此操作,以便循环浏览每个名称,aws帐户和aws帐号,以便每个条目显示一次?

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?

推荐答案

您想要一个一个循环,它使用三个调用 read 三次的次数不同的文件描述符.

You want one loop that calls read three times, using three different file descriptors.

while IFS= read -r aws_user_name
      IFS= read -r aws_key <&3
      IFS= read -r aws_account_num <&4; do
    ...
done < "$aws_users_all" 3< "$aws_env_list" 4< "$aws_account_numbers"

这篇关于在while循环bash中通过多个值重复输出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆