bash - 在根脚本ubuntu上获取当前用户

标签 bash shell ubuntu

我为 /etc/NetworkDispatcher/dispatcher.d/ 中的网络调度程序编写了一个脚本.
如何获取当前登录的用户?

我已经尝试过这些命令:

$USER
$LOGNAME

谢谢你的帮助。

最佳答案

如果使用/usr/bin/sudo 运行脚本,您可以从 $SUDO_USER 环境变量访问原始用户。

例如,如果这是脚本 test.sh 的内容:

#!/bin/sh

echo "USER:      $USER"
echo "SUDO_USER: $SUDO_USER"

然后,如果您以“alex”身份运行它:
alex@yuzu:~$ ./test.sh 
USER:      alex
SUDO_USER: 

如果你通过 sudo 运行它:
alex@yuzu:~$ sudo ./test.sh
USER:      root
SUDO_USER: alex

关于bash - 在根脚本ubuntu上获取当前用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971593/

相关文章:

linux - 在 sudo 调用的 Bash 脚本中识别用户

python - bash脚本将整数作为参数并添加它们

linux - 将时间转换为 unix 时间戳

c - Ubuntu C代码如何在另一个文件夹中执行命令

python - 如何卸载 setuptools python

regex - 使用\b 和 grep 模式

arrays - Bash 脚本 - unix - 未找到命令

php - 为什么 php 脚本变慢了?

linux - 在哪里放置 shell 脚本/函数? .bashrc 还是 bin?

linux - 创建没有密码的用户