Subject: Re: bug#12336: test bug

From: Bob Proulx <[email-masked]>
Date: Tue, 4 Sep 2012 18:18:20 -0600

Please keep the mailing list in the reply.  I have set Reply-To accordingly.

Bala Murugan wrote:
> I am using bash shell. When I tried to do this i getting this.I am getting
> this error.
> 
> [balamup2@cl-flor-dvvm026 ~]$ echo $SHELL
> */bin/bash*

I am sure that those '*' characters are not there.  You must have
added them for emphasis.

> [balamup2@cl-flor-dvvm026 ~]$ ls
> *test1.txt  test3.txt  testdir  test.txt*

So you have three files that match test*.txt in that directory.

> [balamup2@cl-flor-dvvm026 ~]$ test -f test*.txt
> *-bash: test: too many arguments*

That is correct.  You are passing in too many arguments.  The test -f
operator requires one argument.  You have passed *three* arguments.
That is two too many.

You can verify this by using 'echo'.  Try this:

  $ echo test -f test*.txt

Using that you will see that the test*.txt is matching and being
expanded by the shell into three arguments.

It is almost always incorrect to pass a '*' or other shell file glob
metacharacter unquoted on the command line to test.  Your usage is
very problematic.  Instead you may want to loop over the files.

  for file in test*.txt; do
    test -f "$file" && echo file: "$file" || echo nofile: "$file"
  done

> But my friend is using ksh shell. He is fine with that command .

But does your friend have three files that match that file glob in his
directory?  Have you friend do this:

  $ touch test1.txt test2.txt test3.txt
  $ echo test -f test*.txt
  $ test -f test*.txt

By creating the extra files with touch that should ensure that
multiple files are matched.

> I will past this O/P also.
> 
> $ echo $SHELL
> */usr/bin/ksh*
> 
> $ ls
> *123.txt  a1.txt  abc.txt  a.txt  dead.letter  nohup.out  ww1.txt*
> 
> $ test -f a*.txt
> $ echo $?
> *0*

I cannot reproduce that result.  I tried this:

  $ mkdir /tmp/testdir
  $ cd /tmp/testdir
  /testdir$ touch 123.txt a1.txt abc.txt a.txt dead.letter nohup.out ww1.txt
  /testdir$ ls a*.txt
  a.txt  a1.txt  abc.txt
  /testdir$ test -f a*.txt
  bash: test: too many arguments

That is correct behavior because a*.txt matches several files.

> Please take a deep look and resolve this problem

I am sorry but I think this is simply incorrect usage.

Bob