简体   繁体   中英

How do I write to a file and print to a terminal concurrently in Unix?

I have a little bash function to log my Macports outputs to a file (since installs often spew little tidbits that are easy to lose in terminal noise), then I just cat the file to the terminal:

function porti {
    command sudo port install $@ >> $1.log 2>&1; cat $1.log
}

Is there a way to do this concurrently?

BTW I pass $@ to install but only $1 for the file name so that I can do something like:

porti git-gore +bash_completion

and only get the file git-core.log however someone else might prefer to include variants in the file name...

The usual solution is to use tee(1) :

sudo port install $@ 2>&1 | tee -a $1.log

should do what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM