gpt4 book ai didi

python - 如何让辅助脚本(Bash/Python/Ruby/Perl)执行复杂的命令?

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:37 25 4
gpt4 key购买 nike

请参阅底部的我的问题。

这是我想使用 system() 在 Perl 脚本中执行的命令

zfs send tank/Test0@snap | mbuffer -4 -O 10.10.47.47:8023

但我收到此错误

mbuffer: warning: error connecting to 10.10.47.47:8023: Connection refused
mbuffer: error: unable to connect to 10.10.47.47:8023
mbuffer: fatal: no output left - nothing to do

这是误导性的,因为我可以在终端中执行该命令,并且它工作正常。

所以现在我开始创造性地寻找一种方法来解决这个限制。将命令放入文件中并执行

system("ssh localhost /tmp/test");

可以工作,但必须通过 SSH 连接到本地主机并创建临时文件,这不太好。需要 ssh localhost 否则我会得到相同的错误。

为了避免创建临时文件,我尝试制作一个“解析器文件”

#!/usr/bin/bash
$1 | $2

这是调用pipe.sh,然后在 Perl 中执行

my $a = 'zfs send tank/Test0@snap';
my $b = "mbuffer -4 -O 10.10.47.47:8023";
system("pipe.sh $a $b");

但是pipe.sh执行zfs |发送

我的下一个想法是

my $c = "zfs send tank/Test0\@snap \| mbuffer -4 -O 10.10.47.47:8023";
system("pipe.sh $c");

pipe.sh成为

#!/usr/bin/bash
$@

但这将每个命令视为一个单独的命令。 IE。 zfs,然后send等等。

问题

我已经不知道如何在不创建临时文件的情况下执行该命令,这很糟糕,因为 tank/Test0 实际上是 for 循环内的变量,因此它会导致许多写入。

我愿意执行 Ruby 或 Python 脚本,而不是调用 bash 脚本,只要它们能比 Bash 更好地处理这个问题。

更新

如果我从终端做

perl -e'system "bash", "-c", q{zfs send tank/Test0@snap | mbuffer -4 -O 10.10.47.47:8023}'

然后它就可以工作了,但是在脚本中执行相同的操作

system("bash", "-c", q{zfs send tank/Test0@snap | mbuffer -4 -O 10.10.47.47:8023});

给我(启用 mbuffer verbose 5)

mbuffer: Verbose = 5
mbuffer: total # of phys pages: 1132576 (pagesize 4096)
mbuffer: default buffer set to 22651 blocks of 4096 bytes
mbuffer: getting address info for 10.10.47.47:8023
mbuffer: warning: error connecting to 10.10.47.47:8023: Connection refused
mbuffer: error: unable to connect to 10.10.47.47:8023
mbuffer: allocating memory for 22651 blocks with 4096 byte (90604 kB total)...
mbuffer: creating semaphores...
mbuffer: opening input...
mbuffer: direct I/O hinting failed for input: Invalid argument
mbuffer: fatal: no output left - nothing to do

更新2

这确实有效!

system("ssh", "localhost", q{zfs send tank/Test0@snap | mbuffer -4 -v 5 -O 10.10.47.47:8023});

我在启用最详细的情况下收到以下错误,但由于某种原因有效。

mbuffer: Verbose = 5
mbuffer: total # of phys pages: 1130435 (pagesize 4096)
mbuffer: default buffer set to 22608 blocks of 4096 bytes
mbuffer: getting address info for 10.10.47.47:8023
mbuffer: successfully connected to 10.10.47.47:8023
mbuffer: set TCP send buffer size to 1048576
mbuffer: allocating memory for 22608 blocks with 4096 byte (90432 kB total)...
mbuffer: creating semaphores...
mbuffer: opening input...
mbuffer: direct I/O hinting failed for input: Invalid argument
mbuffer: direct I/O hinting failed for output to 10.10.47.47:8023: Invalid argument
mbuffer: checking if we have a controlling terminal...
mbuffer: registering signals...
mbuffer: starting threads...
mbuffer: checking output device...
mbuffer: no device on output stream
mbuffer: checking input device...
mbuffer: no device on input stream
mbuffer: outputThread: starting output on 10.10.47.47:8023...
mbuffer: inputThread: starting with threadid 3...
mbuffer: inputThread: last block has 2640 bytes
mbuffer: inputThread: exiting...
mbuffer: outputThread: last block has 2640 bytes
mbuffer: outputThread: syncing 10.10.47.47:8023...
mbuffer: syncing unsupported on 10.10.47.47:8023: omitted.
mbuffer: outputThread: finished - exiting...
mbuffer: waiting for senders...
mbuffer: joining sender for 10.10.47.47:8023

summary: 46.0 KiByte in 0.0 sec - average of 22.9 MiB/s

最佳答案

mbuffer 开发人员对错误有这样的说法

Concerning the warning related to I/O hinting - these warning can safely be ignored. mbuffer is just telling you that no I/O hinting is possible. I/O hinting just gives tips to the operating system what kind of I/O sequence is coming, so that the OS is able to tune its operation for that specific scenario. I/O hinting is implemented differently for most OSs...

对于实际问题,Mojo::IOLoop::ForkCall 就是解决方案,如 this 所示。模块,它正是我想要做的。

关于python - 如何让辅助脚本(Bash/Python/Ruby/Perl)执行复杂的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com