www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/21/01:17:24

From: pjfarley AT dorsai DOT org (Peter J. Farley III)
Newsgroups: comp.os.msdos.djgpp
Subject: Possible bash bug in "for x in $path/*x*"
Date: Thu, 21 May 1998 01:55:24 GMT
Organization: http://extra.newsguy.com
Lines: 68
Message-ID: <35637b2a.6087655@enews.newsguy.com>
NNTP-Posting-Host: p-504.newsdawg.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi all.  I don't know if this is a bug or a feature, so I'm asking
those in the know to please enlighten me.

The short test shell script listed at the end of this message looks
for all programs in the $PATH that contain the string "awk" somewhere
in the name.  The original shell script was authored by Heiner Steven
<heiner AT darwin DOT noris DOT de>, and does some testing of awk features on the
executables it finds.  On my W95/DJGPP V2.01 system (DOS box), though,
under bash version "GNU bash, version 1.14.7(2) r3.1 w/multibyte
extension", when there is NOT any file in a directory in the $PATH
which matches *awk*, the syntax "for awk in $path/*awk*" sets the
variable $awk to the value of the path element plus "/*awk*".  For
example, if "//C/WINDOWS" is in the $PATH variable, and there are NOT
any files in that directory matching *awk*, then the $awk variable
will be set to a value of "//C/WINDOWS/*awk*".

This isn't a problem until the script reaches the test expression:

	[ `basename "$awk"` = "$PN" ] && continue

When $awk's value is "//C/WINDOWS/*awk*", this is expanded to *every*
filename IN THE CURRENT DIRECTORY which matches *awk*, which causes
the test expression to fail with the message:

./testawk: [: too many arguments

When you invoke the test script with the "-x" argument so all of the
statements and their expansions are displayed, the offending basename
expression has been translated to (for example):

[ trmperf1.awk trmperf2.awk trmperf3.awk wrap.awk wrap2.awk wrap3.awk
wrap4.awk wrap5.awk wrap6.awk wrap7.awk = testawk ]

My system generates a much larger list of names, which I truncated for
the above example, but you get the idea.

Is this behavior of "for awk in $path/*awk*" normal, or is this a bug
in DJGPP bash?

TIA for any help or info you can provide.

==================== Test Script ================================
:
# testawk - test awk features (stv, 3/98)
# usage: testawk
# modified & stripped down to check functioning of
# for awk in $path/*awk* do ... done (pjf3, 5/20/98)

PN=`basename "$0"`                      # Program name

for path in `echo "$PATH" | sed 's|^:|./ |;s|:$| ./|;s|:| |g'`
do
    for awk in $path/*awk*
    do
        case "$awk" in
            *.exe|*.com|*.bat);;            # DOS/Windows
            *.*)    continue;;              # ignore "script.awk"
        esac
        [ `basename "$awk"` = "$PN" ] && continue
        [ -x $awk ] &&
            { echo "Found executable awk: '$awk'"; continue }
        echo "Found non-executable awk: '$awk'"
    done
done
==================== Test Script ================================

----------------------------------------------------
Peter J. Farley III (pjfarley AT dorsai DOT org)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019