若是不是root用戶執行腳本,會致使有些命令或文件有權限設置(/etc/passwd,/etc/group,/etc/shadow),致使不能寫數據進去,因此在腳本以前能夠添加一個判斷語句,判斷當前用戶是否是超級用戶。vim
[root@cnsz142728 scripts]# vim root.sh #!/bin/bash if [ ` id $MYSELF | cut -c 1-5` != "uid=0" ] then echo "This scripts must be run as root!" exit 1 fi [chkusr@cnsz142728 scripts]$ ./root1.sh This script MUST be run as root!