简体   繁体   English

如何从MinGW Shell将任意命令行参数传递给本机Windows程序?

[英]How do I pass an arbitrary command line argument to a native Windows program from a MinGW shell?

How do I pass an arbitrary command line argument to a native Windows program from a MinGW shell? 如何从MinGW Shell将任意命令行参数传递给本机Windows程序?

I would like a general solution, but a solution that works for any valid Windows filename would be acceptable. 我想要一个通用的解决方案,但是可以接受适用于任何有效Windows文件名的解决方案。

That shell is Bash. 那壳是Bash。 Cygwin/MSYS2 Bash can accept Windows paths, but you need to deal with spaces and backslashes. Cygwin / MSYS2 Bash可以接受Windows路径,但是您需要处理空格和反斜杠。 Regarding backslashes: 关于反斜杠:

program 'C:\alfa.txt'
program C:\\alfa.txt
program C:/alfa.txt

Regarding spaces: 关于空间:

program 'C:\alfa bravo.txt'
program C:\\alfa\ bravo.txt
program C:/alfa\ bravo.txt

As you can see, if you are supplying Windows paths, this is pretty straight forward. 如您所见,如果要提供Windows路径,这很简单。 The only issue you might get is if you are trying to supply Bash paths to a Windows native program: 您可能会遇到的唯一问题是,如果您尝试提供Windows本机程序的Bash路径:

program /tmp/alfa.txt

Windows native programs have no concept of /tmp or even / . Windows本机程序没有/tmp甚至/概念。 Cygwin/MSYS2 have cygpath to assist in converting these paths: Cygwin / MSYS2具有cygpath来协助转换这些路径:

program $(cygpath -m /tmp/alfa.txt)
program "$(cygpath -w /tmp/alfa.txt)"
program "$(cygpath -m '/tmp/alfa bravo.txt')"
program "$(cygpath -m /tmp/alfa\ bravo.txt)"
program "$(cygpath -w '/tmp/alfa bravo.txt')"
program "$(cygpath -w /tmp/alfa\ bravo.txt)"

Side note: MinGW is an old project. 旁注:MinGW是一个旧项目。 You should be using Cygwin or MSYS2. 您应该使用Cygwin或MSYS2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows上如何从命令行使用MinGW编译器? - How do I use the MinGW compiler from the command line on Windows? 如何从Windows命令行将带有空格的参数传递给MinGW-MSYS Shell脚本? - How to pass arguments with spaces to MinGW-MSYS shell scripts from the Windows command line? 如何将CRLF作为参数传递给命令行程序? - How to pass CRLF as an argument to command line program? 在Windows Shell中将包含*(星号)字符的字符串作为命令行参数传递 - Pass a string containing the * (asterisk) character as a command line argument in Windows Shell 如何在Windows中转义任意字符串以用作命令行参数? - How can I escape an arbitrary string for use as a command line argument in Windows? 如何通过php将参数传递给Windows命令行程序中的可执行文件? - How to pass arguments to executeable in windows command line program from php? 如何从Windows命令行shell脚本中打开(锁定)文件? - How do I hold a file open (locked) from a Windows command-line shell script? 如何在Windows 7命令行上执行.jar java程序? - How do I execute .jar java program on Windows 7 command line? 如何在Windows上从命令行启动MingW控制台(GitBash)? - How to start MingW Console (GitBash) from Command Line on Windows? 如何在PHP中使用WScript.Shell传递命令行参数? - How to pass a command line argument using WScript.Shell in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM