pg_dump tables exclude doesnt work in bash script

Alles was sonst nirgends rein passt.
Antworten
Zika
Neuer Benutzer
Beiträge: 1
Registriert: Di 15. Sep 2015, 19:38

pg_dump tables exclude doesnt work in bash script

Beitrag von Zika » Di 15. Sep 2015, 19:40

PostgreSQL 9.4, Debian 8.1

It works with this script

Code: Alles auswählen

#!/bin/bash
pg_dump -h 127.0.0.1 -U postgres -T 'loolz_*' wazap -f /tmp/wazap.sql
exit 0
but not with this one

Code: Alles auswählen

#!/bin/bash
dumpcommand="pg_dump -h 127.0.0.1 -U postgres -T 'loolz_*' wazap -f /tmp/wazap.sql"
$dumpcommand
exit 0
Please help me to understand why the second option does not work, I tried to play with brackets and quotes. Nothing helps...

DaniloE
Benutzer
Beiträge: 33
Registriert: Fr 4. Mär 2016, 12:39

Re: pg_dump tables exclude doesnt work in bash script

Beitrag von DaniloE » Mi 9. Mär 2016, 15:28

In Case someone will read this: Bash escaping is the Problem here:

Code: Alles auswählen

 bash -vx ./test.sh
#!/bin/bash

dumpcommand="pg_dump -h 127.0.0.1 -U postgres -T 'loolz_*' wazap -f /tmp/wazap.sql"
+ dumpcommand='pg_dump -h 127.0.0.1 -U postgres -T '\''loolz_*'\'' wazap -f /tmp/wazap.sql'
$dumpcommand
+ pg_dump -h 127.0.0.1 -U postgres -T ''\''loolz_*'\''' wazap -f /tmp/wazap.sql
You can resolve the problem via the following:

Code: Alles auswählen

 bash -vx ./test.sh
#!/bin/bash

exclude='loolz_*'
+ exclude='loolz_*'
dumpcommand="pg_dump -h 127.0.0.1 -U postgres -T ${exclude} wazap -f /tmp/wazap.sql"
+ dumpcommand='pg_dump -h 127.0.0.1 -U postgres -T loolz_* wazap -f /tmp/wazap.sql'
$dumpcommand
+ pg_dump -h 127.0.0.1 -U postgres -T 'loolz_*' wazap -f /tmp/wazap.sql
Maybe this will help anyone :)

Antworten