透過管道傳輸互動式 shell 腳本的輸出

透過管道傳輸互動式 shell 腳本的輸出

Arch Linux 的套件管理器,pacman,輸出搜尋結果,每個結果跨兩行:

~ % 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,並添加了用於管理包中的功能Arch 用戶儲存庫(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                                                              
[...]

為了統一起見,我希望也將我的搜尋結果修復應用於此函數,但是交互性以及存在沒有換行符(==>)的行的事實,它需要一些不同的方法。

我只需要更改搜尋結果部分,然後直接傳遞腳本的其餘部分。

我寫了一個小 perl 腳本,有點像我的paste/ column,但有一個終止開關:

#!/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...

在另一次嘗試中,我重新創建了第一個提示之前的所有內容,然後將答案傳遞給第二個呼叫:

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

開始得很好,但它輸出了兩次結果,我不得不運行該命令兩次。不去。

附註:我可能應該提到,這裡的投資報酬率非常低。為什麼我要 grep 一個互動式清單呢?此時,我不妨 forkyay並刪除\n從我的副本中我做到了。但我仍然很好奇,是否有一種直接的方法來改變這樣的互動式程式的輸出?

相關內容