Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Problems with Parameter Expansion :=



On Wed, Feb 14, 2001 at 09:28:04PM +0100,
Heinrich Götzger <goetzger@xxxxxxx> wrote:
> 
> Hi there again,
> 
> my second problem is that I want to set NAME to WORD,
> if NAME is not set, example from zsh.info:
> ${NAME:=WORD}

Fine :-)

> my script (called script.sh):
> -- begin --
> if [ ${NAME:=`hostname | cut -c5-8`} ]
> then
>         echo $NAME
> fi
> 
> if [ ${NAME1:=""} = "true" ]
> then
>         echo "true"
> fi
> -- end --
> asuming, my hosts name is exploding:
> 
> $ echo $NAME
> zsh: NAME: parameter not set
> $ echo $NAME1
> zsh: NAME1: parameter not set
> $ . script.sh
> odin
> script.sh:6: parse error: condition expected: =

Of course :-)

> Again, it runs with bash and ksh.
> 
> Why does it not expand NAME1 to "" ?

It expands NAME1 to an empty string, which I would expect. Why? Let's
see:
if [ ${NAME1:=""} = "true" ] ; then echo "true" ; fi
     ^^^^^^^^^^^^
     Put value of $NAME1 here. Use empty default. Oops, the shell sees
     this:
if [  = "true" ] ; then echo "true" ; fi


> running it with set -x it shows:
> $ . script.sh
> +-zsh:8> . script.sh
> +script.sh:1> [ odin ]
> +script.sh:3> echo odin
> odin
> +script.sh:6> [ = true ]
> script.sh:6: parse error: condition expected: =
> 
> Any ideas?

Of course :-)

Either use [[ ]] like this:
if [[ ${NAME1:=""} = "true" ]] ; then echo "true" ; fi

Or, if your shell is also supposed to run with bash/ksh, use something
like this:
if [ "${NAME1:=''}" = "true" ] ; then echo "true" ; fi

> Thanks for our help.
> 
> Regards
> 
> Heinrich

Ciao,
Thomas

-- 
 Thomas Köhler Email:   jean-luc@xxxxxxxxxxxxxxxxx     | LCARS - Linux
     <><        WWW:     http://jeanluc-picard.de      | for Computers
                IRC:             jeanluc               | on All Real
               PGP public key available from Homepage! | Starships

Attachment: pgpLLJyNU6ylh.pgp
Description: PGP signature



Messages sorted by: Reverse Date, Date, Thread, Author