如何以其他用户身份登录,然后登出bash脚本?
问题描述:
我需要写一个bash脚本做一些事情以其他用户身份,然后返回到初始用户...如何以其他用户身份登录,然后登出bash脚本?
假设我运行下面的根:
#!/bin/bash
USER=zaraza
su - "${USER}"
#do some stuff as zaraza
________ #here I should logout zaraza
#continue doing things as root
在我应该写控制台“退出”,但在bash是一个关键字,它退出脚本...
由于提前,
答
使用su -c
。从手册页:
su man -c catman Runs the command catman as user man. You will be asked for man's password unless your real UID is 0.
答
最简单的方法是使具有与其他用户运行一个单独的脚本的东西,用“-c”操作来调用它苏,如su -c otherscript userid
。
您或许可以将它作为<< EOF
的“这里是脚本”,但我从来没有尝试过。
答
你也可以使用sudo
。
答
这将做到这一点。
su - $ {username} -c /path/to/the/shellscript.sh