bash - 在kubernetes pod中执行命令(bash脚本)

标签 bash kubernetes kubectl

我正在尝试从shell脚本在postgres容器内执行命令。这是我到目前为止所拥有的:
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name='FOO';'"
我收到以下错误:
ERROR: column "foo" does not exist LINE 1: select count from table where name=FOO; ^
查询在容器内运行良好,因此我传递命令的方式一定存在问题。我确实尝试了另一个查询:

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select * from table;'"

运行良好。因此,我猜测它与我传递where子句where name='FOO'的方式有关。我怎样才能使它工作。请帮我。

更新:

尝试使用以下方法进行转义:

1:双引号
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\"FOO\";'"
ERROR:  column "FOO" does not exist
LINE 1: select count from table where name="FOO";
                                            ^

2:单引号
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\'FOO\';'"
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
command terminated with exit code 1

最佳答案

我在where子句中使用了$$ dollar-quoted string ,并使用/$对其进行了转义。

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\$\$FOO\$\$;'"

关于bash - 在kubernetes pod中执行命令(bash脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53588840/

相关文章:

linux - 如何使用 bash 根据模式列出 Linux 中的文件

bash - 用sed修改key ="Value"配置

java - 在 Bash 中自动安装需要多次按键的 Java 应用程序

amazon-web-services - kubernetes + coreos 集群 - 替换证书

kubernetes statefulsets 索引/序数暴露在模板中

kubernetes - 证书续订后,错误 : "You must be logged in to the server (Unauthorized)"

regex - 如何在 bash 中获取变量配置的值?

kubernetes - 在GCP的GKE上通过kubernetes中的IP限制访问

kubernetes - 已批准 Kubernetes CSR,但证书未显示在状态中

kubernetes - 为什么 Kubernetes v1.12 不再需要升级延迟?