After the question In a shell script: echo shell commands as they are executed I wonder how can I redirect the command executed/echoed to a file (or a variable)?
I tried the usual stdout redirection, like ls $HOME > foo.txt
, after setting the bash verbose mode, set -v
, but only the output of ls was redirected.
PS: What I want is to have a function (call it "save_succ_cmdline()") that I could put in front of a (complex) command-line (e.g, save_succ_cmdline
grep -m1 "model name" /proc/cpuinfo | sed 's/.*://' | cut -d" " -f -3
) so that this function will save the given command-line if it succeeds.
Notice that the grep -m1 "model name" ...
example above is just to give an example of a command-line with special characters (|,',"). What I expect from such a function "save_succ_cmdline()" is that the actual command (after the function name, grep -m1 "model name"...
) is executed and the function verifies the exit code ([$? == 0]
) to decide if the command-line can be save or not. If the actual command has succeeded, the function ("save_succ_cmdline") can save the command-line expression (with the pipes and everything else).
My will is to use the bash -o verbose
feature to have and (temporarily) save the command-line. But I am not being able to do it.
Thanks in advance.