简体   繁体   English

Bash脚本捕获输出到终端

[英]Bash script capturing output to terminal

I want to capture into my bash script (in a variable) the output of some command that prints its output to terminal. 我想捕获我的bash脚本(在一个变量中)一些命令的输出,该命令将其输出打印到终端。 I have tried the following: 我尝试过以下方法:

TEST_OUT=`the_command ARG1`   #Nope

#Putting the line "the_command ARG1" into a separate script, testing2.sh,

TEST_OUT=$(./testing2.sh)   #Nope

testing2.sh
TEST_OUT=$?  #Nope

I am 100% sure that when I run... 我100%肯定当我跑...

> the_command ARG1

...in a terminal, it prints to the terminal exactly the information I want to capture. ...在终端中,它会向终端打印我想要捕获的信息。

Thank you for any help! 感谢您的任何帮助!

If the output is being sent to stderr, you'll need to redirect that to stdout before it can be capture in your var. 如果将输出发送到stderr,则需要将其重定向到stdout才能在var中捕获。 Try: 尝试:

TEST_OUT=$(the_command ARG1 2>&1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM