用户管理是Linux系统维护的工作之一,其中设计用户添加、删除等简单操作.
需求:一次添加很多用户.在一个文本文件中.
格式:以行为单位,每行是一条用户信息.用户名和密码之间使用特定的分隔符分开,可是是空格,逗号,Tab键等。这里用空格区分
eg:
[rhat@localhost ~]$ cat addusers.txt
username001 password001
username002 password002
username003 password003
username004 password004
usernmae005 paswword005
按行读取文件中用户信息.可以用for、while、until循环等.这里先暂时用for循环.
[rhat@localhost ~]$ vim useradd_for01.sh [rhat@localhost ~]$ cat useradd_for01.sh #!/bin/bash COUNT=0 for LINES in `cat addusers.txt` do echo $LINES let COUNT+=1 done echo echo "$0 looped $COUNT times" [rhat@localhost ~]$ sh ./useradd_for01.sh username001 password001 username002 password002 username003 password003 username004 password004 usernmae005 paswword005 ./useradd_for01.sh looped 10 times
以上按理说应该循环5次,可为什么循环了10次?,原来addusers.txt是用空格隔开的,而for循环在读取文件时,任何空白字符都可以作为其读取的分隔符.下面是while循环的例子:
[rhat@localhost ~]$ vim useradd_while01.sh [rhat@localhost ~]$ cat useradd_while01.sh #!/bin/bash COUNT=0 while read LINES do echo $LINES let COUNT+=1 done < addusers.txt echo echo "$0 looped $COUNT times" [rhat@localhost ~]$ sh ./useradd_while01.sh username001 password001 username002 password002 username003 password003 username004 password004 usernmae005 paswword005 ./useradd_while01.sh looped 5 times
从脚本运行结果来看,while的按行读取确实没有问题,因为while使用是换行符作为标记。(read 命令)
加工:从每行中分割出用户名和密码,每行空格前部分是用户名,空格后的部分为密码。值需要使用cut命令就能进行简单的拆分.
[rhat@localhost ~]$ vim useradd_while02.sh [rhat@localhost ~]$ cat useradd_while02.sh #!/bin/bash while read LINES do USERNAME=`echo $LINES | cut -f1 -d' '` USERPASS=`echo $LINES | cut -f2 -d' '` echo -n "USERNAME:$USERNAME PASSWORD:$USERPASS" echo done < addusers.txt [rhat@localhost ~]$ sh ./useradd_while02.sh USERNAME:username001 PASSWORD:password001 USERNAME:username002 PASSWORD:password002 USERNAME:username003 PASSWORD:password003 USERNAME:username004 PASSWORD:password004 USERNAME:usernmae005 PASSWORD:paswword005
其中运行passwd命令时,需要手动输入密码,可以通过man passwd 观察到 --stdin这个选项可以将密码用管道“|“传送密码.
--stdin
This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
脚本修改后如下:(需要root权限)
[root@localhost rhat]# cat useradd_while03.sh #!/bin/bash while read LINES do USERNAME=`echo $LINES | cut -f1 -d' '` USERPASS=`echo $LINES | cut -f2 -d' '` useradd $USERNAME echo $USERPASS | passwd --stdin $USERNAME done < addusers.txt [root@localhost rhat]# sh ./useradd_while03.sh Changing password for user username001. passwd: all authentication tokens updated successfully. Changing password for user username002. passwd: all authentication tokens updated successfully. Changing password for user username003. passwd: all authentication tokens updated successfully. Changing password for user username004. passwd: all authentication tokens updated successfully. Changing password for user usernmae005. passwd: all authentication tokens updated successfully.
以上脚本”可以工作“了,但还不是很完美,再运行一便,会发现新增用户不行了。但却又重修修改了密码.这是很危险的.应该增加一个用户是否存在的判断.另外所有非Shell内建命令都建议使用全路径,以避免由于环境变量的问题造成command not found。最后,脚本主体要尽量少使用变量,所以需要在脚本开头定义变量.如下:
[root@localhost rhat]# vim useradd_while04.sh [root@localhost rhat]# cat useradd_while04.sh #!/bin/bash USER_INFO=/home/rhat/addusers.txt USERADD=/usr/sbin/useradd PASSWD=/usr/bin/passwd CUT=/bin/cut while read LINES do USERNAME=`echo $LINES | $CUT -f1 -d' '` USERPASS=`echo $LINES | $CUT -f2 -d' '` $USERADD $USERNAME if [ $? -ne 0 ];then echo "$USERNAME exists,skip set password" else echo $USERPASS | $PASSWD --stdin $USERNAME fi done < $USER_INFO [root@localhost rhat]# sh ./useradd_while04.sh useradd: user 'username001' already exists username001 exists,skip set password useradd: user 'username002' already exists username002 exists,skip set password useradd: user 'username003' already exists username003 exists,skip set password useradd: user 'username004' already exists username004 exists,skip set password useradd: user 'usernmae005' already exists usernmae005 exists,skip set password
返 回 顶 部