当前位置:网站首页>Shell基本语法--流程控制
Shell基本语法--流程控制
2022-06-29 17:59:00 【__万波__】
if else 语句
基本结构
注意:
if和elif语句后面要跟then才能开始执行语句块then可以换行写, 写在同一行要加分号隔开, 建议写在同一行, 结构会更清晰- 没有 else if 这种语句, 只能写
elif
if [ ]; then
# if 语句块
elif [ ]; then
# 语句块
else
# 语句块
fi
条件运算符
[]与[[]]的不同之处
[[]]运算符只有bash中才有, 其他shell如ash, dash中是没有这个语法的[[]]有[]中没有的功能, 比如比较两个字符串是否相等, 且字符串中有空格的时候, 使用[]时必须加双引号而使用if [ "$str" = "hello man" ]; then[[]]时就不必担心这个问题if [[ $str = "hello man" ]]; then- 逻辑运算. 使用
[]条件运算符时,&&,||,<,>这几个符号都会被当作命令的一部分, 而不是当作逻辑运算符, 使用[[]]条件运算符时就不会有这个问题, 如:# 下面的 > 符号会被当作重定向命令, && 也作为命令的一部分, 会创建200 和 100两个文件, 且语句会报错: [: missing `]' if [ 300 > 200 && 200 > 100 ]; then # 下面的语句就可以正常的执行逻辑运算 if [[ 300 > 200 && 200 > 100 ]]; then- 模糊匹配. 使用
[[]]时可以使用=~运算符及正则语法实现模糊匹配如匹配yes:# 使用 [] 时只能使用如下写法 if [ $str = "y" -o $str = "yes" ];then # 使用 [[]] 时就可以有如下写法 if [[ $str =~ ^y(es)$ ]];then echo ${ BASH_REMATCH[1]} # 匹配到的值存在这个系统变量中 ... # 还可以使用下面的通配符写法 if [[ $str = y* ]];then- 经验, 一般做判断时会使用
[]条件运算符, 但在做字符串比较时, 最好用[[]]会降低出错概率
条件标识符
判断数字
if [ $a -eq $b ]; then # 等于 equal
if [ $a -ne $b ]; then # 不等于 not equal
if [ $a -gt $b ]; then # 大于 greater than
if [ $a -ge $b ]; then # 大于等于 greater equal
if [ $a -lt $b ]; then # 小于 little than
if [ $a -le $b ]; then # 小于等于 little equal
判断字符串
# 使用 [] 运算符
if [ -z "$str" ]; then # 判断为空(字符串变量加引号)
if [ -n "$str" ]; then # 判断不为空(字符串变量加引号)
if [ "$str" = "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" == "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" != "abc" ]; then # 判断不相等(字符串变量加引号)
if [ "$str1" \> "$str2" ]; then # 判断大于(字符串变量加引号)
if [ "$str1" \< "$str2" ]; then # 判断小于
# 使用 [[]] 运算符
if [[ -z $str ]]; then # 判断为空
if [[ -n $str ]]; then # 判断不为空
if [[ $str = "ab c" ]]; then # 判断相等
if [[ $str == "ab c" ]]; then # 判断相等
if [[ $str != "ab c" ]]; then # 判断不相等
if [[ $str1 > $str2 ]]; then # 判断大于
if [[ $str1 < $str2 ]]; then # 判断小于
文件判断
if [ -e $file ]; then # 判断文件存在
if [ -L $file ]; then # 判断文件存在且是链接文件(要最先判断, 否则会先去判断软链接指向的文件的其他属性)
if [ -s $file ]; then # 判断文件存在且是字符设备
if [ -b $file ]; then # 判断文件存在且是块设备文件
if [ -d $file ]; then # 判断文件存在且是文件夹/目录
if [ -f $file ]; then # 判断文件存在且是普通文件
if [ -S $file ]; then # 判断文件存在且是套接字文件
if [ -p $file ]; then # 判断文件存在且是管道文件
# 对文件权限的判断
if [ -r $file ]; then # 判断文件具有可读权限
if [ -w $file ]; then # 判断文件具有可写权限
if [ -x $file ]; then # 判断文件具有可执行权限
逻辑运算
# 使用 [] 时
if [ $a -o $b ];then # 或 or
if [ $a -a $b ];then # 且 and
if [ ! $a ];then # 非
if [ expression1 ] || [ expression2 ];then # 或
if [ expression1 ] && [ expression2 ];then # 且
if ! [ expression ] # 非
# 使用 [[]]时
if [[ $a && $b ]];then # 或
if [[ $a && $b ]];then # 且
if [[ ! $a ]];then # 非
if [[ expression1 ]] || [[ expression2 ]];then # 或
if [[ expression1 ]] && [[ expression2 ]];then # 且
if ! [[ expression ]] # 非
switch case 选择
case ${var} in
[1-9]) # 范围
# do something
;;
[^a-zA-Z]) # 非此范围
# do something
;;
*)
#do something
;;
esac
select in 选择
select var in ch_a ch_b ch_c ch_d
do
#经常结合case in 语句使用
done
while 循环
while test
do
# 循环体
done
while true # 死循环的写法
do
done
for 循环
for ((i=0; i<=100; i++))
do
# 循环体
done
for var # 不加任何参数会循环所有的位置参数
do
done
for in 循环
for var in a b c d e
do
#
done
for var in {
1..100}
do
#
done
break 与 continue
shell 中的break和continue与普通C系列程序中的同名关键词有着不同的行为
break n: 退出n层循环continue n: 跳过n层循环, 执行
边栏推荐
- On adding and subtracting dates
- Distributed | several steps of rapid read / write separation
- Dictionary tree (optional)
- You can do sideline work
- Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)
- Mongotemplate - distinct use
- It's really easy to make money in foreign lead and build a website
- Function independent watchdog (iwdg) experiment based on stm32f103zet6 Library
- 金鱼哥RHCA回忆录:DO447构建高级作业工作流--创建作业模板调查以设置工作的变量
- 最长异或路径(dfs+01trie)
猜你喜欢

Proxmox VE Install 7.2

C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad

ISO 32000-2 international standard 7.7

Opencv+yolo-v3 for target tracking

QQ如何开通在线客服

Configure the local domain name through the hosts file

Serial port experiment based on stm32f103zet6 library function

Wechat applet development reserve knowledge

Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题

idea怎么使用?
随机推荐
jdbc_相关代码
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
牛客小白月赛52 E 分组求对数和(容斥定理+二分)
js两个二维数组合并并去除相同项(整理)
Automatic software test - read SMS verification code using SMS transponder and selenium
Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题
Image migration and data migration synchronization of old and new servers with different Alibaba cloud accounts
You can do sideline work
Relationship among controller, service and Dao
记录服务器被入侵病毒:ssh密码被更改登录失败、恶意程序跑满了cpu、jar包启动失败自动kill、一直弹出You have new mail in /var/spool/mail/root
最长异或路径(dfs+01trie)
Servlet student management system (Mengxin hands-on version)
Lodash deep copy usage
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
Adobe Premiere基础-不透明度(混合模式)(十二)
Let Google search your blog
Top 30 open source software
3h精通OpenCV(七)-颜色检测
3H proficient in opencv (IX) - the simplest face detection
VMware installation esxi