2017-10-18 93 views

回答

4

您需要将命令的stdout重定向到stderr。

your_command.sh 1>&2

如果你想从脚本中做到这一点,你可以用你的整个脚本到一个功能,它的输出重定向到stderr:

main() { 
    echo hello 
    echo world 
    some_script.sh 
} 

main 1>&2 
+0

因此,像? echo hello 1>&2此外,这是否与(>&2 echo hello)相同 –

+2

@PhilO是的,重定向操作符可以出现(几乎)在命令行的任何位置,因为shell在调用命令之前识别并删除它们。它们可以在一个简单命令的名字前面,就像你的例子一样,但是它们必须出现在像'while','for','if'等复合命令之后。'echo 1>&2 hello'也可以工作。 – chepner

3
exec >&2 

将是在脚本的顶部将所有未来输出重定向到stderr。

$ help exec 
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...] 
    Replace the shell with the given command. 

    Execute COMMAND, replacing this shell with the specified program. 
    ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, 
    any redirections take effect in the current shell. 
+0

简单!假设我想要输出到stdout和stderr? –

+0

这不会重定向工具的输出 –

+0

当然可以。它会影响所有命令。 –

1

为我工作的解决方案是中(的)封闭脚本的文本和标准输出重定向到stderr像这样:
( echo 1 echo 2 tool1 ) 1>&2

+0

尝试用卷边。 '{...}>&2' –

相关问题