Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[shell script] batch create and delete users

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

First, write shell scripts to add users in batches

implementation method: judge whet that user exist, if yes, return error prompt, judge whether the user file exists, if no, exit

1. Create Add User Script

[root@localhost ~]# vim useradd.sh

#!/ bin/bash

if [ $# -eq 0 ];then

echo "You haven't entered any files! "

exit 1

fi

if [ ! -f $1 ];then

echo "input error! "

exit 2

fi

for user in `cat $1`

do

id $user &>/dev/null

if [ $? -eq 0 ];then

echo "User $user already exists! "

else

useradd -s /sbin/nologin $user

echo "password" | passwd --stdin $user

echo "User $user created successfully. "

fi

done

2. Check the script and add executable permissions

[root@localhost ~]# sh -n useradd.sh

[root@localhost ~]# chmod +x useradd.sh

3. Add users

[root@localhost ~]# ./ useradd.sh

You haven't entered any files!

[root@localhost ~]# ./ useradd.sh user.txt

Change the password for user1.

passwd: All authentication tokens have been successfully updated.

User1 was successfully created.

Change the password for user2.

passwd: All authentication tokens have been successfully updated.

User2 was successfully created.

Change the password for user3.

passwd: All authentication tokens have been successfully updated.

User3 was successfully created.

Change the password for user4.

passwd: All authentication tokens have been successfully updated.

User4 was successfully created.

Change the password for user5.

passwd: All authentication tokens have been successfully updated.

User5 was successfully created.

Second, delete users

1. Write deletion script

[root@localhost ~]# vim userdel.sh

#!/ bin/bash

#userdel

if [ $# -eq 0 ];then

echo "You have no input file! "

exit 1

fi

if [ ! -f "$1" ];then

echo "input error! "

exit 2

fi

for user in `cat $1`

do

id $user &>/dev/null

if [ $? -eq 0 ];then

userdel -r $user &>/dev/null

echo "Delete $user succeeded! "

else

The page you requested does not exist! "

fi

done

[root@localhost ~]# sh -n userdel.sh

[root@localhost ~]# chmod a+X userdel.sh

2. Delete users

[root@localhost ~]# ./ userdel.sh 111

Input error!

[root@localhost ~]# ./ userdel.sh user.txt

User1 deleted successfully!

User2 deleted successfully!

User3 deleted successfully!

User4 deleted successfully!

User5 deleted successfully!

[root@localhost ~]# ./ userdel.sh user.txt

The page does not exist!

The page does not exist!

The page does not exist!

The page does not exist!

The page does not exist!

while read user

do

id $user &>/dev/null

if [ $? -eq 0 ];then

echo "User $user already exists! "

else

useradd -s /sbin/nologin $user

echo "password" | passwd --stdin $user

echo "User $user created successfully. "

fi

done < $1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report