[root@mail ~]# echo 1234568 |passwd --stdin zhangsan Changing password for user zhangsan passwd: all authentication tokens updated successfully.
[root@mail ~]# echo $RANDOM 31427 [root@mail ~]# echo $RANDOM 10943 [root@mail ~]# echo $RANDOM 4070 [root@mail ~]# echo $RANDOM 20273
[root@mail ~]# echo $RANDOM |md5sum d908b56aea8ba230846e9a6a21093f86 - [root@mail ~]# echo $RANDOM |md5sum d6a8e0adb2607506fc0e07a53c0e1448 - [root@mail ~]# echo $RANDOM |md5sum c2be3c5e60858b9416a4b72cd6968a03 -
[root@mail ~]# echo $RANDOM |md5sum |cut -c 1-8 0c61bdc7 [root@mail ~]# echo $RANDOM |md5sum |cut -c 1-8 0bc8559f [root@mail ~]# echo $RANDOM |md5sum |cut -c 1-8 b531b827
[root@mail sh]# echo {1..10} 1 2 3 4 5 6 7 8 9 10
[root@mail sh]# id zhangsan uid=1000(zhangsan) gid=1000(zhangsan) groups=1000(zhangsan) [root@mail sh]# id lisi id: lisi: no such user
[root@mail sh]# id lisi id: lisi: no such user [root@mail sh]# echo $? 1 [root@mail sh]# ls 1.sh [root@mail sh]# echo $? 0
For example: if the execution is successful, execute the else inside, and if the execution fails, execute the following then
#!/bin/bash USER_LIST=./user.list #Store the user name and provide a list for deleting users in the future USER_FILE=./user.info #Record the password corresponding to the user in the user.info file under the Dangqia directory for USER in user{1..20};do #Create 20 users if ! id $USER &>/dev/null; then #Determine whether the user exists, if it exists, execute else below, if it does not exist, then execute then PASS=$ ( echo $RANDOM | md5sum |cut -c 1-8) &>/dev/null #Intercept 8 random passwords useradd $USER #Create user echo $PASS | passwd --stdin $USER &>/dev/null #Interaction-free Assign a password to the user echo " $USER $PASS " >> $USER_FILE #Write the password corresponding to the user in a file echo " $USER " >> $USER_LIST echo " $USER create sucessful! " #Execute successfully and return the value else echo "$USER already exists!!!" #If the user already exists, return the value fi done
[root@mail sh]# ./1.sh user1 create sucessful! user2 create sucessful! user3 create sucessful! user4 create sucessful! user5 create sucessful! [root@mail sh]# ./2.sh The user1 delete success ! The user2 delete success ! The user3 delete success ! The user4 delete success ! The user5 delete success !
#!/bin/bash USER_LIST=$@ USER_FILE=./user.info for USER in $USER_LIST; do if ! id $USER &>/dev/null; then PASS=$(echo $RANDOM | md5sum |cut -c 1-8) &>/dev/null useradd $USER echo $PASS | passwd --stdin $USER &>/dev/null echo " $USER $PASS " >> $USER_FILE echo " $USER create sucessful! " else echo "$USER already exists!!!" fi done
[root@mail sh]# ./1.sh lisi wangwu zhao lisi create sucessful! wangwu create sucessful! zhao create sucessful!
#!/bin/bash for user in `cat user.list` do userdel -r $user echo "The $user delete success !" done
#!/bin/bash USER_FILE=./user.info for USER in `cat user2.list`; do if ! id $USER &>/dev/null; then PASS=$(echo $RANDOM | md5sum |cut -c 1-8) &>/dev/null useradd $USER echo $PASS | passwd --stdin $USER &>/dev/null echo " $USER $PASS " >> $USER_FILE echo " $USER create sucessful! " else echo "$USER already exists!!!" fi done