Weiterleiten der Ausgabe eines interaktiven Shell-Skripts

Weiterleiten der Ausgabe eines interaktiven Shell-Skripts

Der Paketmanager von Arch Linux,pacman, gibt Suchergebnisse aus, wobei sich jedes Ergebnis über zwei Zeilen erstreckt:

~ % 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

Es gibt Möglichkeiten, diese Ausgabe zu greppen ( grep -C 1usw.), aber ich bevorzuge es, den Aufruf zu umschließen und die Ausgabe durchzuleiten paste - - | column -s $'\t' -t. Dadurch bleibt jedes Suchergebnis in einer eigenen Zeile:

~ % 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)

So weit, so gut. Aber ich benutze auch ein Tool namensyay, im Wesentlichen ein Wrapper um pacman, mit zusätzlicher Funktionalität für die Verwaltung von Paketen imArch-Benutzer-Repository(AUR). yayenthält einen interaktiven Such-/Installationsbefehl, der durch die bloße Übergabe eines Suchbegriffs ohne weitere Optionen ausgelöst wird:

~ % 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                                                              
[...]

Der Einheitlichkeit halber wollte ich meine Ergebniskorrektur auch auf diese Funktion anwenden, aber aufgrund der Interaktivität und der Tatsache, dass es Zeilen ohne Zeilenumbrüche ( ==>) gibt, sind einige andere Ansätze erforderlich.

Ich muss nur den Teil mit den Suchergebnissen ändern und dann den Rest des Skripts direkt weitergeben.

Ich habe ein kleines Perl-Skript geschrieben, das ähnlich wie mein paste/ ist column, aber mit einem 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;
}

Aber wenn ich die Ausgabe dort hindurchleite, verliere ich Fortschrittsbalken und den Text vor den Eingabeaufforderungen.

Hier ist ein Unterschied:

--- 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...

In einem weiteren Versuch habe ich alles vor der ersten Eingabeaufforderung neu erstellt und dann die Antwort an einen zweiten Anrufer weitergegeben:

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 "$@"

Die Ausgabe:

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

Fängt gut an, aber es gab die Ergebnisse zweimal aus und ich musste den Befehl zweimal ausführen. Das ging nicht.

Randbemerkung: Ich sollte wahrscheinlich erwähnen, dass die Kapitalrendite hier sehr gering ist. Warum sollte ich überhaupt eine interaktive Liste greppen wollen? An diesem Punkt könnte ich genauso gut einfach forken und die aus meiner Kopie yaylöschen (\nIch tat. Aber ich bin immer noch neugierig: Gibt es eine einfache Möglichkeit, die Ausgabe eines interaktiven Programms wie diesem zu ändern?

verwandte Informationen