[英]Execute command inside kubernetes pod (bash script)
I am trying to execute a command inside postgres container from shell script.我正在尝试从 shell 脚本在 postgres 容器内执行命令。 This is what I have so far:
这是我到目前为止:
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name='FOO';'"
I am getting the following error:我收到以下错误:
ERROR: column "foo" does not exist LINE 1: select count from table where name=FOO; ^
The query runs fine inside the container so there must be something wrong with the way I am passing the command.查询在容器内运行良好,因此我传递命令的方式一定有问题。 I did try another query:
我确实尝试了另一个查询:
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select * from table;'"
This runs fine.这运行良好。 So, I am guessing that its someting with the way I am passing the where clause
where name='FOO'
.所以,我猜这与我传递 where 子句
where name='FOO'
。 How can I get this to work.我怎样才能让它发挥作用。 Kindly help me out.
请帮帮我。
Update:更新:
Tried escaping using:尝试使用以下方法进行转义:
1: Double Quotes 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: Single Quotes 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 子句中使用了$$
美元引用的字符串,并使用/$
它们进行了转义。
kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\$\$FOO\$\$;'"
That's because the quotes are not properly escaped and then FOO is assumed to be a column name.那是因为引号没有正确转义,然后 FOO 被假定为列名。
Try尝试
kubectl exec -it <pod_id> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\"FOO\";'"
Looks like the Query condition element, name='value' is expected to be within single quotes.看起来像查询条件元素,name='value' 应该在单引号内。
Try this: it works!试试这个:它有效!
kubectl exec -it <pod_id> -n <deployment> -- bash -c "psql -U postgres -d database -c \"select count from table where name='FOO'\""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.