Register
It is currently Thu Jul 24, 2014 8:37 am

STDIN and STDOUT to file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Nov 02, 2011 5:39 pm   

Joined: Wed Nov 02, 2011 5:17 pm
Posts: 1
Hi, I only know basic bash —just enough to get by for my day-to-day needs.

Basically I am learning Java at Uni, and they want example outputs.
Code:
java MyProgram | tee output.txt

is what I'm using at the moment, but this only grabs the STDOUT, but not the STDIN data I'm typing into the file.

Is there a way to get both STDIN and OUT into the same file, in the correct place?

a simple (stupidly pointless) example to show what I mean; say MyProgram simply asks for your favourite number, and you type in '5':

Code:
$ java MyProgram | tee output.txt && echo "#####" && cat output.txt

Welcome.
Enter your favourite number: 5
You entered: 5
Thank You.

#####

Welcome.
Enter your favourite number: You entered: 5
Thank You.


As you can see, output.txt only contains the STDOUT, no input, and generally there wasn't a newline.
Whereas I would like to include the STDIN in the file

Code:
$ java MyProgram {some code} | tee output.txt && echo "#####" && cat output.txt

Welcome.
Enter your favourite number: 5
You entered: 5
Thank You.

#####

Welcome.
Enter your favourite number: 5
You entered: 5
Thank You.


As you can see the input was recorded into the file too, including the newline character.

If this isn't possible from this way of thinking, perhaps if there is a command which copies everything — like a print screen, but for the command line?

I honestly have not idea, any insight and guidance will be much appreciated.
Thank You.


Top
 Profile  
 PostPosted: Thu Nov 03, 2011 4:23 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I can't think of a way directly of capturing STDIN for an interactive session, but there are a few ways to capture the terminal output.
Firstly your terminal software will have an option for capturing the screen output. On Konsole the option is File->Save output as
all this does is capture whatever is on the screen (and in the scrollback buffer) to a txt file.

When I do production work I often use the screen (-L switch for logging) and script commands to log what I'm doing. I suggest you check the man pages for these.

All of these have minor issues, Konsole's output save does not capture hidden characters and this makes the displaying the information afterwards look "clean".
script and screen do capture hidden characters but this can also make the output less clear as the software used to display the txt file (eg vi or less) will not interpret the hidden characters in the same way as the terminal software.

DW


Top
 Profile  
 PostPosted: Thu Nov 03, 2011 4:24 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Code:
tee STDIN | (echo Enter your favourite number: ; read x; echo Your favourite number is $x) | tee STDOUT

Note that you have to close STDIN after the program is completed (press Ctrl+D).


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP