bash - 将来自 postgresql 的值存储在 bash 变量中

标签 bash postgresql psql

<分区>

我正在用 bash 编写一个脚本,我用这个运行 psql 查询:

VAR="$(psql -h prov-db-cl -p 5446 -d prov -U prov -c "SELECT value FROM table where query = 'query'")"

问题是 $VAR 上的内容是这样的:

value ------------------ 结果(1 行)

我只需要在我的 $VAR 中使用 result 即可在脚本的其余部分中使用它。

最佳答案

VAR=`psql -t -h prov-db-cl -p 5446 -d prov -U prov -c "SELECT value FROM table where query = 'query'"`

VAR=$(psql -t -h prov-db-cl -p 5446 -d prov -U prov -c "SELECT value FROM table where query = 'query'")

-t 只返回元组(数据)。

See psql documentation about available options.

编辑

我已经能够按照这里的建议使用 subsheel:https://stackoverflow.com/a/21193276/14673

psql -t -h prov-db-cl -p 5446 -d prov -U prov -c "SELECT value FROM table where query = '`echo $VAR`'"

关于bash - 将来自 postgresql 的值存储在 bash 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35209611/

相关文章:

bash - 更改 bash 配置文件中提示的颜色

mysql - mysql、Nosql、Cassandra、Mongodb、postgresql 等不同的数据库解决方案有什么区别,你什么时候使用它们?

postgresql - 使用 sequelize 将 JSON 数据播种到 PostgreSQL 数据库时出错

linux - Bash 脚本 : Replace (or delete) string in a file if line starts with (or matches) another string

linux - 开关不工作

ruby-on-rails - Rails - 获取具有重复对象的对象

postgresql - 需要在 7 天内获得特定 30 分钟时间范围内的结果 :45 - :15 on the hour every hour

node.js - ForeignKeyConstraintError [SequelizeForeignKeyConstraintError] : insert or update on table "avatars" violates foreign key constraint

postgresql - 如何从 postgresql 中的 plpgsql 函数返回的 SELECT 查询中提取字符串(或文本)?

bash - 我如何判断一个文件在 Bash 中是否不存在?