Bash 脚本检查是否存在多个组
Bash scripting check if multiple groups exists
我需要使用命令
将用户添加到多个补充组
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
请帮忙谢谢!
对不起,我是新手,如果可能的话让我知道一些阅读链接。
好吧,我想出了一些实际有效的方法。如果有人可以 "cleaned" 将不胜感激。 XD
read -p "Enter user" user
read -p "Enter groups" groups
storegroups=$(echo $groups | awk -F, '{print " "" "}')
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
read -p "Enter user: " user
read -p "Enter groups: " groups
# Replace , with space
groupswithSpace=$(echo $groups | tr ',' ' ')
#getent returns 0 only if all groups are valid
getent group $groupswithSpace > /dev/null
if [ $? -eq 0 ]
then
usermod -ag $groups $user
else
echo "1 or more groups does not exists"
fi
我需要使用命令
将用户添加到多个补充组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
请帮忙谢谢! 对不起,我是新手,如果可能的话让我知道一些阅读链接。
好吧,我想出了一些实际有效的方法。如果有人可以 "cleaned" 将不胜感激。 XD
read -p "Enter user" user
read -p "Enter groups" groups
storegroups=$(echo $groups | awk -F, '{print " "" "}')
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
read -p "Enter user: " user
read -p "Enter groups: " groups
# Replace , with space
groupswithSpace=$(echo $groups | tr ',' ' ')
#getent returns 0 only if all groups are valid
getent group $groupswithSpace > /dev/null
if [ $? -eq 0 ]
then
usermod -ag $groups $user
else
echo "1 or more groups does not exists"
fi