-eq equals
-ne Not equal to
-GT Greater than
-lt less than
-le less than or equal to
-ge greater than or equal to
-Z Empty string
= Two characters equal
! = Two characters are not equal
-N Non-empty string
Strings are commonly used to check user input, whether the system environment satisfies the criteria, and in shell scripts that provide interactive operations, to determine whether the user input positional parameters are compliant, and the usual operations of strings are as follows:
Document comparison operators:
-e filename If filenanme exists, it is true
-d filename If filename is a directory, true
-F filename True if filename is a regular document
-l filename If filename is a symbolic link, true
-r filename If filename is readable, true
-W filename if filename is writable, true
-x filename If filename is executable, true
The IF statement is a circular statement in the form of a control statement, by establishing conditions for the execution of the judgment, only when the condition is true, the corresponding code will be executed, otherwise no action will be taken.
* Single Branch if statement format:
If condition test action
Then
Command sequence
Fi
Example 1
Vim A.txt
#!/bin/bash
If
[!-d/media/cdrom]
Thenk
Mkdir-p/media/cdrom
Fi
wq!
Sh-x a.txt x option for viewing the debugging process
Example 2
Vim B.txt
#! /bin/bash
num=100
if (($NUM > 4)); then
echo "This num is $NUM greater 4!"
Fi
wq!
Sh-x B.txt
* Dual-Branch if statement format:
Two-branch selection results, the requirements for the condition is set up, the conditions are not set up to perform different operations, respectively
Grammatical structure
If condition test action
Then (conditional execution)
Command sequence 1
else (otherwise, if the condition is not set, then execute)
Command Sequence 2
Fi
Example 3
Vim C.txt
#!/bin/bash
Ping-c 3-i 0.2-w 3 $
If
[$?-eq 0]
Then
echo "IP is Up"
Else
echo "IP is down"
Fi
Sh-x C.txt 172.0.0.1
Example 4
Vim B.txt
#!/bin/bash
Word=$1
grep ^$1$ "/usr/share/dict/words-q
If [$?-eq 0];then
echo "Word is a dictionary word"
Else
echo "Word is not a dictionary word"
Fi
This article is from the Oracle Personal Learning Notes blog, so be sure to keep this source http://wuchunqiang.blog.51cto.com/1022331/1853496
The IF statement for Linux shell programming