实现文件批量创建用户
创建用户文件
[root@localhost script]# cat user.txt
user01 123456
user02 abcdef
user03 hjklmn
user04 654321
user05 opqrst
脚本案例
#!/bin/bash
#create user by file
#v1.0 itwangqiang 2020/12/28
if [ $# -eq 0 ];then
echo "usage: `basename $0` file"
exit
fi
if [ ! -f $1 ];then
echo "error file"
exit
fi
#IFS内部字段分隔符,重新定义分隔符
#IFS的两种使用方式:
#IFS=$'\n' \n:表示回车
#单引号内直接回车
IFS='
'
#for循环是以空格或TAB健作分隔符
for line in `cat $1`
do
user=`echo "$line" | awk '{print $1}'`
pass=`echo "$line" | awk '{print $2}'`
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "user $user is exist"
else
useradd $user
echo "$pass" |passwd --stdin $user &> /dev/null
fi
done
文件出现空行
#!/bin/bash
#create user by file
#v1.0 itwangqiang 2020/12/28
if [ $# -eq 0 ];then
echo "usage: `basename $0` file"
exit
fi
if [ ! -f $1 ];then
echo "error file"
exit
fi
#IFS内部字段分隔符,重新定义分隔符
#IFS的两种使用方式:
#IFS=$'\n' \n:表示回车
#单引号内直接回车
IFS='
'
#for循环是以空格或TAB健作分隔符
for line in `cat $1`
do
if [ ${#line} -eq 0 ];then
continue
fi
user=`echo "$line" | awk '{print $1}'`
pass=`echo "$line" | awk '{print $2}'`
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "user $user is exist"
else
useradd $user
echo "$pass" |passwd --stdin $user &> /dev/null
fi
done