if - then - else
statements
If-then-else statements are similar to if-else statements.
if command
then
commands
else
commands
fi
Copy the code
Different from if-else, if-else depends on true or false to determine whether the condition is true. If-then statements depend on whether shell command is successfully executed after if. That is, the exit status code of the command is 0.
If 0, then the command is executed. If not 0, the else command is executed. Fi indicates the end of the if-then-else statement.
If-then-else statements can also be nested like if-else statements, with the basic format:
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
elif command4
then
command set 4
fi
Copy the code
test
The command
What if if-then-else statements are required to determine conditions other than exit status codes, such as true and false? To resolve this, use the test command: test condition.
test
Command description:
Test evaluates the expression, and if it evaluates true, it returns the exit status code :0 (true); Otherwise, return the exit status code :1 (false); If there is no expression, the test command also returns the exit status code :1 (false).
Test with if-then-else:
if test command
then
commands
else
commands
fi
Copy the code
test
A synonym of the command, square brackets:[]
Basic usage format:
if [ condition ] then
commands
fi
Copy the code
Note: You must add a space after the first square bracket and before the second square bracket, otherwise an error will be reported.
Boolean logic support:
[condition1] && [condition2] # and computing [condition1] | | [condition2] # or operationCopy the code
test
Supporting judgment:
- The numerical comparison
The numerical comparison | describe |
---|---|
n1 -eq n2 |
Check whether n1 is equal to n2 |
n1 -ge n2 |
Check whether n1 is greater than or equal to n2 |
n1 -le n2 |
Check whether n1 is less than or equal to n2 |
n1 -gt n2 |
Check whether N1 is greater than n2 |
n1 -lt n2 |
Check whether N1 is less than n2 |
n1 -ne n2 |
Check whether n1 does not equal n2 |
#! /bin/bash
# control flow
var1=133
var2=145
if [ $var1 -eq $var2 ]
then
echo "$var1Is equal to the$var2"
elif [ $var1 -ge $var2 ]
then
echo "$var1Greater than or equal to$var2"
elif [ $var1 -le $var2 ]
then
echo "$var1Less than or equal to$var2" 133 is less than or equal to 145
elif [ $var1 -lt $var2 ]
then
echo "$var1Less than$var2"
elif [ $var1 -gt $var2 ]
then
echo "$var1Is greater than$var2"
elif [ $var1 -ne $var2 ]
then
echo "$var1Is not equal to$var2"
else
echo "Do not recognize"
fi
exit 0
Copy the code
Note that using floating-point numbers in the test command returns an error: 145.5: Integer expression expected, the bash shell can only handle integers.
- String comparison
String comparison | describe |
---|---|
str1 = str2 | Check if STR1 and STR2 are equal |
str1 ! = str2 | Check if STR1 and STR2 are not equal |
str1 > str2 | Check whether STR1 is greater than STR2 |
str1 < str2 | Check whether STR1 is smaller than STR2 |
-n str1 | Check if the length of str1 is non-zero |
-z str1 | Check whether the length of STR1 is 0 |
#! /bin/bash
# string comparison
var1="abc"
var2="acb"
var3=""
var4="abc"
if [ -n $var1 ];then
echo "The string var1 has a length greater than zero" Execute script output
fi
if [ $var2 ];then
echo "The string var2 has a value"Execute script output
fi
if [ -z $var3 ];then
echo "The string var3 has a length of 0"# execute script output, var3 will be executed even if it is not initialized
fi
if test $var4 = $var1 ; then
echo "String var4=var1"Execute script output
if [ $var1\ >$var2 ]; then
echo "String var1 > var2"Execute script output
elif [ $var1! =$var2 ]
then
echo "The string var1 does not equal var2."
else
echo "The string var1 is less than var2"
fi
else
echo "String var4! =var1"
fi
exit 0
Copy the code
Note: Greater-than and less-than signs must be escaped, otherwise the shell will treat them as redirection symbols and string values as file names; The greater than and less than order is different from that used by the sort command.
- The file is
The file is | describe |
---|---|
-d file |
Check that file exists and is a directory |
-e file |
Check whether file exists |
-f file |
Check that file exists and is a file |
-r file |
Check that file exists and is readable |
-w file |
Check that file exists and is writable |
-x file |
Check that file exists and is executable |
-s file |
Check that file exists and is not empty |
file1 -nt file2 |
Check whether file1 is newer than file2 |
file1 -ot file2 |
Check whether file1 is older than file2 |
-O file |
Check that file exists and is owned by the current user |
-G file |
Check that file exists and the default group is the same as the current user |
Example: Create a new directory, new file, write script data, change file permissions, and execute the script.
#! /bin/bash
# file comparison
var1="./compare"
var2="wonderful"
if [ -d $var1 ]; then
echo "${var1}Directory exists,"
if [ -f $var1/$var2 ] && [ -s $var1/$var2 ]
then
echo "${var2}File exists and is not empty. Switch workspace."
cd. /${var1}
if [ -x $var2 ]; then
echo "The file exists and is executable with the following content:"
cat -n $var2. /$var2
else
echo "File exists, not executable, write script"
( echo "#! /bin/bash" ) > $var2
( echo "echo $(date)perform$var1/${var2}Script successful 🔥") > >$var2
echo -n "Modify permissions and execute results"
chmod u+x $var2. /$var2
fi
else
echo "${var2}File does not exist or file is empty"
cd. /${var1}
if [ -e $var2 ]; then
echo "File exists"
else
touch $var2
fi
echo "Write 'script' to the file$var2 "
( echo "#! /bin/bash" ) > $var2
( echo "echo $(date)perform$var1/${var2}Script successful 🔥") > >$var2
echo "Change$var2Is executable."
chmod u+x $var2
echo "Perform${var2}The script". /${var2}
fi
else
echo "${var1}Directory does not exist"
echo "To create$var1Directory"
mkdir $var1
echo "Change directory"
cd $var1
echo "Current working directory:$PWD"
echo "Create an empty file:$var2"
touch $var2
echo "Write 'script' to the file$var2 "
( echo "#! /bin/bash" ) > $var2
( echo "echo $(date)perform$var1/${var2}Script successful 🔥") > >$var2
echo "Change$var2Is executable."
chmod u+x $var2
echo "Perform${var2}The script". /${var2}
fi
Copy the code
if - then - else
Advanced features of
- Double parentheses for mathematical expressions
- Double-sided parentheses for advanced string handling
Double brace
Double parentheses: Allows advanced mathematical expressions to be used during comparisons, whereas test only supports simple arithmetic comparisons: GT, GE… .
Note: You can use the double parenthesis command in an if statement or in a normal command in a script to assign values.
Basic format:
(( expression ))
Copy the code
Advanced mathematical expressions supported by double parentheses:
symbol | describe |
---|---|
Val++, + + val | Before/after |
Val – and – val | Before/after reduction |
! , &&, | |, | Logic takes the opposite, and, or |
~, &, |, > >, < < | Reverse, and, or, move right, move left |
六四屠杀 | Power operation |
Example: |
#! /bin/bash
# control flow
var=5
if (( $var** 3 > 100));then
echo "Double parenthesis assignment :"
var1=$var
(( var = $var ** 3 ))
echo -n "The output$var1 ** 3的值为: $var" Output 5 ** 3:125
echo ""
fi
Copy the code
The two sides brackets
Double-sided parentheses: This command provides an advanced feature for string comparisons. Basic format:
[[ expression ]]
Copy the code
Expression in parentheses can be compared using the standard string used in the test command, but it also uses a feature that the test command does not: pattern matching. Pattern matching uses ==, and the string to the right of the double equals sign is treated as a pattern. Example:
#! /bin/bash
# control flow
if [[ $USER == q* ]]
then
echo "Hello $USER"
else
echo "Match failed"
fi
Copy the code
case
The command
The case command matches variables in the mode of pattern matching.
The format of the case command is as follows:
case variable in
pattern1 | pattern2) The vertical line operator separates multiple patterns on a single line
commands1;;
pattern3)
commands2;;# Note that double semicolons are required as part of the syntax
*)
default commands;
esac # Closing sign
Copy the code
Example:
#! /bin/bash
# control flow
case $USER in
*li|*n*)
echo "hello $USER";;
li)
echo "hi li";;
*)
echo "by";;
esac
Copy the code
References:
Linux command line and shell scripting