shell脚本第一篇——自定义创建用户和批量创建用户

1、shell脚本建立Linux用户

# vim /root/user.sh

  #!/bin/bash

  #通过脚本自定义创建用户

read -p  "请输入需创建的用户名:" $1

useradd $1

read -p  "请设置$1用户密码:" $2

echo  "$2" | passwd --stdin  $1 > /dev/null

# /root/user.sh 或 # cd /root # ./user.sh或#bash  user.sh或# . user.sh

 

#####################################################################

 

2、用脚本批量创建Linux用户

# vim /root/piuser.sh

  #!/bin/bash

  #通过脚本批量创建用户

   PREFIX="stu"   :定义用户名前缀

i=1

while [ $i -le 20 ]

do

       useradd  ${PREFIX}$i   :添加的用户名为:前缀+编号

echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null

userdel -r ${PREFIX}$i &> /dev/null  :批量删除用户

       let i++

done

 

 创建用户stu1到stu50,指定组为student组!每个用户需设定不同的密码!

 #!/bin/bash
for i in `seq 1 50`
do
    useradd -G student stu$i; 
    echo stu$i | passwd stu$i --stdin
done