Hoo*_*oob 5 shell powershell command-line escaping
cwd我正在 PowerShell 控制台会话中,尝试从 PowerShell 命令行运行存在于其中的 Gradle 脚本。
Gradle 命令接受一个参数,其中包含可以包含一个或多个管道字符的引号,但我一生都无法弄清楚如何让 PowerShell 仅使用一行(或任意多行,实际上 -但我对多线解决方案不感兴趣)。
这是命令:
./gradlew theTask -PmyProperty="thisThing|thatThing|theOtherThing"
Run Code Online (Sandbox Code Playgroud)
...这会产生此错误:
'thatThing' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有这些变体,但都不起作用。任何想法?
./gradlew theTask -PmyProperty="thisThing`|thatThing`|theOtherThing"
./gradlew theTask -PmyProperty=@"thisThing|thatThing|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\|thatThing\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\\|thatThing\\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Run Code Online (Sandbox Code Playgroud)
嗯,我发现:
首先按照我首先建议的那样调用您的脚本:
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Run Code Online (Sandbox Code Playgroud)
然后通过添加引号来修改 gradlew.bat 脚本:
set CMD_LINE_ARGS="%*"
Run Code Online (Sandbox Code Playgroud)
问题是:现在 CMD_LINE_ARGS 必须在引号内调用,否则会发生相同的错误。
所以我假设你的命令行参数不能是其他东西,我正在一一处理每个参数
rem now remove the quotes or there will be too much quotes
set ARG1="%1"
rem protect or the pipe situation will occur
set ARG1=%ARG1:""=%
set ARG2="%2"
set ARG2=%ARG2:""=%
set ARG3="%3"
set ARG3=%ARG3:""=%
echo %ARG1% %ARG2% %ARG3%
Run Code Online (Sandbox Code Playgroud)
输出是(对于我的模型命令):
theTask -PmyProperty "thisThing|thatThing|theOtherThing"
Run Code Online (Sandbox Code Playgroud)
“=”消失了,因为它已将参数与 PowerShell 分开。我想如果您的命令具有标准参数解析,这不会成为问题。
添加任意数量的参数,但无论如何将其限制为 9 个。