Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

The most important early product on the way to developing a good product is an imperfect version.


devel / comp.unix.shell / Musings about redirection and (only) new information overwriting existing file

SubjectAuthor
* Musings about redirection and (only) new information overwritingJanis Papanagnou
`* Musings about redirection and (only) new information overwritingOğuz
 `* Musings about redirection and (only) new information overwriting existing fileHelmut Waitzmann
  `* Musings about redirection and (only) new information overwritingJanis Papanagnou
   `- Musings about redirection and (only) new information overwriting existing fileHelmut Waitzmann

1
Musings about redirection and (only) new information overwriting existing file

<tdamvd$34rnd$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6632&group=comp.unix.shell#6632

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Musings about redirection and (only) new information overwriting
existing file
Date: Sun, 14 Aug 2022 13:40:29 +0200
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <tdamvd$34rnd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 14 Aug 2022 11:40:29 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="6621d38eb9f68c6abf91d6bb9a469ca8";
logging-data="3305197"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uIcxcDC77s3zC1wlaZvca"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:vmNtSEgnVFRUICEmLF4nq8fH700=
X-Enigmail-Draft-Status: N1110
X-Mozilla-News-Host: news://news.eternal-september.org:119
 by: Janis Papanagnou - Sun, 14 Aug 2022 11:40 UTC

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 }'

I used that pattern already a couple of times so that I now have it in a
local bin-directory as executable file 'cf'

# cf - conditionally create file
awk -v fn="${1:?}" '{ print > fn }'

using it in contexts like

news-process | cf latest-news

where the latest-news file gets not overwritten if there's no newer news.

Redirecting with '>' to a file in shell will always overwrite the file,
unless 'noclobber' shell option is set; in that case you need '>|' to
overwrite an existing file.

In ksh there's also the '>;' redirection available. It writes output to
a temporary file and creates/overwrites the file only if no error occurs.

The tee(1) command has also no option to create or overwrite files only
conditionally if data is present, as far as I can see.

Is there some tool or shell function that I missed that does conditional
overwrites as described?

To reproduce standard input to standard output as well I added option -t
so that I can see what's getting written to that file (or to process any
output further in a pipeline).

#!/bin/ksh
#
# cf - conditionally create file if data is present

t=0
while getopts ":t" opt
do
case ${opt} in
(t) t=1 ;;
(\?) printf "Usage: cf [-t] filename\n" ; exit 1 ;;
esac
done
shift OPTIND-1
fn=${1:?}
awk -v fn="${fn}" -v t="${t}" '{ print > fn } t'

I can use that function in my environment but I think it would better
fit in shell, maybe as a new shell redirection ('>@', like ksh's '>;'),
or have it as feature of existing standard tools, maybe in tee(1) with
a new option supporting this semantics, like the -a is used to support
'>>'.

Janis

Re: Musings about redirection and (only) new information overwriting existing file

<tdfbba$ti$1@oguzismail.eternal-september.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6633&group=comp.unix.shell#6633

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!oguzismail.eternal-september.org!.POSTED!not-for-mail
From: oguzismailuysal@gmail.com (Oğuz)
Newsgroups: comp.unix.shell
Subject: Re: Musings about redirection and (only) new information overwriting
existing file
Date: Tue, 16 Aug 2022 08:52:41 +0300
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <tdfbba$ti$1@oguzismail.eternal-september.org>
References: <tdamvd$34rnd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Aug 2022 05:52:42 -0000 (UTC)
Injection-Info: oguzismail.eternal-september.org; posting-host="2ed14b32b489865efcec06e4ea9d4c81";
logging-data="946"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mr0ePQt0Wip6mF7GluYtLCr6he1f4TMc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:XKtbT9XQYUZJSMzQT5JUOeSOBeA=
Content-Language: en-US
In-Reply-To: <tdamvd$34rnd$1@dont-email.me>
 by: Oğuz - Tue, 16 Aug 2022 05:52 UTC

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? Like:

... > temp-file
test -s temp-file && mv temp-file some-file

Re: Musings about redirection and (only) new information overwriting existing file

<83o7wkp5b8.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6638&group=comp.unix.shell#6638

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!scIElgHJAN9TXI7f0CAVhQ.user.46.165.242.75.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: Musings about redirection and (only) new information overwriting existing file
Date: Tue, 16 Aug 2022 17:38:03 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83o7wkp5b8.fsf@helmutwaitzmann.news.arcor.de>
References: <tdamvd$34rnd$1@dont-email.me>
<tdfbba$ti$1@oguzismail.eternal-september.org>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="46607"; posting-host="scIElgHJAN9TXI7f0CAVhQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Copies-To: nobody
Cancel-Lock: sha1:L/05fycA9WtfdgPPWV70Z8bl4hQ=
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
 by: Helmut Waitzmann - Tue, 16 Aug 2022 15:38 UTC

Oğuz <oguzismailuysal@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?
>

It depends.  While

> ... > temp-file
> test -s temp-file && mv temp-file some-file

copies the access permissions from “temp-file” to “some-file” and
replaces “some-file” if it already exists,

>> ... | awk -v fn="some-file" '{ print > fn }'

as well as

... > temp-file &&
if test -s temp-file
then
cat -- temp-file >| some-file &&
rm -f -- temp-file
fi

will overwrite “some-file” rather than replace it.  Also, if
“some-file” doesn't already exist, it will be just created rather
than being created and having the access modes modified according to
the access modes of “temp-file”.

Re: Musings about redirection and (only) new information overwriting existing file

<tdihsl$e7ae$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6649&group=comp.unix.shell#6649

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Musings about redirection and (only) new information overwriting
existing file
Date: Wed, 17 Aug 2022 13:02:45 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <tdihsl$e7ae$1@dont-email.me>
References: <tdamvd$34rnd$1@dont-email.me>
<tdfbba$ti$1@oguzismail.eternal-september.org>
<83o7wkp5b8.fsf@helmutwaitzmann.news.arcor.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Aug 2022 11:02:45 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="9c1fe1a1415706e0244133710f4c94eb";
logging-data="466254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WNLeVpp1whFXKO3IiU7yx"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:vlYrAdipSqYZWDNynCwE0/5LPFQ=
In-Reply-To: <83o7wkp5b8.fsf@helmutwaitzmann.news.arcor.de>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Wed, 17 Aug 2022 11:02 UTC

On 16.08.2022 17:38, Helmut Waitzmann wrote:
> Oğuz <oguzismailuysal@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

Re: Musings about redirection and (only) new information overwriting existing file

<83tu6an9uq.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6681&group=comp.unix.shell#6681

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!4TrPrQM7Ajv/sKjasjpgNg.user.46.165.242.75.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: Musings about redirection and (only) new information overwriting existing file
Date: Wed, 17 Aug 2022 17:55:09 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83tu6an9uq.fsf@helmutwaitzmann.news.arcor.de>
References: <tdamvd$34rnd$1@dont-email.me>
<tdfbba$ti$1@oguzismail.eternal-september.org>
<83o7wkp5b8.fsf@helmutwaitzmann.news.arcor.de>
<tdihsl$e7ae$1@dont-email.me>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="36703"; posting-host="4TrPrQM7Ajv/sKjasjpgNg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Copies-To: nobody
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:qicxPMEq8OksERqxxj/SNMYjYDI=
 by: Helmut Waitzmann - Wed, 17 Aug 2022 15:55 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com>:

>I haven't pondered about advantages or disadvantages of the one or
>the other option. Here are just a few obvious thoughts I have...

[…]

I totally agree with you (therefore I deleted your explanations).

>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 don't know of any.  Of course one can do that using the shell and
“cat”:

(
unset -v -- line &&
lf="$( printf '%s\n' '' '.' )" && lf="${lf%.}" &&
if
IFS= read -r -- line && line="${line}${lf}"
${line:+:} false
then
exec > some-file &&
printf '%s' "$line" &&
cat
fi
)

>I wouldn't want to use my own tool if there's already some standard
>tool supporting it.

I don't know of a better way to do that than yours.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor