detecting difference between unset variable and variable whose value is null in Korn Shell

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Jeffrey Ladouceu » Thu, 28 Dec 2000 12:56:28



I'm writing a Korn Shell.

 I want to assign default values to environment variables only if they
 are
 not already set.  A null string value is valid.

 ex.

 Before invoking script.

 export str1="hello"
 export str2=
 unset str3

 Now inside my script I want the following logic, (pseudo code)

 if ( $str1 doesn't exist )
   export str1="str1 set to default"
 fi
 if ( $str2 doesn't exist )
   export str3="str2 set to default"
 fi
 if ( $str3 doesn't exist )
   export str3="str3 set to default"
 fi

 print "str1= $str1"
 print "str2= $str2"
 print "str3= $str3"

 I should see the following on the screen:

 str1= hello
str2=
str3= str3 set to default

 My problem is that I don't know how to detect the difference between an
 unset variable and a variable whose value is null.
 ( i.e. str2 and str3 ).

 Does anybody know how?

 Thanks in advance.

 Jeff

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Michael Wa » Thu, 28 Dec 2000 21:23:52


The answer is in THE Korn shell book by Korn page 185-186.

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Jeffrey Ladouceu » Thu, 28 Dec 2000 22:27:10


I just want to clarify your reference:

Are you referring to:

'The Korn Shell: Linux and Unix Programming Manual"
by Anatole Olczak  ?

Thanks
Jeff


Quote:> The answer is in THE Korn shell book by Korn page 185-186.

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Dan Merc » Thu, 28 Dec 2000 23:03:26




> I'm writing a Korn Shell.

>  I want to assign default values to environment variables only if they
>  are
>  not already set.  A null string value is valid.

>  ex.

DELETIA

Quote:

>  Does anybody know how?

>  Thanks in advance.

>  Jeff

This can be done using the null operator (nop) ":" and variable substitution
operators:

   $ unset X
   $ echo "$X"

   $ : ${X=y}
   $ echo "$X"
   y
   $ X=""
   $ : ${X=y}
   $ echo "$X"

   $

Now,  if you want to set the variable if it is null or not set,  you
prepend the "=" with a ":"

   $ X=""
   $ : ${X:=y}
   $ echo "$X"
   y

You can set up multiple default values in the same statement:

   : ${X=y} ${W=z} ${Y=w}

You don't have to use the null operator,  any command will work:

   $ unset X
   $ if [[ ${X=y} = y ]];then echo "ok: $X";else echo bad;fi
   ok: y
   $ echo $X
   y

For more information,  consult the "Parameter Substitution"
section of the man page.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Eric Amic » Thu, 28 Dec 2000 23:45:22



> I just want to clarify your reference:

> Are you referring to:

> 'The Korn Shell: Linux and Unix Programming Manual"
> by Anatole Olczak  ?

I have no idea, but any half-decent man page will have it as well.  You
don't need an explicit if test; just do something like

"${str1='default value'}"

the first time you use the parameter's value in the script.  You do
realize any changes to the variables will go away once the script exits,
don't you?

--
Eric Amick
Columbia, MD

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by <scott.walkins.. » Sat, 30 Dec 2000 08:27:02


what's wrong with:
export VAR1="${VAR1:=value}"




> > I'm writing a Korn Shell.

> >  I want to assign default values to environment variables only if they
> >  are
> >  not already set.  A null string value is valid.

> >  ex.

> DELETIA

> >  Does anybody know how?

> >  Thanks in advance.

> >  Jeff

> This can be done using the null operator (nop) ":" and variable
substitution
> operators:

>    $ unset X
>    $ echo "$X"

>    $ : ${X=y}
>    $ echo "$X"
>    y
>    $ X=""
>    $ : ${X=y}
>    $ echo "$X"

>    $

> Now,  if you want to set the variable if it is null or not set,  you
> prepend the "=" with a ":"

>    $ X=""
>    $ : ${X:=y}
>    $ echo "$X"
>    y

> You can set up multiple default values in the same statement:

>    : ${X=y} ${W=z} ${Y=w}

> You don't have to use the null operator,  any command will work:

>    $ unset X
>    $ if [[ ${X=y} = y ]];then echo "ok: $X";else echo bad;fi
>    ok: y
>    $ echo $X
>    y

> For more information,  consult the "Parameter Substitution"
> section of the man page.

> --
> Dan Mercer

> Opinions expressed herein are my own and may not represent those of my
employer.

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Matthew Land » Sat, 30 Dec 2000 00:25:13



> what's wrong with:
> export VAR1="${VAR1:=value}"

This doesn't do what is intended.  He wants to tell if it is unset.  Null
is NOT unset.  The export above will set VAR1 even if it is set but
set to a null value.

The only way I could think of checking if a var is set, including set
to null, was using the "set" command and awk.

if [[ -n $(set |awk -F"=" '/^VAR1$/') ]]
then
  echo VAR1 is set
else
  echo VAR1 is NOT set
fi

 - Matt
--
_______________________________________________________________________

   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by <scott.walkins.. » Sat, 30 Dec 2000 10:57:58


Matt,

I don't know what version of the korn shell you are using or on what
flavour, but I can assure you that on SVR4 systems this detects unset as
well as null variables. I have just tried the following on an M class HP-UX
box running unix 11, and on a box running intel redhat 6.2. I substituted
#!/bin/ksh for #!/bin/bash on the linux box:
#!/bin/ksh

echo "${SCOTT}test"  # to show unset in first place
export SCOTT="${SCOTT:=SUBSTITUTEDa}"
echo "$SCOTT"
SCOTT=
export SCOTT="${SCOTT:=SUBSTITUTEDb}"
echo "$SCOTT"
SCOTT=""
export SCOTT="${SCOTT:=SUBSTITUTEDc}"
echo "$SCOTT"
unset SCOTT
export SCOTT="${SCOTT:=SUBSTITUTEDd}"
echo "$SCOTT"
SCOTT="NOT_SUBSTITUTED"
export SCOTT="${SCOTT:=SUBSTITUTEDd}"
echo "$SCOTT"

exit 0

The output was:
test
SUBSTITUTEDa
SUBSTITUTEDb
SUBSTITUTEDc
SUBSTITUTEDd
NOT_SUBSTITUTED

(The line "SCOTT=" assigns a null value to the variable...

S.



> > what's wrong with:
> > export VAR1="${VAR1:=value}"

> This doesn't do what is intended.  He wants to tell if it is unset.  Null
> is NOT unset.  The export above will set VAR1 even if it is set but
> set to a null value.

> The only way I could think of checking if a var is set, including set
> to null, was using the "set" command and awk.

> if [[ -n $(set |awk -F"=" '/^VAR1$/') ]]
> then
>   echo VAR1 is set
> else
>   echo VAR1 is NOT set
> fi

>  - Matt
> --
> _______________________________________________________________________

>    << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Eric Amic » Sat, 30 Dec 2000 03:21:56



> The only way I could think of checking if a var is set, including set
> to null, was using the "set" command and awk.

That's overkill.  Something like

if [[ "${var1+XYZ}" -eq XYZ ]]; then
echo var1 is set
else
echo var1 is unset
fi

will do the trick.

--
Eric Amick
Columbia, MD

 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by Dan Merc » Sat, 30 Dec 2000 03:23:51





>> what's wrong with:
>> export VAR1="${VAR1:=value}"

> This doesn't do what is intended.  He wants to tell if it is unset.  Null
> is NOT unset.  The export above will set VAR1 even if it is set but
> set to a null value.

If you would read the parameter substitution section of the man page you
would find that:

   : ${vname=alternate}

sets vname to alternate if and only if vname is unset,  while

   : ${vname:=alternate}

sets vname to alternate if vname is unset or vname is set to a null string.

--
Dan Mercer

Quote:> The only way I could think of checking if a var is set, including set
> to null, was using the "set" command and awk.

> if [[ -n $(set |awk -F"=" '/^VAR1$/') ]]
> then
>   echo VAR1 is set
> else
>   echo VAR1 is NOT set
> fi

>  - Matt

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

detecting difference between unset variable and variable whose value is null in Korn Shell

Post by <scott.walkins.. » Sat, 30 Dec 2000 11:32:41


thanks Dan - perhaps the colon before the equals wasn't to visible..

windows for you :-)

> Matt,

> I don't know what version of the korn shell you are using or on what
> flavour, but I can assure you that on SVR4 systems this detects unset as
> well as null variables. I have just tried the following on an M class
HP-UX
> box running unix 11, and on a box running intel redhat 6.2. I substituted
> #!/bin/ksh for #!/bin/bash on the linux box:
> #!/bin/ksh

> echo "${SCOTT}test"  # to show unset in first place
> export SCOTT="${SCOTT:=SUBSTITUTEDa}"
> echo "$SCOTT"
> SCOTT=
> export SCOTT="${SCOTT:=SUBSTITUTEDb}"
> echo "$SCOTT"
> SCOTT=""
> export SCOTT="${SCOTT:=SUBSTITUTEDc}"
> echo "$SCOTT"
> unset SCOTT
> export SCOTT="${SCOTT:=SUBSTITUTEDd}"
> echo "$SCOTT"
> SCOTT="NOT_SUBSTITUTED"
> export SCOTT="${SCOTT:=SUBSTITUTEDd}"
> echo "$SCOTT"

> exit 0

> The output was:
> test
> SUBSTITUTEDa
> SUBSTITUTEDb
> SUBSTITUTEDc
> SUBSTITUTEDd
> NOT_SUBSTITUTED

> (The line "SCOTT=" assigns a null value to the variable...

> S.




> > > what's wrong with:
> > > export VAR1="${VAR1:=value}"

> > This doesn't do what is intended.  He wants to tell if it is unset.
Null
> > is NOT unset.  The export above will set VAR1 even if it is set but
> > set to a null value.

> > The only way I could think of checking if a var is set, including set
> > to null, was using the "set" command and awk.

> > if [[ -n $(set |awk -F"=" '/^VAR1$/') ]]
> > then
> >   echo VAR1 is set
> > else
> >   echo VAR1 is NOT set
> > fi

> >  - Matt
> > --
> > _______________________________________________________________________

> >    << Comments, views, and opinions are mine alone, not IBM's. >>