Ubuntu_shell脚本_自动添加用户名

用shell脚本 来实现以下操作:

#!/bin/bash
    user_name="$1"
    user_group="$2"
    
    if [ "$#" == 1 ]
    then
    	id $user_name >> /dev/null 2>&1
    	if [ "echo $? != 0" ]
    	then
    		sudo groupadd $user_name 
    		sudo useradd -m -g $user_name $user_name
    		sudo passwd $user_name
    	else
    		echo "用户名已存在"
    	fi
    elif [ "$#" == 2 ]
    then
    	id $user_name >> /dev/null 2>&1	
    	if [ "echo $? != 0" ]
    	then
    		cat /etc/group | grep $user_group >> /dev/null 2>&1
    		if [ "echo $? != 0" ]
    		then
    			sudo groupadd $user_group 
    			sudo useradd -m -g $user_group $user_name
    			sudo passwd $user_name
    		else
    			echo "组名已存在"	
    		fi
    	else
    		echo "用户名已存在"
    	fi
    else
    	echo "$0脚本的使用方式是:/bin/bash $0 user_name 或者 /bin/bash $0 user_name user_grp"
    fi

大家遇到问题可以评论交流。

猜你喜欢

转载自blog.csdn.net/weixin_44786482/article/details/88812539