[Comprehensive use of shell 4] Delete users in batches

If there are a large number of users, it will be troublesome to delete them one by one. Write a shell script to delete them in batches

Comprehensive use 3 We talked about creating users in batches. For creating users with such rules, users can be deleted in batches.

The result of running this program is as follows:

 The idea is basically the same as when it was created, first specify the deletion range, then prompt success or failure, and finally display the cumulative

1. First of all, stty erase ^H allows the user to backspace when typing

2. Get the user's input and define two variables, and assign them to start: i and end: e

3. Define the cumulative variable count=0, error=0, the purpose is to display when outputting

4. Write the loop, the for loop used here

5. In order to filter information and find out whether the user exists in the system, you need to call the /etc/passwd file. In order to avoid vim, you can use awk, where the syntax of awk is: awk options 'BEGIN{proggram}END' FILENAME where BEGIN and END can be omitted

awk -F ':' '$1=="user_'$i'"{print "user_'$i'"}' /etc/passwd

-F is the delimiter of the specified data field. The default is a space. Here it is under /etc/passwd, so it is a quotation mark

$1 == "user_x" is to match the data whose first column is user_x, here is inside the loop, so use '' to enclose the variable, otherwise it will not work, the same after pirnt, and finally connect the file path

For example, if the loop runs for the 4th time, $i is 4 at this time, as follows, the output result is user_4, but the output content is only consistent with the execution of print after matching, and can be changed to 1 or other, then the following judgment will also be changed

[root@ice shfile]# awk -F ':' '$1=="user_'4'"{print "user_'4'"}' /etc/passwd
user_4

 6. Judgment: Assign the above command to the variable con, define var=user_$i and then judge between the two. If they are equal, it means that the user exists, and then execute the deletion. Note that the deletion application userdel -r is not written at this time -r cannot delete the user's home directory, and the execution result 2>/dev/null, 2 means that if the execution is incorrect, then > (redirect) put it into the null under /dev, and null is a black hole ', all things redirected here will disappear. If you don’t write this, the system will execute an error before you. The deleted user does not exist. At this time, else will not be executed, thus losing statistical significance.

if [ "$con" == "$var" ];then
        userdel -r user_$i 2>/dev/null
        echo -e "\033[32m"已成功将user_$i删除"\033[0m"
        let count++
else
        echo -e "\033[35m"user_$i不存在"\033[0m"
        let error++
fi

7. Then we use green fonts to show success, and red to show failure

8. Final statistical results

echo -e "总共删除了\033[33m`echo "1+$e-$s"|bc` \033[0m个用户,成>功\033[32m"$count"\033[0m个,失败\033[35m"$error"\033[0m个"

The complete code is as follows:

#!/bin/bash
stty erase ^H
read -p "删除user_的起始序号:" i
s=$i
read -p "终止序号:" e
count=0
error=0
for(( i;i<=e;i++ ))
do
var=user_$i
con=`awk -F ':' '$1=="user_'$i'"{print "user_'$i'"}' /etc/passwd`
if [ "$con" == "$var" ];then
        userdel -r user_$i 2>/dev/null
        echo -e "\033[32m"已成功将user_$i删除"\033[0m"
        let count++
else
        echo -e "\033[35m"user_$i不存在"\033[0m"
        let error++
fi
done
echo -e "总共删除了\033[33m`echo "1+$e-$s"|bc` \033[0m个用户,成功\033[32m"$count"\033[0m个,失败\033[35m"$error"\033[0m个"

Guess you like

Origin blog.csdn.net/qq_53521409/article/details/126589461