JAN
JAN

Reputation: 21865

Kubectl with Bash command is always passed in LowerCase and not CamelCase

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 ?

Upvotes: 0

Views: 86

Answers (2)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23804

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

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

Upvotes: 1

Ivan
Ivan

Reputation: 7287

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';"

Upvotes: 0

Related Questions