1825782 Members
2131 Online
109687 Solutions
New Discussion

test not working

 
SOLVED
Go to solution
Wim Korten
Advisor

test not working

Hi,

For some reason my test for non-zero is not working on $newgrp. any ideas?

unset who
who=`/usr/bin/who am i`

if [ -n "$who" ]
then
if [ -n "$newgrp" ]
then
newgrp $newgrp
fi
fi

The error is :
${HOME:-.}/.profile[35]: newgrp: parameter not set

This is why I had the test in, right? It's quoted, so I don't see the problem?

Regs David
Limited by Technology
11 REPLIES 11
David Burgess
Esteemed Contributor

Re: test not working

What if you do

newgrp=""

first and then try.

#newgrp=""
#if [ -n "$newgrp" ]
>then
>echo yes
>fi

returns nothing

#unset newgrp
#if [ -n "$newgrp" ]
>then
>echo yes
>fi

sh: newgrp: Parameter not set.

newgrp="1"

if [ -n "$newgrp" ]^Jthen^Jecho yes^Jfi
yes

Seems to work.

Regards,

Dave.
Martin Johnson
Honored Contributor

Re: test not working

newgrp is undefined as a variable. Put in the begining of the script:

newgrp=

HTH
Marty
Michael Schulte zur Sur
Honored Contributor

Re: test not working

Hi,

look for set -u
If set, unset variables will result in an error.

Michael
Jeroen Peereboom
Honored Contributor

Re: test not working

Weird,

No typos? strange characters?
First script line is used to spceify shell (#!/bin/ksh or something like that)?

??
JP
Wim Korten
Advisor

Re: test not working

It's a combination of scripts. At the total end I would like to test if $newgrp is set.

So first unsetting would not be really apropriate :)

I'dd say that test with a quoted var would always succeed?

Question is still open.


Regs David
Limited by Technology
Michael Schulte zur Sur
Honored Contributor

Re: test not working

Hi again,

look for set -u
or #!/bin/sh -u
or similar.

Michael
Mark Grant
Honored Contributor

Re: test not working

Martin is right.

The $newgrp variable is not set. the "-n" option of test does not test for the existance of a variable, only that it is not empty.

There may be a smarter way but you need something like

if [ $newgrp ] && [ -n $newgrp ]

However, if newgrp is not defined, this will always be false.
Never preceed any demonstration with anything more predictive than "watch this"
Wim Korten
Advisor

Re: test not working

Hi,

There is no applicable man for set, nor sh that explains the -u flag.

Regs David
Limited by Technology
Sridhar Bhaskarla
Honored Contributor
Solution

Re: test not working

Hi David,

Well. This is because you can find a line "set -u" in your .profile. Irrespective of -n test, 'set -u' going to error out if newgrp is not set. You can override it with

set +u
your_script
set -u

However, you will need to use it at your own risk.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Michael Schulte zur Sur
Honored Contributor

Re: test not working

Hi David,

look man sh-bourne
and search for -u

Michael
Jeroen Peereboom
Honored Contributor

Re: test not working

In the shell man pages:
set -u # treats unset variable as error

Als add a
set -xv
to your script. This will show the script commands as they are being executed.

JP.