YAkshBug

Karl Swartz kls at ditka.UUCP
Sat Feb 3 20:53:20 AEST 1990


In article <682 at uncle.UUCP> jbm at 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 at ditka $ ls
    -0
    -a
    -f
    1
    kls at ditka $ if [ -f * ]; then echo yes; fi
    yes
    kls at 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 at 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



More information about the Unix-pc.general mailing list