rhel7 批量新建和刪除用戶

若須要批量新建或者刪除用戶,可以使用簡單腳本操做。bash

首先新建 users.txt,將要新建或者刪除的用戶放進去,spa

1 andy
2 barry
3 carl
4 duke
5 eric
6 george
7 kim
8 mary

而後編寫Example.sh,(新建用戶)code

 1 then 
 2 echo "Already exists"
 3 else
 4 useradd $UNAME &> /dev/null
 5 echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
 6 if [ $? -eq 0 ]
 7 then
 8 echo "$UNAME , Create Success"
 9 else
10 echo "$UNAME , Create Failure"
11 fi
12 fi
13 done

編寫userdel.sh,(刪除用戶)blog

 1 #!/bin/bash
 2 for UNAME in `cat users.txt`
 3 do
 4 id $UNAME &> /dev/null
 5 userdel $UNAME &> /dev/null
 6 if [ $? -eq 0 ]
 7 then
 8 echo "$UNAME delete success"
 9 else
10 echo "$UNAME ,not exist,deleted failed"
11 fi
12 done

最後運行便可。class

相關文章
相關標籤/搜索