如何像下面的脚本一样在bash变量上放置一个标量postgresql-value?
dbname="testlauf"
username="postgres"
vartest='psql -c -d $dbname -U $username -h localhost -p 5432 "SELECT gid FROM testtable WHERE aid='1';"'
echo "$vartest"
我尝试了几种不同的写作,但似乎都不起作用。提前谢谢。
发布于 2018-01-17 14:46:39
使用-t选项或--tuples-only将只给出行,因此更容易将它们存储在数组变量中(如果查询的结果不止一个)
vartest =(`psql -t -d $dbname -U $username -c "SELECT gid FROM testtable WHERE aid='1';"`)
echo $vartest
示例:
查询结果
ubuntu@ratnakri:~$ psql -h localhost -p 5432 -t -U postgres -d postgres -c "select slot_name from pg_replication_slots"
barman
barman2
使其成为数组变量
ubuntu@ratnakri:~$ RESULT=(`psql -h localhost -p 5432 -t -U postgres -d postgres -c "select slot_name from pg_replication_slots"`)
ubuntu@ratnakri:~$ echo ${RESULT[0]}
barman
ubuntu@ratnakri:~$ echo ${RESULT[1]}
barman2
https://stackoverflow.com/questions/15242752
复制相似问题