How to keep return code and log of sub function in bash -


i want run sub function(with interactive operation read) , keep it's log.

original

#!/bin/bash foo() {   echo "error"   return 1 } bar() {   local data   read -p "data=" data   echo "ok: $data"   return 0 } foo  echo "return code=$?" bar echo "return code=$?" 

after keep log

#!/bin/bash foo() {   echo "error"   return 1 } bar() {   local data   read -p "data=" data   echo "ok: $data"   return 0 } log=my.log foo | tee -a $log echo "return code=$?" bar | tee -a $log echo "return code=$?" 

use named pipe send return value:

#!/bin/bash foo() {   echo "error"   return 1 } bar() {   echo "ok"   return 0 }  rm -f retcode trap 'rm -f retcode' exit mkfifo retcode  log=my.log {      foo      echo $? > retcode } | tee -a $log & read rc < retcode echo "return code=$rc" {     bar     echo $? > retcode } | tee -a $log & read rc < retcode echo "return code=$rc" 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -