gpt4 book ai didi

bash - 用于在 postgres 中使用密码创建用户的 Shell 脚本因引号失败

转载 作者:行者123 更新时间:2023-11-29 14:26:02 25 4
gpt4 key购买 nike

我正在尝试创建一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com