You can store the result of a shell command inside a psql
variable like this:
set afile `echo "$outputdir/a.csv"`
COPY (SELECT * FROM a) TO :'afile';
Another (better in my opinion) solution is to use only psql
variables, see this answer of mine about psql variables, which is similar to your example. A example for your case would be:
set outputdir '/path/to/output'
set afile :outputdir '/a.csv'
COPY (SELECT * FROM a) TO :'afile';
Note that, in the example, you need to set the variable inside the script file, but you can skip the first line if you set it when you call psql
:
psql --set=outputdir="$outputdir" <conn parameters> -f /path/to/yourscript.sql
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…