shell脚本批量创建用户并设置密码的两种方法

#!/bin/bash
# version
# date
# author
. /etc/init.d/functions
user="Uname_"
passfile=/tmp/passfile.log   #存储用户密码文件
if [ ! -f $passfile ];then
   touch $passfile
fi
for i in `seq 31 40`
do
  pass=`echo "test@$RANDOM"|md5sum|cut -c 3-13`
  useradd $user$i &>/dev/null  && echo "$pass" |passwd --stdin $user$i
  echo -e "user:$user$i\tpasswd:$pass" >>$passfile
  if [ $? -eq 0 ];then
    action "$user$i is ok " /bin/true
  else 
    action "$user$i is fail " /bin/false
  fi
done
echo ----------------------------------------------------
cat $passfile


echo ****************************************************
#!/bin/bash
# version
# date
# author
. /etc/init.d/functions
file=/tmp/passfile.log
while read LINES
do
user=`echo $LINES|awk '{print $1}'|cut -d':' -f2`
passwd=`echo $LINES|awk '{print $2}'|cut -d':' -f2` 
useradd $user 
if [ $? -ne 0 ];then 
 echo "$user exists,skip set password"
 action "$user is fail " /bin/false
else 
 echo $passwd|passwd --stdin $user
 action "$user is ok" /bin/true 
fi
done < $file

echo ***************************************************
#!/bin/bash
# description 批量删除用户
# author
. /etc/init.d/functions
file=/tmp/passfile.log
array=(`cat $file|awk  '{print $1}'|awk -F":" '{print $2}'`)
for i in ${array[*]}
do
    userdel -r $i
    if [ $? -eq 0 ];then
     action "$i is ok" /bin/true
    else
     action "$i is fail" /bin/false
    fi
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值