
bash if elif
Bash provides programmatic conditional statements. Conditionals provide a decision point for the application flow. For example, if the file exists to do this if not do another thing like logic operations.
Bash提供程序化条件语句。 条件为应用程序流提供了决策点。 例如,如果文件存在,则执行此操作(如果不执行其他操作,如逻辑运算)。
如果是其他语法 (if else Syntax)
Bash if-else
statement can be used in 3 different cases. Here list of their syntax.
Bash if-else
语句可以在3种不同情况下使用。 这里列出他们的语法。
单箱检查 (Single Case Check)
We can check for a single case or situation with the if .. then .. fi
.
我们可以使用if .. then .. fi
检查单个案例或情况。
if CASE
then
COMMANDS
fi
- We will check the CASE with different logical operations like equal, greater then, same, etc. and if this case is true we will execute the COMMANDS, if not we will do not execute any commands. 我们将使用不同的逻辑运算(例如等于,大于,等于,等等)来检查CASE,如果是这种情况,我们将执行COMMANDS,否则将不执行任何命令。
两例检查 (Two Case Check)
In two case check, we will check two cases. If the first given CASE is true we will execute COMMANDS1, if it is not true we will execute else COMMANDS2.
在两种情况下,我们将检查两种情况。 如果第一个给定的CASE为true,则将执行COMMANDS1;如果为true,则将执行else COMMANDS2。
if CASE
then
COMMANDS1
else
COMMANDS2
fi
多案检查 (Multiple Case Check)
In multiple case check, we will check the CASE. If it is not true CASE2 will be checked if not CASE3 will be checked. We can create a lot of cases with the elif
. When the case is true then its COMMAND will be executed. If all of the if
and elif
is false than the last part which is else
COMMANDSN will be executed.
在多案例检查中,我们将检查案例。 如果不是,将检查CASE2,否则将检查CASE3。 我们可以使用elif
创建很多案例。 如果为真,则将执行其命令。 如果所有if
和elif
都比最后一部分( else
COMMANDSN)错误,则将执行该命令。
if CASE
then
COMMANDS1
elif CASE2
COMMANDS2
elif CASE3
COMMANDS3
...
else
COMMANDSN
fi
As we see there is a different type of syntax of if statements. Actually, they are very similar to each other in logic. We will look at them in detail below.
如我们所见,if语句有另一种语法。 实际上,它们在逻辑上非常相似。 我们将在下面详细介绍它们。
单一条件如果 (Single Condition If)
This type of if the syntax is very simple we will just check the single condition and then execute the required code.
如果这种类型的语法非常简单,我们将只检查单个条件,然后执行所需的代码。
#!/bin/bash
if true;
then
echo "TRUE";
fi;
This is a very simple example first we look if the line where it is true which means the condition is met so below then the branch will be executed. Then branch prints “TRUE” and fi ends if branch.
这是一个非常简单的示例,首先我们查看如果该行为true,即表示满足条件,那么下面将执行该分支。 然后分支打印“ TRUE” ,如果分支则结束。
其他两个条件 (Two Condition With Else)
We can improve our previous if condition and add a condition if the first condition is not met. Think this like true or false. For two situations there are two branches.
我们可以改善先前的if条件,如果不满足第一个条件,则可以添加条件。 认为这是对还是错。 对于两种情况,有两个分支。
#!/bin/bash
myvalue=false
if $myvalue;
then
echo "TRUE";
else
echo "FALSE";
fi;
多条件如果 (Multiple Condition If)
Generally, there will be more than one condition. We can specify multiple conditions using multiple elif statements like below.
通常,将存在多个条件。 我们可以使用多个elif语句指定多个条件,如下所示。
#!/bin/bash
age=12;
if [ $age -ge 18 ];
then
echo "Age is equal or greater than 18";
elif [ $age -ge 7 ];
then
echo "Age is between 7 and 17"
else
echo "Age is below 7";
fi;
In this example, we have three cases where first is equal or greater than 18 with the if line. Second is the elif line and looks equal to or greater than 7. The third one is else statement which is logically below 7.
在此示例中,在三种情况下,如果if行,则first等于或大于18。 第二条是elif行,看起来等于或大于7。第三条是else语句,逻辑上低于7。
-ge
is used as greater or equal operations.
-ge
用作更大或相等的操作。
Square brackets [
, ]
are used to output the result of the compare operation inside it to the condition.
方括号[
, ]
用于将内部比较操作的结果输出到条件。
For more information about the logic operations like equal, greater than, etc. look following tutorial.
有关等于,大于等的逻辑运算的更多信息,请参见以下教程。
Linux Bash Operators Like Assignment, Plus, Calculation
比较和检查运算符(Comparison and Check Operators)
During the usage of the if-else
we generally need to check cases by using comparison and check operators. They can be used to check if a file exists or a given number is greater than the other. Here we will list some of the most useful comparisons and check operators which can be used for if-else-elif
cases.
在使用if-else
期间,我们通常需要使用比较和检查运算符来检查案例。 它们可用于检查文件是否存在或给定数量是否大于另一个。 在这里,我们将列出一些最有用的比较和检查运算符,这些运算符可用于if-else-elif
情况。
真正 (True)
true
is a logical value that specifies the positive case. true
will match the case in if-else-elif
.
true
是指定肯定情况的逻辑值。 true
将匹配if-else-elif
。
true
假 (False)
false
is a logical value that specifies the negative case. false
will not match the case in if-else-elif
.
false
是指定否定情况的逻辑值。 false
与if-else-elif
中的情况不匹配。
负表达 (Negative EXPRESSION)
While using if-elif-else
we will use logical true and false. We can revert given logic back by prefixing the logic value with !
. If the given value is true and prefixed with !
it will be false. If the given value is false and prefixed with !
it will be true.
在使用if-elif-else
我们将使用逻辑true和false。 我们可以通过在逻辑值前面加上!
来还原给定的逻辑!
。 如果给定值是true并以!
为前缀这将是错误的。 如果给定值为false并以!
为前缀这将是真的。
!true
is equal to the false!true
等于false!false
is equal to the true!false
等于true
STRING大于零 (STRING Is Greater Than Zero)
We can check the size or character count of the STRING has more than 0 characters and it is not empty.
我们可以检查STRING的大小或字符数是否超过0个字符,并且不为空。
-n STRING
STRING为零 (STRING Is Zero)
We can check the size or character count of the STRING has 0 characters and it is empty.
我们可以检查STRING的大小或字符数是否为0个字符,并且为空。
-z STRING
STRING1等于STRING2 (STRING1 Is Equal To STRING2)
One of the most used comparisons and check operator is checking if two given string is equal or the same. We can use an equal sign =
like below.
最常用的比较和检查运算符之一是检查两个给定的字符串是否相等或相同。 我们可以使用等号=
如下所示。
STRING1 = STRING2
STRING1不等于STRING2 (STRING1 Is Not Equal To STRING2)
We can check if given STRING1 is not equal to another STRING2 with the !=
.
我们可以使用!=
检查给定的STRING1是否不等于另一个STRING2。
STRING1 != STRING2
INTEGER1等于INTEGER2 (INTEGER1 Is Equal To INTEGER2)
Another useful and popular comparison and check operation are checking if two given integer is equal numerically. We can use -eq
operator like below.
另一个有用且流行的比较和检查操作是检查两个给定的整数在数值上是否相等。 我们可以使用-eq
运算符,如下所示。
INTEGER1 -eq INTEGER2
INTEGER1大于INTEGER2 (INTEGER1 Is Greater Than INTEGER2)
Another useful and popular comparison and check operation are checking if INTEGER1 is greater than INTEGER2 numerically. We can use -gt
operator like below.
另一个有用且流行的比较和检查操作是检查INTEGER1在数值上是否大于INTEGER2。 我们可以使用-gt
运算符,如下所示。
INTEGER1 -gt INTEGER2
INTEGER1大于或等于INTEGER2 (INTEGER1 Is Greater Than Or Equal To INTEGER2)
Another useful and popular comparison and check operation are checking if INTEGER1 is greater than or equal to INTEGER2 numerically. We can use -ge
operator like below.
另一个有用且流行的比较和检查操作是检查INTEGER1在数值上是否大于或等于INTEGER2。 我们可以使用-ge
运算符,如下所示。
INTEGER1 -ge INTEGER2
INTEGER1小于INTEGER2 (INTEGER1 Is Less Than INTEGER2)
Another useful and popular comparison and check operation are checking if INTEGER1 is less than INTEGER2 numerically. We can use -lt
operator like below.
另一个有用且流行的比较和检查操作是检查INTEGER1的数字是否小于INTEGER2。 我们可以像下面那样使用-lt
运算符。
INTEGER1 -lt INTEGER2
INTEGER1小于或等于INTEGER2 (INTEGER1 Is Less Than Or Equal To INTEGER2)
Another useful and popular comparison and check operation are checking if INTEGER1 is less than or equal to INTEGER2 numerically. We can use -le
operator like below.
另一个有用且流行的比较和检查操作是检查INTEGER1在数值上是否小于或等于INTEGER2。 我们可以像下面那样使用-le
运算符。
INTEGER1 -le INTEGER2
文件存在并且是目录 (FILE Exists and Is A Directory)
We can also check some file and directory properties. We can check if the given file is a directory and exists with the -d
operator.
我们还可以检查一些文件和目录属性。 我们可以使用-d
运算符检查给定文件是否为目录并存在。
-d FILE
文件已存在 (FILE Exists)
We can only check if a given file or directory exist with the -e
operator like below.
我们只能使用-e
运算符检查给定的文件或目录是否存在,如下所示。
-e FILE
文件存在且大小大于零 (FILE Exists and Size Is Greater Than Zero)
We can check if the given file exists and there is some content inside it which means it is not empty and size is greater then zero.
我们可以检查给定文件是否存在,并且其中是否包含一些内容,这意味着该文件不为空且大小大于零。
-s FILE
单行使用 (Use If Else In A Single Line)
In some cases, we may need some clarity and readability. So we should use If Else
in a single line which is valid as far as we obey the syntax. We can express if else like below. We should be aware that the spaces before and after if
, then
, else
and fi
are important.
在某些情况下,我们可能需要一定的清晰度和可读性。 因此,我们应在单行中使用If Else
,只要我们遵守语法即可。 我们可以表达是否像下面这样。 我们应该意识到if
, then
, else
和fi
之前和之后的空格很重要。
#!/bin/bash
myvalue=false
if $myvalue; then echo "TRUE"; else echo "FALSE"; fi;
如果其他则嵌套 (Nested If Else)
We may a complex situation where nested logic exists. We can use If Else
in a nested manner to solve or process this case. In this example, we first check myvalue
variable whether it is True
or False
. If it is True we enter nested if-elif-else
. If not True we execute else
which prints FAILURE
. If it is True then we check myothervalue
whether it is 1
.If it is 1 then we print OK
if not we print nothing.
我们可能会遇到一个复杂的情况,即存在嵌套逻辑。 我们可以嵌套使用If Else
来解决或处理这种情况。 在此示例中,我们首先检查myvalue
变量是True
还是False
。 如果为True,则输入嵌套的if-elif-else
。 如果不是True,则执行else
,它会打印FAILURE
。 如果为True,则检查myothervalue
是否为1
如果为1,则打印OK
否则为myothervalue
。
#!/bin/bash
myvalue=false
myothervalue=1
if $myvalue; then
if $myothervalue -eq 1; then
echo "OK"
fi;
else
echo "FAILURE";
fi;
检查文件是否存在 (Check If A File Exist)
I think one of the most wanted examples is to check if a file exists and execute code according to the result. We will use -a
operator which is used to check folder existence and returns true if the given file exists.
我认为最想要的示例之一是检查文件是否存在,并根据结果执行代码。 我们将使用-a
运算符,该运算符用于检查文件夹是否存在,如果给定文件存在,则返回true。
#!/bin/bash
if [ -a "/home" ]
then
echo "/home directory exists."
fi

检查是否存在软链接(Check If A Softlink Exist)
We can check if the provided file exists and the symbolic link. We will use the -h
option which will check for the given file is a soft link or not.
我们可以检查提供的文件是否存在以及符号链接。 我们将使用-h
选项,该选项将检查给定文件是否为软链接。
#!/bin/bash
if [ -h "/bin/ping4" ];
then
echo "/bin/ping4 exist and symbolic link";
fi;

比较数字并检查A是否小于B(Compare Numbers and Check If A Is Lower Than B)
Another useful if-else check is comparing numbers. We can check two numbers and find if A is lower or equal to B.
另一个有用的if-else检查是比较数字。 我们可以检查两个数字并确定A是否小于或等于B。
#!/bin/bash
a=12
b=15
if [ $a -le $b ]
then
echo "$a is lower or equal to $b"
fi

We compare two variables named $a
and $b
with -le
with means lower or equal. If compare returns true We print the message to the console.
我们将两个变量$a
和$b
与-le
,其均值-le
或等于。 如果compare返回true,则将消息打印到控制台。
字符串比较(String Compare)
String variables can be compared too. For example, we may want to check the current user and compare if it is root.
字符串变量也可以比较。 例如,我们可能要检查当前用户并比较它是否是root用户。
#!/bin/bash
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi
We get the current user name with whoami
command and compare it if it is different from “root” with !=
. If the username is different than root we print a message to the console.
我们使用whoami
命令获取当前用户名,如果与!=
“ root”用户名不同,则将其进行比较。 如果用户名与root用户名不同,则将消息输出到控制台。
检查是否定义了变量 (Check If A Variable Is Defined)
It is very useful to check if a variable is set in the script file.
检查脚本文件中是否设置了变量非常有用。
#!/bin/bash
if [ -z ${HOME+x} ];
then
echo "HOME is unset";
else
echo "HOME is set to '$HOME'";
fi
bash if elif