I want a script to redirect stdout and stderr to a file, do a bunch of stuff, then undo those redirections and take action on the file contents. I'm trying:
function redirect(){
exec 3>&1
exec 4>&2
exec 1>outfile 2>&1
}
function undirect(){
exec 1>&3
exec 2>&4
}
echo first
redirect
echo something
cat kjkk
undirect
if some_predicate outfile; then echo ERROR; fi
Which seems to do what I want, but it seems rather complex. Is there a cleaner/clearer way to do this?
{ echo something; cat afile; } >outfile 2>&1
– Nearsighted