若是不是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!