简体   繁体   中英

Bash script with psql command tells nothing, but doesn't work

I am really confused with this piece of code:

...
COM="psql -d $DBNAME -p $PGPORT -c 'COPY (SELECT * FROM $TABLE_NAME s WHERE cast(s.$COLUMN_NAME as DATE) < DATE '$DATE_STOP' ) TO '$SCRIPTPATH/$ARCHIVE_NAME--$DBNAME' WITH CSV HEADER;'"

su postgres -c '$COM' &> pg_a.log
...

in psql shell this SQL code works fine, but in script he is not creating archive and tells me nothing about mistakes or fails.

Thanks in advance!

You'll get one hint if you replace your "su" command with this:

echo '$COM'

What you'll see is that it prints out $COM -- not an expansion, but the string itself.

You'll probably find a number of other problems with this approach. You're going to have to escape a bunch of characters so the shell doesn't interpret them for you. It's going to be a real pain.

I would put the sql into a file and use the -f option to psql.

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