Operating System - HP-UX
1767188 Members
3445 Online
108959 Solutions
New Discussion юеВ

Help with LIKE operator or $GROUP variable

 
SOLVED
Go to solution

Help with LIKE operator or $GROUP variable

I'm trying to fix the a profile script and would like to validate against a unix group instead of username. Is there a variable that will allow me to do this? Below is a portion of the script as it is currently written.

if [ "$LOGNAME" = "user1"]

I'd like to change this to either
"$GROUPNAME =" OR "$LOGNAME like".

Thanks for any assistance,
Staci
5 REPLIES 5
Geoff Wild
Honored Contributor

Re: Help with LIKE operator or $GROUP variable

Don't think so, but you could find out the primary group:

GROUP = `groups $LOGNAME |awk '{print $1}'`

if [ "$GROUP" = "adm"]


...etc


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Jordan Bean
Honored Contributor

Re: Help with LIKE operator or $GROUP variable

If you're only interested in a user's primary group, then `id -gn` would be helpful. Perhaps case statements would also be useful.

case "$(id -gn)" in
(grp1) ... ;;
(grp2) ... ;;
esac

If you need to look at all of the user's groups, then `groups` would be more appropriate.

for g in $(groups); do
case "$g" in
(grp1) ... ;;
(grp2) ... ;;
esac
done

For simple matching of similar usernames, like user123, user124, etc, try this:

if [ "${LOGNAME%[0-9][0-9][0-9]}" == "user" ]

or

case "$LOGNAME" in
(user*) ... ;;
(*tst) ... ;;
([a-z][0-9]admin?) ... ;;
esac

Geoff Wild
Honored Contributor
Solution

Re: Help with LIKE operator or $GROUP variable

Jordan's right - id -gn will retrive primary group, or modify mine to be:

groups -p $LOGNAME

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.

Re: Help with LIKE operator or $GROUP variable

I'd like to thank you both for the quick response to my question. The one that worked best for my situation was

GROUP = $(groups -d $LOGNAME)

if [$GROUP=support]
then ...

Staci
Hein van den Heuvel
Honored Contributor

Re: Help with LIKE operator or $GROUP variable

>> I'd like to thank you both for the quick response to my question.

Don't just thank them... give them some points! :-).

>> The one that worked best for my situation was : GROUP = $(groups -d $LOGNAME)

Uh.... that's a typo right? -d really is -p.


btw... if you are really concerned about a user being part of multipel groups (root), then you may want to try something like:

if groups $LOGNAME | grep -q support ; then echo "yes"; else echo "no"; fi


Cheers,
Hein.