首页 > 解决方案 > 我在我的 bash 脚本中使用 break 还是 continue ?

问题描述

我有一个 bash 脚本来收集我们的 AWS 用户的信息。它列出了用户名、他所在的帐户(带有帐号)、他所属的组以及他有权访问的角色(如果有的话)。

我使用 4 个级别的 while 循环来实现这一点。

我在一个 AWS 账户中获取了他所属的一组用户组以及该 AWS 账户中的所有角色(您必须做一些体操来解析我已经基本解决的角色的内容)。

但是,如果数组有一个空(或 null)数组成员,我希望它继续读取组名的外部角色。

我像这样构建数组:

readarray aws_groups < <(aws iam list-groups-for-user --user-name "$aws_user_name" --profile="$aws_key" | jq -r '.Groups[].GroupName')
readarray aws_roles < <(aws iam list-roles --profile="$aws_key" | jq -r '.Roles[].RoleName')

我的问题是,如果我使用“break”,我是否只打破了包含此信息的 if/then 语句:

 if [[ -n "$aws_group" ]]; then
             echo "Null group."
             echo "*****BREAK HERE******"
             break
   fi

或者,当我使用 break 时,我是否会跳出它所属的循环遍历 aws_group 值:

while IFS= read -r aws_group
        do
          if [[ -n "$aws_group" ]]; then
             echo "Null group."
             echo "*****BREAK HERE******"
             break
          fi

以下是我的代码,我希望有助于展示我用来循环遍历 aws 用户名、aws 帐户和帐号、aws 组和 aws 角色的结构:

while IFS= read -r aws_user_name
  do
    echo "*************************************************************************"
    echo "*        Search for AWS User: $aws_user_name in All KPMG Accounts       *"
    echo "*************************************************************************"
    echo
    while IFS= read -r aws_key && read -ru 3 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 [[ -n "$user_lives_here" ]]; then
        readarray aws_groups < <(aws iam list-groups-for-user --user-name "$aws_user_name" --profile="$aws_key" | jq -r '.Groups[].GroupName')
        readarray aws_roles < <(aws iam list-roles --profile="$aws_key" | jq -r '.Roles[].RoleName')
        while IFS= read -r aws_group
        do
          if [[ -n "$aws_group" ]]; then
             echo "Null group."
             echo "*****BREAK HERE******"
             break
          fi
          while IFS= read -r aws_role
          do
             if [[ -z "$aws_role" ]]; then
             echo "Null role."
             echo "*****BREAK HERE******"
             break
          fi
          echo "Debug statement"
          echo "***********************************************************************************************************************************************"
          printf "This is the role name: %s\\nThis is the user name: %s\\nThis is the group: %s\\nand this is the account: %s\\n*"  "$aws_role" "$aws_user_name" "$aws_user_group" "$aws_key"
          echo "***********************************************************************************************************************************************"
          **** some more code ****
         done  <<< "${aws_roles[@]}"
    done <<< "${aws_groups[@]}"
   done < "$aws_env_list"  3< "$aws_account_numbers"
 done < "$aws_users_all"

您在此处看到的是调试代码,因为我正在构建脚本。

如果您能弄清楚中断是什么,我将不胜感激,并且最好在此处使用 continue 。

标签: bash

解决方案


break并且continue只对循环进行操作,而不是if. 这在文档中有明确解释:

break [n]
forwhileuntilselect循环中退出。如果n指定,则打破n水平。

continue [n]
for恢复封闭、whileuntilselect循环 的下一次迭代。如果n指定,则在第nth 封闭循环处继续。


推荐阅读