ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 批量加用户脚本

批量加用户脚本

原创 Linux操作系统 作者:kidking2010 时间:2011-08-15 09:17:30 0 删除 编辑

批量加用户脚本

创建100个系统用户,登录用户名取值范围为(exam0~exam99)用户登录密码与其用户名相同,用户的默认shell为要求为bash,用户的home目录要求在/home 下的与其用户名相同的文件夹下;这100个用户的uid应该从1001开始依次向后排列,同时这些用户都应该属于gid为1001的study组。
这个脚本怎么写?
你看过鸟哥的私房菜没有?第三版的??哪里面有介绍,而且还有一个事例,也是讲的如何批量的添加用户!!你可以看看!好像是在shell脚本这一章!   脚本这个东西我也在学习中,不是太懂!所以.......你看看鸟哥的介绍吧!
 
 
===
#!/bin/bash
read -p "您想要创建多少个用户:" count
i=1
name=user
while [ $i -le $count ]
do
if [ $i -le 9 ];then
i=0$i
fi
useradd $name$i
i='expr $i + 1'
done
-------------------------------------------------
自己修改下

 
=====
 
我就拿十个来搞~~

[root@alibaba ~]# cat useradd
#!/bin/bash
groupadd study -g 1001
for i in $(seq 0 9)
do
  ii=$[ $i+1000 ]
  useradd exam$i -s /bin/bash -u $ii -g study
  echo exam$i | passwd --stdin exam$i
done

++++++++++++++++++++++++++++++++++++++++++++++++++

[root@alibaba ~]# ./useradd
Changing password for user exam0.
passwd: all authentication tokens updated successfully.
Changing password for user exam1.
passwd: all authentication tokens updated successfully.
Changing password for user exam2.
passwd: all authentication tokens updated successfully.
Changing password for user exam3.
passwd: all authentication tokens updated successfully.
Changing password for user exam4.
passwd: all authentication tokens updated successfully.
Changing password for user exam5.
passwd: all authentication tokens updated successfully.
Changing password for user exam6.
passwd: all authentication tokens updated successfully.
Changing password for user exam7.
passwd: all authentication tokens updated successfully.
Changing password for user exam8.
passwd: all authentication tokens updated successfully.
Changing password for user exam9.
passwd: all authentication tokens updated successfully.
[root@alibaba ~]# tail -10 /etc/passwd
exam0:x:1000:1001::/home/exam0:/bin/bash
exam1:x:1001:1001::/home/exam1:/bin/bash
exam2:x:1002:1001::/home/exam2:/bin/bash
exam3:x:1003:1001::/home/exam3:/bin/bash
exam4:x:1004:1001::/home/exam4:/bin/bash
exam5:x:1005:1001::/home/exam5:/bin/bash
exam6:x:1006:1001::/home/exam6:/bin/bash
exam7:x:1007:1001::/home/exam7:/bin/bash
exam8:x:1008:1001::/home/exam8:/bin/bash
exam9:x:1009:1001::/home/exam9:/bin/bash
 
========
 
 
 
从板凳的理念出发~~
[root@alibaba ~]# cat useradd
#!/bin/bash
groupadd study123 -g 3001
read -p "创建用户数:" count
for i in $(seq 0 "$count")
do
  ii=$[ $i+3000 ]
  useradd exama$i -s /bin/bash -u $ii -g study123
  echo exama$i | passwd --stdin exama$i
done
 
 
 
 
========
建立10个系统账户,并加入到yewu组的脚本
#!/bin/bash
i=1
groupadd yewu
while  [ $i -le 10  ]
do
if  [ $i -le 10   ] ;then
useradd stu0$i -g yewu
else
useradd stu$i  -g yewu
fi
let i++
done
 
 
===========
 
 
交互式建立账户的脚步
#!/bin/bash
echo -n  "please input a username:"
read AA
useradd $AA -s /sbin/nologin
echo "$AA" | passwd --stdin $AA &> /dev/null
echo -n "Do you want to setup another user?(yes/no)"
read BB
while [ $BB = yes  ]
do
echo -n  "please input a username:"
read EE
useradd $EE -s /sbin/nologin
echo "$EE" | passwd --stdin $EE &> /dev/null
echo -n "Do you want to setup another user?(yes/no)"
read BB
done
 
 
====
循环语句  比如for   
   
useradd    home目录自动解决了

密码那 可以用  echo username:passwd | chpasswd     相关位置 变量代替
 
====
 
 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23141985/viewspace-704838/,如需转载,请注明出处,否则将追究法律责任。

上一篇: wget用法
请登录后发表评论 登录
全部评论

注册时间:2011-02-27

  • 博文量
    114
  • 访问量
    312907