其他命令中的管道命令输出
Pipe command output inside other command
我正在尝试将第一个命令的输出通过管道传递到第二个命令末尾的引号中。
kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":" Output from command 1 "}}'
我已经试过了:,但是我在使用这个命令时遇到以下错误:
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"`password":"kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}`"}}'
The request is invalid: patch: Invalid value: "map[data:map[`password:kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}`]]": error decoding from json: illegal base64 data at input byte 7
您可以将命令分成两部分以便于理解,并让 bash shell 在第二行扩展变量 pgsecret
:
pgsecret="$(kubectl --context foo -n foo get secret \
postgres.foo-db.credentials -o jsonpath={.data.password})"
kubectl \
--context foo -n foo patch secret \
postgres.foo-db.credentials -p '{"data":{"password":"'$pgsecret'"}}'
Davide Madrisan的想法很好。代码将更具可读性。但是使用此解决方案,您可能会收到错误消息。命令中:
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":"'$pgsecret'"}}'
对变量的引用 "'$pgsecret'"
带有双引号和单引号。 double 都可以,因为它必须是 "key":"value"
结构。但是在我的环境中,单引号是错误的。我收到一个错误:
Error from server (BadRequest): invalid character '<wrong character>' after object key:value pair
您可以详细了解 '
和 "
here 之间的区别。
我建议去掉单引号:
pgsecret="$(kubectl --context foo -n foo get secret \
postgres.foo-db.credentials -o jsonpath={.data.password})"
kubectl \
--context foo -n foo patch secret \
postgres.foo-db.credentials -p '{"data":{"password":"$pgsecret"}}'
或者如果您想在没有变量的情况下在一行中执行此操作(代码可读性较差):
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":"$(kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password})"}}'
也应该和你一起工作。这种处理方法叫做command substitution
, as user1934428 评论中很好的提到:
A pipe won't do here. You need command substitution, because one part of your command is supposed to be substituted by the standard output of a different command.
反引号(又名 $())不用作单引号内的命令替换,而是文字:
$ echo '$(echo foo)'
$(echo foo)
它们被插入双引号内:
$ echo "$(echo foo)"
foo
所以你只需要正确引用你的替代:
$ kubectl --context ... -p '{"data":{"password":"'"$(cmd to get passwd)"'"}}'
你也可以这样写:
$ kubectl --context ... -p "{\"data\":{\"password\":\"$(cmd to get passwd)\"}}"
我正在尝试将第一个命令的输出通过管道传递到第二个命令末尾的引号中。
kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":" Output from command 1 "}}'
我已经试过了:
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"`password":"kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}`"}}'
The request is invalid: patch: Invalid value: "map[data:map[`password:kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}`]]": error decoding from json: illegal base64 data at input byte 7
您可以将命令分成两部分以便于理解,并让 bash shell 在第二行扩展变量 pgsecret
:
pgsecret="$(kubectl --context foo -n foo get secret \
postgres.foo-db.credentials -o jsonpath={.data.password})"
kubectl \
--context foo -n foo patch secret \
postgres.foo-db.credentials -p '{"data":{"password":"'$pgsecret'"}}'
Davide Madrisan的想法很好。代码将更具可读性。但是使用此解决方案,您可能会收到错误消息。命令中:
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":"'$pgsecret'"}}'
对变量的引用 "'$pgsecret'"
带有双引号和单引号。 double 都可以,因为它必须是 "key":"value"
结构。但是在我的环境中,单引号是错误的。我收到一个错误:
Error from server (BadRequest): invalid character '<wrong character>' after object key:value pair
您可以详细了解 '
和 "
here 之间的区别。
我建议去掉单引号:
pgsecret="$(kubectl --context foo -n foo get secret \
postgres.foo-db.credentials -o jsonpath={.data.password})"
kubectl \
--context foo -n foo patch secret \
postgres.foo-db.credentials -p '{"data":{"password":"$pgsecret"}}'
或者如果您想在没有变量的情况下在一行中执行此操作(代码可读性较差):
kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":"$(kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password})"}}'
也应该和你一起工作。这种处理方法叫做command substitution
, as user1934428 评论中很好的提到:
A pipe won't do here. You need command substitution, because one part of your command is supposed to be substituted by the standard output of a different command.
反引号(又名 $())不用作单引号内的命令替换,而是文字:
$ echo '$(echo foo)'
$(echo foo)
它们被插入双引号内:
$ echo "$(echo foo)"
foo
所以你只需要正确引用你的替代:
$ kubectl --context ... -p '{"data":{"password":"'"$(cmd to get passwd)"'"}}'
你也可以这样写:
$ kubectl --context ... -p "{\"data\":{\"password\":\"$(cmd to get passwd)\"}}"