[英]how to find the user is logged in as root in Linux using bash script?
[英]How to know if a process is user or root in bash, linux
我需要知道一个过程是否来自用户。 我正在使用此代码:
#c is the PID
Aid=$(cat /proc/$c/status | grep -e ^Uid)
Uid="Uid: 0 0 0 0"
if [ "$Aid" != "$Uid" ]; then
echo "is from user"
fi
但是我对字符串中的“选项卡”不太满意,我认为这可能会导致某些意外行为。
还有其他方法吗?
您可以在不使用ps -o uid= -p $pidhere
进行解析的情况下获取某个pid的UID:
mypid=1
if uid=$(ps -o uid= -p "$mypid")
then
if [[ $uid -eq 0 ]]
then
echo "The process runs as root"
else
echo "The process runs as something else"
fi
else
echo "The process doesn't exist"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.