我需要编写一个bash脚本来作为另一个用户执行某些操作然后返回到初始用户...
假设我以root身份运行以下命令:
#!/bin/bash
USER=zaraza
su - "${USER}"
#do some stuff as zaraza
________ #here I should logout zaraza
#continue doing things as root
在控制台中我应该写“exit”,但在bash中是一个关键字,它会退出脚本......
提前致谢,
答案 0 :(得分:5)
使用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.
答案 1 :(得分:3)
最简单的方法是将必须作为另一个用户运行的东西作为单独的脚本,并使用su中的“-c”选项调用它,如su -c otherscript userid
。
您可以使用<< EOF
作为“此脚本”进行此操作,但我从未尝试过。
答案 2 :(得分:0)
您也可以使用sudo
。
答案 3 :(得分:0)
这样做。
su - $ {username} -c /path/to/the/shellscript.sh