Seeing input that you redirect to a program on the shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seeing input that you redirect to a program on the shell
# 1  
Old 02-14-2013
Seeing input that you redirect to a program on the shell

Suppose I have a program that I've written that accepts input, ie this C++ program:
Code:
#include <iostream>

using namespace std;

int main()
{
    cout << "Enter something:" << endl;
    int x;
    cin >> x;
    cout << "You entered data" << endl;
}

Suppose that I have a text file, in.txt containing this:
1

Now I run my program on the command line (supposing I named it input.exe
Code:
input.exe < in.txt

If I do this, I see the "Enter something" and the "You entered", but I will not see the entered 1 show up where the cin took place.

How do I get the 1 to print in between them (without modifying the C++ program)? I know that some tools such as expect will do this, but I've found expect to be a pain to get it to not pause when it doesn't match a regular expression. Printing input to programs would be really useful while grading C++ programs for students when testing with a lot of inputs.

Here's my Linux version:
Code:
% cat /etc/*-release
Red Hat Enterprise Linux Workstation release 6.3 (Santiago)
Red Hat Enterprise Linux Workstation release 6.3 (Santiago)

I'm currently using this shell:
Code:
% echo $SHELL
/bin/tcsh


Last edited by Franklin52; 02-15-2013 at 03:47 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 02-15-2013
Well, you can 'tee -a |' what is flowing into stdin to /dev/tty or the log, but it will not be in sync, as stdin as a buffered FILE* that often reads ahead, especially if stdin is not tied to a terminal but a pipe. Modifying the C++ is the only way, and if it is worth saying that you are asking and that you got something, then it is worth saying what you got.
Code:
cout << "You entered data '" << x << "'." << endl;

In the world of debug tools, you can strace/truss/tusc the shell running this and all its children and see what is going on, as it shows all system calls and on some systems (Solaris truss -u'*' ) can also show library calls.

Last edited by DGPickett; 02-15-2013 at 01:30 PM..
# 3  
Old 02-15-2013
Modifying C++ files is not effective, because there are 45 of them per assignment, and the goal is to save time by generating a "report" of people's output. The tee may be the best solution as it would show the input directly above the output (with input stripped out there).

I was trying to get them to show together using expect, as below.
However, when a regular expression does not match, it pauses and waits for me to manually type the desired input. This doesn't work well if I set it loose on a large batch of files to grade.

Where should I post this tcl/expect problem so that someone knowledgeable might see it? a.out is the executable.

#!/usr/bin/expect
set one [lindex $argv 0]
set two [lindex $argv 1]
set three [lindex $argv 2]
set four [lindex $argv 3]

spawn "./a.out"

expect -nocase -regexp "(.*Enter.*|.*Input.*)" { send -- "$one\r" }
expect -nocase -regexp "(.*)" { send -- "$two\r" }
expect -nocase -regexp "(.*Enter.*|.*Input.*)" { send -- "$three\r" }
expect -nocase -regexp "(.*)" { send -- "$four\r" }

interact
# 4  
Old 02-15-2013
If you run under script, you have a full transcript: Man Page for script (opensolaris Section 1) - The UNIX and Linux Forums
# 5  
Old 02-15-2013
I gave it a quick try, but it did not show the input alongside the output, since this command will only record what showed up on my console window, which does not display input within output.

% script -a script.txt
Script started, file is script.txt

% a.out < in.txt
Enter something:
You entered data

% exit

exit
Script done, file is script.txt

$ cat script.txt
Script started on Fri 15 Feb 2013 04:13:39 PM EST

% a.out < in.txt/script
Enter something:
You entered data

%

exit

Script done on Fri 15 Feb 2013 04:13:47 PM EST
Script started on Fri 15 Feb 2013 04:14:48 PM EST

%
Enter something:
You entered data

%

exit
# 6  
Old 02-15-2013
You must have the stty set to no echo in full duplex or raw. Did the C++ do that?

Command autoexpect records everything in and out into an expect script, so you can do it all over again unless there are time artifacts. It creates an intermediate pseudo tty.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write a shell program with input

Hi, Here is my question: I want a shell script which I name as 'del', and can be used as del(string). when run del(string), it will delete several directories at different locations in my system,like: rm -fr /lustre/fs/scratch/user/$string rm -fr /home/user/$string rm -fr... (4 Replies)
Discussion started by: 1988PF
4 Replies

2. Shell Programming and Scripting

Shell script to input as if from command line to the java program

Hi, We are having a java server which can run on command line and once initiated, it will prompt options to enter from 0 to 5. The java program kickoff respective operation once number is entered between 0 to 5. However i want to always enter "1" and write another shell program wrapper to start... (4 Replies)
Discussion started by: surya5kn
4 Replies

3. UNIX for Dummies Questions & Answers

how to pass input from c program to shell script?

Hello.. I am developing a Graphical User Interface using GTK. As part of our project I need to take inputs from GTK entries and pass those inputs to shell script and use them in shell script. The problem which struck me is only limited number of inputs are getting passed to shell script. For now... (14 Replies)
Discussion started by: kalyanilinux
14 Replies

4. Shell Programming and Scripting

Shell variable to c++ program as input

I have an Shell script which has few global variables eg : range=100; echo "$range" I want to use the same variable in my C++ program for example int main() { cout << range << "\n"; } i tried using this int main(int argc, char *argv) { cout << range << "\n"; } but... (5 Replies)
Discussion started by: shashi792
5 Replies

5. Shell Programming and Scripting

input stored procedure to shell program

Hello, I have to call the stored procedure as argument from the unix shell program. Looks like unix doesnt like, can someone comment pls USERID=scott PASSWD=xxxxxx PLSQLCALL=$2 STDT=`sqlplus /nolog <<END >> $LOGFILE conn ${USERID}/${PASSWD}@${ORACLE_SID} whenever sqlerror exit failure... (9 Replies)
Discussion started by: tvanoop
9 Replies

6. Programming

Redirect input and output to a shell script?

Dear All: I am trying to do something that (I thought) was relatively straightforward, but my code snippet does not seem to work. Any suggestions? Thank you Sincerely yours Misha Koshelev #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include... (0 Replies)
Discussion started by: misha680
0 Replies

7. Shell Programming and Scripting

Launching a C program that needs input from a shell script

hi there, i need some help, i am trying to run a script to launch a C program and a Java program but before running both I want to get a user input and then invoke both programs with input received. In the programs the inputs are not command line arguments. This is the code, after the java... (4 Replies)
Discussion started by: momal
4 Replies

8. UNIX for Dummies Questions & Answers

redirect input from file?

I need to run a command inside root-environment, and want to use: su - root -c "some command..." Then I am prompted for a password, of course. Is it possible to put this password in a file, and present this files content, to the command above?:confused: (1 Reply)
Discussion started by: bjornrud
1 Replies

9. Shell Programming and Scripting

redirect exception messages from program

Hi, I have a script which invoke a java program and I would like to know whether it is possible to log the exception thrown by the program to a log file in the script? Thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

10. Shell Programming and Scripting

please help: how to redirect input into a variable

I'm trying to write a simple script program (C shell). I have a problem redirecting input into a variable. Say I have a variable called J, and there is file called result which contains just some number, say 5. Which command should I use to assign J value 5 from the file result. I tried the... (2 Replies)
Discussion started by: artur80
2 Replies
Login or Register to Ask a Question