Ubuntu 批量添加用户

时间:2022-06-01 12:42:09

#!/bin/bash

cat user.txt | while read line
do
    user=$(echo $line | cut -d ' ' -f1)
    passwd=$(echo $line | cut -d ' ' -f2)
    
    useradd -r -m -s /bin/bash $user >/dev/null
    if [ $? -eq 0 ];
    then
        echo $user":"$passwd | chpasswd
    echo "$user create success!"
    else
        echo "$user exists,skip set passwd"
    fi  
    
done

其中user.txt中的格式如下:

a 123

b 123

c 123