Bash script global redirection with exec and write to screen -
i have large bash script , want globally redirect stdout/stderr file, except progress indicators such specific echo messages. example, consider following script
#!/bin/bash # stdout , stderr go out.log exec &> out.log special_echo "starting" # many other commands go here # output both stdout , stderr # special_echo commands in here, # example echo "msg1" special_echo "middle" echo "msg2" >&2 special_echo "finished"
when run output should be
$ ./script starting middle finished $
but, out.log
should contain
msg1 msg2
how implement special_echo
functionality? i've tried using file descriptor , echoing can't display on screen.
is there way achieve without appending redirection every line or doing in answer?
yes, using file descriptor way go:
#!/bin/bash exec 3>&1 special_echo () { echo "$@" >&3 } exec &> out.log special_echo "starting" echo "msg1" special_echo "middle" echo "msg2" >&2 special_echo "finished"
Comments
Post a Comment