|
|
#1 |
|
Prospect
Join Date: Apr 2009
Posts: 8
|
If Statements and unary operator expected
I have a script I am building to move Network Admin users (leveraging Active Directory) to the /Groups/admin Local Admin group, since users lose Admin rights once they are off the network.
#!/bin/sh The following script gets me the results I am looking for however, when the result is No (a null value) the script reports the following error: /bin/bash: line 6: [: username: unary operator expected Was wondering if anyone had any tips for this error. |
|
|
|
|
|
#2 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,010
|
Put quotes around $group in the if expression. That way, even if it's the empty string (because grep didn't find any matching lines), the shell will still see a parameter there.
Code:
user=$(stat -f%Su /dev/console)
group=$(dseditgroup -o read -n /Local/Default admin | grep $user)
if [ "$user" = "$group" ] ; then
echo Yes
else
echo No
fi
(In similar vein, if you want to know who owns /dev/console, just ask. No need to bring in ls and cut and hope the owner will always be in the fourth field. Whenever you can, ask for the precise information you want, rather than asking for much more and then trying to winnow out the irrelevant chatter.) Code:
user=$(stat -f%Su /dev/console) if dseditgroup -o checkmember -n /Local/Default -m $user admin >/dev/null then echo Yes else echo No fi |
|
|
|
|
|
#3 |
|
All Star
Join Date: Dec 2004
Posts: 677
|
Is your pipe even working? For me on 10.6, dseditgroup spews its output to STDERR whereas | only propagates STDOUT to the next command.
|
|
|
|
|
|
#4 |
|
League Commissioner
Join Date: Mar 2003
Location: Kansas City
Posts: 11,347
|
Hmm, this script looks familiar, haha....
Here is my take on it, but my take is specific to my preferred methods, so this is like my opinion man! Code:
#!/bin/bash
# generate list of users that have UID grater than 500
userList=$(dscl . list /Users UniqueID | awk '$2 > 500 { print $1 }')
for u in ${userList} ; do
dseditgroup -o read -n /Local/Default admin | awk '/${u}/ { print $1 }'
#test to see if it was successful
if [[ `echo "$?"` == 0 ]]
then echo "${u} is in the admin group"
else echo "${u} is not in the admin group"
fi
done
exit 0
__________________
sudo make me a sammich http://www.tlarkin.com "It just told me what I already knew, that I'm a great and amazing guy, didn't I tell you baby, I'm Zaphod Beeblebrox." |
|
|
|
|
|
#5 | |||||||||||||||||||||||
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,010
|
Yes. For me on 10.7, dseditgroup sends its output to stdout, for both the read and the checkmember operations. I tested that both of the scripts I posted work. (That is, the OP's script with quotes around "$group", and mine which pipes dseditgroup's stdout to /dev/null.) Just for completeness, I tested under 10.6. dseditgroup -o read sends its output to stderr; dseditgroup -o checkmember sends a superfluous message to stdout. (Superfluous, in the sense that it merely rephrases the exit status.) |
|||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|