如何在 bash 脚本执行期间更改用户并使用新用户继续 运行 命令?

How can I change users in during my bash script excecution and continue running commands with the new user?

所以我正在为安装制作一个 ubuntu bash 脚本,在其中我必须将用户更改为 postgres 用户 (运行ning sudo su postgres) 然后我必须输入 postgres shell 和 运行 一些命令。 问题是,当我将用户更改为 postgres 用户时,命令会保留 运行 root 用户。我如何使用 postgres 用户 运行 这些命令?

当您 运行 sudo su user 切换到此用户 shell,但您的脚本仍在您的第一个根 shell 中,并且正在等待 sudo su user 完成,以便继续。

您应该 运行 像这样针对另一个用户的每个命令: sudo -u other_user your_command