简体   繁体   English

如何记录使用bash脚本和标准输入运行的可执行文件的输出?

[英]How to record the ouput of executable running with bash script and standard input?

I am new to writing bash scripts (and not very good). 我是刚开始编写bash脚本的人(不是很好)。 So it would be great if I can get some explanatory help with my question. 因此,如果我的问题能得到一些解释性的帮助,那将是很好的。

The following is the bash script I have written to provide standard input to ./runnable (executable file) carrying input.aa as argument. 以下是我编写的bash脚本,用于将标准输入提供给带有input.aa作为参数的./runnable(可执行文件)。 I want to record output of this ./runnable input.aa in another file say, output. 我想将此./runnable input.aa的输出记录在另一个文件中,例如output。 Any suggestions on how to reframe my code? 关于如何重新构建代码的任何建议? If there is anything wrong with my script, please drop in suggestions. 如果我的脚本有任何问题,请提出建议。

#!/bin/bash

./runnable input.aa <<EOF

>2       #input I want runnable to take 
>15
>7
>12
>16
>92
>18
EOF

Sure, just change line 3 to 当然,只需将第3行更改为

./runnable input.aa >output <<EOF

The > is the output redirection operator - it sends standard output to the named file. >是输出重定向运算符-将标准输出发送到命名文件。

If you want to capture standard error as well (typically error messages etc.), then use 如果您还想捕获标准错误(通常是错误消息等),请使用

./runnable input.aa >output 2>errput <<EOF

instead. 代替。 Or you can get them both intermixed in the same file with 或者您可以将它们混在一起在同一个文件中

./runnable input.aa >alloutput 2>&1 <<EOF

An addition to David's suggestion is to pipe your command through tee . 大卫的建议的另一项内容是通过tee传达您的命令。 This allows you to dump the output to a file and see it at the same time (useful for interactive scenarios). 这使您可以将输出转储到文件中并同时查看(对于交互式方案很有用)。

since runnable is expecting inputs. 因为runnable期望输入。 you can not use output redirection before starting here documents . 在开始此处的文档之前,不能使用输出重定向。

you will have to do the following to send the output of runnable to a separate file. 您将必须执行以下操作才能将runnable的输出发送到单独的文件。

#!/bin/bash

OUTFILE=file3.txt
(
./runnable input.aa << EOF
2
4
3
4
3
3
2
EOF
) > $OUTFILE

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

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