Consider the Bash code:
function dropMyDB() {
kubectl -n $1 exec -ti $1-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c "truncate table "$2";"
}
dropMyDB $1 "myTableNameInCamelCase"
When I execute the code it produces:
ERROR: relation "mytablenameincamelcase" does not exist
command terminated with exit code 1
Which means that the table name is not passed in its CamelCase form.
How can we fix this ?
CodePudding user response:
IMO it's no kubectl's fault:
fun(){ k exec aaaaaaaaaaaaa -it -- echo "$1"; }
fun AdsdDasfsFsdsd
AdsdDasfsFsdsd
But probably psql's one, try it like this:
... psql -d MYDBNAME -U postgres -c "truncate table '$2';"
CodePudding user response:
First
Escape your "$2"
because it is inside another double quote
postgres -c "truncate table "$2";"
# to
postgres -c "truncate table $2;"
# or
postgres -c "truncate table \"$2\";"
Second
You can test that the issue is not bash
function dropMyDB() {
echo "kubectl -n $1 exec -ti $1-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c \"truncate table \"$2\";\""
}
dropMyDB $1 "myTableNameInCamelCase"
Then
chmod x script.sh
./script.sh foo
kubectl -n foo exec -ti foo-CLUSSTER-0 -- psql -d MYDBNAME -U postgres -c "truncate table "myTableNameInCamelCase";"