shell command of linux & & & | test

& &;

&&: used to execute the command after the condition is established
||: used to execute the command after the execution condition is not established

Example:

[root@localhost mnt]# id toto   # User toto does not exist and cannot be viewed
id: toto: no such user
[root@localhost mnt]# id toto &> /dev/null && echo yes || echo no  # Judge whether the user exists
no    # Output no because the user does not exist
[root@localhost mnt]# useradd toto   # User exists after user creation 
[root@localhost mnt]# id toto &> /dev/null && echo yes || echo no  # Conditional output yes
yes

Test: check whether it can ping the host 172.25.47.204, the general output is yes, the general output is no

Write a script to check whether the network can ping
You can ping to output this ip is up;ping to output this ip is down

test command

The test command is usually used for judgment. The test command and [] are the same

 [ "$A" = "$B"  ]        	                ##Are they equal?
 [  "$A" != "$B"  ]        	                ##Unequal or not
 [  "$A" -eq "$B"  ]      	                ##Are they equal?
 [  "$A" -ne "$B"  ]       	                ##Unequal or not
 [  "$A" -le "$B"  ]       	                ##Less than or equal to
 [  "$A" -lt "$B"  ]                        ##Is it smaller than
 [  "$A" -ge "$B "  ]                       ##Is greater than or equal to
 [  "$A" -gt "$B"  ]                        ##Is it greater than
 [  "$A" -ne "$B" -a "$A" -gt "$B"  ]       ##A is not equal to B and a is greater than B
 [  "$A" -ne "$B" -o "$A" -gt "$B"  ]       ##A is not equal to B or a is greater than B
 [  -z "$A"  ]                              ##Is it empty?
 [  -n "$A"  ]                              ##Whether it is not empty
Example

1. The test command is usually used for judgment. The test command and [] are the same

[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# test "$a" != "$b" && echo yes || echo no
no
[root@localhost mnt]# [ "$a" != "$b" ] && echo yes || echo no
no

2 -eq ××× equals

[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
yes

3 -ne is not equal to

[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes

4 -le × less than or equal to

[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=0
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no

5 -lt less than

[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no

6 - Ge ××× greater than or equal to

[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=2
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=3
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes

7 -gt × greater than

[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=2
[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes

8 -a ×××××××××××

[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# c=3
[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
no

If one of the two conditions of 9-o is satisfied

[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# c=3
[root@localhost mnt]# [ "$a" -lt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" -gt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
no

10 -z ×××× whether it is empty

[root@localhost mnt]# c=''
[root@localhost mnt]# [ -z  "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# c=2
[root@localhost mnt]# [ -z  "$c" ] && echo yes || echo no
no

11 -n is not empty

[root@localhost mnt]# c=''
[root@localhost mnt]# [ -n  "$c" ] && echo yes || echo no
no
[root@localhost mnt]# c=2
[root@localhost mnt]# [ -n  "$c" ] && echo yes || echo no
yes
What is the type of a document
[ -f "file" ]                 ##Common file is true
[ -e "file" ]                 ##The existence of the file is true
[ -L "file" ]                 ##Soft link is true
[ -S "file" ]                 ##Set knot as true
[ -b "file" ]                 ##Hard disk device is true
[ -d "file" ]                 ##Directory is true
[ -c "file" ]                 ##Character device is true
[ "file1" -ef "file2" ]       ##Two files are the same file is true
[ "file1" -nt"file2" ]        ##File 1 created later than file 2 is true
[ "file1" -ot "file2" ]       ##File 1 created earlier than file 2 is true

Example:
["file1" - ef "file2"] (it is true that two files are the same file)

[root@localhost mnt]# touch file1
[root@localhost mnt]# ln file1 file2
[root@localhost mnt]# ls 
file1  file2  ping.sh
[root@localhost mnt]# ls -i
8845808 file1  8845808 file2  8845813 ping.sh
[root@localhost mnt]# [ "file1" -ef "file2" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "file1" -ef "ping.sh" ] && echo yes || echo no
no

["file1" - nt"file2"] (it is true that file1 is created later than File2)
["file1" - ot "file2"] (it is true that file1 is created earlier than file2)

[root@localhost mnt]# ll
total 0
-rw-r--r--. 1 root root 0 May 20 06:25 file1
-rw-r--r--. 1 root root 0 May 20 06:27 file2
[root@localhost mnt]# [ "file1" -nt "file2" ] && echo yes || echo no
no
[root@localhost mnt]# [ "file1" -ot "file2" ] && echo yes || echo no
yes
Test 1 write script to judge output file type


Test 2 script to create or delete users

When creating a user, if the user exists, the output user exists; if not, the user is created
When deleting a user, if the user exists, the user will be deleted; if the user does not exist, the output user does not exist


Tags: Operation & Maintenance less network

Posted on Thu, 07 Nov 2019 10:31:11 -0800 by gamesmad