Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to make a demo Virtual Machine. On the desktop, I have a script foo.sh and it outputs stdout to foo.log (e.g. with some command > foo.log)

When I double click on the script, it generates the foo.log. However, I would like for the script to also present the stdout in real time (e.g. using tail -f foo.log).

How can I do this? Do I need to pipe the output to two places?

share|improve this question

3 Answers 3

This may not be the most elegant approach, but it appears to work:

Add the following to the script:

touch foo.log
uxterm -e "tail -f foo.log" &
some command > foo.log

with gnome-terminal, replace uxterm -e with gnome-terminal -x.

Thanks to the Hilltop Yodeler for directing me to this solution.

share|improve this answer
    
I was posting that sort of information. Use gnome-terminal -e to call your script, within your script use tee, but tail works as well. –  bodhi.zazen Jan 24 '12 at 22:53

Unix's standard tee command allows you to redirect the command's standard input to one or more files and also print it to the standard output. So you can do something like

myscript.sh|tee mylog.log

provided that myscript.sh just prints to standard output (which would actually simplify it)

To open a terminal windows when you click on icon, you can wrap it in another script:

xterm -e "myscript.sh | tee mylog.log; read -p FINISHED"

The difference with your solution is that you have a choice to either close the terminal window when the script finished or display "Press any key to continue..." message - with tail -f the terminal basically has no way of knowing when your script finished so you have to interrupt it with Ctrl-C.

Also, tail -f must be less efficient that printing directly to terminal, but this is probably not very important these days.

share|improve this answer
    
nope, that does not work; it neither writes a log file nor opens the terminal. Note that I am not executing the script from the terminal, I am executing it by clicking on the desktop icon that represents the script. –  David Jan 24 '12 at 22:30
    
The original solution was answering your question "how to pipe the output in two places" - to open the script in a terminal window you use xterm -e or any other terminal program. I updated the answer. –  Sergey Jan 24 '12 at 23:03

I did Sergy's suggestion and if it worked, it may be a path problem. Here is the exact command I used and the result was an xterm opening, showing the output of the script and then says "Press return to close ". I suggest you add path in front of the script name, and possibly the log file name.

xterm -e "./myscript.sh | tee ./mylog.log; read -p Press\ return\ to\ close\ "

All my version of myscript.sh did was to echo several lines, but it worked.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.