如何检查组是否存在,如果不在Linux Shell脚本中则添加

时间:2022-01-24 11:49:16

this is a summary of what i want my code to do:

这是我希望我的代码做的总结:

if (group exists)
then

  (add user to group)

else

  (create group)

  (add user to group)

fi

I am using the Ubuntu virtual machine but all of the results i have found on similar sites do not work.

我正在使用Ubuntu虚拟机,但我在类似网站上找到的所有结果都不起作用。

4 个解决方案

#1


8  

This script may help you:

此脚本可以帮助您:

   read -p "enter group name: " group
   if grep -q $group /etc/group
    then
         echo "group exists"
    else
         echo "group does not exist"
    fi

#2


37  

The grep statement in the solution of rups has some flaws:

rups解决方案中的grep语句有一些缺陷:

E.g. grepping for a group admin may return true ("group exists") when there is a group lpadmin.

例如。当有一个组lpadmin时,grepping for group admin可能会返回true(“group exists”)。

Either fix the grep-query

修复grep查询

grep -q -E "^admin:" /etc/group

or use

或使用

if [ $(getent group admin) ]; then
  echo "group exists."
else
  echo "group does not exist."
fi

#3


6  

Grepping /etc/group works, but only on a machine where /etc/nsswitch.conf has:

Grepping / etc / group可以工作,但只能在/etc/nsswitch.conf具有的机器上运行:

group: files

meaning that only /etc/group is consulted when determining available groups. Use:

意味着在确定可用组时仅咨询/ etc / group。使用:

getent group <groupname>

for a more generic solution, checking the exit status: 0 means "exists", non-zero means "does not exist". For example, to check to see if group 'postgres' exists, and create it if it does not (assuming bash shell, running as a user able to create new groups) run:

对于更通用的解决方案,检查退出状态:0表示“存在”,非零表示“不存在”。例如,要检查组'postgres'是否存在,如果不存在则创建它(假设bash shell,作为能够创建新组的用户运行)运行:

/usr/bin/getent group postgres 2>&1 > /dev/null || /usr/sbin/groupadd postgres

#4


3  

I've found it more useful, to compose andiba's solution into a proper function:

我发现将andiba的解决方案组合成一个合适的函数更有用:

function grpexists {
if [ $(getent group $1) ]; then
  echo "group $1 exists."
else
  echo "group $1 does not exist."
fi

}

}

This can for e.g be invoked into your environment by including this function in your /etc/bash.bashrc*, such that you can then check for the existence of a group, using the following spell:

这可以通过在/etc/bash.bashrc*中包含此函数来调用到您的环境中,这样您就可以使用以下符号检查组是否存在:

grpexists group_name

Which should then return one of:

哪个应返回以下之一:

group group_name exists.

group group_name存在。

or

要么

group group_name does not exist.

group group_name不存在。

#1


8  

This script may help you:

此脚本可以帮助您:

   read -p "enter group name: " group
   if grep -q $group /etc/group
    then
         echo "group exists"
    else
         echo "group does not exist"
    fi

#2


37  

The grep statement in the solution of rups has some flaws:

rups解决方案中的grep语句有一些缺陷:

E.g. grepping for a group admin may return true ("group exists") when there is a group lpadmin.

例如。当有一个组lpadmin时,grepping for group admin可能会返回true(“group exists”)。

Either fix the grep-query

修复grep查询

grep -q -E "^admin:" /etc/group

or use

或使用

if [ $(getent group admin) ]; then
  echo "group exists."
else
  echo "group does not exist."
fi

#3


6  

Grepping /etc/group works, but only on a machine where /etc/nsswitch.conf has:

Grepping / etc / group可以工作,但只能在/etc/nsswitch.conf具有的机器上运行:

group: files

meaning that only /etc/group is consulted when determining available groups. Use:

意味着在确定可用组时仅咨询/ etc / group。使用:

getent group <groupname>

for a more generic solution, checking the exit status: 0 means "exists", non-zero means "does not exist". For example, to check to see if group 'postgres' exists, and create it if it does not (assuming bash shell, running as a user able to create new groups) run:

对于更通用的解决方案,检查退出状态:0表示“存在”,非零表示“不存在”。例如,要检查组'postgres'是否存在,如果不存在则创建它(假设bash shell,作为能够创建新组的用户运行)运行:

/usr/bin/getent group postgres 2>&1 > /dev/null || /usr/sbin/groupadd postgres

#4


3  

I've found it more useful, to compose andiba's solution into a proper function:

我发现将andiba的解决方案组合成一个合适的函数更有用:

function grpexists {
if [ $(getent group $1) ]; then
  echo "group $1 exists."
else
  echo "group $1 does not exist."
fi

}

}

This can for e.g be invoked into your environment by including this function in your /etc/bash.bashrc*, such that you can then check for the existence of a group, using the following spell:

这可以通过在/etc/bash.bashrc*中包含此函数来调用到您的环境中,这样您就可以使用以下符号检查组是否存在:

grpexists group_name

Which should then return one of:

哪个应返回以下之一:

group group_name exists.

group group_name存在。

or

要么

group group_name does not exist.

group group_name不存在。