简体   繁体   English

为什么更改用户后我的Shell脚本退出?

[英]Why does my shell script exit after changing users?

I am trying to create this little script to basically recreate the steps I take everytime I start a new ubuntu machine and install some python project on it. 我试图创建这个小脚本,以基本上重新创建每次启动新的ubuntu机器并在其上安装一些python项目时所采取的步骤。 At one point the script stops. 在某一时刻,脚本停止。 Which is when I switch to the postgres user. 当我切换到postgres用户时。 It never runs the psql commands 它从不运行psql命令

pip install --upgrade pip
sudo pip install virtualenv
useradd -m -p $2 $1
sudo -i -u postgres
psql -c "CREATE DATABASE $1;"
psql -c "CREATE USER $1 WITH PASSWORD '$2';"
psql -c "GRANT ALL PRIVILEGES ON DATABASE $1 to $1;"

I probably suck bash scripting and I am trying to get better. 我可能会吮吸bash脚本,并且正在尝试变得更好。

When you run the command 运行命令时

sudo -i -u postgres

a new shell is spawned. 产生了一个新的外壳。 If you type exit at this point, this new shell will exit and your original shell will continue running the psql commands (but not as the postgres user). 如果此时键入exit ,则新的Shell将退出,您的原始Shell将继续运行psql命令(但不以postgres用户身份运行)。

You can do the following to fix this: 您可以执行以下操作来解决此问题:

sudo -u postgres psql -c "CREATE DATABASE $1;"
sudo -u postgres psql -c "CREATE USER $1 WITH PASSWORD '$2';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $1 to $1;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM