Why /bin/sh doesn't like the line: if test "x$my_var" == "xyes"; then

Polytropon freebsd at edvax.de
Thu Sep 3 04:59:42 UTC 2009

On Thu, 3 Sep 2009 04:14:56 +0000 (GMT), jerry M <jerrry94087 at yahoo.com> wrote:
> configure file got this line and it causes the message: test: xyes: unexpected operator
> But removing spaces around == or replacing == with = makes it to work.

Maybe those files are not intended to run on FreeBSD's standard
Bourne shell?

> On Linux though this line works fine.

This may be due to the fact that on most Linusi, sh is bash.
On FreeBSD, sh is a different shell than bash; it's the
"original" Bourne shell.

> Why spaces around == would cause failure?

First of al, refer to "man test" for the string operators. The
form "==" isn't mentioned there.

     s1 = s2       True if the strings s1 and s2 are identical.

     s1 != s2      True if the strings s1 and s2 are not identical.

     s1 < s2       True if string s1 comes before s2 based on the binary value
                   of their characters.

     s1 > s2       True if string s1 comes after s2 based on the binary value
                   of their characters.

Furthermore, it's wise to enclose the variable names in curly
brackets when used in a compositum. Your statement

	if test "x$my_var" == "xyes"; then

would correctly read as follows:

	if test "x${my_var}" = "xyes"; then

You can of course use [ instead of test, so it would be

	if [ "x${my_var}" = "xyes" ]; then

> What
> is the version of /bin/sh currently used in 7.2? Where is it taken
> from? --version, -v, -version don't ever print version I guess due to
> FreeBSD policy of not versioning individual utilities.

FreeBSD's Bourne shell does not have GNU long options such
as --version. According to the shell's manpage, -v is used
for verbosity, and -version isn't even defined. Refer to
"man sh" for more options.

The parts of the base system (as in opposite to Linux where no
real "the OS" does exist) usually have the version of the
release, but to be more precise, the individual version can be
obtained from the source code. It can be found in /usr/src/bin/sh.
For example, I have

	@(#)main.c      8.6 (Berkeley) 5/28/95
	$FreeBSD: src/bin/sh/main.c,v 1.29 2006/10/07 16:51:16 stefanf Exp $

in the source file main.c, and the Makefile indicates

	@(#)Makefile    8.4 (Berkeley) 5/5/95
	$FreeBSD: src/bin/sh/Makefile,v 1.46 2006/04/17 17:55:11 schweikh Exp $

as its particular version number and date.

The manual "man sh" includes this statement:

     The sh utility is the standard command interpreter for the system.  The
     current version of sh is in the process of being changed to conform with
     the IEEE Std 1003.2 (``POSIX.2'') specification for the shell.  This ver-
     sion has many features which make it appear similar in some respects to
     the Korn shell, but it is not a Korn shell clone like pdksh.  Only fea-
     tures designated by POSIX, plus a few Berkeley extensions, are being
     incorporated into this shell.  This man page is not intended to be a
     tutorial nor a complete specification of the shell.

as well as

     This version of sh was rewritten in 1989 under the BSD license after the
     Bourne shell from AT&T System V Release 4 UNIX.

It's important, especially for interoperability, to declare
#/bin/sh only and ONLY if the script is for Bourne shell. If
you're using bash specifics, use #!/usr/local/bin/bash instead.
Keep in mind that you'll have to install bash (pkg_add -r bash)
in this case. A tidy way of coding, such as using the curly
brackets, is an important thing, too, as you can see. :-)

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list