简体   繁体   English

在鱼壳中管道 eval/source 的输出

[英]Pipe the output of eval/source in fish shell

I want to pipe the output of eval<\/code> to a file.我想将eval<\/code>的输出通过管道传输到文件中。 That works as expected if the command execution is successful:如果命令执行成功,这将按预期工作:

eval ls > log.txt 2>&1
cat log.txt # Documents Desktop

However, I do not manage to redirect stderr if the command does not exist但是,如果命令不存在,我无法重定向 stderr

That's because the output is not coming from eval or the command, it's coming from your command-not-found handler.那是因为输出不是来自eval或命令,而是来自您的 command-not-found 处理程序。

Try checking if the command exists before you try to execute it.在尝试执行该命令之前,请尝试检查该命令是否存在。 If you absolutely can't, it's technically possible to silence the command-not-found error entirely by redefining __fish_command_not_found_handler :如果你绝对不能,技术上可以通过重新定义__fish_command_not_found_handler完全__fish_command_not_found_handler command-not-found 错误:

function __fish_command_not_found_handler; end

You'd have to handle moving it back afterwards via functions --copy :之后您必须通过functions --copy处理将其移回:

functions --copy __fish_command_not_found_handler oldcnf

Overall I don't recommend any of this and suspect you might be overusing eval .总的来说,我不推荐任何这些,并怀疑您可能过度使用eval

Something that works with source would also be very much appreciated:与源一起使用的东西也将非常感激:

That's what eval is for, quite literally.从字面上看,这就是 eval 的用途。 Up to the upcoming 3.1 release eval is a function that's just source with some support code that mostly boils down to handling these redirections.在即将发布的 3.1 版本之前,eval 只是一个带有一些支持代码的source代码,这些代码主要归结为处理这些重定向。

You should eval if command exists, this is possible with如果命令存在,您应该评估,这可以通过

test -f (whereis -b command | awk '{print $2}')

whereis -b will to search for the binaries for the command in your system whereis -b将在您的系统中搜索该command的二进制文件

awk filter the output to only show the first result awk过滤输出以仅显示第一个结果

test -f going to verify if the file exists test -f验证文件是否存在

If the command exist, it return status 0 .如果命令存在,则返回status 0 So to finish you can write it like所以要完成你可以这样写

test -f (whereis -b abcde | awk '{print $2}') && abcde > log.txt 2>&1

Also can use this form也可以使用这个表格

test (whereis -b abcde | awk '{print $2}') != '' && abcde > log.txt 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM