手把手教你写shell脚本——shell循环结构
来源:互联网 发布:源码分享是什么意思 编辑:程序博客网 时间:2024/04/30 13:28
一 条件语句
1.1 if—then
#!/bin/bashif root then echo "hello"fi如果命令运行成功(退出码为0),则then部分的命令被执行
1.2 if—then—else
#!/bin/bashif hunterthen echo "hello"else echo "goodbye" fiif语句中退出码非0,则执行else部分语句
1.3 elif
#!/bin/bashif Athen echo "nice to meet you"elif B 多重If判断then echo "hello"fi
1.4 test命令
与其它编程语言思维一样的方式
#!/bin/bashif test 1 -eq 2 test命令列出条件成立则退出并返回状态码0,命令等同if [ 1 -eq 2]then echo "right"else echo "wrong"fi
二 条件比较
2.1 数据比较
n1 -eq n2 相等n1 -ge n2 大于或等于n1 -gt n2 大于n1 -le n2 小于等于n1 -lt n2 小于n1 -ne n2 不等于
2.2 字符串比较
str1 = str2str1 != str2str1 < str2str1 > str2-n str1 检查str1的长度是否非0-z str1 检查str1的长度是否为0
#!/bin/bashstr1=goodstr2=wrongif [ $str1 \> $str2 ] 大于号需要转义,否则脚本会当重定向符号then echo "$str1 is greater than $str2"else echo "$str1 is less than $str2"fi
#!/bin/bashstr=if [ -z "$str" ] 判断字符串是否为空,空的或未初始化的变量对Shell脚本很致命then echo "empty"else echo "not empty"fi
2.3 文件比较
2.4 复合条件—>&&与、||或
#!/bin/bashif [ -e str ] && [ -f zero ]then echo "file all exist"fi
2.5 双圆括号
#!/bin/bashstr1=goodstr2=wrongif (( $str1 > $str2 )) 双圆括号中表达式里的大于号不用转义then echo "$str1 is greater than $str2"else echo "$str1 is less than $str2"fi
2.6 双方括号
#!/bin/bashif [[ $USER == r* ]] 支持通配符模式then echo "welcome root user"else echo "sorry"fi
2.7 case分支
#!/bin/bashcase $USER inroot|hunterno4) echo "welcome root user";; 尾部两个分号mysql) echo "welcome to database";;surfftp) echo "nice to meet you";;*) 都不匹配时的default语句 echo "i don't know you";;esac
三 循环语句
3.1 for循环
#!/bin/bashfor command in `ls /bin` for循环默认每个值以空格来分割do echo "command in /bin is $command"done
#!/bin/bashfor file in /usr/local/program/package/*do if [ -d "$file" ] 文件用引号圈起,以避免文件含空格等情况 then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fidone
3.2 字段分隔符
#!/bin/bashIFS.OLD=$IFS IFS=$'\n' 修改IFS分隔符为换行符,也可同时赋值为:;"等等for str in `cat /home/hunterno4/passwd`do echo "str in passwd is $str"doneIFS=$IFS.OLD 恢复IFS分隔符默认值
3.3 C语言风格的for循环
#!/bin/bashfor ((i = 0;i<5 ;i++)) 此时变量与值间可以有空格,变量不用加$号do echo "current number is $i"done
3.4 while命令
#!/bin/bashi=5while [ $i -gt 0 ]do echo $i i=$[ $i-1 ]done
3.5 嵌套循环
#!/bin/bashIFS.OLD=$IFSIFS=$'\n'for row in `cat /etc/passwd`do echo "passwd row in $row ———" IFS=$':' for words in $row do echo " $words" donedoneIFS=$IFS.OLD
4 循环控制
4.1 break
#!/bin/bashi=5while [ $i -gt 0 ]do echo $i i=$[ $i-1 ] if [ $i -eq 3 ] then break 自动终止最里面的循环,break n,终止n层循环 fidone
4.2 continue
#!/bin/bashi=10while [ $i -gt 0 ]do i=$[ $i-1 ] if [ $i -gt 3 ] && [ $i -lt 6 ] then continue continue n则指定继续执行哪级循环 fi echo "$i"done > output.txt 将循环过程中的结果重定向输出到文件
0 0
- 手把手教你写shell脚本——shell循环结构
- shell脚本1——分支结构、循环、函数
- AIX写shell脚本的for循环
- 脚本乐园 shell编程循环结构
- Shell脚本学习笔记-循环结构
- Shell 脚本知识回顾 (五) —— Shell 循环
- Shell 脚本知识回顾 (五) —— Shell 循环
- 教你玩shell脚本
- Shell脚本之循环
- shell脚本----for循环
- shell脚本----while循环
- shell脚本--while循环
- shell脚本中的循环
- Shell脚本之循环
- 循环执行shell脚本
- shell日循环脚本
- shell循环执行脚本
- shell脚本-循环语句
- 关于ListView的知识(3)
- 工具类Application和UIUtils
- 职场晋升有内幕,快来看看吧
- python学习
- NSObject-拷贝 NSCopy-NSMutablecopy
- 手把手教你写shell脚本——shell循环结构
- 笔记_Maya绑定基础_骨骼的 关节显示与设置
- ios cell.imageview大小修改
- ssh快速整合
- Onboarding
- FileUtil关于文件的工具类,LogUtils日志相关的工具类,可以在发布时候关闭
- 用 ElementTree 在 Python 中解析 XML
- 网络加载的基类http_get请求
- Git 操作命令