bash - 我在我的 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 。
解决方案
break
并且continue
只对循环进行操作,而不是if
. 这在文档中有明确解释:
break [n]
从for
、while
、until
或select
循环中退出。如果n
指定,则打破n
水平。
continue [n]
for
恢复封闭、while
、until
或select
循环 的下一次迭代。如果n
指定,则在第n
th 封闭循环处继续。
推荐阅读
- excel - 如何根据不同工作簿中匹配的单元格删除行?
- java - 找出两次之间更接近的时间
- excel - 通过 if 条件获取数据并导出到另一个 excel 的代码
- android - Android Google Map-如何将地图划分为4个象限并知道标记象限的位置
- arduino-ide - 如何修复 Arduino IDE 中的“avr-g++:错误:device-specs/specs-atmega328p:没有这样的文件或目录”错误
- c# - 用 C# 在 excel 中格式化多行的最快方法
- c# - 将日期时间对象列表转换为特定的 JSON 格式
- javascript - javascript对象按数组排序
- python - 尽管 IP 和用户代理轮换抓取,但仍被禁止
- python - 烧瓶应用程序运行时,Bamboo 无法检测存储库提交