我需要使用以下命令将用户添加到多个补充组中
usermod -aG group1,group2 username
需要帮助以进行错误检查,以使脚本搜索/ etc / group文件中是否存在多个组。这就是我所拥有的。
read -p "Enter groups" groups
if (Check if those groups exists)
then
usermod -aG "$groups $username
else
echo "Group(s) does not exists"
fi
请帮忙谢谢!对不起,我是一个新手,如果可能的话,请告诉我一些阅读链接。
好吧,我提出了一些切实可行的方法。如果它可以被某人“清理”,将不胜感激。XD
read -p "Enter user" user
read -p "Enter groups" groups
storegroups=$(echo $groups | awk -F, '{print $1" "$2" "$3}')
if [ "$(getent group $storegroups | wc -l)" == $(echo $storegroups | wc -w)" ]
then
usermod -ag $groups $user
else
echo "1 or more groups does not exists"
fi
#!/bin/bash
read -p "Enter username: " username
read -p "Enter groups: " groups
for g in ${groups//,/ }; do
grep -q "^$g:" /etc/group
ret=$? # save returncode of grep
if [[ $ret -eq 0 ]]; then
usermod -aG "$g" "$username"
else
echo "Group $g does not exists"
fi
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句