【发布时间】:2020-09-06 23:21:08
【问题描述】:
在我的脚本中有:
#!/bin/bash
# Take sudo password from the first argument if present
SUDO="sudo -u postgres"
if [ "$1" != "" ]; then
SUDO="echo $1 | sudo -S -u postgres"
fi
${SUDO} psql -c "create database foo;"
... several other similar commands here ...
当我使用myPassword 运行脚本时,我得到:
myPassword | sudo -S -u postgres psql -c create database foo;
所以它只是呼应了这条线。显然,我想要的是逐字运行该命令,但这样$1 就会被扩展:
$ echo myPassword | sudo -S -u postgres psql -c create database foo;
我该怎么做?
【问题讨论】:
-
理想情况下,您应该只配置
sudo以允许适当的用途以用户postgres的身份运行psql而无需输入密码。