Batch create users:
1. Create the list file users.txt of user name, one line for each user.
[root@linuxprobe ~]# vim users.txt
andy
barry
carl
duke
eric
ergou
Next, write the shell script useradd.sh.
[root@linuxprobe ~]# vim useradd.sh
#!/bin/bush
read -p "Enter The Users Password : " PASSWD
for UNAME in `cat users.txt`
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "Already exists"
else
useradd $UNAME &> /dev/null
echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "$UNAME , Create success"
else
echo "$UNAME , Create failure"
fi
fi
done
Execute the script (in the script, / dell/null is a file called linux black hole, redirecting the output information to this file is equivalent to deleting the data, which can keep the user's screen window concise.).
[root@linuxprobe ~]# bash useradd.sh
[root@linuxprobe ~]# bash Example.sh
Enter The Users Password : 123456
andy , Create success
barry , Create success
carl , Create success
duke , Create success
eric , Create success
ergou , Create success
See if it was created successfully.
[root@linuxprobe ~]# tail -6 /etc/passwd
andy:x:1001:1001::/home/andy:/bin/bash
barry:x:1002:1002::/home/barry:/bin/bash
carl:x:1003:1003::/home/carl:/bin/bash
duke:x:1004:1004::/home/duke:/bin/bash
eric:x:1005:1005::/home/eric:/bin/bash
ergou:x:1006:1006::/home/ergou:/bin/bash
Finished creating users in batch.
Bulk delete users.
First create a user list file to delete.
[root@linuxprobe ~]# vim userdel.txt
andy
barry
carl
duke
eric
ergou
Write a shell script to delete the user.
[root@linuxprobe ~]# vim deluser.sh
#!/bin/bash
for user in `cat userdel.txt`
do
userdel -r $user
echo "The user deletc success !"
done
"deluser.sh" 6L, 100C 6,1 All
Then execute the script.
[root@linuxprobe ~]# bash deluser.sh
The user deletc success !
The user deletc success !
The user deletc success !
The user deletc success !
The user deletc success !
The user deletc success !
[root@linuxprobe ~]#
Verify that the deletion was successful.
[root@linuxprobe ~]# tail -6 /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
[root@linuxprobe ~]#
We found that users created in batch have been deleted.