bash - 用于在 postgres 中创建带有密码的用户的 shell 脚本失败并带有引号

标签 bash postgresql shell

我正在尝试创建一个 shell 脚本来引导新的数据库。 我能够创建用户、授予权限并执行所有操作,但使用密码运行任何查询除外。 shell 脚本中的单引号创建 postgres 不接受的语句。 因此,我们无法完全自动化此过程。

下面是 shell 脚本中使用的 postgres 行之一。

PGPASSWORD=change123 psql -h $DB -p 5432 -d postgres -U root -c \"CREATE USER $(echo "$j" | cut -d "_" -f1)dbuser WITH PASSWORD \'$(echo $DBPASSWD|base64 --decode)\';\"

执行上述脚本时,命令转换为

psql -h testdb -p 5432 -d postgres -U root -c '"CREATE' USER admindbuser WITH PASSWORD ''\''ZnuLEmu72R'\'''

我想要的命令是这样的

psql -h testdb -p 5432 -d postgres -U root -c "CREATE USER admindbuser WITH PASSWORD 'ZnuLEmu72R';"

非常感谢任何帮助。我需要一些帮助来指导如何修改 shell 中的行以实现所需的命令。

最佳答案

改变

PGPASSWORD=change123 psql\
  -h $DB \
  -p 5432 \
  -d postgres \
  -U root \
  -c \"CREATE USER $(echo "$j" | cut -d "_" -f1)dbuser WITH PASSWORD \'$(echo $DBPASSWD|base64 --decode)\';\"

PGPASSWORD=change123 psql \
  -h "$DB" \
  -p 5432 \
  -d postgres \
  -U root \
  -c "CREATE USER ${j%%_*}dbuser WITH PASSWORD '$(printf '%s' "$DBPASSWD" | base64 --decode)';"

关于bash - 用于在 postgres 中创建带有密码的用户的 shell 脚本失败并带有引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57824510/

相关文章:

linux - 从文本文件中读取不同的行并在 bash shell 中设置不同的变量

ruby - 续集将 "returning null"添加到我的插入内容中。我该如何禁用它?

python - 我怎样才能让 Python 程序使用通过模块 sys 运行的命令来杀死自己?

bash - 为字符串运行 ssh 远程命令和 grep

shell - sed:同一输出行上的多个模式

linux - 在 html/php 输出中显示/var/log/messages?

linux - 如何使用 Bash 中的 iconv 递归重命名文件和文件夹

linux - 如何将生成的文件从一个 grep 传递传递到另一个传递,以便我只在第二次传递时 grep 通过子集?

java - 如何为多个请求实例 1 个连接类?

postgresql - 这些凭据与我们的记录不符,laravel 5.2