"test" and "if (-n)" in Csh

"test" and "if (-n)" in Csh

Post by FACELIF » Wed, 13 May 1998 04:00:00



I found some kind of undocumented bug while using the test construct in
Csh to test for the length of a string.

if  (-n  "<string>" ) then
  A
else B
endif

At the cshell prompt,  using   "if  (-n "xxx") then......  always gives
a "badly formed number" error and fails.    However using the
contrasting  construct,  "-z", to test for the emptiness of a string
works fine:

 if  (-z "<string>") then
  B
else A
endif

From the cshell prompt,  "if (-z "xxx") then....   gives an if-shell
prompt , "if? ".

However, still, the command "test"  still works fine with the -n option.

Anyone use the if (-n ....) construct??

Thanks in advance for any response.

-Marc U,
MITRE Corp

 
 
 

"test" and "if (-n)" in Csh

Post by Christian Bauernfei » Wed, 13 May 1998 04:00:00




Quote:> I found some kind of undocumented bug while using the test construct in
> Csh to test for the length of a string.

Now if it were documented, it would be a feature...
As it turns out, it is neither. csh if has different operators than
/bin/test or ksh.

Quote:> if  (-n  "<string>" ) then
>   A
> else B
> endif

> At the cshell prompt,  using   "if  (-n "xxx") then......  always gives
> a "badly formed number" error and fails.    However using the

A strange error message, but not a bug: csh if does not support string
tests. Check the man page.

Quote:> contrasting  construct,  "-z", to test for the emptiness of a string
> works fine:

>  if  (-z "<string>") then
>   B
> else A
> endif

> From the cshell prompt,  "if (-z "xxx") then....   gives an if-shell
> prompt , "if? ".

-z tests for a empty file. Check the man page.
Non-existing files apparently count.

Quote:> However, still, the command "test"  still works fine with the -n option.

Because test is a separate command with a separate set of operators.

Quote:

> Anyone use the if (-n ....) construct??

No, because it doesn't exist.

Try using /bin/test or ksh.

Christian
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM