bug-gawk
[Top][All Lists]
Advanced

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

Re: lint warning: unnecessary mixing of `>' and `>>'


From: arnold
Subject: Re: lint warning: unnecessary mixing of `>' and `>>'
Date: Tue, 24 Oct 2023 03:01:05 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hi Manuel.

You didn't test it properly:

$ gawk --lint -i openout 'BEGIN{openout("data.txt", "a");  print "xyz" > 
"data.txt"}'
gawk: cmd. line:2: warning: unnecessary mixing of `>' and `>>' for file 
`data.txt'
gawk: warning: no explicit close of file `data.txt' provided

:-)

Arnold

Manuel Collado <mcollado2011@gmail.com> wrote:

> El 23/10/23 a las 13:51, Wolfgang Laun escribió:
> > On Mon, 23 Oct 2023 at 13:38, <arnold@skeeve.com> wrote:
> >>
> >> The awk program should simply print[f] to standard output and the
> >> decision as to > or >> should be made at the shell level.
> >>
> >> So, if the decision between rewrite and append depends on the data
> > (or the date or ...) one should make this in the shell??
> > 
> > Forgive me, but I think that you cannot save awk from the lack of
> > an open with explicit mode and the indistinguishability of print >>x
> > and print >x by rules that must be broken.
> > 
> > Perhaps a really consistent and good style would be to wrap
> > both opens and the write in functions.
>
> What about the following?
>
> pruebas/gawk$ cat openout.awk
> # Open a file for output
> # mode = w[write] or a[ppend]
> function openout(file, mode) {
>      if (mode ~ /^w/) {
>          printf("") > file
>      } else if (mode ~ /^a/) {
>          printf("") >> file
>      }
> }
>
> pruebas/gawk$ cat data.txt
> abc
>
> pruebas/gawk$ gawk -i openout 'BEGIN{openout("data.txt", "a");
>   print "xyz" > "data.txt"}'
>
> pruebas/gawk$ cat data.txt
> abc
> xyz
>
> pruebas/gawk$ gawk -i openout 'BEGIN{openout("data.txt", "w");
>   print "xyz" > "data.txt"}'
>
> pruebas/gawk$ cat data.txt
> xyz
>
> pruebas/gawk$ gawk --lint -i openout 'BEGIN{openout("data.txt", "w");
>   print "xyz" > "data.txt"}'
> gawk: warning: no explicit close of file `data.txt' provided
>
> Now --lint doesn't complain about mixing > and >>. Because the file name 
> in openout() is not given literally, but as a variable, I assume.
>
> HTH.
> -- 
> Manuel Collado - http://mcollado.z15.es





reply via email to

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