[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sh script Q



Ariel Biener wrote:

> To all the helpfull people:

I don't care to participate in religious wars, which is basically what this
thread is about.  But factual mistakes need to be corrected.

> I believe that when you create shell scripts, they'd better be generic,
> unless there is a special reason, and if there is, it should be documented
> in the script. (like using functions, which work in bash/ksh, but not in
> the generic /bin/sh on many OSs.)
> 
> I know "$variable" works on bash/ksh.

Sigh.  

As long as we're trying to be ``generic'', how about consulting the original
sh(1) manual from the Seventh Edition release of the Unix OS, from (then) Bell
Telephone Laboratories.  You know, the sh written by Steve Bourne.

	Blank interpretation.
	     After parameter and command  substitution,  any  results  of
	     substitution  are scanned for internal field separator char-
	     acters (those found in $IFS) and split into  distinct  argu-
	     ments  where such characters are found.  Explicit null argu-
	     ments ("" or '')  are  retained.   Implicit  null  arguments
	     (those  resulting  from  parameters that have no values) are
	     removed.

> So just treat Vadik and I as purists.

The suggestions made here have been anything but ``pure''.  The answer to the
original question is in the man page.

	${parameter:=word}
	     If parameter is not set or is null set it to word;  the
	     value  of  the  parameter  is  substituted.  Positional
	     parameters may not be assigned in this way.

This is from a SunOS 5 man page, with the following heritage.

	.\" @(#)sh.1 1.68 97/05/09 SMI; from SVr4
	.\" Copyright 1989 AT&T
	.\" Copyright (c) 1997 Sun Microsystems, Inc.
	.\" All Rights Reserved.
	.\" Portions Copyright (c) 1992, X/Open Company Limited
	.\" All Rights Reserved

(It's also in the Open Group UNIX 98 specification.  But the use of the colon,
which adds the null test is missing in the original Bourne shell.)

I think most people can settle for this.  If you want to be outrageously
``generic'' you can use "$VAR" or x$VAR.  Using them both works too, but since
we're analyzing the ``best'' way to do something, it's just plain...
unnecessary.