一 检查用户是否是root权限的方式:
#!/bin/bash
ROOT_UID=0 # Root has $UID
0.
E_WRONG_USER=65 # Not root?
E_NOSUCHUSER=70
SUCCESS=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo
echo "Only root can run
this script."
echo
exit $E_WRONG_USER
else
echo
echo "You
should know better than to run this script, root."
echo "Even root users
get the blues... "
echo
二
#!/bin/bash
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0
have root privileges.
LINES=50 # Default number of lines
saved.
E_XCD=66 # Can‘t change directory?
E_NOTROOT=67 # Non-root
exit error.
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can‘t change to
$LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory, before
messing with log file.
原文地址:http://www.cnblogs.com/createyuan/p/3778241.html