0

I'm trying to print output/error to console and log file using below code.

fun1(){

echo "inside fun1"

fun2

var=5

}

fun2(){

echo "inside fun2"

}

fun1 2>&1 | tee -a testlog.txt

echo $var # printing null value

Any way to print value of var as 5 after fun1 execution?? I need output in both console and log file. Also fun1 should be executed only once.

please help.

Anand Tomi
  • 181
  • 1
  • 1
  • 4

1 Answers1

0

If you write it as a bash script it is possible:

#!/bin/bash

exec 3>&1 1> >(tee testlog.txt) 2>&1

fun1(){
    echo 'inside fun1'
    fun2
    var=5
}

fun2(){
    echo 'inside fun2'
}

fun1
echo "$var"

If you rather want to do it as a normal bourn shell, you could separate it into two scripts:

script2.sh:

#!/bin/sh

fun1(){
    echo 'inside fun1'
    fun2
    var=5
}

fun2(){
    echo 'inside fun2'
}

fun1
echo $var

and script1.sh:

#!/bin/sh

script2.sh 2>&1 | tee testlog.txt
emil
  • 1,642
  • 13
  • 12