gpt4 book ai didi

linux - 重定向 stdout, stderr without) grep

转载 作者:可可西里 更新时间:2023-11-01 11:51:18 25 4
gpt4 key购买 nike

我在 Linux 中遇到重定向问题,这里有一个小示意图来说明目标,比文字更好(我希望)

+------------+                              +----------------+
| std_out +----------+------------------>+ log.log |
+------------+ | +----------------+
|
| +----------------+
+----------------->>+ |
| histo.log |
+----------------->>+ |
| +----------------+
|
|
+------------+ | grep -v "exp" +----------------+
| std_err +---------+------------------>+ error.log |
+------------+ +----------------+

我想用脚本的输出来做到这一点。我成功地用 grep 做了 log.log 和 error.log,

./script.sh >log.log 2> >(grep -v "Expression" > error.log )

但我在 histo.log 中遇到了麻烦,它应该添加(我的意思是 >> 或 tee -a 不是 >)std_out 和 std_err(没有 grep)而且我真的不了解重定向的高级内容.

如果有人有办法解决我的问题,我将不胜感激:)

最佳答案

这是输出过程替换的可怕用法:

$ cat driver.sh
echo line 1 to stdout
echo line 1 to stderr >&2
echo line 2 to stderr is expected >&2
echo line 3 to stderr >&2

$ sh driver.sh > >(tee -a histo.log > log.log) 2> >(tee -a histo.log | grep -v exp > error.log)

$ cat log.log
line 1 to stdout

$ cat histo.log
line 1 to stdout
line 1 to stderr
line 2 to stderr is expected
line 3 to stderr

$ cat error.log
line 1 to stderr
line 3 to stderr

这将附加到 histo.log,但覆盖 log.log 和 error.log 文件,如您在图表中所示。

关于linux - 重定向 stdout, stderr without) grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773930/

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