Note that there are some explanatory texts on larger screens.

plurals
  1. POQuoting command-line arguments in shell scripts
    text
    copied!<p>The following shell script takes a list of arguments, turns Unix paths into WINE/Windows paths and invokes the given executable under WINE.</p> <pre><code>#! /bin/sh if [ "${1+set}" != "set" ] then echo "Usage; winewrap EXEC [ARGS...]" exit 1 fi EXEC="$1" shift ARGS="" for p in "$@"; do if [ -e "$p" ] then p=$(winepath -w $p) fi ARGS="$ARGS '$p'" done CMD="wine '$EXEC' $ARGS" echo $CMD $CMD </code></pre> <p>However, there's something wrong with the quotation of command-line arguments.</p> <pre><code>$ winewrap '/home/chris/.wine/drive_c/Program Files/Microsoft Research/Z3-1.3.6/bin/z3.exe' -smt /tmp/smtlib3cee8b.smt Executing: wine '/home/chris/.wine/drive_c/Program Files/Microsoft Research/Z3-1.3.6/bin/z3.exe' '-smt' 'Z: mp\smtlib3cee8b.smt' wine: cannot find ''/home/chris/.wine/drive_c/Program' </code></pre> <p>Note that:</p> <ol> <li>The path to the executable is being chopped off at the first space, even though it is single-quoted.</li> <li>The literal "\t" in the last path is being transformed into a tab character.</li> </ol> <p>Obviously, the quotations aren't being parsed the way I intended by the shell. How can I avoid these errors?</p> <p>EDIT: The "\t" is being expanded through two levels of indirection: first, <code>"$p"</code> (and/or <code>"$ARGS"</code>) is being expanded into <code>Z:\tmp\smtlib3cee8b.smt</code>; then, <code>\t</code> is being expanded into the tab character. This is (seemingly) equivalent to</p> <pre><code>Y='y\ty' Z="z${Y}z" echo $Z </code></pre> <p>which yields </p> <pre><code>zy\tyz </code></pre> <p>and <em>not</em></p> <pre><code>zy yz </code></pre> <p>UPDATE: <code>eval "$CMD"</code> does the trick. The "<code>\t</code>" problem seems to be echo's fault: "If the first operand is -n, or if any of the operands contain a backslash ( '\' ) character, the results are implementation-defined." (<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html" rel="nofollow noreferrer">POSIX specification of <code>echo</code></a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload