onetozero 2020-01-08
数字循环1
#!/bin/bash for((i=1;i<=3;i++)); do echo ${i} done
数字循环2
#!/bin/bash for i in $(seq 1 3); do echo ${i} done
数字循环3
#!/bin/bash for i in {1..3}; do echo ${i} done
数字循环4
#!/bin/bash awk ‘BEGIN{for(i=1; i<=3; i++) print i}‘
字符循环1
#!/bin/bash for s in "hello"; do echo ${s} done
字符循环2
#!/bin/bash for c in $*; do echo ${c} is the input char done
字符循环3
#!/bin/bash for c in 1 2 3; do echo ${c} done
字符循环4
#!/bin/bash str1="hello world" for c in ${str1}; do echo ${c} done
路径循环1
#!/bin/bashfor file in /proc/*; do echo $file is file path \! ; done
路径循环2
#!/bin/bash for file in $(ls *.sh) do echo $file is file path \! ; done
用于执行一系列命令和从输入文件中读取数据
#!/bin/bash x=1 while((${x}<5)); do echo ${x} #let "x++" x=`expr ${x} + 1` done
说明:let命令,用于执行一个或多个表达式,变量计算中不需要加上$来表示变量
无限循环
#!/bin/bash while true do echo 1 done
or
#!/bin/bash for((;;)); do echo 1 done
用于执行一系列命令直至条件为true时停止,一般while优于until
#!/bin/bash x=1 until((x>3)); do echo ${x} x=`expr ${x} + 1` done
#!/bin/bash score="A" case ${score} in "A") echo "A SCORE" ;; "B") echo "B SCORE" ;; *) echo "UNKNOWN SCORE" esac
比较运算符
-gt 大于
-lt 小于
-eq 等于
== 等于则条件为真
!= 不等于则条件为真
-z 字符串长度为0则为真
-n 字符串长度不为0则为真
逻辑运算符
-a 并且
-o 或者
参考资料:Linux下Shell的for循环语句
参考资料:《与shell脚本编程大全》