我写了一个bash脚本。它有几个回声陈述。它调用了一些第三方工具。我想重定向到stdout(从我的echo语句和所有工具输出)到stderr的所有内容。我怎么做?如何将输出从bash脚本重定向到stderr
回答
您需要将命令的stdout重定向到stderr。
your_command.sh 1>&2
如果你想从脚本中做到这一点,你可以用你的整个脚本到一个功能,它的输出重定向到stderr:
main() {
echo hello
echo world
some_script.sh
}
main 1>&2
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.
简单!假设我想要输出到stdout和stderr? –
这不会重定向工具的输出 –
当然可以。它会影响所有命令。 –
为我工作的解决方案是中(的)封闭脚本的文本和标准输出重定向到stderr像这样:
( echo 1 echo 2 tool1 ) 1>&2
尝试用卷边。 '{...}>&2' –
- 1. Bash脚本 - 将子脚本stderr重定向到父级的stdout
- 2. 将输出重定向到stderr
- 3. 如何重定向标准输出stderr在蚂蚁脚本?
- 4. 重定向Bash子脚本的输出
- 5. bash脚本,错误输出重定向
- 6. 重定向输出bash脚本
- 7. 将bash输出重定向到python脚本
- 8. 如何将子进程输出重定向到stderr?
- 9. 如何在groovy中将输出重定向到stderr?
- 10. 如何将bash脚本的输出重定向到即时屏幕?
- 11. 如何将stdout和stderr重定向到文件AND仍只输出stderr?
- 12. 如何将RUNTIME ERRORS重定向到STDERR?
- 13. 如何将Python脚本的输出重定向到终端
- 14. 如何将python脚本cmd输出重定向到文件?
- 15. 如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件?
- 16. 将函数的输出重定向到脚本输出
- 17. 将输出从shell脚本重定向到文件
- 18. Bash脚本不会将输入重定向到子进程
- 19. 在bash脚本中测试stdout和stderr重定向
- 20. stderr在bash脚本中的临时重定向
- 21. Bash吃stderr输出
- 22. 如何在Bash中重定向输出
- 23. 做一个bash脚本输出STDOUT&STDERR到一个文件
- 24. 了解stderr标准输出重定向
- 25. 重定向stdout和stderr的输出?
- 26. 将stderr重定向到C中的文件和标准输出
- 27. PERL无法将stdout和stderr重定向到输出文件
- 28. 将stderr重定向到c shell中的标准输出
- 29. go - 将stderr重定向到文件并输出到标准输出
- 30. 将wget输出重定向到perl脚本输入
因此,像? echo hello 1>&2此外,这是否与(>&2 echo hello)相同 –
@PhilO是的,重定向操作符可以出现(几乎)在命令行的任何位置,因为shell在调用命令之前识别并删除它们。它们可以在一个简单命令的名字前面,就像你的例子一样,但是它们必须出现在像'while','for','if'等复合命令之后。'echo 1>&2 hello'也可以工作。 – chepner