In a Bash script I need to execute a command which takes the list of the existing databases from PostgreSQL:
getdblist_cmd=(sudo -u $backup_user $psql -p $pgsql_port -U $pgsql_user -d postgres -q -t -c 'SELECT datname from pg_database')
# Get databases list
msg
msg "================================================================="
msg "Getting databases list ..."
dblist=`"${getdblist_cmd[@]}"`
status=$?
if [ "$status" -ne "0" ]; then
return $status
fi
This works but I need to exclude the template0 database, but this does not work:
getdblist_cmd=(sudo -u $backup_user $psql -p $pgsql_port -U $pgsql_user -d postgres -q -t -c 'SELECT datname from pg_database WHERE datname != \'template0\')'
# Get databases list
msg
msg "================================================================="
msg "Getting databases list ..."
dblist=`"${getdblist_cmd[@]}"`
status=$?
if [ "$status" -ne "0" ]; then
return $status
fi
It returns:
root@postgres /u/l/sbin# ./pgsql-backup-full-func.sh
./pgsql-backup-full-func.sh: line 145: unexpected EOF while looking for matching `''
./pgsql-backup-full-func.sh: line 187: syntax error: unexpected end of file
I know that no escaping is possible for single quotes in Bash, so how I can manage this?
'in your second copy of the script to stackoverflow? I feel like a single quote is missing beforeSELECT...