用SHELL脚本怎样判断当前用户不为ROOT
-
- 帖子: 15
- 注册时间: 2005-09-28 13:32
用SHELL脚本怎样判断当前用户不为ROOT
不知怎样做,编写了就是不行
s=root
if [ s != `whoami` ]
then
echo $s
echo `whoami`
echo "The user is not root!"
fi
s=root
if [ s != `whoami` ]
then
echo $s
echo `whoami`
echo "The user is not root!"
fi
- oneleaf
- 论坛管理员
- 帖子: 10441
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
- yonsan
- 帖子: 887
- 注册时间: 2005-07-01 18:56
- 来自: 广州市
- greco
- 帖子: 2487
- 注册时间: 2007-04-06 10:24
- 来自: ~/Shanghai
if [ `id -u` != 0 ]; then
echo "You must be root to run this command."
exit 1
fi
echo "You must be root to run this command."
exit 1
fi
http://silenceisdefeat.com/~greco
代码: 全选
''.join([chr(ord(c)-2) for c in 'O{"G/ockn"ku<"itgeq0ujkBiockn0eqo'])
echo -n "Z3JlY28uc2hpQGdtYWlsLmNvbQ==" | base64 -d
- alinmn
- 帖子: 185
- 注册时间: 2006-05-19 21:42
- 来自: NIT
Re: 用SHELL脚本怎样判断当前用户不为ROOT
songqing 写了:不知怎样做,编写了就是不行
s=root
if [ s != `whoami` ]
~~~~~ "$s"
then
echo $s
echo `whoami`
echo "The user is not root!"
fi
-
- 帖子: 44
- 注册时间: 2007-02-04 14:22
- wen1987
- 帖子: 88
- 注册时间: 2007-07-04 21:54
root@atlas:/home/wen# ls -l /usr/bin/sudoambitiousboy 写了:经验证,如果考虑sudo的情况,使用$UID/$USER不太合适。
whoami / id 都会认为是root。但$UID/$USER还会是当前的用户。看来系统变量是不会因为使用sudo而变化的,必须切换回root才行。
-rwsr-xr-x 1 root root 91508 2006-10-09 19:37 /usr/bin/sudo
sudo的作用就是伪装成root(注意到权限中user的运行位是s),一般来说没有必要区分sudoer和root,或者说不应该区分,否则会造成其他人的困惑。
一定要区分的话,恐怕只能分析ps命令的结果。
Study or die.