Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading