For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2006 > Test for multiple User IDs in Script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Test for multiple User IDs in Script
ChrisS

2006-03-29, 7:02 pm

I realize this is an easy question for some, but I can't figure it out.

If I'm testing for a set of user IDs in Korne shell or Bash, what's the
best way to do that.

It's simple to do:

if [ $USER != "bobouser" ]; then
exit 1
else
echo "Hello $USER"
if

BUT, how 'bout I have 5 user IDs to test against, I rather not do
multiple "elses". How would you write it in Unix script. Not perl
please. I'm in Bash now, but I know the Korne shell syntax would most
likely work too.

Thanks for the "logic"

Chris

Pascal Bourguignon

2006-03-29, 7:02 pm

"ChrisS" <chris.scarff@gmail.com> writes:
> I realize this is an easy question for some, but I can't figure it out.
>
> If I'm testing for a set of user IDs in Korne shell or Bash, what's the
> best way to do that.
>
> It's simple to do:
>
> if [ $USER != "bobouser" ]; then
> exit 1
> else
> echo "Hello $USER"
> if
>
> BUT, how 'bout I have 5 user IDs to test against, I rather not do
> multiple "elses". How would you write it in Unix script. Not perl
> please. I'm in Bash now, but I know the Korne shell syntax would most
> likely work too.


case "$USER" in
bobouser|bibiuser|babauser|bubuuser|bebe
user) printf "Hello %s!\n" "$USER" ;;
*) exit 1 ;;
esac

--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
Rainer Temme

2006-03-29, 7:02 pm

ChrisS wrote:

> if [ $USER != "bobouser" ]; then
> exit 1
> else
> echo "Hello $USER"
> if
>
> BUT, how 'bout I have 5 user IDs to test against, I rather not do
> multiple "elses". How would you write it in Unix script.


Chris,

how about a for loop ...

FOUND=0
SEARCH=$USER
for LIST in "bobouser" "user2" "user3" "user4" "user5"
do
if test $SEARCH = $LIST
then
FOUND=1
fi
done

if test $FOUND -eq 1
then
echo "Hello $USER"
else
exit 1
fi

.... Rainer
loic-dev@gmx.net

2006-03-29, 7:02 pm

Hi Chris,

> BUT, how 'bout I have 5 user IDs to test against, I rather not do
> multiple "elses". How would you write it in Unix script. Not perl
> please. I'm in Bash now, but I know the Korne shell syntax would most
> likely work too.
>
> Thanks for the "logic"


A possibility is to use to "for" loop construct:

#!/bin/bash

USER_LIST="user1 user2 user3 user4 user5"

not_found=1
for u in $USER_LIST
do
if [ $USER == $u ]
then
echo "Hello $u"
not_found=0
fi
done

exit $not_found

Jim Cochrane

2006-03-29, 7:02 pm

On 2006-03-29, loic-dev@gmx.net <loic-dev@gmx.net> wrote:
> Hi Chris,
>
>
> A possibility is to use to "for" loop construct:
>
> #!/bin/bash
>
> USER_LIST="user1 user2 user3 user4 user5"
>
> not_found=1
> for u in $USER_LIST
> do
> if [ $USER == $u ]
> then
> echo "Hello $u"
> not_found=0
> fi
> done
>
> exit $not_found
>


I would add a break within the for loop, to save some time in case the
number of users gets large. If it's part of a larger script, you can also
put it into a function for better code structure - e.g.:

VALID_USERS="bill betty joe john sally errol" # ...

valid_user() {
user=$1
result=1
for u in $VALID_USERS; do
if [ $u = "$user" ]; then
result=0
break
fi
done
return $result
}

if ! valid_user $USER; then
echo "Sorry, $USER is not a valid user - aborting ..." >&2
exit 2
fi


If the valid user list gets really large, it'd be much more efficient to
use a hash table. (bash doesn't support hash tables, does it? If not, you
might want to use, e.g., awk or perl.)


--

*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
ChrisS

2006-03-29, 10:00 pm

Thanks guys. Naturally all the different ways work just find, and
gives me some more practice out of the normal "simple" scripting that I
normally perform.

Now the most difficult part is deciding which way I want to skin this
cat.

Cheers,

Chris

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com