Write a script:
1. Add 10 users user1 to User10, password with user name, but only if the user does not exist to add;
#!/bin/bash# forIinch{1..Ten}; Do if!IDUser$i &>/dev/NULL; ThenUseradd user$iEcho "user$i"|passwd--stdin user$iElse Echo "User$i exists" fi Done
Delete the above 10 users
#!/bin/bash# forIinch{1..Ten}; Do if IDUser$i &>/dev/NULL; ThenUserdel-r user$i &>/dev/nullEcho "user$i deleted" Else Echo "user$i NOT EXISTS" fi Done
Extension: Adds or deletes a specified number of users based on input parameters,
#!/bin/Bash2 # 3 if[$#-lt1]; Then 4 Echo "Usage:no ARGs" 5 fi 6 if[ $1=="--add"]; Then 7 forIinch`Echo$2|sed 's/,//g'`; Do 8 if ID$I &>/dev/NULL; Then 9 Echo "$I exists" Ten Else OneUseradd $I A Echo$I |passwd--stdin $I &>/dev/NULL - Echo "Add $I finished" - fi the Done - elif[ $1=="--del"]; Then - forIinch`Echo$2|sed 's/,//g'`; Do - if!ID$I &>/dev/NULL; Then + Echo "$I NOT EXISTS" - Else +Userdel-R $I A Echo "Delete $I finished" at fi
Test:
Integer test
-le-lt-gt-ge-eq-ne
Character test
= = = > <-n-z
File test
-e-f-d-r-w-x
Combination test conditions
-A: With relation
-O: or relationship
! : Non-relational
If [$#-gt 1-a $#-le 3]
If [$#-gt 1] && [$#-le 3]
Linux--shell Programming (vi) using scripting options and combination condition testing