Mike Conley
Mike Conley

Reputation: 125

Redirect stdin and stdout in bash for comprehensive log files

I am having some troubles creating a comprehensive log file using I/O redirection. What I have is, a bash shell script that creates text files that are later used for input into a few FORTRAN programs. So basically I have;

echo -e "Responses \nTo \nFORTRAN \nPromts" > myprogram.in

myprogram < myprogram.in 

This works fine and I get the results I need from the FORTRAN program, but I would like to have a record (file.log) displaying the FORTRAN prompt (stdout) followed by the response (stdin), then another FORTRAN prompt and response.

So far I have only been able to come up with something like this;

echo -e "Responses \nTo \nFORTRAN \nPromts" > myprogram.in

tee file.log < myprogram.in | myprogram >> file.log

which produces a file.log like the following example;

Responses
To
FORTRAN
Prompts

This is the opening prompt for myprogram

"Please enter something"
You replied "Responses"

"Please enter something else"
You replied "To"

etc...

I understand that basically what I have done is used tee to copy the stdin to the file.log and pipe it into myprogram as well. Then just have redirected the program prompts to the file.log.

Is there anyway possible to redirect the responses and prompts to be redirected to the program simultaneously so the questions and responses are in chronological order in my log file like this;

This is the opening prompt for myprogram

"Please enter something"
Responses

You replied "Responses"

"Please enter something else"
To

You replied "To"
etc....

Upvotes: 0

Views: 1764

Answers (3)

MarcoS
MarcoS

Reputation: 17711

Try this:

#!/bin/bash

exec 6<&0
exec 6>stdin.log

echo -e "Responses\nTo\nFORTRAN\nPrompts" > myprogram.in
tee file.log < myprogram.in | ./myprogram >> file.log

In stdin.log you should have a log of your inputs and of your program...

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207355

I am not exactly sure what you want, but think the following could be made to work, using "expect". First, I simulate your FORTRAN program crudely with some Perl - of course you would use your own real FORTRAN program:

Filename: FORTRAN_prog

#!/usr/bin/perl
use strict;
use warnings;
print "FORTRAN Question 1:";
my $name = <STDIN>;
print "FORTRAN Question 2:";
$name = <STDIN>;
print "FORTRAN Question 3:";
$name = <STDIN>;
for(my $i=0;$i<10;$i++){
print "FORTRAN output $i\n"
}

Then I run and control and log it using "expect", like this:

#!/usr/bin/expect
spawn ./FORTRAN_prog
expect "Question 1:"
send "Answer 1\n"
expect "Question 2:"
send "Answer 2\n"
expect "Question 3:"
send "Answer 3\n"
interact

The output looks like this, which I presume you can copy/paste, or capture using the "script" command before running it (see my previous answer).

FORTRAN Question 1:Answer 1
FORTRAN Question 2:Answer 2
FORTRAN Question 3:Answer 3
FORTRAN output 0
FORTRAN output 1
FORTRAN output 2
FORTRAN output 3
FORTRAN output 4
FORTRAN output 5
FORTRAN output 6
FORTRAN output 7
FORTRAN output 8
FORTRAN output 9

If this doesn't do quite what you want, you could have a look at the "send_tty" command inside "expect" to send an extra copy of program inputs to the terminal - maybe.

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207355

You might try the "script" program - it may or may not help depending on how your FORTRAN program addresses the screen. Basically, you run:

script somefilename

and then run your program as before. When you are done, hit Ctrl-D. All the session is recorded in the file you specified after the word "script".

Or, depending on your terminal, you may be able to type "Ctrl-A" after you have run your program to select all the input and output, and then you may have a menu option to copy it all so you can paste it into a document.

Upvotes: 1

Related Questions