脚本:
[root@localhost shell]# cat auto_add_user
#!/bin/bash
#auto add some user
adduser() {
username=$1
if ! id -u $username &> /dev/null
then
useradd $username
echo $username | passward --stdin $username &>/dev/null
return 0
else
return 1
fi
}
for i in {1..3}
do
[ -d /home/user$i/ ]
rm -rf /home/user$i
[ -f /var/spool/mail/user$i ]
rm -rf /var/spool/mail/user$i
done
for i in {1..3}
do
adduser user$i
if [ $? -eq 0 ]
then
echo "add user$i complete..."
else
echo "add user$i failure...."
fi
done
#auto delete user
echo "if or not delete the users you created? (y|n)"
read chioce
if [ $chioce == 'y' ]
then
for i in {1..3}
do
userdel user$i
echo "user$i delete complete..."
done
else
exit 7
fi
结果
1.[root@localhost shell]# ./auto_add_user
add user1 complete...
add user2 complete...
add user3 complete...
if or not delete the users you created? (y|n)
n
[root@localhost shell]# cat /etc/passwd | grep user
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
smbuser:x:501:501::/home/smbuser:/bin/bash
user1:x:503:503::/home/user1:/bin/bash
user2:x:504:504::/home/user2:/bin/bash
user3:x:505:505::/home/user3:/bin/bash
2.[root@localhost shell]# ./auto_add_user
add user1 failure....
add user2 failure....
add user3 failure....
if or not delete the users you created? (y|n)
y
user1 delete complete...
user2 delete complete...
user3 delete complete...
[root@localhost shell]# cat /etc/passwd | grep user
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
smbuser:x:501:501::/home/smbuser:/bin/bash
阅读(574) | 评论(0) | 转发(0) |