将包含 *(星号)字符的字符串作为 Windows Shell 中的命令行参数传递
我正在寻找一种通过命令行将包含“*”字符的字符串传递给可执行文件的方法。
command.exe 3*2
我想传递3*2的字符串。 Windows 所做的是在当前目录中搜索与文件掩码“3*2”匹配的文件,并将找到的任何文件传递给 command.exe
将“3*2”放在双引号之间并没有帮助,仍然是同样的问题。
我还尝试了“3*2”(在单引号之间),但随后整个字符串(包括单引号)被传递,这不是我需要的。
有没有办法将字符串 3*2 (不带任何引号)传递给命令?
I am looking for a way to pass a string containg the "*" character to an executable via the command line.
command.exe 3*2
I want to pass the string 3*2. What Windows does instead is search the current directory for files matching the file mask "3*2", and passes any files it found to command.exe
Putting "3*2" between double quotes does not help, still the same problem.
I also tried '3*2' (between single quotes), but then this whole string (including the single quotes) is passed, which is not what I need.
Is there any way to pass the string 3*2 (without any quotes) to the command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 命令 shell 中,您执行的命令负责扩展参数中存在的任何通配符。此行为与 Unix 及其类似版本不同,其中通配符扩展通常由 shell 完成。
一个简单的例子证明了这一点。
Windows (Windows 7):
如您所见,该命令输出的参数与命令行传入的参数完全相同。
Linux(CentOS Linux 5 上的 bash):
这里通配符参数由 shell 替换为当前目录中的文件/目录列表。
因此,如果您的可执行文件通过扩展通配符来处理通配符,则您无能为力。具体行为取决于您的命令。
如果您提供有关您的命令以及您想要实现的目标的更多详细信息,我们也许能够提供更多帮助。
In Windows command shells, the command you execute is responsible for expanding any wildcards present in the parameters. This behaviour is different to Unix and friends, where wildcard expansion is usually done by the shell.
A simple example demonstrates this.
Windows (Windows 7):
As you can see, the command outputs the parameter exactly as passed in by the command line.
Linux (bash on CentOS Linux 5):
Here the wild card parameter is substituted by the shell to a list of file/directories in the current directory.
So if your executable handles wildcard characters by expanding them, there is not much you can do about it. The concrete behaviour depends on your command.
If you provide more details about your command and what you want to achieve, we might be able to give some more help.
Windows 实际上将整个原始命令行作为单个字符串传递给程序;请参阅 GetCommandLine。当您编写
main(int argc, char **argv)
时,程序所链接的 C 运行时库负责将命令行拆分为argv
中的单词。因此,我们需要更多信息:您的 shell 是什么、如何调用命令以及您使用什么 C 运行时库?您看到的问题肯定不是 Windows 本身,我无法使用 cmd.exe 和 MSVC 的 CRT 重现它。
如果我使用 Cygwin 的 CRT,并从 Cygwin 的 Bash shell 运行,那么我也看不到问题。
只有当我尝试混合它们时 - 使用 Cygwin 的 CRT 从 cmd.exe 运行程序 - 我可能会看到你的问题,
但我没有看到双引号的问题。
Windows actually passes the entire, raw command line as a single string to a program; see GetCommandLine. When you write
main(int argc, char **argv)
, the C runtime library that your program links with is responsible for splitting up the command line into words in yourargv
.So we need more information: what is your shell, how are you invoking your command, and what C runtime library are you using? The issue you're seeing definitely isn't Windows itself, and I can't reproduce it here with cmd.exe and MSVC's CRT.
If I use Cygwin's CRT, and run from Cygwin's Bash shell, then I don't see a problem either.
It's only if I try to mix them — run a program using Cygwin's CRT from cmd.exe — where I potentially see your problem
but I don't see a problem with double quotes.
你可以尝试逃跑
you can try escaping