I'm working with a set of scripts with functions treated as readonly. The functions are more than just a list of commands, for example, there can be loops and change directories and even calls to other functions:
func() {
cd folder/
run command1
mkdir folder2/ ; cd folder2/
run command2
}
For a moment, pretending that I could change the scripts, to show you what I want one might look like this:
func() {
cd folder/
string[0]="command1" ; run command1 |& tee out0.log ; result[0]="$?" ; finished_command_number 0
mkdir folder2/ ; cd folder2/
string[1]="command2" ; run command2 |& tee out1.log ; result[1]="$?" ; finished_command_number 1
}
So, for commands which can be piped, but not for cd
or loops, I want to store a string, store the stdout (stderr), store the exit code, and run another command afterwards. However, I cannot add this in, it must be done in my script which is invoking the one with func().
To just get the post command call functionality alone, I've tried copying the function and running it from my script with a trap foo debug
, but that doesn't seem to propagate into functions. I don't think literally copying line by line will work because the cd
and loops
can't really be isolated since they are control statements rather than subshell commands.
If the strings can just be a function of the commands themselves, that's still useful.
-o functrace
option has been enabled with theset
builtin, (in which case all functions inherit the DEBUG and RETURN traps)"