1.if-then
#!/bin/bash
If date if the command runs successfully (exit code 0), then the command in the then section is executed
Then
echo "Good"
Fi
2.if-then-else
#!/bin/bash
If Hunter
Then
echo "Good"
Else
echo "bad" if statement in exit code not 0, execute else part of statement
Fi
3.elif
#!/bin/bash
If Hunter
Then
echo "Command not Found"
Elif Date Multiple if judgment
Then
echo "Date"
Fi
4.test commands the same way as other programming languages think
#!/bin/bash
The IF test 1-eq 2 Test command lists the condition and exits with a status code of 0, and the command equals if [1-eq 2]
Then
echo "Right"
Else
echo "Wrong"
Fi
5. Condition comparison
1) data comparison
N1-eq n2 equal
N1-ge n2 &NBS P greater than or equal to
n1-gt n2 Greater
N1-le n2 &NBS P Less than or equal to
n1-lt n2 less
N1-ne n2 &NBSP ; not equal to
2) string comparison
str1 = str2
str1! = str2
str1 < str2
str1 > str2
-n str1 Check if the length of the str1 is 0
-Z str1 Check if the length of the str1 is 0
#!/bin/bash
Str1=good
Str2=wrong
If [$str 1 \> $str 2] The greater than sign needs to be escaped, otherwise the script will be redirected
Then echo "$str 1 is greater than $STR 2"
Else
echo "$str 1 is less than $STR 2"
Fi
#!/bin/bash
Str=
If [-Z ' $STR '] determines whether a string is empty, an empty or uninitialized variable is fatal to the shell script
Then
echo "Empty"
Else
echo "Not Empty"
Fi
4) Compounding conditions && and, | | Or
#!/bin/bash
If [-e str] && [-F Zero]
Then
echo "File all exist"
Fi
5) Double Parenthesis
#!/bin/bash
Str1=good
Str2=wrong
if (($str 1 > $str 2)) The greater than sign in the expression in double parentheses does not escape
Then echo "$str 1 is greater than $STR 2"
Else
echo "$str 1 is less than $STR 2"
Fi
6) both brackets
#!/bin/bash
if [[$USER = = r*]] Supports wildcard mode
Then
echo "Welcome root user"
Else
echo "Sorry"
Fi
7) Case Branch
#!/bin/bash
Case $USER in
Root|hunterno4)
echo "Welcome root user";; Trailing two semicolon
MySQL
echo "Welcome to database";;
SURFFTP)
echo "Nice to meet you";;
*) does not match when the default statement
echo "I don ' t know you";;
Esac
#! /bin/BASHSTR1=Tenstr2=Tenif[$str 1-GT $str 2] #大于号需要转义, otherwise the script will be redirected symbols Then Echo "$str 1 is greater than $STR 2"elif[$str 1-eq $str 2] Then Echo "$str 1 is equal to $STR 2"Else Echo "$str 1 is less than $STR 2"fiStr="ABC"if[ -Z $str] Then Echo "Empty"Else Echo "Not empty, the string is:"$strfiSTR3="AAA"if[[$str 3 ="AAA" ]] Then Echo "STR3 ($str 3) equals \ "Aaa\""ElseEcho "Not equals"fiifTest1=1 Then Echo "Test"Test" "Else Echo "Not equals"fi
Linux Shell Programming (1): Conditional statements