首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell Bash/Zsh命令中的多参数

Powershell Bash/Zsh命令中的多参数
EN

Stack Overflow用户
提问于 2020-04-16 11:37:05
回答 1查看 432关注 0票数 3

无法在Powershell中运行以下Bash/Zsh命令:

代码语言:javascript
复制
$KeyPath = Join-Path -Path $this.Plate -ChildPath "install/tekton.key"
kubectl create secret docker-registry regcred `
    --docker-server="https://gcr.io" `
    --docker-username=_json_key `
    --docker-email="name@org.iam.gserviceaccount.com" `
    --docker-password="$(cat $KeyPath)"

我得到了错误:

代码语言:javascript
复制
error: exactly one NAME is required, got 5
See 'kubectl create secret docker-registry -h' for help and examples

如果我在bash中直接运行这个命令,它可以工作:

代码语言:javascript
复制
kubectl create secret docker-registry regcred --docker-server="https://gcr.io" --docker-username=_json_key --docker-email="name@org.iam.gserviceaccount.com" --docker-password="$(cat ./tekton.key)"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-16 12:09:10

我不知道这是否是你问题的原因,但是有两个

包含空格的

  • An扩展值导致PowerShell在幕后(在Windows上):

重新构建命令行时将参数作为一个整体引用。

代码语言:javascript
复制
- For instance, if `$(cat $KeyPath)` (`$(Get-Content $KeyPath)`) expands to `one two`, PowerShell passes `"--docker-password=one two"` behind the scenes, _not_ `--docker-password="one two"`.
- Whether this changes the meaning of the argument depends on how the target program parses its command line - I don't know what `kubectl` does.
代码语言:javascript
复制
    - If you do need to address this, escape the enclosing `"` (double quotes) with `` ``` (the backtick, PowerShell's escape character to make PowerShell pass your argument in the original syntax form:
代码语言:javascript
复制
        - `--docker-password=`"$(cat ./tekton.key)`"`

代码语言:javascript
复制
    - Note that - unlike in POSIX-like shells such as Bash and Zsh - you normally do _not_ enclose a variable reference or subexpression in `"..."` in order to pass it through safely; e.g., `--foo=$someVar` or `--foo=$(Get-Date)` work fine, even if `$someVar` or the output from `Get-Date` contains spaces or wildcard characters.

  • If文件 $KeyPath 包含多个行,这些行与参数中的空格连接在一起:H 233F 234

代码语言:javascript
复制
- For instance, if the file contains `"a`nb`n"` (`"`n"` being a newline), PowerShell will pass

"--docker-password=a b.

-相反,类似POSIX的shell(如Bash或Zsh )将保留内部换行符,同时裁剪(任意数量)尾随行。

顺便提一句:PowerShell在传递给外部程序的参数中对嵌入式双引用的处理是失败的--参见this answer

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61249289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档