shell脚本判断执行用户

在脚本中,判断执行者是否为root。

判断方法1,

#!/bin/bash
if [ `whoami` != "root" ];then
echo " only root can run me"
exit 1
fi 

判断方法2(但是用sudo执行回报脚本有语法错误,不知为何,有知道的高手请回复我)。

if [ $UID == 0 ];then
echo "you are root"
fi
原文地址:https://www.cnblogs.com/zhaojk2010/p/4675606.html