[unix-pc.general] YAkshBug

jbm@uncle.UUCP (John B. Milton) (02/02/90)

Well, bug or inconsistancy:

if [ -f * ]; then echo yes; fi

will print "yes" if there are one or more files when using /bin/sh, but
not with ksh. Ksh gives a syntax error.

if sh -c "test -f *"; then echo yes; fi

Does work.

John
-- 
John Bly Milton IV, jbm@uncle.UUCP, n8emr!uncle!jbm@osu-cis.cis.ohio-state.edu
(614) h:252-8544, w:469-1990; N8KSN, AMPR: 44.70.0.52; Don't FLAME, inform!

scs@iti.org (Steve Simmons) (02/03/90)

jbm@uncle.UUCP (John B. Milton) writes:

>Well, bug or inconsistancy:

>if [ -f * ]; then echo yes; fi

>will print "yes" if there are one or more files when using /bin/sh, but
>not with ksh. Ksh gives a syntax error.

>if sh -c "test -f *"; then echo yes; fi

>Does work.

Inconsistancy.  Strictly speaking

	if [ -f a b ] ; then . . .

is incorrect.  The '-f' test takes one and only one arguement.  /bin/sh
silently ignores the extra, while /bin/ksh reports the error.  My source
on this is David Korn.  Interestingly enough, what would

	if [ -f a b -a -f c ] ; then ...

do?  How many arguements does sh ignore, and which ones?  'if [ -f a b ]'
is definately a shell bug waiting to happen, and while it was disturbing
to find the inconsistancy between the two I'd much prefer the error notice.

Steve

kls@ditka.UUCP (Karl Swartz) (02/03/90)

In article <682@uncle.UUCP> jbm@uncle.UUCP (John B. Milton) writes:
>Well, bug or inconsistancy:
>
>if [ -f * ]; then echo yes; fi
>
>will print "yes" if there are one or more files when using /bin/sh, but
>not with ksh. Ksh gives a syntax error.

Bug ... in sh (not ksh).  And perhaps an inconsistancy, in that ksh
does not continue the erroneous behavior.

In fact, your statement isn't quite true, as ksh behaves the same as
sh if there are zero or one files.  It does not give a syntax error
for either of these cases.  And in fact, with suitable choices of
filenames, ksh will not give a syntax error for more than one file:

    kls@ditka $ ls
    -0
    -a
    -f
    1
    kls@ditka $ if [ -f * ]; then echo yes; fi
    yes
    kls@ditka $

Ksh simply expands the * as it's supposed to and substitutes what
it finds.  Since you weren't careful about what you wrote, you most
often ended up feeding test what it saw as garbage; it then justly
registered its indignation.  Why sh doesn't behave the same way is
at present an unresolved puzzle.

Assuming you wish to echo "yes" if and only if there are files in
the directory (ignoring dot files if you aren't root), the following
will do what you want:

    if [ -n "`ls`" ]; then echo yes; fi

-- 
Karl Swartz			 |UUCP	uunet!apple!zygot!ditka!kls
1-408/223-1308			 |INet	zygot!ditka!kls@apple.com
"I never let my schooling get in |BIX	kswartz
the way of my education."(Twain) |Snail	1738 Deer Creek Ct., San Jose CA 95148