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

发布于 2024-08-23 12:05:19 字数 298 浏览 3 评论 0原文

我正在寻找一种通过命令行将包含“*”字符的字符串传递给可执行文件的方法。

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

む无字情书 2024-08-30 12:05:19

在 Windows 命令 shell 中,您执行的命令负责扩展参数中存在的任何通配符。此行为与 Unix 及其类似版本不同,其中通配符扩展通常由 shell 完成。

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

Windows (Windows 7):

C:\Users\Frank>echo *
*

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

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

这里通配符参数由 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):

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):

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

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.

三寸金莲 2024-08-30 12:05:19

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

因此,我们需要更多信息:您的 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:\>

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

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

只有当我尝试混合它们时 - 使用 Cygwin 的 CRT 从 cmd.exe 运行程序 - 我可能会看到你的问题,

$ 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:\>

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

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

C:\>

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.

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.

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.

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

$ 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:\>
一个人练习一个人 2024-08-30 12:05:19

你可以尝试逃跑

command.exe 3^*2

you can try escaping

command.exe 3^*2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文