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?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
So you can access
$SUDO_USER
and ask the system for his homedir withgetent passwd $SUDO_USER | cut -d: -f6
.Try to avoid eval. Especially with root perms.
You can do:
Update:
here is why to avoid eval.
Try accessing the environment variable $SUDO_USER
Unless I misunderstood the question, when the user runs the script with sudo, the
$HOME
environment variable does not change. To test out, I created this script:... and run it:
Output:
The output tells me that
$HOME
is still pointing to the user's home (in this case, /home/haiv).The user's home directory would be
~$SUDO_USER
. You can useeval
as follows: