传递包含 *(星号)字符的字符串作为 Windows Shell 中的命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2374566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:03:31  来源:igfitidea点击:

Pass a string containing the * (asterisk) character as a command line argument in Windows Shell

windowsshellwindows-shell

提问by Mike Warner

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

我想传递字符串 3*2。Windows 所做的是在当前目录中搜索与文件掩码“3*2”匹配的文件,并将找到的任何文件传递给 command.exe

Putting "3*2" between double quotes does not help, still the same problem.

将 "3*2" 放在双引号之间没有帮助,仍然是同样的问题。

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.

我也试过 '3*2' (在单引号之间),但是整个字符串(包括单引号)都被传递了,这不是我需要的。

Is there any way to pass the string 3*2 (without any quotes) to the command?

有没有办法将字符串 3*2 (不带任何引号)传递给命令?

回答by Frank Bollack

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. 

在 Windows 命令外壳中,您执行的命令负责扩展参数中存在的任何通配符。这种行为与 Unix 和朋友不同,其中通配符扩展通常由 shell 完成。 

A simple example demonstrates this.

一个简单的例子证明了这一点。

Windows (Windows 7):

视窗(视窗 7):

C:\Users\Frank>echo *
*

As you can see, the command outputs the parameter exactly as passed in by the command line.

如您所见,该命令输出的参数与命令行传入的参数完全相同。

Linux (bash on CentOS Linux 5):

Linux(CentOS Linux 5 上的 bash):

> echo *
centos-release-5-0.0.el5.centos.2.i386.rpm centos-release-notes-5.0.0-2.i386.rpm glibc-2.5-12.i386.rpm glibc-common-2.5-12.i386.rpm virtualmin-install.log

Here the wild card parameter is substituted by the shell to a list of file/directories in the current directory.

这里通配符参数被 shell 替换为当前目录中的文件/目录列表。

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.

如果您提供有关您的命令以及您想要实现的目标的更多详细信息,我们可能会提供更多帮助。

回答by ephemient

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 your argv.

Windows 实际上将整个原始命令行作为单个字符串传递给程序;请参阅GetCommandLine。编写main(int argc, char **argv)时,程序链接的 C 运行时库负责将命令行拆分为argv.

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.

所以我们需要更多信息:你的 shell 是什么,你如何调用你的命令,以及你使用的是什么 C 运行时库?您看到的问题肯定不是 Windows 本身,我无法在此处使用 cmd.exe 和 MSVC 的 CRT 重现它。

C:>type CON > test.c
#include <stdio.h>
int main(int argc, char **argv) {
    int i;
    printf("%d\n", argc);
    for (i = 0; i < argc; i++)
        printf("[%d] <%s>\n", i, argv[i]);
    return 0;
}
^Z

C:\>cl test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\>test.exe *
2
[0] <test.exe>
[1] <*>

C:\>

If I use Cygwin's CRT, and run from Cygwin's Bash shell, then I don't see a problem either.

如果我使用 Cygwin 的 CRT,并从 Cygwin 的 Bash shell 运行,那么我也看不到问题。

C:\>C:\cygwin\bin\bash -l
$ cd /cygdrive/c
$ cc test.c
$ ./a.exe '*'
2
[0] <./a>
[1] <*>
$

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

只有当我尝试混合它们时——使用来自 cmd.exe 的 Cygwin 的 CRT 运行一个程序——我可能会看到你的问题

$ exit

C:\>a.exe *
19
[0] <a>
[1] <AUTOEXEC.BAT>
[2] <BOOT.INI>
[3] <CONFIG.SYS>
[4] <Documents and Settings>
[5] <IO.SYS>
[6] <MSDOS.SYS>
[7] <NTDETECT.COM>
[8] <NTLDR>
[9] <Program Files>
[10] <RECYCLER>
[11] <Recycled>
[12] <System Volume Information>
[13] <WINDOWS>
[14] <cygwin>
[15] <hiberfil.sys>
[16] <pagefile.sys>
[17] <temp>
[18] <a.exe>

C:\>

but I don't see a problem with double quotes.

但我没有看到双引号的问题。

C:\>a.exe "*"
2
[0] <test.exe>
[1] <*>

C:\>

回答by ghostdog74

you can try escaping

你可以尝试逃避

command.exe 3^*2