Search code examples
unixechokshtee

Writing a log file with tee


There is my problem, I work with ksh script and i try to make a log file with function tee and with :

  • informations that will be shown on screen

  • informations that will not be shown on screen.

So i used tee to handle all echo in my script and i wanted to redirect output from specific echo to go only in log file.

foo(){
        echo Hello               # screen + log file
        echo World >> "tee.txt"  # only log file
        echo !                   # screen + log file      
}             
rm -f "tee.txt"
foo | tee -a "tee.txt" 

Screen output

Hello
!

That's okay.

Log file

But in log file :

World
Hello
!

echo redirection writed before tee.

Conclusion

So, does there is something that is like echo who will only store output and not showing it to screen ? To get that output in log file with tee ?

Hello
World
!

Solution

  • Try this. It worked for me on ksh.

    rm -f "tee.txt"
    
    foo()
    {
        echo Hello
        stty -echo
        echo World >> "tee.txt"
        stty echo
        echo !
    }
    foo | tee -a "tee.txt"