How to check if a group exists and add if it doesn’t in Linux Shell Script

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

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

Either fix the grep-query

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

or use

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)