Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 Jan 2009 21:07:17 -0600
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Jeffrey Goldberg <jeffrey@goldmark.org>
Cc:        "mail.list freebsd-questions" <freebsd-questions@freebsd.org>
Subject:   Re: bash versus sh test builtin
Message-ID:  <20090112030717.GA57874@dan.emsphone.com>
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
In the last episode (Jan 11), Jeffrey Goldberg said:
> 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 and how to write tests reasonably portably?

UID=$(id -u)
if [ $UID -ne 0 ] ; then
 echo not root
fi

UID is not a variable set by /bin/sh, which is why the test fails.

http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_03

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?20090112030717.GA57874>