批次加使用者指令碼

kidking2010發表於2011-08-15

批次加使用者指令碼

建立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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章