【问题标题】:Parsing postgres-config.ini using shell script returns nothing, am I missing anything?使用 shell 脚本解析 postgres-config.ini 什么都不返回,我错过了什么吗?
【发布时间】:2020-02-25 12:58:20
【问题描述】:

我在 postgres-config.ini 文件中有这些参数:

PGUSER=usr
PGPASSWORD=pass
PGDATABASE=myDB
PGHOST=localhost
PGPORT=5432

我想解析它并使用 shell 脚本(sh 文件)连接到数据库。我像这样使用 source 和 grep 命令:

source <( grep = postgres-config.ini )

但是,当我尝试使用 echo $PGHOST 打印值时,它没有打印任何内容。

谁能帮我解决我做错了什么?

【问题讨论】:

  • stackoverflow.com/questions/19331497/… 这能回答你的问题吗?
  • 不,还是一样的问题(没有打印)
  • 您是否从上面的问答中尝试过这个想法:set -o allexport; source conf-file; set +o allexport

标签: postgresql shell grep sh


【解决方案1】:

来自进程替换的 source 在命令行中对我来说很好:

$ cat foo.sh
PGUSER=usr
PGPASSWORD=pass
PGDATABASE=myDB
PGHOST=localhost
PGPORT=5432
$ source <(grep = foo.sh)
$ echo $PGHOST
localhost

请注意,shell 变量不会被导出,因此它们在子进程中不可用。如果您在子流程中需要它们,请尝试:

source <(sed '/=/!d;s/^/export /' foo.sh)

它使用sedexport 命令添加到与= 匹配的行中。

另外,你在哪里运行source?如果是在脚本中,请记住,脚本结束时变量会消失,不会传递给父进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多