bug-sh-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with test -d


From: Bob Proulx
Subject: Re: Problem with test -d
Date: Fri, 31 May 2002 15:32:53 -0600
User-agent: Mutt/1.3.28i

Thanks for your report.  There does indeed seem to be a problem on
HP-UX.  I am filing a defect against it.

> ENTW-HP: /home/user/work # test -d man???
> ENTW-HP: /home/user/work # echo $?
> 0
> ENTW-HP: /home/user/work # echo $USERDIR/work/man???
> /home/user/work/man000 /home/user/work/man001 /home/user/work/man002 
> /home/user/

What shell are you using?  'test' is a builtin to the shell.  On HP-UX
this might be /bin/sh, /bin/ksh, /bin/csh, etc.

  type test
  test is a shell builtin

> On Linux:
> 
> address@hidden /home]# echo $USERDIR/work/man???
> /home/work/man100 /home/work/man101 /home/work/man102
> address@hidden /home]# test -d $USERDIR/work/man???
> test: too many arguments

I was able to recreate your problem on hpux.

On HP-UX:
  sh -c 'test -d . one two three;echo $?'
  0

But IMNHO that is incorrect behavior.  The GNU utilities and probably
the bash shell(?) are implementing correct behavior.  Those extra
arguments should not be there.  This is apparently covering up a bug
in the shell script.

> address@hidden bin]# ./test --version
> test (GNU sh-utils) 2.0

> address@hidden bin]# ./test -d $USERDIR/work/man???
> ./test: zuviele Argumente

The GNU sh-utils version of test.

> address@hidden bin]# test -d $USERDIR/work/man???
> test: too many arguments

The shell builtin version of test.  At a guess this is apparently an
older version without locale support.

> Any chance to get returncode 0 ?

I will argue against it since it hides a shell code programming bug.
It is much better to fix the script code not to have that bug instead.
As documented the test -d option takes a single name of a file and
your example is handing it more than one.  There is an ambiguity if
you allow sometimes ignored arguments.

Suggestions:

If you want to test each filename in turn from the globbing you should
use a for loop.

  for file in $USERDIR/work/man???; do
    if [ -d "$file" ]; then
      echo $file is a directory
    else
      echo $file is not a directory
    fi
  done

If you only care about the first file name then only test the first one.

  for file in $USERDIR/work/man???; do
    if [ -d "$file" ]; then
      echo $file is a directory
    fi
    break
  done

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]