I would like to redirect the output stream of a command to a function so I did this:
function aClassicCommand {
sleep 1
echo "this is logs"
}
##############################
function logsReceiver {
read IN
echo "received logs: $IN"
}
aClassicCommand | logsReceiver &
# doing stuff in parallel
wait
The problem is that the logsReceiver
has no longer access to the external variables:
function aClassicCommand {
sleep 1
echo "this is logs"
}
##############################
var=0
function logsReceiver {
read IN
echo "received logs: $IN"
# prints "in subshell var=0" and not "in subshell var=1"
echo "in subshell var=$var"
}
aClassicCommand | logsReceiver &
var=1
echo "var=$var"
wait
The output is:
var=1
received logs: this is logs
in subshell var=0
but I want this:
var=1
received logs: this is logs
in subshell var=1
How can I solve it?
Thanks.