On 16.08.2022 17:38, Helmut Waitzmann wrote:
> Oğuz <
oguzism...@gmail.com>:
>> On 8/14/22 2:40 PM, Janis Papanagnou wrote:
>>> Occasionally it happens that I want to redirect output to a file, but
>>> create that file or overwrite an existing file only if there's
>>> actually new data available. (So I don't want to and cannot use '>'
>>> or '>>'.)
>>>
>>> Since I haven't seen that feature in shell I usually use awk to
>>> achieve that function by something like
>>>
>>> ... | awk -v fn="some-file" '{ print > fn }'
>>
>> Does this have any advantage over using a temporary file?
I haven't pondered about advantages or disadvantages of the one or
the other option. Here are just a few obvious thoughts I have...
It doesn't create a temporary file. - An advantage for itself; since
it happens - as we see in your test/mv based code below demonstrated -
that you forget to clean up that temporary file. (So it's getting yet
more complex than the workaround already is if you want to maintain a
tidy runtime environment.)
It's also less complex; an additional test and a conditional command.
The awk pattern doesn't create the file in the first place, if it's
not needed.
Also if you're at the system disk memory limit your code will create
an additional file requiring (if only temporary) additional space
that the system might not be able to allocate.
If you dislike awk you can of course put your test/mv commands set
in a file 'cf' (or whatever you call it) and it behaves (mostly) the
same. The main point of my post was to have a compact pattern of a
command I find useful, and with the tee-extension I also suggested,
I can also use it in a pipe. The other point of my post was whether
that semantics is already present in shell in some way I missed, or
in some tools; I wouldn't want to use my own tool if there's already
some standard tool supporting it.
>
> It depends. [...]
> [ considerations about access modes ]
Janis