Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
Show only stderr on screen but write both stdout and stderr to file
stdout , stderr and logging using the script command

cat test.sh:

rm -v foo.tmp
date
pwd

cat test2.sh:

script bar.log -c './test.sh'

When executing test2.sh I would like to see the following for example on my terminal:

Fri Feb 17 18:04:30 PST 2012  
/home/me/me

However in bar.log I would like to see:

removed 'foo.tmp'  
Fri Feb 17 18:04:30 PST 2012  
/home/me/me

or (in case foo.tmp does not exist)

rm: cannot remove 'foo.tmp'  
Fri Feb 17 18:04:30 PST 2012  
/home/me/me

Is this doable?

share|improve this question
add comment

marked as duplicate by Mat, Caleb, Gilles, Michael Mrozek Feb 23 '12 at 7:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

This is difficult to do directly because script collects all the output on a temporary terminal and displays that appears on that terminal on its own standard output.

In test.sh, you need to have some way to distinguish the messages you want on the terminal from the messages you only want in the transcript. For example, add a prefix to each line indicating its importance level. As the other lines in your example don't start with @, I'll just prefix non-essential (transcript-only) lines with @.

rm -v foo.tmp | sed 's/^/@/'
date
pwd

And in the wrapper script:

script bar.log -c './test.sh | grep -v "^@"'

I don't think you can do much better without modifying the script utility. (If you want to go that route, I think the simplest method would involve a few lines of expect.)

share|improve this answer
add comment

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