linux中批量建立使用者指令碼示例
阿新 • • 發佈:2020-10-14
1、 直接建立使用者
[root@linuxprobe home]# pwd
/home
[root@linuxprobe home]# ls
a.txt linuxprobe software test.sh
[root@linuxprobe home]# seq -f liujiaxin%02g 10 > a.txt ## 建立使用者列表
[root@linuxprobe home]# cat a.txt
liujiaxin01
liujiaxin02
liujiaxin03
liujiaxin04
liujiaxin05
liujiaxin06
liujiaxin07
liujiaxin08
liujiaxin09
liujiaxin10
[root@linuxprobe home]# bash test.sh
please input passwd for the users:123456 ## 這裡需要輸入使用者密碼,批量均為123456
liujiaxin01 has created successfully!
liujiaxin02 has created successfully!
liujiaxin03 has created successfully!
liujiaxin04 has created successfully!
liujiaxin05 has created successfully!
liujiaxin06 has created successfully!
liujiaxin07 has created successfully !
liujiaxin08 has created successfully!
liujiaxin09 has created successfully!
liujiaxin10 has created successfully!
[root@linuxprobe home]# pwd
/home
[root@linuxprobe home]# ls ## home目錄下出現建立的使用者
a.txt liujiaxin01 liujiaxin03 liujiaxin05 liujiaxin07 liujiaxin09 software
linuxprobe liujiaxin02 liujiaxin04 liujiaxin06 liujiaxin08 liujiaxin10 test.sh
2、檢視建立使用者指令碼test.sh
[root@linuxprobe home]# cat test.sh
#!/bin/bash
read -p "please input passwd for the users:" PASSWD
for USERS in `cat a.txt`
do
id $USERS &> /dev/null
if [ $? -eq 0 ]
then
echo "$USERS has existed!"
else
useradd $USERS &> /dev/null
echo $PASSWD | passwd --stdin $USERS &> /dev/null
if [ $? -eq 0 ]
then
echo "$USERS has created successfully!"
else
echo "$USERS has not created successfully!"
fi
fi
done
3、刪除測試中建立的使用者
[root@linuxprobe home]# ls a.txt liujiaxin01 liujiaxin03 liujiaxin05 liujiaxin07 liujiaxin09 software test.sh linuxprobe liujiaxin02 liujiaxin04 liujiaxin06 liujiaxin08 liujiaxin10 test2.sh [root@linuxprobe home]# bash test2.sh ## 直接執行指令碼 liujiaxin01 has removed successfully! liujiaxin02 has removed successfully! liujiaxin03 has removed successfully! liujiaxin04 has removed successfully! liujiaxin05 has removed successfully! liujiaxin06 has removed successfully! liujiaxin07 has removed successfully! liujiaxin08 has removed successfully! liujiaxin09 has removed successfully! liujiaxin10 has removed successfully! [root@linuxprobe home]# cat test2.sh ## 檢視指令碼 #!/bin/bash for USERS in $(cat a.txt) do userdel -r $USERS &> /dev/null if [ $? -eq 0 ] then echo "$USERS has removed successfully!" else echo "$USERS has removed failure!" fi done
[root@linuxprobe home]# ls
a.txt linuxprobe software test2.sh test.sh