Bash 脚本 su 退出 shell
我正在尝试编写一个 bash 脚本,该脚本以不同的用户身份执行多个命令来自动化安装过程。
在某一时刻,我以
su gpadmin
gpperfmon_install --enable --password password --port 5432
y
gpstar
y
root 身份运行 su gpadmin,一旦运行,脚本就会停止,直到我退出 gpadmin 并以 root 身份返回。
如何在不停止脚本的情况下切换到新用户?
抱歉我的无知,我尝试用谷歌搜索但没有发现任何有用的东西。 任何帮助将不胜感激。
Im trying to write a bash script that executes several commands as different users to automate an install process.
At one point I have
su gpadmin
gpperfmon_install --enable --password password --port 5432
y
gpstar
y
The su gpadmin is run as root, and once it is run the script stops until I exit gpadmin and return as root.
How would one switch to a new user without halting the script?
Sorry for my ignorance, I attempted to google but found nothing that worked.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 su 的 -c 选项才能运行命令然后退出。使用您为 su 提供的选项,它将运行 gpadmin 的默认 shell,该 shell 将阻塞,直到 shell 退出。从您已有的代码来看,gpperfmon_install 似乎是一个交互式应用程序,这是另一个问题。有时您可以使用此处文档,但在其他情况下您需要使用
expect
。You need to use the -c option to su in order for it to run a command and then exit. With the options you have given to su, it will run gpadmin's default shell, which will block until the shell has exited. Judging by the code you already have, it appears that gpperfmon_install is an interactive application, which is another issue. Sometimes you can use a here document, but in other cases you will need to use
expect
.