<?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: test with -n or -z not working in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859102#M98314</link>
    <description>Peter, your right.  I found the "set -u" command in my .profile.  That was set up that way before my time.  What are the implications of taking that out?  This is the oracle account in case that would make any difference.</description>
    <pubDate>Fri, 08 Sep 2006 06:56:26 GMT</pubDate>
    <dc:creator>Steve Givens</dc:creator>
    <dc:date>2006-09-08T06:56:26Z</dc:date>
    <item>
      <title>test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859097#M98309</link>
      <description>&lt;!--!*#--&gt;I'm porting a script that runs fine on Sun and AIX, however fails on HP with the message:&lt;BR /&gt;&lt;BR /&gt;"sh: 1: Parameter not set."&lt;BR /&gt;&lt;BR /&gt;The code that is failing is:&lt;BR /&gt;if [[ -n ${1} ]] ; &lt;BR /&gt;then&lt;BR /&gt;  ...&lt;BR /&gt;else&lt;BR /&gt;  ...&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;I've replaced the if statement with the following to get it to work:&lt;BR /&gt;&lt;BR /&gt;if [ $# -eq 1 ] ; &lt;BR /&gt;then ...&lt;BR /&gt;&lt;BR /&gt;Can someone explain what's going on?&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;Steve</description>
      <pubDate>Fri, 08 Sep 2006 02:55:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859097#M98309</guid>
      <dc:creator>Steve Givens</dc:creator>
      <dc:date>2006-09-08T02:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859098#M98310</link>
      <description>Steve,&lt;BR /&gt;have you tried:&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;echo $1&lt;BR /&gt;if [[ -n ${1} ]]&lt;BR /&gt;then&lt;BR /&gt;echo string found&lt;BR /&gt;else&lt;BR /&gt;echo emptry string&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Ensure spaces between -n and bracket and } and bracket.&lt;BR /&gt;It will test for the presence of a parameter</description>
      <pubDate>Fri, 08 Sep 2006 03:24:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859098#M98310</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2006-09-08T03:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859099#M98311</link>
      <description>It appears you have used set -u to make all references to undefined variables as errors.  There is nothing under -u that says you can use  [[ -n ]] on them.&lt;BR /&gt;&lt;BR /&gt;So you need to use the following instead:&lt;BR /&gt;   if [[ -n ${1-} ]]; then ...&lt;BR /&gt;&lt;BR /&gt;The "-" says to set it to null if it is not set.</description>
      <pubDate>Fri, 08 Sep 2006 03:26:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859099#M98311</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2006-09-08T03:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859100#M98312</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;the code tested with double brackets worked.&lt;BR /&gt;&amp;gt; cat test-2B.ksh&lt;BR /&gt;if [[ -n ${1} ]]&lt;BR /&gt;then&lt;BR /&gt; echo true&lt;BR /&gt;else&lt;BR /&gt; echo false&lt;BR /&gt;fi&lt;BR /&gt;&amp;gt; sh test-2B.ksh xxx&lt;BR /&gt;true&lt;BR /&gt;&amp;gt; sh test-2B.ksh&lt;BR /&gt;false&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;however if you use only one bracket, it will not.&lt;BR /&gt;&amp;gt; cat test.ksh&lt;BR /&gt;if [ -n ${1} ]&lt;BR /&gt;then&lt;BR /&gt; echo vrai&lt;BR /&gt;else&lt;BR /&gt; echo faux&lt;BR /&gt;fi&lt;BR /&gt;&amp;gt; sh test.ksh xxx&lt;BR /&gt;vrai&lt;BR /&gt;&amp;gt; sh test.ksh&lt;BR /&gt;test.ksh: test: Specify a parameter with this command.&lt;BR /&gt;faux&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&lt;BR /&gt;it is quite simple to understand, when ${1} resolve to nothing ($1 is not set)&lt;BR /&gt;the test is&lt;BR /&gt;if [ -n ]&lt;BR /&gt;and -n argument of test, expect a string.&lt;BR /&gt;&lt;BR /&gt;solution is to quote the string : "${1}"&lt;BR /&gt;&lt;BR /&gt;I can't see why it work on AIX/SUN, which shell are you using ? ksh ? bash ? csh ?&lt;BR /&gt;&lt;BR /&gt;Jean-Yves Picard</description>
      <pubDate>Fri, 08 Sep 2006 03:31:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859100#M98312</guid>
      <dc:creator>Jean-Yves Picard</dc:creator>
      <dc:date>2006-09-08T03:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859101#M98313</link>
      <description>I've tried both sh and ksh.  Same results.  That is also true for using double quotes around ${1}.  I'll try a couple of the other suggestions and get back to you all.&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
      <pubDate>Fri, 08 Sep 2006 06:43:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859101#M98313</guid>
      <dc:creator>Steve Givens</dc:creator>
      <dc:date>2006-09-08T06:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859102#M98314</link>
      <description>Peter, your right.  I found the "set -u" command in my .profile.  That was set up that way before my time.  What are the implications of taking that out?  This is the oracle account in case that would make any difference.</description>
      <pubDate>Fri, 08 Sep 2006 06:56:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859102#M98314</guid>
      <dc:creator>Steve Givens</dc:creator>
      <dc:date>2006-09-08T06:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859103#M98315</link>
      <description>&amp;gt;Peter, you're right. I found the "set -u" command in my .profile.&lt;BR /&gt;&lt;BR /&gt;(I thought I said that.  ;-)&lt;BR /&gt;&lt;BR /&gt;&amp;gt;What are the implications of taking that out?&lt;BR /&gt;&lt;BR /&gt;Basically you don't have checking for poorly written shell scripts.  As I mentioned, you can replace ${1} by ${1-} and you won't get that error.</description>
      <pubDate>Fri, 08 Sep 2006 20:41:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859103#M98315</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2006-09-08T20:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: test with -n or -z not working</title>
      <link>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859104#M98316</link>
      <description>set -u is one of those "oops" preventors. The default is for the shell to ignore undefined variables which could be disastrous in a root script. Consider the following snippet:&lt;BR /&gt; &lt;BR /&gt;TMP=tmp/some_dir&lt;BR /&gt;rm -rf /$TEMP&lt;BR /&gt; &lt;BR /&gt;Because of the spelling error, $TEMP was used in the rm command and with set +u in force, the rm command looks like this:&lt;BR /&gt; &lt;BR /&gt;rm -rf /&lt;BR /&gt; &lt;BR /&gt;Having made a similar (but less disastrous) spelling error that deleted files and directories on more than 200 computers, using set -u is mandatory in all my scripts. Putting it in /etc/profile or .profile is also a good idea.&lt;BR /&gt; &lt;BR /&gt;One of the classic shell tasks is to be able to test 3 separate conditions:&lt;BR /&gt; &lt;BR /&gt;1. not defined at all&lt;BR /&gt;2. defined with a null value&lt;BR /&gt;3. defined with a value&lt;BR /&gt; &lt;BR /&gt;One technique is to assign a special value when the variable is not defined:&lt;BR /&gt; &lt;BR /&gt;VAL=${VAL:-IamNOTdefined}&lt;BR /&gt; &lt;BR /&gt;Now, VAL is either undefined and can be tested with "$VAL" = "IamNOTdefined" or null and can be tested with "$VAL". You can even retain the unset value later in the script like this:&lt;BR /&gt; &lt;BR /&gt;VAL=${VAL:-IamNOTdefined}&lt;BR /&gt; &lt;BR /&gt; ... code ...&lt;BR /&gt; &lt;BR /&gt;[ "$VAL" = "IamNOTdefined" ] &amp;amp;&amp;amp; unset VAL</description>
      <pubDate>Sat, 09 Sep 2006 19:25:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/test-with-n-or-z-not-working/m-p/3859104#M98316</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2006-09-09T19:25:38Z</dc:date>
    </item>
  </channel>
</rss>

