我有辅助组名称列表,例如group_1 group_2.. group_n
和用户名,例如:user1
现在我需要做
确保所有群组都存在
确保没有额外的组
我尝试使用id -nG user1 | grep <group_1> | grep <group_2> | .. | grep <group_ n>
并重新评估exitcode
,但这只会确保所需的群组存在。我不确定如何验证没有额外的群组(不在我的列表中的群组)在场。
答案 0 :(得分:1)
您可以像这样使用grep
:
grep -oFf a_file_with_secondary_group_names_per_line
示例代码如何实现您的目标:
#!/bin/bash
user=username
file=file_with_secondary_groups
if [[ $(id -G "$user" |wc -w) == $(id -nG "$user" | grep -coFf "$file") ]]; then
echo "*All groups are present"
# i.e the number of group and the number of group matched is the same
if [[ $(id -G "$user" |wc -w) == $(grep -co '.' "$file") ]]; then
echo "*No extra groups"
# i.e the number of groups and the number of groups in the file are same
else
echo "-Extra groups present"
fi
else
echo "-All groups are not present"
fi