在bash脚本中执行SU

我正在运行远程bash脚本(使用ssh -t'bash doscript.sh')。 在“doscript.sh”我有一个sudo su anotheruser。 在这一点上,脚本似乎推壳,我留在那个“交互式”的脚本和我的脚本(doscript.sh)的其余部分不会运行,除非我input“退出”。 有没有解决这个问题的解决方法?

提前致谢。

这里是我的testing脚本“doscript.sh”的内容:

sudo su diy cd pwd whoami exit whoami 

sudo启动一个shell,除非你另有指示。

它看起来像你真的想要作为另一个用户运行这个脚本。 要做到这一点,请尝试这样的事情:

 #!/bin/bash if [ `id -nu` != diy ]; then sudo -u diy $0 # Re-run this script as user diy else # Everything you want to do goes here fi 

请记住, /etc/sudoers必须设置为允许原始用户以新用户身份运行此脚本。

你在脚本中使用的su只作为另一个用户运行su行本身。

使用su - otheruser -c "cmds to execute"