2017-10-18 91 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' –

相關問題