Bash脚本检查是否存在多个组

时间:2015-07-21 14:10:08

标签: bash unix awk scripting grep

我需要使用命令

将用户添加到多个补充组
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

2 个答案:

答案 0 :(得分:0)

InstanceHandle handle = instanceStore.CreateInstanceHandle(instanceId);
InstanceView view = instanceStore.Execute(handle, new LoadWorkflowCommand(), TimeSpan.FromSeconds(30));
string activityType = view.InstanceData[MyParticipantExtension.GetActivityTypeXName()].Value as string;

答案 1 :(得分:0)

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