Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 Jan 2009 19:14:29 -0800
From:      George Davidovich <freebsd@optimis.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: bash versus sh test builtin
Message-ID:  <20090112031429.GA42596@marvin.optimis.net>
In-Reply-To: <7740275E-77AA-45C1-B95E-C1F62D2AAB29@goldmark.org>
References:  <7740275E-77AA-45C1-B95E-C1F62D2AAB29@goldmark.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Jan 11, 2009 at 08:08:18PM -0600, Jeffrey Goldberg wrote:
> The -ne operator for [ in /bin/sh doesn't seem to work as in bash.   
> Also the bash behavior here is what matches /bin/[ most closely.
> 
> $ /bin/sh
> $ if [ $UID -ne 0 ] ; then
>  > echo not root
>  > fi
> [: -ne: unexpected operator
> $ exit
> $ echo $SHELL
> /usr/local/bin/bash
> [jeffrey@dobby ~/src/mount-rsnap]$ if [ $UID -ne 0 ] ; then
>  > echo not root
>  > fi
> not root
> 
> Does anyone have a recommendation of how to run this simple test in / 
> bin/sh 

if [ $(id -u) -ne 0 ]; then ...

As to why your test isn't working as expected, rewrite your script to
read:

#!/bin/sh
echo $UID

and you'll discover that UID is a bash environmental variable.

> and how to write tests reasonably portably?

That's a different question, and merits a much longer discussion
probably better had elsewhere.  I'd suggest comp.unix.shell.

-- 
George



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090112031429.GA42596>