I run a script with the nounset (-u) option if I suspect that an unset variable
may be used. The typical way to trap such a situation is to use a construct
like 'if [[ -n $VAR ]]; then ...', but I do not want to find all those traps
when I use the -u option.
I do not believe that I am the only one who does not use ${VAR-} which means
'if VAR is unset use an empty string instead'. But I would have to change all
occurrences to be able to use the -u option for debugging purposes.
I do not want to use [[ -v TESTVAR ]] because it is a ksh93 only extension.
Mit freundlichen Gruessen / Best Regards
Axel PHILIPP
MTU Aero Engines AG
mailto:[email protected]
-----Ursprüngliche Nachricht-----
Von: Lionel Cons [mailto:[email protected]]
Gesendet: Mittwoch, 16. Oktober 2013 14:24
An: PHILIPP, Axel, Dr.
Cc: [email protected]
Betreff: 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
--
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