我想做以下事情;
没有stdout到屏幕的代码:
#!/bin/bash
exec 1> >(sed -u 's/^/INF: /' >> common.log)
exec 2> >(sed -u 's/^/ERR: /' >> common.log)
echo "some txt"
echo "an error" >&2
echo "some more txt"
echo "one more error" >&2
日志:
INF: some txt
INF: some more txt
ERR: an error
ERR: one more error
第一个问题是缓冲,我试图用sed'-u'取消无缓冲。
带stdout到屏幕的代码:
#!/bin/bash
exec 1> >(sed -u 's/^/INF: /' | tee -a common.log)
exec 2> >(sed -u 's/^/ERR: /' >> common.log)
echo "some txt"
echo "an error" >&2
echo "some more txt"
echo "one more error" >&2
屏幕挂起(必须按Ctrl-C)并且日志仍然缓冲。建议?
答案 0 :(得分:2)
这对你有用吗?
command 2> >(sed -u 's/^/ERR: /' >> common.log) | sed -u 's/^/INF: /' | tee -a common.log
command
是你的命令。