|
From: Yuri K. <yur...@gm...> - 2014-06-27 11:35:22
|
> Let your script generate the .cmd file when it runs:
> echo "myprog \"arg 1\" arg2"> test.cmd
> cmd /c test.cmd # will execute myprog "arg 1" arg2
> rm test.cmd
That's slightly better than piping to cmd, but not as good as it could be.
With bash, it's generally better off passing arguments through command
line, rather then interpolating them into command to be executed (because
bash handles escaping by itself then):
a='some"string'
bash -c 'echo "'"$a"'"'
vs
a='some"string'
bash -c 'echo "$1"' -- "$a"
But since msys bash executes cmd and bat files by itself, this way is out
of the question:
tmp=`mktemp` && trap 'rm "$tmp" "$tmp.cmd"' EXIT
echo "hstart /elevate \"cmd /k \"\"%1\" ${NGINX_CONF_PATH:+-c \"%2\"}
-s reload\"\"" >"$tmp.cmd"
"$tmp.cmd" "$NGINX_PATH" ${NGINX_CONF_PATH:+"$NGINX_CONF_PATH"}
So the only advantage is that cmd doesn't pollute the output with
unnecessary noise:
qNGINX_PATH=$(echo "$NGINX_PATH" | sed -r 's/("|\\)/\\\1/g')
qNGINX_CONF_PATH=$(echo "$NGINX_CONF_PATH" | sed -r 's/("|\\)/\\\1/g')
tmp=`mktemp` && trap 'rm "$tmp" "$tmp.cmd"' EXIT
echo "@echo off & hstart /elevate \"cmd /k \"\"$qNGINX_PATH\"
${NGINX_CONF_PATH:+-c \"$qNGINX_CONF_PATH\"} -s reload\"\"" >"$tmp.cmd"
cmd /c "$(cd -- "$(dirname -- "$tmp")" && pwd -W)/$(basename --
"$tmp.cmd")"
Note, that I convert here path `/tmp/tmp....` to its windows form for cmd
to understand it.
And the solution without mktemp is as follows:
qNGINX_PATH=$(echo "$NGINX_PATH" | sed -r 's/("|\\)/\\\1/g')
qNGINX_CONF_PATH=$(echo "$NGINX_CONF_PATH" | sed -r 's/("|\\)/\\\1/g')
echo "hstart /elevate \"cmd /k \"\"$qNGINX_PATH\" ${NGINX_CONF_PATH:+-c
\"$qNGINX_CONF_PATH\"} -s reload\"\"" | cmd
As to interpolating variables into double quotes, quoting only " and \ must
be safe: http://x-yuri.tumblr.com/post/90048221548/cmd-quoting
Regards,
Yuri
|