1833016 Members
2170 Online
110048 Solutions
New Discussion

Zero + Blanks

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

Zero + Blanks

OK, still trying to write this script and its just about done but I am wondering why when using a test statement and looking for a zero in a certain field, returns the same if it is blank.

Isnt there a way to say, zero is a 0, not a blank?
while read pwline; do
user="$(echo "$pwline" | cut -d':' -f1)"
for gid in $(id -G "$user"); do
if [ "$gid" -eq 0 -a "$user" != "root" ]; then
echo "$user is a member of GID 0"
fi

Im looking for a zero, not a blank. The script still thinks a blank is the same as 0.
done
done
UNIX IS GOOD
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: Zero + Blanks

Hi:

You need to change '-eq' to '='. Using '-eq' infers integer comparisons; Using "=" is for string comparisons. See the manpages for 'test'.

Regards!

...JRF...
Yogeeraj_1
Honored Contributor

Re: Zero + Blanks

hi,


try = instead of eq

e.g.

Server1> if [ "0" = 0 ]; then
> echo "0 is zero"
> fi
0 is zero
Server1>


hope this helps!
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
spex
Honored Contributor
Solution

Re: Zero + Blanks

Hello,

As an alternative, you could take advantage of parameter substitution:

$ gid=''
$ [ "${gid:-9999}" -eq 0 ]; echo $?
1

PCS
Nobody's Hero
Valued Contributor

Re: Zero + Blanks

Thanks Spex, that did it.
I was really struggling with this one. Probably why I was never a good cobol programmer either. Im a hardware person.

Thanks.
UNIX IS GOOD