対話型シェルスクリプトの出力をパイプします。

対話型シェルスクリプトの出力をパイプします。

Arch Linux用のパッケージマネージャ、pacman、検索結果出力、各結果は2行にわたっています。

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

この出力をgrepする方法はいくつかありますが、grep -C 1私は呼び出しをラップして出力をパイプすることを好みますpaste - - | column -s $'\t' -t。これにより、各検索結果が独自の行に保持されます。

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

今まではそんなに良くなった。しかし、私もyayはデフォルトではラッパーpacmanで、パッケージ管理機能を追加します。アーチユーザーストア(AUR).yay追加オプションなしでクエリのみを渡すと、実行できる対話型検索/インストールコマンドが含まれています。

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

統一性のために検索結果の変更をこの関数にも適用したいが、相互作用性と改行==> 文字()のない行があるという事実のために、やや異なるアプローチが必要です。

検索結果部分のみを変更し、残りのスクリプトは直接渡すだけです。

paste私は/に似ていますcolumnが、Killスイッチを使用して小さなPerlスクリプトを作成しました。

#!/usr/bin/perl

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

ただし、出力をパイプすると、プログレスバーとプロンプトの前のテキストが失われます。

違いは次のとおりです。

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

別の試みでは、最初のプロンプトより前のすべてを再作成し、2番目の呼び出しに対する応答を渡しました。

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

出力:

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

正常に起動しましたが、結果が2回出力され、コマンドを2回実行する必要がありました。行かないでください。

注:ここでROIが非常に低いことに言及する必要があるようです。なぜインタラクティブなリストをgrepするのですか?この時点で、コピーから分岐して削除することをお勧めしますyay\n私がやった。しかし、まだ疑問です。このような対話型プログラムの出力を変更する簡単な方法はありますか?

ベストアンサー1

おすすめ記事