我正在努力实现这个目标:
PIPE=""
if [ $DEBUG = "true" ]; then
PIPE="2> /dev/null"
fi
git submodule init $PIPE
但是$PIPE
被解释为git的命令行参数。如何仅在调试模式下显示stdout
和stderr
,而在非调试模式下仅管道stderr
?
感谢您的深刻见解。结束这样做,如果不在调试模式下将所有内容重定向到/ dev / null,并在调试模式下打印stderr和stdout:
# debug mode
if [[ ${DEBUG} = true ]]; then
PIPE=/dev/stdout
else
PIPE=/dev/null
fi
git submodule init 2>"${PIPE}" 1>"${PIPE}"
答案 0 :(得分:2)
在>
之后使用变量if [[ ${DEBUG} = true ]]; then
errfile=/dev/stderr
else
errfile=/dev/null
fi
command 2>"${errfile}"
修改文件描述符
您可以将stderr复制到新文件描述符3
if [[ ${DEBUG} = true ]]; then
exec 3>&2
else
exec 3>/dev/null
fi
然后为每个命令使用新的重定向
command 2>&3
关闭fd 3,如果不再需要
exec 3>&-
答案 1 :(得分:1)
首先我猜你的逻辑是错误的,DEBUG = true你会将stderr发送到/ dev / null。此外,你的字符串比较缺少第二个" =",
简单的解决方案怎么样?
if [ "${DEBUG}" == "true" ]; then
git submodule init
else
git submodule init 2>/dev/null
fi
根据你的回复编辑:
或者你可以使用eval
,但要小心它被认为是邪恶的;)
if [ "${DEBUG}" == "true" ]; then
PIPE=""
else
PIPE="2>/dev/null"
fi
eval git submodule init $PIPE