Powershell Bash/Zsh命令中的多个参数 [英] Multi parameters in Powershell Bash/Zsh command

查看:153
本文介绍了Powershell Bash/Zsh命令中的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Unable to run the following Bash/Zsh command in Powershell:

$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)"

我收到错误消息:

error: exactly one NAME is required, got 5
See 'kubectl create secret docker-registry -h' for help and examples

如果我直接在bash中运行此命令,它将起作用:

If I run this command directly in bash it works:

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

推荐答案

我不知道是否是您遇到问题的原因,但是存在两个潜在问题:

I don't know if it's the cause of your problem, but there are two potential problems:

  • 包含空格的扩展值会导致PowerShell在幕后重建命令行时(在Windows上)对参数作为整体 进行双引号:

  • An expanded value that contains spaces causes PowerShell to double-quote the argument as a whole when it rebuilds the command line behind the scenes (on Windows):

  • 例如,如果 $(cat $ KeyPath)( $(Get-Content $ KeyPath))扩展为一二,PowerShell在幕后传递-docker-password = one two" not -docker-password =一二" .

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

是否会改变参数的含义取决于目标程序如何解析其命令行-我不知道 kubectl 会做什么.

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.

  • 如果确实需要解决此问题,请使用```''(反引号,PowerShell的转义字符,使PowerShell在原始语法形式:

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

  • -docker-password =`"$(cat ./tekton.key)`"

请注意-与Bash和Zsh等类似POSIX的外壳不同,通常将变量引用或子表达式包含在"..." 为了使其安全通过;例如,即使 $ someVar 或输出,-foo = $ someVar -foo = $(Get-Date)都可以正常工作来自 Get-Date 的空格或通配符.

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.

如果文件 $ KeyPath 包含多条行,则这些行将以空格 连接起来.参数:

If file $KeyPath contains multiple lines, the lines are concatenated with spaces in the argument:

  • 例如,如果文件包含"a`nb`n" (`n" 是换行符),PowerShell将通过
    -docker-password = a b ".

  • For instance, if the file contains "a`nb`n" ("`n" being a newline), PowerShell will pass
    "--docker-password=a b".

相比之下,像Bash或Zsh这样的POSIX类外壳将保留内部换行符,同时修剪(任意数量的)后缀.p>

By contrast, POSIX-like shells such as Bash or Zsh will preserve the interior newlines, while trimming (any number of) trailing ones.

另一方面,PowerShell对传递给外部程序的参数中的嵌入式双引号的处理已损坏-请参见

On a side note: PowerShell's handling of embedded double-quoting in arguments passed to external programs is broken - see this answer.

这篇关于Powershell Bash/Zsh命令中的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆