单引号和双引号:如何在UNIX和Windows中具有相同的行为? [英] Single quotes and double quotes: How to have the same behaviour in unix and windows?

查看:74
本文介绍了单引号和双引号:如何在UNIX和Windows中具有相同的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,该脚本使用一些参数调用exe.

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

当前,我正在像这样将参数传递给我的exe:

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

$>my_cmd.exe %*

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是多平台的,在Unix上,单引号和双引号都是特殊字符.

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

根据平台,我希望避免在my_cmd程序中做特定的事情.

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

有没有办法在Shell脚本和批处理脚本中具有相同的行为. 例如,如果批处理脚本存在,则批处理脚本可以使用单引号吗?

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.

非常感谢

推荐答案

在Windows上,应用程序负责参数处理(以及引号,通配符等规则).如果您的代码使用除包含完整带引号的所有参数的单个字符串以外的任何内容,请理解这是因为您的开发工具已经对GetCommandLine的结果进行了一些预处理.因此,对于不同的报价处理,您需要查看开发工具,而不是操作系统.最好的选择通常是自己调用GetCommandLine并使用您选择的库来处理它,而不是编译器随附的库.

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.

也就是说,Windows Shell团队提供了这些库之一.请参阅 CommandLineToArgvW .但这不是核心操作系统的一部分,使用它是完全可选的.

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.

这篇关于单引号和双引号:如何在UNIX和Windows中具有相同的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆