Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"One lawyer can steal more than a hundred men with guns." -- The Godfather


devel / comp.unix.shell / Re: Basta: protecting $_ variable.

SubjectAuthor
* Basta: protecting $_ variable.Kaz Kylheku
`* Re: Basta: protecting $_ variable.Janis Papanagnou
 `- Re: Basta: protecting $_ variable.Kaz Kylheku

1
Basta: protecting $_ variable.

<20240409222039.303@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Basta: protecting $_ variable.
Date: Wed, 10 Apr 2024 05:29:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <20240409222039.303@kylheku.com>
Injection-Date: Wed, 10 Apr 2024 05:29:34 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a02df6af16e190f6c7ee895d512b8f84";
logging-data="819942"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MBbTg4Seh5CWncqDOzHnIjKPd+GhQz54="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:fBLZWZ0DgVhJSjb2GxcQ5XVn39o=
 by: Kaz Kylheku - Wed, 10 Apr 2024 05:29 UTC

Hi all,

In the constext of the Basta project https://www.kylheku.com/cgit/basta/
it came to my attention that the background interrupt was clobbering the
occasionally useful $_ variable which holds the last argument of the
previous command.

This was introduced in the Korn shell; Bash has it.

After some failed experiments, I worked out a stable trick for
protecting it.

You cannot simply save $_ into a local variable on entry into your trap
handler, and restore it at the end because Bash clobbers it afterward,
when the trap command finishes executing. $_ ends up with the last
argument of the trap command.

The trap setup for catching the SIGWINCH and SIGALRM signals now looks
like this:

trap 'basta_uln_save=$_; basta.update_status; : "$basta_uln_save"' ALRM WINCH

instead of just

trap basta.update_status ALRM WINCH

The first command of the sequence saves $_ into a global basta_uln_save.

The last command is the null command

: "$basta_uln_save"

You can see how that works. Bash executes the null command, expanding
the quoted parameter to form the rightmost argument, and that argument
becomes the value of $_. Mission accomplished!

I now see a stable value of $_ at the prompt, in spite of the
periodic interrupt that refreshes the status line.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Basta: protecting $_ variable.

<uv5bf2$psbu$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Basta: protecting $_ variable.
Date: Wed, 10 Apr 2024 08:26:42 +0200
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uv5bf2$psbu$1@dont-email.me>
References: <20240409222039.303@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 10 Apr 2024 06:26:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="63977abd1b4cb9268d9f2058a222d662";
logging-data="848254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ucUPxDbPR+4UIfwM8uThq"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:OGV8TG+FxHRdQqVklMqwEQN/ONw=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <20240409222039.303@kylheku.com>
 by: Janis Papanagnou - Wed, 10 Apr 2024 06:26 UTC

On 10.04.2024 07:29, Kaz Kylheku wrote:
> [...]
>
> The trap setup for catching the SIGWINCH and SIGALRM signals now looks
> like this:
>
> trap 'basta_uln_save=$_; basta.update_status; : "$basta_uln_save"' ALRM WINCH
>
> instead of just
>
> trap basta.update_status ALRM WINCH

I know that this looks like a hack, but since 'update_status' needs no
arguments you could also more simply just write...?

trap 'basta.update_status "$_"' ALRM WINCH

(Not sure whether introducing a global variable and supplementary code
or this hack is "better". Just mentioning it for a possible variant.)

Janis

> [...]

Re: Basta: protecting $_ variable.

<20240410061038.707@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Basta: protecting $_ variable.
Date: Wed, 10 Apr 2024 13:26:47 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <20240410061038.707@kylheku.com>
References: <20240409222039.303@kylheku.com> <uv5bf2$psbu$1@dont-email.me>
Injection-Date: Wed, 10 Apr 2024 13:26:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a02df6af16e190f6c7ee895d512b8f84";
logging-data="1043687"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MQvluy1lFB10ZyafG+XfHNFolBmerWDo="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:j+oxP7DLUGULXNINwhQmP4OxGN4=
 by: Kaz Kylheku - Wed, 10 Apr 2024 13:26 UTC

On 2024-04-10, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 10.04.2024 07:29, Kaz Kylheku wrote:
>> [...]
>>
>> The trap setup for catching the SIGWINCH and SIGALRM signals now looks
>> like this:
>>
>> trap 'basta_uln_save=$_; basta.update_status; : "$basta_uln_save"' ALRM WINCH
>>
>> instead of just
>>
>> trap basta.update_status ALRM WINCH
>
> I know that this looks like a hack, but since 'update_status' needs no
> arguments you could also more simply just write...?
>
> trap 'basta.update_status "$_"' ALRM WINCH
>
> (Not sure whether introducing a global variable and supplementary code
> or this hack is "better". Just mentioning it for a possible variant.)

Nice streamlining; I changed to this.

One command, and no extra variable is better.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor