1

I have a batch script that call an exe with some parameters.

Currently I was passing the parameters to my exe like that:

$>my_cmd.exe %*

One of the options of the my_cmd.exe program takes arguments that can contain spaces

$>my_cmd.bat --req "in: lava"  (OK my prog receives in: lava)

$>my_cmd.bat --req 'in: lava'  (NOK my program receives 'in: lava')

Users use indifferently single quotes or double quotes.

It works with double quotes because they are eaten at the batch script level but when they use ' (single quotes) it is left and passed to my program.

my_cmd is multiplatform and on unix both single quote and double quote are special characters.

I would like to avoid having to do something specific in my_cmd program depending on the platform.

Is there a way to have the same behaviour in shell scripts and batch scripts. For example the batch script could eat single quote if they are present ?

Tell me what would be the best solution for you.

Many thanks

zoobert
  • 552
  • 1
  • 5
  • 13
  • 1
    My solution for this kind of problem is to use something that is present in Windows and Unix (instead of trying to make 2 different interpreters behave the same way). For example: Perl or Ruby. Or switch to Cygwin and use Bash on both sides. Actually, this is how i got into Perl programming, when i started. – theglauber May 24 '12 at 12:38
  • 1
    This is what I do as I use Python but I still need to pass parameters to the python program. Problem is that on Windows when the option is --req 'in: lava' I get on the python side 'in: lava' as on Linux I get in:lava. So I need to trick my parsing classes to take care of single quote and I am not satisfied with that. – zoobert May 24 '12 at 12:47
  • Yes, quote handling in Windows is an annoyance. Try "cmd /?" and read the help text carefully. The /s switch may help. – theglauber May 24 '12 at 13:34
  • ... or not... i couldn't get your example to work. The best reference i've found so far is [[here](http://stackoverflow.com/a/4094897/1118101)]. – theglauber May 24 '12 at 14:01
  • Thanks for the reference. What do you mean, you could not get my example to work ? – zoobert May 24 '12 at 14:18
  • I couldn't make the single-quotes in `python -req 'in: lava'` disappear, using different combinations of cmd with /c and /s. – theglauber May 24 '12 at 15:52

2 Answers2

1

On Windows, argument handling (and rules for quoting, globbing, etc) is the responsibility of the application. If your code uses anything except a single string containing all parameters with quotes intact, understand that this is because your development tools have done some preprocessing on the result of GetCommandLine. Therefore, for different quote handling, you need to look at your development tools, not at the OS. The best option is often to call GetCommandLine yourself and use your choice of library for processing it, instead of the one provided with your compiler.

That said, the Windows shell team has provided one of these libraries. See CommandLineToArgvW. But this is not part of the core OS, and using it is completely optional.

In addition, the batch processor does consider quotes when doing variable substitution. And that behavior is hard to change or disable, but it doesn't sound like it is the source of your problems.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Why not just change quotes to double?

set args=%*
my_cmd.exe %args:'="%
panda-34
  • 4,089
  • 20
  • 25
  • what happens what you want to quote a string containing quotes? `perl -e 'my $var = "this is a quoted string"; print $var;'` – Ben Voigt May 24 '12 at 18:01
  • Nothing good will come out of it, I was going on assumption that both quotes are used only as special characters as OP stated. – panda-34 May 24 '12 at 18:22