使用bash仅输出特定的文件描述符
以下命令将fd 2重定向到使用bash仅输出特定的文件描述符,bash,Bash,以下命令将fd 2重定向到/dev/null是否只能输出2 node ./index.js 2>/dev/null 在经典的Unix编程模型中,没有办法做到这一点。程序可能会尝试扫描所有可能的FD并尝试关闭它们: ( limit=$(ulimit -n) # Try to estimate some upper bound if not set [ "$limit" = "unlimited" ] && limit=1024 for ((i=0; i<
/dev/null
是否只能输出2
node ./index.js 2>/dev/null
在经典的Unix编程模型中,没有办法做到这一点。程序可能会尝试扫描所有可能的FD并尝试关闭它们:
(
limit=$(ulimit -n)
# Try to estimate some upper bound if not set
[ "$limit" = "unlimited" ] && limit=1024
for ((i=0; i<limit; i++))
do
[ "$i" != 2 ] && exec {i}>&-
done
exec node ./index.js
)
您可能希望保持0和1处于打开状态和/或将它们从/重定向到
/dev/null
,因为有些程序无法很好地处理stdin和stdout被关闭的情况。您希望只输出关于标准错误的文本吗?然后将fd 1(标准输出)重定向到/dev/null。@Shawn不完全是假设没有其他文件描述符可用于此进程。如果有其他输出文件描述符,请将其他描述符(不同于2)重定向到/dev/null。
(
for fd in "/proc/$BASHPID/fd"/*
do
fd="${fd##*/}"
[ "$fd" != 2 ] && exec {fd}>&-
done
exec node ./index.js
)