Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

batch file - Single quotes and double quotes: How to have the same behaviour in unix and windows?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...