Issue with Special Characters and Variable Substitution in Shell Script Running pgloader Command

66 views Asked by At

I'm encountering a problem when attempting to run a pgloader command directly, resulting in a parser issue due to special characters. To address this, I've decided to execute the pgloader command using a shell script. However, during execution, the echo command within the script prints the following without substituting the variables as pgsql://pguser:@/db_dev. Along with that, Im getting IndexError: list index out of range exception

#!/bin/bash
# Function to URL encode a string
urlencode() {
  python3 -c "import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))"
}

#MySQL connection string
mysql_password="Tes^((***))"
mysql_connection="mysql://mysqluser:$(urlencode "$mysql_password")@$(urlencode "test_mysql_url.rds.amazonaws.com:3306")/db_af"

# PostgreSQL connection string
pgsql_password="Test%%*$"
pgsql_connection="pgsql://pguser:$(urlencode "$pgsql_password")@$(urlencode "test_pg_url.rds.amazonaws.com:5432")/db_dev"

echo $pgsql_connection

# Combine both connection strings and execute pgloader
pgloader_command="pgloader $mysql_connection $pgsql_connection"
eval "$pgloader_command"
1

There are 1 answers

1
tripleee On BEST ANSWER

Your function forgot to include its argument(s).

urlencode() {
  python3 -c "import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))" "$@"
}

In case it's not obvious, the last token on the line which starts with python -c is "$@" which contains the (correctly quoted) arguments to the function.