PowerShell 中的命令几乎与 Bash 类似,但点符号扩展给我带来了很多工作。目前我必须将很多命令参数用引号引起来:
.\mvnw.cmd -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar
becomes
.\mvnw.cmd -D"maven.repo.local"=".m2/repository" deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId="com.zippo" -DartifactId=test -Dversion="1.0" -Dpackaging=jar -Dfile="test-1.0.jar"
如何禁用点符号,或覆盖点运算符,将其替换为其他内容等?
如何禁用点表示法?
-
问题不在于点符号,it is a bugPowerShell 如何解析要传递给的参数外部程序:
- 一个令牌,例如
-foo.bar
意外被闯入two论据,-foo
and .bar
- see GitHub 问题 #6291 https://github.com/PowerShell/PowerShell/issues/6291(意外重复:GitHub 问题 #15541 https://github.com/PowerShell/PowerShell/issues/15541);这只是其中之一几个相关的错误 https://github.com/PowerShell/PowerShell/issues?q=is%3Aissue+is%3Aopen+parameter+parsing%2Fpassing+in%3Atitle,至少存在 PowerShell 7.3.6。
-
Escaping or quoting确实需要解决这个问题, 但是你可以simplify it by `
- 逃避最初的-
每个受影响的论点 or by 封闭每个作为一个整体 in '...'
(or "..."
,如果需要字符串插值):
# `-escape the initial - of all affected arguments.
.\mvnw.cmd `-Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus `-DgroupId=com.zippo -DartifactId=test `-Dversion=1.0 -Dpackaging=jar `-Dfile=test-1.0.jar
# Alternative: Quote the affected arguments.
.\mvnw.cmd '-Dmaven.repo.local=.m2/repository' deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus '-DgroupId=com.zippo' -DartifactId=test '-Dversion=1.0' -Dpackaging=jar '-Dfile=test-1.0.jar'
-
如果您不想考虑哪些特定参数可能会受到影响,请引用它们all,其中 - 结合您可以将参数作为array字符串,使用@(...)
, the 数组子表达式运算符 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#array-subexpression-operator--- 可用于通过将长命令分散到多行来使长命令更具可读性:
.\mvnw.cmd @(
'-Dmaven.repo.local=.m2/repository'
'deploy:deploy-file'
'-Durl=http://zippo:8081/repository/grinch/'
'-DrepositoryId=nexus'
'-DgroupId=com.zippo'
'-DartifactId=test'
'-Dversion=1.0'
'-Dpackaging=jar'
'-Dfile=test-1.0.jar'
)
备择方案:
-
You can use --%
, the 停止解析标记 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Parsing#the-stop-parsing-token,这消除了引用的需要,但是注意它的许多限制,特别是无法(直接)将 PowerShell 变量和表达式合并到命令中 - 请参阅这个答案 http://stackoverflow.com/a/42601912/45375了解详情。
# --% passes the remainder of the command line as-is to the target program
.\mvnw.cmd --% -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar
-
You can 通过呼叫cmd /c
,在这种情况下你可以引用你的整个命令行,这避免了解析错误(但请注意,字符串中指定的命令必须满足cmd.exe
的语法规则;它们可能与 PowerShell 不同,但在本例中并非如此):
cmd /c '.\mvnw.cmd -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)