tags:

views:

184

answers:

3

I am new to writing bash scripts (and not very good). 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. I want to record output of this ./runnable input.aa in another file say, 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
+2  A: 

Sure, just change line 3 to

./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
David Zaslavsky
@David: thanks for prompt reply! :)
shubster
OP wants output redirection of the output coming from running that whole command. so output redirection operator will come after the whole command.
Omnipresent
+2  A: 

An addition to David's suggestion is to pipe your command through tee. This allows you to dump the output to a file and see it at the same time (useful for interactive scenarios).

Brian Agnew
+1  A: 

since runnable is expecting inputs. 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.

#!/bin/bash

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