#!/bin/bash read -t 30 -p "Input name:" name read -t 30 -p "Input number:" num #输入要批量添加的用户数量并将变量赋给num read -t 30 -p "Input password:" pass if [ -n "$name" -a -n "$num" -a -n "$pass" ] #判断都不为空 then y=$(echo $num | sed ‘s/^[0-9]//g‘) #如果$num包含0-9,则由sed替换为空并赋给y if [ -z "$y" ] #判断y是否为空,(也就是num是否为数字,为空则为数字反之非数字) then for (( i=1;i<$num;i=i+1 )) do useradd $name$i &>/dev/null echo $pass | passwd --stdin "$name$i" &>/dev/null #将$pass传值给passwd命令 done else echo "The number is only for numbers!" fi else echo "Your input is invalid!" fi
原文地址:http://lidongfeng.blog.51cto.com/4390516/1943279