<?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: If statement variable not set in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945377#M289422</link>
    <description>hi Adam:&lt;BR /&gt;&lt;BR /&gt;The 'grep' filter will return zero (0) if there are matches; one (1) if not.  Hence:&lt;BR /&gt;&lt;BR /&gt;# grep local /etc/hosts &amp;gt; /dev/null || echo "no matches!"&lt;BR /&gt;&lt;BR /&gt;...is a terse way to meet your goal.&lt;BR /&gt;&lt;BR /&gt;You  can also count and return the number of matches with the '-c' switch.  See the manpages for more information.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Thu, 15 Feb 2007 11:33:15 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2007-02-15T11:33:15Z</dc:date>
    <item>
      <title>If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945371#M289416</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;If I want to do a test on a variable that may or may not be set is this possible.&lt;BR /&gt;&lt;BR /&gt;I can't for example do. &lt;BR /&gt;&lt;BR /&gt;grep noddy /etc/hosts&lt;BR /&gt;&lt;BR /&gt;if [ $noddy = " " ];then &lt;BR /&gt;do whatever&lt;BR /&gt;else&lt;BR /&gt;do whatever&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Is there anyway to do a test on just the variable being set?</description>
      <pubDate>Thu, 15 Feb 2007 10:01:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945371#M289416</guid>
      <dc:creator>Adam Noble</dc:creator>
      <dc:date>2007-02-15T10:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945372#M289417</link>
      <description>Adam,&lt;BR /&gt;can't you just check the return code ($?), if the grep can't find, it is not 0.&lt;BR /&gt;&lt;BR /&gt;Otherwise:&lt;BR /&gt;noddy=`grep noddy /etc/hosts`&lt;BR /&gt;if [ -z "$noddy" ]  check for 0 length&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Feb 2007 10:12:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945372#M289417</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2007-02-15T10:12:54Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945373#M289418</link>
      <description>&lt;BR /&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;like this:&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;set | grep VARIABLE_NAME&lt;BR /&gt;&lt;BR /&gt;if [ $? = 0 ]&lt;BR /&gt;then&lt;BR /&gt;echo $VARIABLE_NAME&lt;BR /&gt;else&lt;BR /&gt;'do that'&lt;BR /&gt;fi&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Feb 2007 10:13:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945373#M289418</guid>
      <dc:creator>Dino_4</dc:creator>
      <dc:date>2007-02-15T10:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945374#M289419</link>
      <description>of course I'm being dim I'll just test on the exit code</description>
      <pubDate>Thu, 15 Feb 2007 10:15:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945374#M289419</guid>
      <dc:creator>Adam Noble</dc:creator>
      <dc:date>2007-02-15T10:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945375#M289420</link>
      <description>or you can check on the variable&lt;BR /&gt;&lt;BR /&gt;if [[ -z $STRING ]] - check if string has zero length&lt;BR /&gt;if [[ -n $STRING ]] - check if string non zero length</description>
      <pubDate>Thu, 15 Feb 2007 10:48:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945375#M289420</guid>
      <dc:creator>gstonian</dc:creator>
      <dc:date>2007-02-15T10:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945376#M289421</link>
      <description>or you can check on the variable if you are setting it.&lt;BR /&gt;&lt;BR /&gt;if [[ -z $STRING ]] - check if string has zero length&lt;BR /&gt;if [[ -n $STRING ]] - check if string non zero length</description>
      <pubDate>Thu, 15 Feb 2007 10:49:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945376#M289421</guid>
      <dc:creator>gstonian</dc:creator>
      <dc:date>2007-02-15T10:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945377#M289422</link>
      <description>hi Adam:&lt;BR /&gt;&lt;BR /&gt;The 'grep' filter will return zero (0) if there are matches; one (1) if not.  Hence:&lt;BR /&gt;&lt;BR /&gt;# grep local /etc/hosts &amp;gt; /dev/null || echo "no matches!"&lt;BR /&gt;&lt;BR /&gt;...is a terse way to meet your goal.&lt;BR /&gt;&lt;BR /&gt;You  can also count and return the number of matches with the '-c' switch.  See the manpages for more information.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 15 Feb 2007 11:33:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945377#M289422</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-02-15T11:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945378#M289423</link>
      <description>You need to be careful with some of the replies.&lt;BR /&gt;If your string is empty, you need to quote it:&lt;BR /&gt;if [ "$noddy" = "" ];then&lt;BR /&gt;&lt;BR /&gt;(It appears this isn't needed in [[ ]].)&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Dino: set | grep VARIABLE_NAME&lt;BR /&gt;if [ $? = 0 ]&lt;BR /&gt;&lt;BR /&gt;This should really be: (James' can be simplified with -q too.)&lt;BR /&gt;set | grep -q VARIABLE_NAME&lt;BR /&gt;if [ $? -eq 0 ]&lt;BR /&gt;&lt;BR /&gt;("=" is for string comparisons, -eq for ints.)&lt;BR /&gt;&lt;BR /&gt;Another iffy thing with uninitialized strings is when you use -u in your scripts:&lt;BR /&gt;#!/usr/bin/ksh -u&lt;BR /&gt;(To check for uninitialized shell variables.)&lt;BR /&gt;&lt;BR /&gt;To fix gstonian's case:&lt;BR /&gt;if [[ -z ${STRING-} ]] ; then  #check if string has zero length or not init&lt;BR /&gt;&lt;BR /&gt;This may be an answer to your original question.  You could do:&lt;BR /&gt;&lt;BR /&gt;if [ "${STRING-NOTSET}" = "NOTSET" ]&lt;BR /&gt;&lt;BR /&gt;And to make sure you are aware of if, if you are comparing two strings where it can start with a "-", you need this trick:&lt;BR /&gt;&lt;BR /&gt;if [ X"${STRING-NOTSET}" = X"NOTSET" ]&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Feb 2007 14:34:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945378#M289423</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-02-15T14:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945379#M289424</link>
      <description>One of the best reliability tools is to write scripts that stop before using unset (or more likely, misspelled) variables. This could prevent the accidental remove of an entire directory's contents rather than a set of files as an example. To do this, you code set -u in front of all scripts.&lt;BR /&gt; &lt;BR /&gt;But this brings to light your test requirement: how do you test for an unset variable? The easiest method (and still keep set -u enabled) is to assign a special value like this:&lt;BR /&gt; &lt;BR /&gt;UNSET=IamNOTset&lt;BR /&gt;MYVAR=${MYVAR:-$UNSET}&lt;BR /&gt; &lt;BR /&gt;Now if MYVAR has been defined (ie, appears on the left of = someplace) then MYVAR is unchanged. But if MYVAR was never defined, it will be assigned the contents of UNSET. You can use any unique string but the above IamNOTset works well. Now the following test will always succeed, even with set -u:&lt;BR /&gt; &lt;BR /&gt;if [ "$MYVAR" = "" ]&lt;BR /&gt;then&lt;BR /&gt; echo "MYVAR is null"&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;NOTE: There is a big difference between being unset (never defined) and being null. Assigning any value including nothing defines the variable. &lt;BR /&gt; &lt;BR /&gt;Another advantage to using a common UNSET value is that you can return the variable ot it's original value, including unset:&lt;BR /&gt; &lt;BR /&gt;if [ $MYVAR = $UNSET ]&lt;BR /&gt;then&lt;BR /&gt; unset MYVAR&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;In your example, noddy is always defined so it will succeed with:&lt;BR /&gt; &lt;BR /&gt;if [ "$noddy" = "" ]&lt;BR /&gt; &lt;BR /&gt;With no result from grep, the if statement will look like this:&lt;BR /&gt; &lt;BR /&gt;if [ "" = "" ]&lt;BR /&gt; &lt;BR /&gt;which is a valid comparison. But as mentioned before, grep -c may be a better choice. For instance, there may be 0, 1, 2 or more in which each value is useful.</description>
      <pubDate>Thu, 15 Feb 2007 15:23:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945379#M289424</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-02-15T15:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945380#M289425</link>
      <description>&lt;!--!*#--&gt;Use the shell built-ins for parameter expansion i.e.&lt;BR /&gt;&lt;BR /&gt;if [ ${noddy:-} ]; then&lt;BR /&gt;   echo "noddy is set to $noddy"&lt;BR /&gt;else&lt;BR /&gt;   echo "noddy is unset"&lt;BR /&gt;fi</description>
      <pubDate>Thu, 15 Feb 2007 15:59:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945380#M289425</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-02-15T15:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: If statement variable not set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945381#M289426</link>
      <description>Thanks all your assistance is much appreciated and a great help</description>
      <pubDate>Fri, 16 Feb 2007 04:36:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/if-statement-variable-not-set/m-p/3945381#M289426</guid>
      <dc:creator>Adam Noble</dc:creator>
      <dc:date>2007-02-16T04:36:40Z</dc:date>
    </item>
  </channel>
</rss>

