shell脚本学习总结09--分支与循环结构
2016-08-11 23:55
603 查看
if 条件语句
case条件语句
其中条件部分可以写成 "1|99",[3-9]等
while循环语句
一般用while做死循环的情况较多
还有一种用于一行一行的读取输入文件
for
还有一种c语言风格的,
if [[ $1 = start ]];then echo start app elif [[ $1 = stop ]];then echo stop app elif [[ $1 = ... ]];then .... else echo Please input command! fi
case条件语句
#/bin/bash read -p "Please input number: " option case $option in "1") echo "the number you input is 1" ;; "2") echo "the number you input is 2" ;; [3-9]) echo "the number you input is $option" ;; *) echo ” the number must less 9" ;; esac
其中条件部分可以写成 "1|99",[3-9]等
while循环语句
#/bin/bash sum=0 i=0 while ((i<=10)) do ((sum=sum+i)) ((i++)) done echo $sum
一般用while做死循环的情况较多
#/bin/bash while true do uptime sleep 5 done
还有一种用于一行一行的读取输入文件
lsof -i:80|awk '{print $2}'|while read line;do [ $line != PID ]&& kill -9 $line;done
for
#/bin/bash # for i in `ls /usr/local/src` for i in {sleep,dinner,sport} do echo $i done
还有一种c语言风格的,
##计算0加到100
#/bin/bash sum=0 for ((i=0;i<=100;i++)) do sum=$(($sum+$i)) done echo $sum
相关文章推荐
- Windows 10周年纪念版安装Ubuntu/Linux Shell/Bash
- Linux Shell(二)——Shell的环境配置
- Linux Shell(二)——Shell的环境配置
- shell脚本几个小作业(含答案)
- bash与source等特点和区别
- shell定时器-crontab
- 9.bash2
- 8.bash1
- linux文件管理类命令,类型,用户的权限管理及bash shell的特性:命令别名,文件名通配
- 51nod 1070 Bash游戏 V4 (斐波那契博弈)
- Linux shell 通配符 / glob 模式
- shell 编写脚本批量ping ip
- 数组-在Shell脚本中的基本使用介绍
- 经典实用的自动创建Bash脚本文件头的脚本 推荐
- Android shell命令行中过滤adb logcat输出的几种方法
- shell脚本学习
- Shell脚本编程基础
- Shell学习笔记1
- 无webshell的UDF提权
- shell-sed命令