4.5 case语句 case 语句一般用于选择性来执行对应部分块命令。 格式:case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac case 模式名 in 模式 1) 命令 ;; 模式 2) 2025-07-25 0 0 Server Shell
4.4 break和continue语句 break 是终止循环。continue 是跳出当前循环。 #!/bin/bash N=0 while true; do let N++ if [ $N -eq 5 ]; then break fi echo $N done # bash test.sh 2025-07-25 0 0 Server Shell
4.3 while语句 格式:while list; do list; done while 条件表达式; do 命令 done #!/bin/bash N=0 while [ $N -lt 5 ]; do let N++ echo $N done # bash test.sh 1 2 3 2025-07-25 0 0 Server Shell
4.2 for语句 格式:for name [ [ in [ word ... ] ] ; ] do list ; done for 变量名 in 取值列表; do 命令 done #!/bin/bash for i in {1..3}; do echo $i done # bash test.s 2025-07-24 0 0 Server Shell
4.1 if语句 格式:if list; then list; [ elif list; then list; ] ... [ else list; ] fi 4.1.1 单分支 if 条件表达式; then 命令 fi #!/bin/bash N=10 if [ $N -gt 5 ]; then 2025-07-24 0 0 Server Shell
3.9 Shell括号用途总结 看到这里,想一想里面所讲的小括号、中括号的用途,是不是有点懵逼了。那我们总结一下! ( ) 用途 1:在运算中,先计算小括号里面的内容 用途 2:数组 用途 3:匹配分组 (( )) 用途 1:表达式,不支持-eq 这类的运算符。不支持-a 和-o,支持<=、>=、<、>这类比较符和&&、|| 用途 2025-07-24 0 0 Server Shell
3.8 其他运算工具(let/expr/be) 除了 Shell 本身的算数运算表达式,还有几个命令支持复杂的算数运算: 命令 描述 示例 let 赋值并运算,支持++、-- let VAR=(1+2)*3 ; echo VAR x=10 ; y=5<br/>let x++;echo x 每执行一次x 加 1 let y--;echo $y 每执 2025-07-24 0 0 Server Shell
3.6 逻辑判断符 判断符 描述 示例 && 逻辑和,在[[]]和(())表达式中或判断表达式是否为真时使用 [[ 1 -eq 1 && 2 -eq 2 ]]为 true (( 1 == 1 && 2 == 2 ))为 true [ 1 -eq 1 ] && echo yes 如果&&前面表达式为 true 则执行后面 2025-07-24 0 0 Server Shell
3.5 布尔运算符 运算符 描述 示例 ! 非关系,条件结果取反 [ ! 1 -eq 2 ]为 true -a 和关系,在[]表达式中使用 [ 1 -eq 1 -a 2 -eq 2 ]为 true -o 或关系,在[]表达式中使用 [ 1 -eq 1 -o 2 -eq 1 ]为 true 2025-07-24 3 0 Server Shell