This part 2>
is obviously shell syntax and is intended only to work from the context of the shell interpreter. So, what to do when it is NOT run from the shell, just a simple "command with arguments" interface (for example, env
, xargs
, docker run
, etc.)?
Needless to say, the obvious workaround wrapping it all in sh -c
is a terrible antipattern: quoting and escaping is hard to do right, most programmers won't even try, resulting in a fragile code and a potential security hole. This would be an unreasonable complication to indicate the output file and a clear sign that you are doing something wrong.
A wrapper script allows you to solve the problem correctly.
#!/bin/sh exec " $@ " 2> result.xml
... but it will be a file, and this can be a complication in itself. Fortunately, this script can be written as a string:
sh -c 'exec "$0" " $@ " 2> result.xml' cppcheck β¦
Now it is presented in the form of a simple list of arguments and, therefore, works in all shells, as well as in non-shells such as docker run
.
source share