判斷用戶是否存在,類型是否爲管理員

輸入一個用戶名,判斷該用戶是admin用戶或者普通用戶

由於管理員用戶id是1-500,而普通用戶是501以上

[root@localhost ~]# vim /tmp/uidex.sh

#!/bin/bash
username=$1

if [ "$username" = "" ];then
        echo "Please input a username"
        exit 1
fi

if id $username &> /dev/null;then
        userid=$(id -u $username)
        if [ $userid -lt 500 ]; then
        echo "$username is a admin user"
        else
                echo "$username is a normal user"
        fi
else
    echo "$username is not exit"
    useradd $username
    if [ $? -eq 0 ]; then  #判斷用戶是否添加成功
        echo "Add user $username."
    else
        echo "Can not add $username."
    fi
fi

id $username &> /dev/null 這重定向把無用的輸出消除


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章