tags:

views:

1938

answers:

3

I have a shell script that I want to execute this line:

qtvars.bat vsstart "qt.sln" /BUILD "Debug|Win32"

This works fine (though I had to modify qtvars.bat, but that's beside the point). The problem is that I want the command to execute to be in a variable: EDIT: This doesn't work either, if I type it into bash. Previously I was typing it into cmd.exe, which hardly made for a fair comparison.

command="qtvars.bat"
args="vsstart"

$command $args "qt.sln" /BUILD "Debug|Win32"

Now it chokes on the pipe! I get this message:

'Win32' is not recognized as an internal or external command,
operable program or batch file.

I've tried a bunch of forms of escaping the quotes and/or pipe, all to no avail. Interestingly, it works when it's an executable rather than a batch file, e.g.:

command="devenv.exe"
args=""

$command $args "qt.sln" /BUILD "Debug|Win32"

Thanks for any ideas.

+1  A: 

Hello,

This is a classic case of double-escaping, where both bash and CMD.EXE need to be instructed to ignore the special | (pipe) character.

Try the following:

$command $args "qt.sln" /BUILD '"Debug|Win32"'

This will be the equivalent of you typing, at a CMD.EXE prompt:

qtvars.bat vsstart qt.sln /BUILD "Debug|Win32"

Using the above, you are essentially forcing the passing of the double-quotes on to CMD.EXE (instead of bash eating them away.) The outermost single quotes instruct bash not to interpret or touch in any way what's inside them; the inner double-quotes instruct CMD.EXE to ignore any special characters (the pipe in this case) within.

Alternatively, you can also try:

$command $args "qt.sln" /BUILD 'Debug\|Win32'

This should be the equivalent of you typing, at a CMD.EXE prompt:

qtvars.bat vsstart qt.sln /BUILD Debug\|Win32

Note the use of single quotes (!), which ensure that bash will not interpret the \ (and, instead, will pass it as-is to CMD.EXE.)

Cheers, V.

vladr
Tried those. Did it again just now to be sure. They result in the same error, though sometimes 'Win32' becomes 'Win32\""' in the error message.
Owen
try to set command to "cmd.exe qtvars.bat"
vladr
A: 

Interesting! What does escaping the | do?

Do these work?

  • echo "Debug|Win32"
  • echo "qt.sln" /BUILD 'Debug|Win32'
dirkgently
Do you mean:What is the intent? To have bash or cmd.exe pass the | through as part of the argument, rather than trying to process it as pipe syntax.orWhat is the result in my situation? Nothing.
Owen
I wanted to know what was the result of using escape?
dirkgently
A: 

I'm really thinking this is a bug in Cygwin's bash.

It still fails when I rewrite the line like this:

eval "$command $args \"qt.sln\" /BUILD \"Debug|Win32\""

So taking Vlad R's mention of CMD.EXE as a hint, I tried this instead:

cmd.exe /c "$command $args \"qt.sln\" /BUILD \"Debug|Win32\""

And it works. Oddly enough, without the .exe, it doesn't work. Oh, and now it gives me grief if $command has spaces in it. Yes, I've tried putting \" on either side of it and/or running it through cygpath -w first. That's for another day because I've wasted enough time on this triviality already! (I can get around it all by just specifying Debug instead of Debug|Win32.)

Owen