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