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 一个交互式列表呢?此时,我不妨从我的副本中分叉yay
并删除(\n
我做到了。但我仍然很好奇,是否有一种直接的方法来改变这样的交互式程序的输出?