2

I am trying to run from a shell script a C++ program that print some outputs (using std::cout), and I would like to see them in the console while the program is running.

I tried some things like this :

RES=`./program`
RES=$(./program)

But all I can do is to only display the result at the end : echo $RES...

How to display the outputs in run-time in the console, AND in the variable RES ?

1
  • 3
    Try man tee, and man tty. Then you can split the output stream so it is reproduced to the actual terminal. Commented Oct 27, 2016 at 8:14

5 Answers 5

2
TTY=$(tty);
SAVED_OUTPUT=$(echo "my dummy c++ program" | tee ${TTY});
echo ${SAVED_OUTPUT};

prints

my dummy c++ program
my dummy c++ program

First we save off the name of the current terminal (because tty doesn't work in a pipeline).

TTY=$(tty)

Then we "T" the output (a letter T looks like one stream in at the bottom, 2 out at the top, and comes from the same "plumbing" metaphor as "pipe"), which copies it to the filename given; in this case the "file" is really a special device representing our terminal.

echo "my dummy c++ program" | tee ${TTY}
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, using all-caps names for your own variables is bad form; per the relevant POSIX standard's guidelines on environment variable naming, all-caps names are used for variables with meaning to the operating system and the shell, whereas names with at least one lower-case character are reserved for application use. Since setting a shell variable overwrites any like-named environment variable, this applies even when not explicitly exporting content to the environment.
0
RES=( $(./program) )
echo ${RES[@]}

you can try in this way

6 Comments

Content is still captured, not streamed live (as the OP is asking for). Also, if your program outputs *, your echo will have not that * but a list of files in the current directory -- surely not correct behavior. Similarly, if your program outputs newlines or tabs, they'll be emitted as spaces instead by this code.
can you paste your cpp output
An answer that only works for the OP's output isn't helpful to anyone but the OP. Part of the point of StackOverflow is to have a knowledgebase that's useful to everyone; that's why building robust answers (that work even in unexpected circumstances, such as when a program has wildcards or newlines) is important. Anyhow, for a command that generates outputs that will generate the bugs described, consider using the following in place of ./program for test purposes: printf '%s\n' '*' $'hello\t\tworld'; sleep 5; echo "post-streaming content"
For a fully correct implementation, one would see the first content without the 5-second delay, and the other content would have its original formatting.
you can use this method , i don't know whether this is your result or not
|
0

You can use a temporary file

./program | tee temp
RES=$(< temp)
rm temp

You can generate a temporary file with unique name by using mktemp.

Comments

0
 res=$(sed -n 'p;' <<< $(printf '%s\n' '*' $'hello\t\tworld'; sleep 5; echo "post-streaming content")&;wait)     
 echo $res
 #output
 *
 hello           world
 post-streaming content

6 Comments

That still doesn't print anything -- even the first two lines -- until after the 5-second delay is complete. And &; is invalid syntax in POSIX sh and most if not all released versions of bash (because & itself is a command separator) -- which one are you testing with?
bash --version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
...but are you sure that's actually your running shell? I'm guessing that you're using zsh -- unlike bash, &; is valid there.
Try running echo "$BASH_VERSION"; if that doesn't print anything, then echo "$ZSH_VERSION".
...that also explains why the * is literal for you -- zsh doesn't follow POSIX rules on expansion behavior.
|
0
[sky@kvm35066 tmp]$ cat test.sh 
#!/bin/bash
echo $BASH_VERSION
res=$(printf '%s\n' '*' $'hello\t\tworld'; sleep 5; echo "post-streaming content")
echo "$res"

[sky@kvm35066 tmp]$ bash test.sh 
4.1.2(1)-release
*
hello           world
post-streaming content

i think the result is correct, and it is what you want

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.