while循环创建用户(区别for)

while批量创建用户

vim while_create_user.sh

#!/usr/bin/bash

#while create user

#v1.0 by xfeng

while  read line

do

  if [ ${#line} -eq 0 ];then    #变量的字符串长度

    echo "the line is null..."

    continue        #continue跳过本次循环,break跳过所有循环,exit直接终止程序

  fi

  user=`echo $line|awk '{print $1}'`

  pass=`echo $line|awk '{print $2}'`

  id $user &>/dev/null

  if [ $? -eq 0 ];then

    echo "user $user already exists"

  else

    useradd $user

    echo "$pass"|pass --stdin  $user &>/dev/null

    if [ $? -eq 0 ];then

      echo "$user is created...."

    fi

  fi

done< $1      #while读取文件是按行读取,for读取文件默认是以空格或者tab键作为分隔符

echo  "All is ok....."

continue跳过空行循环,继续执行后面的内容;break跳过本次循环及后面所有内容循环,执行循环以外的内容;exit跳过本次程序,退出shell。

cat a.txt

feng  123456

xiao 445566

sh -vx while_create_user.sh  a.txt

猜你喜欢

转载自www.cnblogs.com/xiaofeng666/p/12243346.html
今日推荐