Home > Blockchain >  How to delete users listed in a file ( Ubuntu Bash):
How to delete users listed in a file ( Ubuntu Bash):

Time:08-19

I have been trying to write bash code which deletes all users listed in a txt file in Ubuntu, however I am not sure how to do this.

For example say that the text in a file has two users, A and B. What I am trying to figure out is how to actually delete the users A and B from the system.

This is my pseudocode:

numusersdel = wc -l userstobedel.txt 
for ((i=0;i < $numusersdel; i  )); do
    usertobedel = grab the username of the user from number line i
    deluser $usertodel
done

Please suggest how I can solve this. Thanks.

CodePudding user response:

You can use a while loop and read the file userstobedel.txt line by line:

while IFS= read -r usertobedel
do
    deluser "$usertobedel"
done < userstobedel.txt

IFS= and -r are explained here.

  • Related