eval - 在鱼壳中管道 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
解决方案
但是,如果命令不存在,我无法重定向 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.
推荐阅读
- python - Python 棋盘游戏 - “TypeError:列表索引必须是整数或切片,而不是元组”
- linux - 如何在 MacOS Mohave 上启用 declare -A?
- scilab - Scialb 中的回文算法
- mysql - 为什么基于 SELECT 的 DELETE 不能与 mysql 5.0 一起使用
- javascript - if else 在某些情况下停止工作
- flutter - 当 textInputField 在更改文本 Flutter 时无效时禁用按钮
- javascript - 使用 React Native 创建复选框的问题 - TypeError: hashtags.map is not a function
- python - 将电子邮件数据导入列表停止在 100
- ios - UIViewController 中的子视图不可点击
- angular - 角度新手问题