简体   繁体   English

将C程序的输出存储在shell中

[英]Store output of C program in shell

I have a C program which gives some output. 我有一个C程序,可以提供一些输出。 I am compiling the C program via the shell but I need the output from the run C program and store in shell. 我正在通过外壳编译C程序,但是我需要run C程序的输出并存储在外壳中。

Edit. 编辑。

Save the output to a shell Variable. 将输出保存到外壳变量。

I assume that you want to store the output of the program in a variable. 我假设您要将程序的输出存储在变量中。 Unix shells offer a facility that's called command substitution to do just that. Unix shell提供了一种称为命令替换的功能来完成此任务。 Depending on your shell, you can do either : 根据您的外壳,您可以执行以下任一操作:

output=$(./run)

or 要么

output=`./run`

Bash supports both. Bash都支持。 If, however, you want to save the output to a file, you will need to redirect the standard output stream to a file. 但是,如果要将输出保存到文件,则需要将标准输出流重定向到文件。 You can do this like this : 您可以这样做:

./run > output.txt

Or, if you want to see the output while the program is running and save it to an output file as well, you can use the tee utility and pipe the output of your program to it. 或者,如果您想在程序运行时查看输出并将其保存到输出文件中,则可以使用tee实用程序并将其输出通过管道传递给它。

./run | tee output.txt

You can redirect your output into a file like this: 您可以将输出重定向到这样的文件中:

./run > file

If you want to store it into a variable, you have to decide which shell we're talking about. 如果要将其存储到变量中,则必须确定我们在谈论哪个shell。 It depends whether you have a windows shell, or linux bash.. 这取决于您是否具有Windows Shell或Linux Bash。

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

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