A typical construct is

if [[ -z $LD_LIBRARY_PATH ]]; then
  export LD_LIBRARY_PATH=$MYLIB
else
  LD_LIBRARY_PATH=$MYLIB:$LD_LIBRARY_PATH
fi
I could replace this with
  export LD_LIBRARY_PATH=$MYLIB${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH-}
but so far I haven't seen this construct in any script.

Using the -v <name> extension of test conditions
        if [[ -v LD_LIBRARY_PATH && -z $LD_LIBRARY_PATH ]]
is not portable and more work than using
        if [[ -z ${LD_LIBRARY_PATH-} ]]

An extension which I would like to see is an additional option -U which works 
like -u but replaces $VAR by ${VAR-} after -n and -z in test conditions.

Best Regards

Axel PHILIPP
MTU Aero Engines AG

Von: Janis Papanagnou [mailto:[email protected]]
Gesendet: Mittwoch, 16. Oktober 2013 14:56
An: PHILIPP, Axel, Dr.
Cc: [email protected]
Betreff: RE: [ast-users] Question about the nounset option

I understand the OP considering that clumsy. And something being defined
by POSIX doesn't prevent it being clumsy or not defined in an optimal way;
sometimes even the opposite is true. But I'd rather like to abstain from such
flame-prone valuations and consider the given application.
@Axel: I'm not sure your sample is only test code. If so, you can trigger the
effecte more simply (without if condition) by /bin/ksh -uc 'echo ${TESTVAR}'.
Otherwise I'd use the less clumsy /bin/ksh -uc 'echo ${TESTVAR-"undefined"}
(without if, but still with the default value substitution).
Part of the clumsiness in your sample stems from the explicit if test and the
implicit test in ${v- }; one can be avoided.

Janis

> Date: Wed, 16 Oct 2013 14:23:57 +0200
> From: [email protected]
> To: [email protected]
> CC: [email protected]
> Subject: Re: [ast-users] Question about the nounset option
>
> On 16 October 2013 09:14, PHILIPP, Axel, Dr. <[email protected]> wrote:
> > I wonder whether it is desirable that test -n and test -z trigger the unset 
> > error condition:
> >
> > /bin/ksh -uc 'if [[ -n $TESTVAR ]]; then echo $TESTVAR; else echo 
> > "undefined"; fi'
> > /bin/ksh: TESTVAR: parameter not set
> >
> > I know all shells from the sh family I have tested behave this way, but I 
> > would prefer a solution (additional option or env var) that the error is 
> > not triggered in these test cases.
>
> You can use [[ -v TESTVAR ]] to test whether a variable does exists or
> not. This uses the variables name (TESTVAR) and not the value
> ($TESTVAR).
>
> >
> > The obvious workaround is somewhat clumsy:
> > /bin/ksh -uc 'if [[ -n ${TESTVAR-} ]]; then echo $TESTVAR; else echo 
> > "undefined"; fi'
> > undefined
>
> Why is this clumsy? This is how POSIX sh was designed and everyone
> uses as designed. Why is this a problem?
>
> Lionel
> _______________________________________________
> ast-users mailing list
> [email protected]
> http://lists.research.att.com/mailman/listinfo/ast-users
--
MTU Aero Engines AG
Geschaeftsfuehrung/Board of Management: Egon W. Behle, Vorsitzender/CEO; Dr. 
Rainer Martens, Michael Schreyögg, Dr. Stefan Weingartner, Reiner Winkler
Vorsitzender des Aufsichtsrats/Chairman of the Supervisory Board: Klaus 
Eberhardt
Sitz der Gesellschaft/Registered Office: Muenchen
Handelsregister/Commercial Register: Muenchen HRB 157206

Diese E-Mail sowie ihre Anhaenge enthalten MTU-eigene vertrauliche oder 
rechtlich geschuetzte Informationen.
Wenn Sie nicht der beabsichtigte Empfaenger sind, informieren Sie bitte den 
Absender und loeschen Sie diese
E-Mail sowie die Anhaenge. Das unbefugte Speichern, Kopieren oder Weiterleiten 
ist nicht gestattet.

This e-mail and any attached documents are proprietary to MTU, confidential or 
protected by law.
If you are not the intended recipient, please advise the sender and delete this 
message and its attachments.
Any unauthorised storing, copying or distribution is prohibited.
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to