Linux under Bash programming combination test and write comprehensive script (v)
1.Bash Programming Combination test conditions
-A: With relation
-O: or relationship
!: Non-relational
Presentation Method 1:[$#-gt 100-a $#-le 500]
Presentation method 2:[$#-gt] && [$#-le 500]
2. Write a script that adds and deletes users as follows:
2.1: If the script option is--add:, the user will be added; If the option is--del, the user will be removed, if it is--help display Help information
2.2: Parameters following the script option can be arbitrarily specified by multiple users and separated by commas , password and user name;
2.3: The user exists on the prompt cannot be added, the user does not exist can not delete the user;
The script reads as follows:
Vim admuseradv.sh Create a script file:
#!/bin/bash
#以下红色部分是用来判断脚本后面参数是否正确, i.e. up to 2 parameters
If [$#-lt 1-o $#-gt 2]; Then
echo "ARGUS is bad,you use Script--help"
Exit 8
elif [$#-eq 1-a ' = = '--add ']; Then
echo "ARGUS is bad,you use Script--help"
Exit 18
elif [$#-eq 1-a ' = = '--del ']; Then
echo "ARGUS is bad,you use Script--help"
Exit 16
Fi
#以下黑色部分是用来判断是否添加用户
if [= = '--add ']; Then
For I in ' echo $ | Sed ' s/,//g '; Do
if! ID $I &>/dev/null; Then
Useradd $I
echo $I | passwd--stdin $I &>/dev/null
echo "Add $I successfully"
Else
echo "$I Exist"
Fi
Done
#以下蓝色部分是用来判断是否删除用户
elif [= = '--del ']; Then
For I in ' echo $ | Sed ' s/,//g '; Do
If ID $I &>/dev/null; Then
Userdel-r $I
echo "Delete $I finishing"
Else
echo "$I not Exist"
Fi
Done
#以下黑色部分是用来判断是否寻求帮助信息
elif [= = '--help ']; Then
echo "./admuser.sh--add user1,user2...|. /admuser.sh--del User1,user2,... | ./admuser.sh--help "
Else
echo "ARGs is error,please Usage:--help"
Fi
The following script executes the test effect:
./ADMUSERADV--add User1,user2,user3 added 3 users
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/11/wKiom1cR1zGQxmnDAADLv21IAJk286.jpg "title=" Adduser.jpg "alt=" wkiom1cr1zgqxmndaadlv21iajk286.jpg "/>
./admuseradv --del USER1,USER2,USER3 Deletion of 3 users
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/0E/wKioL1cR2ArBLVGNAADTUTxr88k533.jpg "title=" Deluser.jpg "alt=" wkiol1cr2arblvgnaadtutxr88k533.jpg "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/11/wKiom1cR16iQ3Av7AACRq6FruD0836.jpg "title=" Error.jpg "alt=" Wkiom1cr16iq3av7aacrq6frud0836.jpg "/>
This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1764400
Linux under Bash programming combination test and scripting (v)