首页 > 解决方案 > 在鱼壳中管道 eval/source 的输出

问题描述

我想将输出通过管道eval传输到文件。如果命令执行成功,这将按预期工作:

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

如果命令不成功,它也可以工作

eval rm Desktop > log.txt 2>&1
cat log.txt # rm: cannot remove 'Desktop': Is a directory

但是,如果命令不存在,我无法重定向 stderr

eval abcde > log.txt 2>&1 # fish: Unknown command abcde
cat log.txt # (empty)

如何将第三种情况的输出也重定向到日志文件?


可以使用的东西source也将不胜感激:

echo abcde | source > log.txt 2>&1

标签: evalfile-descriptorio-redirectionfish

解决方案


但是,如果命令不存在,我无法重定向 stderr

那是因为输出不是来自eval命令,而是来自您的命令未找到处理程序。

在尝试执行之前尝试检查该命令是否存在。如果你绝对不能,技术上可以通过重新定义完全消除 command-not-found 错误__fish_command_not_found_handler

function __fish_command_not_found_handler; end

You'd have to handle moving it back afterwards via 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.

Something that works with source would also be very much appreciated:

That's what eval is for, quite literally. 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.


推荐阅读