NaNtastic
NaNtastic

Reputation: 57

How do I write an if-statement that excludes users depending on groups?

So in Ubuntu, a program has to start on the start of a shell, if the user who is logged in is in a certain group. Using an if-statement in the /etc/bash.bashrc is what we are trying right now, but we dont know how to form a valid if-statement for this situation.

if [ group == familie ]; then
*the program that has to be started*
fi

this is our statement so far..

Upvotes: 0

Views: 39

Answers (2)

rul
rul

Reputation: 824

There are lots of ways to do this. For example, you could directly parse the /etc/group file with grep, cut and tr:

grep -E ^$GROUPNAME: /etc/group | cut -d: -f4 | tr , "\n" | grep -E ^$USERNAME$ | wc -l

If the output of the previous command is non-zero, then the user is in the group. Obviously you'll need to replace $GROUPNAME and $USERNAME with your input.

Upvotes: 1

choroba
choroba

Reputation: 241998

I would probably use id, as its output is easier to parse then groups, which might contain spaces (e.g. in cygwin):

if [[ $(/usr/bin/id) == *groups*[,=]'20(games)'* ]] ; then
    echo yes
else
    echo no
fi

Use your group id and name instead of 20 and games.

It might be more secure, though, to let the system do the hard work. Just create a script that contains the code to start the desired programme, and make that script runnable only for the group; then just run the script.

Upvotes: 1

Related Questions