手把手教你写shell脚本——shell循环结构
一 条件语句
1.1 if—then
#!/bin/bash
if root
thenecho "hello"
fi
如果命令运行成功(退出码为0),则then部分的命令被执行
1.2 if—then—else
#!/bin/bash
if hunter
thenecho "hello"
else echo "goodbye"
fi
if语句中退出码非0,则执行else部分语句
1.3 elif
#!/bin/bash
if A
thenecho "nice to meet you"
elif B 多重If判断
thenecho "hello"
fi
1.4 test命令
与其它编程语言思维一样的方式
#!/bin/bash
if test 1 -eq 2 test命令列出条件成立则退出并返回状态码0,命令等同if [ 1 -eq 2]
thenecho "right"
elseecho "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 = str2
str1 != str2
str1 < str2
str1 > str2
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
#!/bin/bash
str1=good
str2=wrong
if [ $str1 \> $str2 ] 大于号需要转义,否则脚本会当重定向符号
then echo "$str1 is greater than $str2"
elseecho "$str1 is less than $str2"
fi
#!/bin/bash
str=
if [ -z "$str" ] 判断字符串是否为空,空的或未初始化的变量对Shell脚本很致命
then echo "empty"
elseecho "not empty"
fi
2.3 文件比较
item1 | item2 | 含义 |
---|---|---|
-d | file | 检查文件是否存在且为目录 |
-e | file | 检查文件是否存在 |
-f | file | 检查文件是否存在且为文件 |
-r | file | 检查文件是否存在且可读 |
-s | file | 检查文件是否存在且非空 |
-w | file | 检查文件是否存在且可写 |
-x | file | 检查文件是否存在且可执行 |
-O | file | 检查文件是否存在且属当前用户 |
-G | file | 检查文件是否存在且默认组与当前用户相同 |
Item1 | 含义 |
---|---|
file1 -nt file2 | 检查file1是否新于file2 |
file1 -ot file2 | 检查file1是否旧于file2 |
2.4 复合条件—>&&与、||或
#!/bin/bash
if [ -e str ] && [ -f zero ]
thenecho "file all exist"
fi
2.5 双圆括号
#!/bin/bash
str1=good
str2=wrong
if (( $str1 > $str2 )) 双圆括号中表达式里的大于号不用转义
then echo "$str1 is greater than $str2"
elseecho "$str1 is less than $str2"
fi
2.6 双方括号
#!/bin/bash
if [[ $USER == r* ]] 支持通配符模式
thenecho "welcome root user"
elseecho "sorry"
fi
2.7 case分支
#!/bin/bash
case $USER in
root|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/bash
for command in `ls /bin` for循环默认每个值以空格来分割
do echo "command in /bin is $command"
done
#!/bin/bash
for file in /usr/local/program/package/*
doif [ -d "$file" ] 文件用引号圈起,以避免文件含空格等情况thenecho "$file is a directory"elif [ -f "$file" ]thenecho "$file is a file"fi
done
3.2 字段分隔符
#!/bin/bash
IFS.OLD=$IFS
IFS=$'\n' 修改IFS分隔符为换行符,也可同时赋值为:;"等等
for str in `cat /home/hunterno4/passwd`
doecho "str in passwd is $str"
done
IFS=$IFS.OLD 恢复IFS分隔符默认值
3.3 C语言风格的for循环
#!/bin/bash
for ((i = 0;i<5 ;i++)) 此时变量与值间可以有空格,变量不用加$号
doecho "current number is $i"
done
3.4 while命令
#!/bin/bash
i=5
while [ $i -gt 0 ]
doecho $ii=$[ $i-1 ]
done
3.5 嵌套循环
#!/bin/bash
IFS.OLD=$IFS
IFS=$'\n'
for row in `cat /etc/passwd`
doecho "passwd row in $row ———"IFS=$':'for words in $rowdoecho " $words"done
done
IFS=$IFS.OLD
4 循环控制
4.1 break
#!/bin/bash
i=5
while [ $i -gt 0 ]
doecho $ii=$[ $i-1 ]if [ $i -eq 3 ]thenbreak 自动终止最里面的循环,break n,终止n层循环fi
done
4.2 continue
#!/bin/bash
i=10
while [ $i -gt 0 ]
do i=$[ $i-1 ]if [ $i -gt 3 ] && [ $i -lt 6 ]thencontinue continue n则指定继续执行哪级循环fiecho "$i"
done > output.txt 将循环过程中的结果重定向输出到文件
手把手教你写shell脚本——shell循环结构相关推荐
- windows脚本编制引擎_手把手教你写脚本引擎(一)
手把手教你写脚本引擎(一)--挑选语言的特性 陈梓瀚 华南理工大学软件本科05级 脚本引擎的作用在于增强程序的可配置性.从游戏到管理系统都需要脚本,甚至连工业级产品的Office.3DS Max以及A ...
- shell脚本编程-循环(for循环
shell脚本编程-循环(for循环) 知识回顾: 创建一个shell脚本规则 执行脚本 变量 测试 运算 if :单分支.双分支.多分支 实例: 剪刀石头布 检测主机是否存活 判断成绩 循环: fo ...
- linux 循环shell脚本,shell脚本的使用---for循环
shell脚本的循环:重复执行命令 1.for循环 语法 for 变量名称 in 变量值列表 do 命令 done for根据变量值列表中的内容,重复执行命令,直到变量值列中的所有内容都取值完后结束. ...
- Linux Shell脚本多循环语句练习题
Linux Shell脚本多循环语句练习题 99乘法表 矩形 正直角三角形 反直角三角形 倒直角三角形 等腰三角形 倒等腰三角形 菱形 平行四边形 直角梯形 等腰梯形 99乘法表 #!/bin/bas ...
- shell脚本for循环_了解Shell脚本中的for循环
shell脚本for循环 Continuing on from our previous tutorials, let's understand the for loop in shell scrip ...
- Shell脚本之循环
单重循环 for循环 需要指定一个变量及取值列表,针对每个不同的取值重复执行相同的命令序列,直到变量值用完退出循环.适用于对象列表无规律,且列表来源固定的场合. 格式1: for 变量名 in 取值列 ...
- Linux Shell 脚本之循环语句
目录 Shell 脚本之循环语句 一.echo 二.date 三.for 四.while 五.until 六.continue 和 break Shell 脚本之循环语句 一.echo echo - ...
- Shell脚本for循环语句应用
记录:430 场景:Shell脚本for循环语句应用. 版本:CentOS Linux release 7.9.2009. 1.for循环常用格式 1.1格式:for(;;) for((express ...
- Shell脚本,循环语句用于减少程序代码冗余和重复,for语句,while语句,使用let进行变量自增
Shell脚本,循环语句用于减少程序代码冗余和重复,for语句,while语句 一.for语法: 1. for 变量 in 值列表 do 命令序列 done 例子:输出循环中的所有值 for i in ...
- 手把手教你写批处理-批处理的介绍
标题:手把手教你写批处理-批处理的介绍 作者:佚名 编者:Climbing 出处:中国 DOS 联盟之联合 DOS 论坛 题注:willsort 日期:2004-09-21 ------------- ...
最新文章
- POJ 1611 -The Suspects (并查集)
- VC\JS Base64转码
- linux openfire mysql_Openfire 服务器在Linux上的安装
- Print out Android kernel log
- hibernate框架(二)核心配置API
- 无法安装 DotNetCore.1.0.0-VS2015Tools.Preview2解决方法
- 概括计算机程序启动过程,计算机启动过程详解
- 倒计时器c语言,在线倒计时器
- Retrofit完美封装
- JAVASCRIPT设计模式pdf
- 方框加对勾怎么输入_Word怎么输入对号和方框对勾
- ORA-12514 错误的处理
- api-ms-win-crt-stdio-l1-1-0.dll丢失问题解决
- selenium + 石墨文档 自动实现在固定位置写入文字
- 建网站论坛需要多大服务器,论坛搭建如何选择服务器
- vscode代码自动格式化快捷键
- Cadence PCB仿真使用Allegro PCB SI生成振铃ringing仿真报告及报告导读图文教程
- Selenium QQ自动化登录
- 大数据、快速数据和数据湖概念
- netty案例,netty4.1源码分析篇五《一行简单的writeAndFlush都做了哪些事》