<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Problem with the test command in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846840#M274262</link>
    <description>There is a BIG difference between an undefined (never set) variable and one that has a null string as a value. Your script works differently due to the setting of set -u or set +u. I always recommend set -u which means you will get the error: Parameter not set. This is a very good thing because using a variable that was never defined can lead to big mistakes later on. The most common reason for undefined variables is a spelling error (like TEMPDIR versus TMPDIR).&lt;BR /&gt; &lt;BR /&gt;So James's solution is the correct one. You leave set -u in effect to catch errors but use the special shell construct: &lt;BR /&gt; &lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt; &lt;BR /&gt;This handles two very different conditions: TEMPDIR was never defined or given a value, and TEMPDIR="" which has TEMPDIR defined but to a null value. This is one of the best techniques to handle undefined variables without turning on set +u.&lt;BR /&gt; &lt;BR /&gt;Oh, and just to clarify, you no longer need to test the variable with -z because it will always have a value. Try this:&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;unset TEMPDIR&lt;BR /&gt;if [ -z "${TEMPDIR}" ]; then&lt;BR /&gt;TEMPDIR=/tmp; export TEMPDIR&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;and you'll see the error message.&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;TEMPDIR=""&lt;BR /&gt;if [ -z "${TEMPDIR}" ]; then&lt;BR /&gt;TEMPDIR=/tmp; export TEMPDIR&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;Now there is no error and your test will be successful. But use this technique:&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;unset TEMPDIR&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;TEMPDIR=""&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;TEMPDIR=/var/tmp&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;Now you will see that all 3 conditions are handled correctly.</description>
    <pubDate>Sun, 20 Aug 2006 19:27:38 GMT</pubDate>
    <dc:creator>Bill Hassell</dc:creator>
    <dc:date>2006-08-20T19:27:38Z</dc:date>
    <item>
      <title>Problem with the test command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846836#M274258</link>
      <description>Here is a small test example:&lt;BR /&gt;if [ -z "${TEMPDIR}" ]; then&lt;BR /&gt;  TEMPDIR=/tmp; export TEMPDIR&lt;BR /&gt;fi&lt;BR /&gt;~&lt;BR /&gt;&lt;BR /&gt;This runs perfectly fine if run normally but to try and add this to a script and source it, i.e:&lt;BR /&gt;. /tmp/test generates an error.  &lt;BR /&gt;&lt;BR /&gt;sh: TEMPDIR: Parameter not set.&lt;BR /&gt;&lt;BR /&gt;Why does this not work when sourcing the file?&lt;BR /&gt;Thanks.</description>
      <pubDate>Sun, 20 Aug 2006 15:53:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846836#M274258</guid>
      <dc:creator>Radhe Webster</dc:creator>
      <dc:date>2006-08-20T15:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with the test command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846837#M274259</link>
      <description>Shalom,&lt;BR /&gt;&lt;BR /&gt;It would be marginally helpful to know what shell you are using.&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Sun, 20 Aug 2006 15:55:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846837#M274259</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2006-08-20T15:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with the test command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846838#M274260</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Stictly speaking, the '-z' test tests to see if the string is of size zero.  If the string has never been defined, even to an empty string, the shell will report that the parameter isn't set.&lt;BR /&gt;&lt;BR /&gt;An easy, very succinct way to do what you want is:&lt;BR /&gt;&lt;BR /&gt;# export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;&lt;BR /&gt;...now, if the parameter is not set or it is null, then it set it to "/tmp" (or whatever you choose.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sun, 20 Aug 2006 16:14:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846838#M274260</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-08-20T16:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with the test command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846839#M274261</link>
      <description>I'm using HP-UX 11.23 POSIX shell (/sbin/sh).&lt;BR /&gt;&lt;BR /&gt;And i'm not really able to test it like you've specified.  As part of the script, I need to know if the parameter has been set from an environmental variable or not (that is not part of the script).&lt;BR /&gt;&lt;BR /&gt;Like I said it works when calling it normally, ie, (directly fromt the command line)but fails when sourcing it.  Is there a reason for this?</description>
      <pubDate>Sun, 20 Aug 2006 16:42:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846839#M274261</guid>
      <dc:creator>Radhe Webster</dc:creator>
      <dc:date>2006-08-20T16:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with the test command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846840#M274262</link>
      <description>There is a BIG difference between an undefined (never set) variable and one that has a null string as a value. Your script works differently due to the setting of set -u or set +u. I always recommend set -u which means you will get the error: Parameter not set. This is a very good thing because using a variable that was never defined can lead to big mistakes later on. The most common reason for undefined variables is a spelling error (like TEMPDIR versus TMPDIR).&lt;BR /&gt; &lt;BR /&gt;So James's solution is the correct one. You leave set -u in effect to catch errors but use the special shell construct: &lt;BR /&gt; &lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt; &lt;BR /&gt;This handles two very different conditions: TEMPDIR was never defined or given a value, and TEMPDIR="" which has TEMPDIR defined but to a null value. This is one of the best techniques to handle undefined variables without turning on set +u.&lt;BR /&gt; &lt;BR /&gt;Oh, and just to clarify, you no longer need to test the variable with -z because it will always have a value. Try this:&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;unset TEMPDIR&lt;BR /&gt;if [ -z "${TEMPDIR}" ]; then&lt;BR /&gt;TEMPDIR=/tmp; export TEMPDIR&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;and you'll see the error message.&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;TEMPDIR=""&lt;BR /&gt;if [ -z "${TEMPDIR}" ]; then&lt;BR /&gt;TEMPDIR=/tmp; export TEMPDIR&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;Now there is no error and your test will be successful. But use this technique:&lt;BR /&gt; &lt;BR /&gt;set -u&lt;BR /&gt;unset TEMPDIR&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;TEMPDIR=""&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;TEMPDIR=/var/tmp&lt;BR /&gt;export TEMPDIR=${TEMPDIR:=/tmp}&lt;BR /&gt;echo $TEMPDIR&lt;BR /&gt; &lt;BR /&gt;Now you will see that all 3 conditions are handled correctly.</description>
      <pubDate>Sun, 20 Aug 2006 19:27:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/problem-with-the-test-command/m-p/3846840#M274262</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2006-08-20T19:27:38Z</dc:date>
    </item>
  </channel>
</rss>

