[Top][All Lists]

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

Re: [bug-gawk] Awk gets stucked if no filename

From: Neil R. Ormos
Subject: Re: [bug-gawk] Awk gets stucked if no filename
Date: Wed, 29 Nov 2017 11:59:07 -0600 (CST)

address@hidden wrote:
> Xavier Martinez Serrano <address@hidden> wrote:

>> [...] If I add a pattern code but no
>> filename to parse, awk gets stucked waiting:

>> f=""
>> $ awk '{print("hi")}'  $f
>> ... <neverending wait>

> Thank you for submitting a bug report.

> Your understanding below is incorrect. When no
> filenames are named on the command line, awk
> reads from standard input.  This is documented
> and has been awk's behavior ever since it
> existed.

> You can work around this in your script with
> something like

> awk '{print("hi")}'  $f /dev/null

> Or be more careful to check $f before invoking awk, etc.

As the manual is currently formulated, is it
entirely fair to characterize an empty string,
which has been provided as a command-line
argument, as something other than a filename, so
as to satisfy the condition "when no filenames are
named on the command line"?

Relevant excerpts below.

|> 2.3 Other command line arguments.
|> [...] Any additional arguments on the command line
|> are normally treated as input files to be
|> processed in the order specified.
|> *note Assignment Options::.)  [...]

|> 1.1.6 Shell Quoting Issues
|> [...]  * Null strings are removed when they occur
|> as part of a non-null command-line argument, while
|> explicit null objects are kept.  [...]

Indeed, the results of
  gawk 'BEGIN{print ARGC;}' a
  gawk 'BEGIN{print ARGC;}' ""
are both 2.

|> 7.5.3 Using 'ARGC' and 'ARGV'
|> [...] To eliminate a file from the middle of
|> the list, store the null string ('""') into
|> 'ARGV' in place of the file's name.  As a
|> special feature, 'awk' ignores file names that
|> have been replaced with the null string.
|> Another option is to use the 'delete' statement
|> to remove elements from 'ARGV' (*note
|> Delete::). [...]

While section 7.5.3 describes what happens when an
ARGV element is *replaced* with the null string,
it is silent as to the treatment of ARGV elements
that were the null string _ab initio_, and the
specificity of the phrase "that have been
replaced" suggests a distinction between elements
that become null by replacement and those that
were null originally.

Also, gawk reports at least some command-line
arguments that are not valid filenames with an
error message (e.g., when the file name is too

So it's unclear why a user should expect gawk
silently to ignore an empty-string command-line
argument that would otherwise be treated as an
input filename if the argument were non-empty.

I suggest changing the quoted paragraph in section
7.5.3 to read:

   To eliminate a file from the middle of the
   list, store the null string (`""') into `ARGV'
   in place of the file's name, or use the
   `delete' statement to remove the corresponding
   element from `ARGV'. (*note Delete::). As a
   special feature, when processing ARGV elements
   for use as names of input files, `awk' ignores
   elements that contain only the null string,
   have been deleted, or have a subscript greater
   than ARGC-1.

And changing the second full paragraph in Sec. 2.3
to read:

   All the command-line arguments are made
   available to your 'awk' program in the 'ARGV'
   array (*note Built-in Variables::).
   Command-line options and the program text (if
   present) are omitted from 'ARGV'.  All other
   arguments, including variable assignments, are
   included.  As each element of 'ARGV' is
   processed, 'gawk' sets 'ARGIND' to the index in
   'ARGV' of the current element.  A command-line
   argument that consists only of the null string
   occupies an element in 'ARGV' but is silently

Best regards,

--Neil Ormos

reply via email to

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