Canalizando a saída de um script de shell interativo

Canalizando a saída de um script de shell interativo

Gerenciador de pacotes do Arch Linux,pacman, gera resultados de pesquisa com cada resultado abrangendo duas linhas:

~ % pacman -Ss cowsay
extra/cowsay 3.04-2
    Configurable talking cow (and a few other creatures)
community/ponysay 3.0.3-4
    cowsay reimplemention for ponies

Existem maneiras de executar o grep nessa saída ( grep -C 1, etc), mas prefiro encerrar a chamada e canalizar a saída por meio de paste - - | column -s $'\t' -t. Isso deixa cada resultado da pesquisa em sua própria linha:

~ % pacman -Ss cowsay
community/ponysay 3.0.3-4 (1.3 MiB 10.7 MiB)    cowsay reimplemention for ponies
extra/cowsay 3.04-2 (19.5 KiB 38.2 KiB)         Configurable talking cow (and a few other creatures)

Até agora tudo bem. Mas também uso uma ferramenta chamadayay, que é essencialmente um wrapper pacman, com funcionalidade adicional para gerenciar pacotes noRepositório de usuários Arch(AUR). yayinclui um comando interativo de pesquisa/instalação que é acionado apenas passando um termo de pesquisa sem outras opções:

~ % yay cowsay
15 aur/ponysay-free 3.0.2-1 (+1 0.00)
    Awesome cowsay reimplemention with ponies for ponies. Only free ponies edition
[...]
4 aur/xcowsay 1.5.1-1 (+17 0.38)
    a program based on cowsay that displays a cute cow and message on your desktop
3 aur/muccadoro 1.0.1-1 (+1 0.79)
    Pomodoro timer using figlet, cowsay, and optionally lolcat
2 community/ponysay 3.0.3-4 (1.3 MiB 10.7 MiB)
    cowsay reimplemention for ponies
1 extra/cowsay 3.04-2 (19.5 KiB 38.2 KiB)
    Configurable talking cow (and a few other creatures)
==> Packages to install (eg: 1 2 3, 1-3 or ^4)
==> 1
resolving dependencies...
looking for conflicting packages...

Package (1)   New Version  Net Change  Download Size

extra/cowsay  3.04-2         0.04 MiB       0.02 MiB

Total Download Size:   0.02 MiB
Total Installed Size:  0.04 MiB

:: Proceed with installation? [Y/n] Y
:: Retrieving packages...
cowsay-3.04-2-any                                                 19.5 KiB  49.5 KiB/s 00:00 [#######################################################] 100%
(1/1) checking keys in keyring                                                                [#######################################################] 100%
(1/1) checking package integrity                                                              
[...]

Por uma questão de uniformidade, eu esperava aplicar minha correção de resultado de pesquisa a esta função também, mas a interatividade e o fato de haver linhas sem quebras de linha ( ==>) exigem algumas abordagens diferentes.

Preciso alterar apenas a parte dos resultados da pesquisa e, em seguida, passar diretamente pelo restante do script.

Eu escrevi um pequeno script perl que é parecido com o meu paste/ column, mas com um kill switch:

#!/usr/bin/perl

$thru = 0;
while (<>) {
    if ( $thru == 1 || m/Packages to install / ) {
        $thru = 1;
    } else {
        chomp unless (m/^ /);
        s/^\s+/\t/;
    }
    print;
}

Mas quando canalizo a saída por meio disso, perco as barras de progresso e o texto antes dos prompts.

Aqui está uma diferença:

--- correctnot.txt      2021-10-29 15:09:43.645632751 -0500
+++ correct.txt 2021-10-29 15:09:43.655632810 -0500
@@ -1,22 +1,22 @@
==> Packages to install (eg: 1 2 3, 1-3 or ^4)
-1
-==> resolving dependencies...
+==> 1
+resolving dependencies...
looking for conflicting packages...
-:: Proceed with installation? [Y/n]
+
Package (1)   New Version  Net Change

extra/cowsay  3.04-2         0.04 MiB

Total Installed Size:  0.04 MiB

-Y
-checking keyring...
-checking package integrity...
-loading package files...
-checking for file conflicts...
-checking available disk space...
+:: Proceed with installation? [Y/n] Y
+(1/1) checking keys in keyring                                                                [#######################################################] 100%
+(1/1) checking package integrity                                                              [#######################################################] 100%
+(1/1) loading package files                                                                   [#######################################################] 100%
+(1/1) checking for file conflicts                                                             [#######################################################] 100%
+(1/1) checking available disk space                                                           [#######################################################] 100%
:: Processing package changes...
-installing cowsay...
+(1/1) installing cowsay                                                                       [#######################################################] 100%
:: Running post-transaction hooks...
(1/3) Arming ConditionNeedsUpdate...
(2/3) Refreshing PackageKit...

Em outra tentativa, recriei tudo antes do primeiro prompt e depois passei a resposta para uma segunda chamada:

echo q |
    /usr/bin/yay "$@" |
    perl -pe 'if ( m/Packages to install / ) { print; s/Packages to install.*//; chomp; print; exit; } chomp unless (m/^ /); s/^\s+/\t/'
read answer
echo "$answer" | exec /usr/bin/yay "$@"

A saída:

15 aur/ponysay-free 3.0.2-1 (+1 0.00)   Awesome cowsay reimplemention with ponies for ponies. Only free ponies edition
[...]
4 aur/xcowsay 1.5.1-1 (+17 0.38)        a program based on cowsay that displays a cute cow and message on your desktop
3 aur/muccadoro 1.0.1-1 (+1 0.79)       Pomodoro timer using figlet, cowsay, and optionally lolcat
2 community/ponysay 3.0.3-4 (1.3 MiB 10.7 MiB)  cowsay reimplemention for ponies
1 extra/cowsay 3.04-2 (19.5 KiB 38.2 KiB)       Configurable talking cow (and a few other creatures)
==> Packages to install (eg: 1 2 3, 1-3 or ^4)
==> 1
15 aur/ponysay-free 3.0.2-1 (+1 0.00)
    Awesome cowsay reimplemention with ponies for ponies. Only free ponies edition
[...]
4 aur/xcowsay 1.5.1-1 (+17 0.38)
    a program based on cowsay that displays a cute cow and message on your desktop
3 aur/muccadoro 1.0.1-1 (+1 0.79)
    Pomodoro timer using figlet, cowsay, and optionally lolcat
2 community/ponysay 3.0.3-4 (1.3 MiB 10.7 MiB)
    cowsay reimplemention for ponies
1 extra/cowsay 3.04-2 (19.5 KiB 38.2 KiB)
    Configurable talking cow (and a few other creatures)
==> Packages to install (eg: 1 2 3, 1-3 or ^4)
==> resolving dependencies...
looking for conflicting packages...

Package (1)   New Version  Net Change

extra/cowsay  3.04-2         0.04 MiB

Total Installed Size:  0.04 MiB

:: Proceed with installation? [Y/n]  -> exit status 1

Começa bem, mas gera os resultados duas vezes e tive que executar o comando duas vezes. Não vá.

Nota lateral: provavelmente devo mencionar que o retorno do investimento é muito baixo aqui. Por que eu iria querer usar uma lista interativa? Neste ponto, eu poderia simplesmente bifurcar yaye excluir \nda minha cópia (eu fiz. Mas ainda estou curioso: existe uma maneira simples de alterar a saída de um programa interativo como este?

informação relacionada