Sample Header Ad - 728x90

Simulated Execution of Redirections

3 votes
2 answers
101 views
My shell is the Korn Shell, but I think the problem (and its eventual remedy) applies to other shells as well. I usually build a "simulation mode" into my scripts to test potentially destructive/state-changing commands up front. Like this: # export SIMULATE='print -' [...] $SIMULATE /path/to/hazardous_command When the first line is uncommented the command is not executed but only printed instead. The problem I have is: if the command in question involves some redirection this won't work: $SIMULATE /path/to/hazardous_command > /path/to/file 2>/path/to/other_file Is there any elegant(!) solution I haven't been thinking of as now? I know I could do: if [ -n "$SIMULATE" ] ; then $SIMULATE "/path/to/hazardous_command > /path/to/file 2>/path/to/other_file" else /path/to/hazardous_command > /path/to/file 2>/path/to/other_file fi but this is not really making the code clearer and I would prefer a "plug-in" solution of some sorts, although up to now I came up short. I have also thought about (uncomment either of the export-statements): # export SIMULATE='eval ' # export SIMULATE='print -' [...] $SIMULATE "/path/to/hazardous_command >/path/to/file 2>/path/to/other_file" this would work but this replaces the problem of the redirections with the problem of the quotes: what if the command in question involves quoted strings? Escaping the quotes would work, but ultimately I would still have to maintain two different versions of the command (one to be printed, one to be executed). So, has anybody an idea how to overcome this?
Asked by bakunin (683 rep)
Dec 13, 2024, 09:20 AM
Last activity: Dec 13, 2024, 04:44 PM