1、条件选择if (1)用法格式 if判断条件 1; then 条件为真的分支代码 elif判断条件 2; then 条件为真的分支代码 elif 判断条件 3 ; then 条件为真的分支代码 else 以上条件都为假的分支代码 fi 逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if。 (2)经典案例: ① 判断年纪 #!/bin/...
f()for i in "$@"; do echo "$i"; done if语法更短 # One line # Note: The 3rd statement may run when the 1st is true [[ $var == hello ]] && echo hi || echo bye [[ $var == hello ]] && { echo hi; echo there; } || echo bye # Multi line (no else, single stateme...
if [ "$string1" != "$string2" ]; then echo "Strings are different." else echo "Strings are not different." fi 这是我们执行脚本时的结果: 代码语言:txt 复制 $ ./test.sh Strings are different. 例3 我们可以与字符串一起使用的另一个运算符是-z,它允许我们测试字符串长度是否为 0。 代码...
if then else fi Used to test a condition and use a fallback if the test fails. if then elif else fi Used to test a condition and use a fallback if all tests fail. for do done Iterate over a list of values. while do done Used to performs a given set of commands an unknown numb...
else statement5 fi You can use this if .. elif.. if , if you want to select one of many blocks of code to execute. It checks expression 1, if it is true executes statement 1,2. If expression1 is false, it checks expression2, and if all the expression is false, then it enters...
双分支if语句 if condition;then 条件为真执行的代码 else 条件为假执行的代码 fi 多分支if语句 if condition1;then condition1为真时执行的代码 elif condition2;then condition2为真时执行的代码 elif condition3;then condition3为真时执行的代码 ... ...
Now we are going to see the single-line version of the above example. This example will provide similar output, but the code structure will be a single line. A similar code will look like the one below. num=16if[$num-gt 15];thenecho"The value is greater than 15";elseecho"The value...
$ type if; type then; type else; type elif; type fi if is a shell keyword then is a shell keyword else is a shell keyword elif is a shell keyword fi is a shell keyword And as you can see, they all are keywords. Next, try an if on one line, with integers and incorporate the...
ifCONDITION-TO-TEST;then CODE-TO-EXECUTE-1 elifNEXT-CONDITION-TO-TEST;then CODE-TO-EXECUTE-2 elifNEXT-CONDITION-TO-TEST;then CODE-TO-EXECUTE-2 else CODE-TO-EXECUTE-2 fi Note: In the above general syntax that you can include zero, one or multipleELIFconditions in the code block and the...
if [[ $VAL -lt 20 ]] then echo "The value is less than 20." else echo "The value is equal or greater than 20." fi It will ask for the user input and if the input is greater than or equal to 20 then else block will be executed and the output will be: ...