Search code examples
bashtee

Bash output from expect script to two different files


I am trying to output to two different files using tee. My first file will basically be tail -f /myfile and my second output will be a subset of the first file. I have looked online that they were saying we can use `|

tee >(proc1) >(proc2)

I have tried the above but both my files are blank.

Here is what i have so far:

myscript.sh

ssh root@server 'tail -f /my/dir/text.log' | tee >(/mydir/my.log) >(grep 'string' /mydir/my.log > /mydir/mysecond.log)

myexpect.sh

#!/usr/bin/expect -f
set pass password

spawn /my/dir/myexpect.sh
expect {
    "key fingerprint" {send "yes/r"; exp_contiue}
    "assword: " {send "$pass\r"}
}
interact

Solution

  • In your script, there are some problems in the usage of tee,

    1. tee >(/mydir/my.log): can be substitute with tee /mydir/my.log, since tee would write to stdout and files, i.e. /mydir/my.log
    2. grep 'string' /mydir/my.log > /mydir/mysecond.log: as I mentioned, tee would also write to stdout, so no need to grep the string from file, you can grep from stdout directly. Use pipeline to do it.

    So the whole command shall be modified as followed,

    ssh root@server 'tail -f /my/dir/text.log | tee /mydir/my.log | grep --line-buffered "string" > /mydir/mysecond.log'
    

    Edit:

    For your further question

    • The command would hang because of tail -f was still waiting for output the growing file. If you don't want the command hanged, try to remove -f for tail.
    • Depends on the option -f existed for tail, you shall use two different way to allow the grep write file.
      • For tail case: grep can successfully write file
      • For tail -f case: --line-buffered for grep would use line buffering on output