简体   繁体   中英

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM