我想知道此脚本如何为aws_user_group变量提供空白输出。当外部循环在aws_groups数组的值上循环时。
我使用此行在aws帐户中创建一组分组:
readarray aws_groups < <(aws iam list-groups-for-user --user-name "$aws_user_name" --profile="$aws_key" | jq -r '.Groups[].GroupName')
然后我遍历该数组。我在该循环中遍历了另一个数组aws_roles。
在放置了aws_role循环的情况下,在为该迭代打印aws_groups数组的最后一个元素之后,在循环后我在aws_user_group上获得了空白输出:
当缺少aws_group信息时,脚本的输出如下所示:
Echo AWS Group: COMPANY_CSE from group while loop
Echo AWS Group: COMPANY_CSE from role while loop
*************************************************************
AWS User Name: tdunphy
AWS User Group: COMPANY_CSE
AWS Account: COMPANY-bill
AWS Account Number: 123456789101
*************************************************************
Echo AWS Group: COMPANY_POWERUSERS from group while loop
Echo AWS Group: COMPANY_POWERUSERS from role while loop
*************************************************************
AWS User Name: tdunphy
AWS User Group: COMPANY_POWERUSERS
AWS Account: COMPANY-bill
AWS Account Number: 123456789101
*************************************************************
Echo AWS Group: from group while loop
Echo AWS Group: from role while loop
No user group or user role in this interation.
该组数组有两个成员,然后在循环的最后一次迭代中打印出没有组信息。
如果我删除aws_role循环,那么它不会为aws_groups数组输出空白输出。一切都排队了!
这是我的代码的简化版本,显示了我在做什么:
#!/bin/bash
aws_key=lab
aws_users_all=all_users.txt
aws_account_num=123456789101
create_user_list() {
while IFS= read -r aws_user_name
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_user_group
do
echo "This is the AWS Group: $aws_user_group"
echo "Echo AWS Group: $aws_user_group from group while loop"
while IFS= read -r aws_role
do
echo "Echo AWS Group: $aws_user_group from role while loop"
if [[ -z "$aws_role" ]]; then
echo "Null role."
continue 1
fi
sleep 10
aws_user_has_role=$(aws iam get-role --role-name "$aws_role" --profile="$aws_key" | jq -r '.Role.AssumeRolePolicyDocument.Statement[].Principal.AWS' | sed "s/arn:aws:iam::832839043616:user\///g" | grep "$aws_user_name")
if [[ -n "$aws_user_has_role" ]]; then
aws_user_role=$aws_role
echo "Debug statement"
printf "This is the user role name: %s\\nThis is the user name: %s\\nThis is the group: %s\\nThis is the account:%s\\n*" "$aws_user_role" "$aws_user_name" "$aws_user_group" "$aws_key"
sleep 10
fi
if [[ -n "$aws_user_group" && -n "$aws_user_role" ]]; then
printf "AWS User Name: %s\\nAWS User Group: %s\\nAWS User Role: %s\\nAWS Account: %s\\nAWS Account Number: %s\\n\\n" "$aws_user_name" "$aws_user_group" "$aws_user_role" "$aws_key" "$aws_account_num"
break
sleep 10
elif [[ -n "$aws_user_group" ]]; then
printf "AWS User Name: %s\\nAWS User Group: %s\\nAWS Account: %s\\nAWS Account Number: %s\\n\\n" "$aws_user_name" "$aws_user_group" "$aws_key" "$aws_account_num"
break
else
printf "No user group or user role in this interation.\\n\\n"
fi
done <<< "${aws_roles[@]}"
done <<< "${aws_groups[@]}"
else
echo "User $aws_user_name does not exist in: $aws_key"
fi
echo
echo
done < "$aws_users_all"
}
在aws_groups数组的最后一个成员打印后,如何消除空组输出?