I have stucked with a bash scipt which should write both to stdout and into file. I'm using functions and some variables inside them. Whenever I try to redirect the function to a file and print on the screen with tee I can't use the variables that I used in function, so they become local somehow. Here is simple example:
#!/bin/bash
LOGV=/root/log
function var()
{
echo -e "Please, insert VAR value:\n"
read -re VAR
}
var 2>&1 | tee $LOGV
echo "This is VAR:$VAR"
Output:
[root@testbox ~]# ./var.sh
Please, insert VAR value:
foo
This is VAR:
[root@testbox ~]#
Thanks in advance!
EDIT:
Responding on @Etan Reisner suggestion to use
var 2>&1 > >(tee $LOGV)
The only problem of this construction is that log file dosn't receive everything...
[root@testbox~]# ./var.sh
Please, insert VAR value:
foo
This is VAR:foo
[root@testbox ~]# cat log
Please, insert VAR value: