Basta: protecting $_ variable.

Liste des GroupesRevenir à cu shell 
Sujet : Basta: protecting $_ variable.
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.unix.shell
Date : 10. Apr 2024, 06:29:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240409222039.303@kylheku.com>
User-Agent : slrn/pre1.0.4-9 (Linux)
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

Date Sujet#  Auteur
10 Apr 24 * Basta: protecting $_ variable.3Kaz Kylheku
10 Apr 24 `* Re: Basta: protecting $_ variable.2Janis Papanagnou
10 Apr 24  `- Re: Basta: protecting $_ variable.1Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal