扔掉标准输出和错误

Aas*_*shu 3 bash pipe io-redirection stderr

我已经在 bash 脚本中使用/dev/null重定向了我的输出,但它仍然抛出错误。代码如下

ps -p $proc | fgrep $proc> /dev/null
if [ $? -ne '0' ] ; then
......
fi    
Run Code Online (Sandbox Code Playgroud)

下面是错误

error: list of process IDs must follow -p

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
Usage: fgrep [OPTION]... PATTERN [FILE]...
Try 'fgrep --help' for more information.
Run Code Online (Sandbox Code Playgroud)

如何在不影响$ 的情况下抑制此错误输出?

Oth*_*eus 6

&> /dev/null扔掉stderrstdout。与其他人的答案相同,只是短了几个字符。


cuo*_*glm 5

您可以使用命令分组:

{ ps -p "$proc" | fgrep "$proc";} >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)

或将管道包裹在子壳中:

(ps -p "$proc" | fgrep "$proc") >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)