Skip to content

Instantly share code, notes, and snippets.

@laoshuterry
Created October 31, 2015 09:55
Show Gist options
  • Save laoshuterry/aa7abb5e67b21906efe2 to your computer and use it in GitHub Desktop.
Save laoshuterry/aa7abb5e67b21906efe2 to your computer and use it in GitHub Desktop.
#!/bin/bash
#批量添加用户
read -p "Please input user name: " -t 30 name
read -p "Please input the number of users: " -t 30 num
read -p "Please input the password of users: "-t 30 pass
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ];then
#将num变量传给sed,sed进行判断是否为数字,如果为数字
#将数字替换成空,如果有一个字符都不为数字,那么整个y就不会为空
y=$(echo $num|sed 's/[0-9]//g')
#如果num全部为数字,那么条件才能为真,其实进行for循环
if[ -z "$y"]
for((i=1;i<=$num;i=i+1))
do
useradd $name$i &> /dev/null
#--stdin的作用是将管道符前面的作为标准输入
echo $pass|passwd --stdin $name$i > /dev/null
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment