获取用户的主目录,当他们运行一个脚本以root身份 [英] Get users home directory when they run a script as root
本文介绍了获取用户的主目录,当他们运行一个脚本以root身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要以root身份运行一个sh脚本,但它是通过使用sudo最终用户运行。我怎么能当使用sudo运行时,〜/点/根得到用户的主目录?
I have a sh script that needs to be run as root, however it is run by the end user using sudo. How can I get the users home directory when ~/ points to /root when running with sudo?
推荐答案
用户的主目录将是〜$ SUDO_USER
。您可以使用评估
如下:
The user's home directory would be ~$SUDO_USER
. You can use eval
as follows:
USER_HOME=$(eval echo ~${SUDO_USER})
echo ${USER_HOME}
这篇关于获取用户的主目录,当他们运行一个脚本以root身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文