Shell---判断(if)和分支(case)
if 判断
单分支if条件语句
语法:
if [ 条件判断式 ];then# do something
fi
或者
if [ 条件判断式 ]then# do something
fi
案例一,获取当前用户名,当然用 whoami
也可:
#!/bin/bash
test=`env | grep "USER" | cut -d "=" -f 2`
if [ test=="root" ];thenecho "This user is root"
fi
案例二,检查磁盘容量:
#!/bin/bash
rate=`df -h | grep "sda1" | awk '{ print $5 }' | cut -d "%" -f 1`if [ $rate -gt "10" ];thenecho "/ is Full"
fi
PS:
- 条件判断式就是test命令判断,可以用test命令替代,中括号中间必须有空格
- -gt 当变量值为空时会报错
双分支if
语法:
if [ 条件判断式 ]then ...else ...
fi
案例三,判断目录的属性:
#!/bin/bash
read -t 30 -p "input a dir:" dirif [ -d $dir ]thenecho "This is a directory"elseecho "no no no "
fi
案例四,判断tomcat服务是否开启:
#!/bin/bash
test=`ps aux | grep tomcat | grep -v grep`if [ -n "$test" ]thenecho "tomcat is running"elseecho "tomcat is not running"/etc/init.d/tomcat start
# service tomcat start 不建议使用echo "tomcat is started"
fi
PS:
- if判断中的变量最好加上引号,因为如果变量的值中包含空格,将出现too many arguments
错误,所以还是写成 if [ -n "$test" ]
较安全
多分支if
语法:
if [ 条件判断 ]then # do something
elif [ 条件判断 ]then # do something...else # do something
fi
案例五,简易计算器:
#!/bin/bash
read -p "please input num1:" num1
read -p "please input operator:" op
read -p "please input num2:" num2if [ -z "$num1" -o -z "$num2" -o -z "$op" ]thenecho "value shoud not be null"exit 2else# 把所有数字替换成空,如果替换后不为空,则表示变量中不符合规范 test1=`echo $num1 | sed 's/[0-9]//g'`test2=`echo $num2 | sed 's/[0-9]//g'`if [ -n "$test1" -o -n "$test2" ];thenecho "数值格式错误"exit 4fiif [ "$op" == "+" ]thenresult=$(($num1+$num2))elif [ "$op" == "-" ]thenresult=$(($num1-$num2))elif [ "$op" == "*" ]thenresult=$(($num1*$num2))elif [ "$op" == "/" ]thenlet "result=$num1/$num2"elseecho "operator is wrong"exit 3fifiecho ${num1}${op}${num2}=${result}
案例六,判断文件类型:
#!/bin/bash
read -p "请输入文件或目录名:" pathif [ -z "$path" ]thenecho "请输入内容!"exit 10
elif [ ! -e "$path" ]thenecho "文件或目录不存在"exit 11
elif [ -f "$path" ]thenecho "输入的是一个文件"
elif [ -d "$path" ]thenecho "输入的是一个目录"
elseecho "输入的式其他类型的文件"
fi
case 分支
语法:
case $变量名 in"值1")// do something,变量的值等于值1;;"值2")// do something,变量的值等于值2;;...*)// do something,变量的值与上面都不同;;
esca
案例
#!/bin/bash
read -p "请输入 yes/no" resultcase "$result" in"yes")echo "你输入的式yes";;"no")echo "你输入的是no";;*)echo "请输入正确的选择"
esac
Shell---判断(if)和分支(case)相关推荐
- shell编程之函数和case多条件分支语句
shell编程之函数和case多条件分支语句 函数介绍 函数语法定义 通过函数编写功能脚本 case语句 case条件分支语句介绍 函数介绍 函数介绍: shell 中允许将一组命令集合或语句形成一段 ...
- linux分支结构,实验四 Shell脚本中的分支结构
实验四 Shell脚本中的分支结构 #实验内容: ###一.if分支结构 •if结构是最基本的分支结构类型. •使用if结构时,只有满足某个条件才会执行某些语句,而在不满足此条件的情况下执行其他语句. ...
- Linux | Shell 学习笔记(二)Shell 流程控制 if、case、for、while| read读取输入 | 函数的使用 | cut、sed、awk、sort命令 +Demo
文章目录 参考资料 运行环境 一.流程控制 1.1 if 判断 1.2 case 语句 1.3 for 循环 1.4 while 循环 二. read 读取控制台输入 三.函数 3.1 系统函数 ba ...
- Linux Shell脚本入门教程系列之(九)Shell判断 if else 用法
本文是Linux Shell脚本系列教程的第(九)篇,更多shell教程请看:Linux Shell脚本系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.继上一篇之后,今天 ...
- linux shell嵌套if else,Linux Shell系列教程之(九)Shell判断 if else 用法
判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.今天就给大家介绍下Shell判断语句 if else 用法. if 语句通过关系运算符判断表达式的真假来决定执行哪个分支. Shel ...
- linux shell判断当前目录下的文件或目录
linux shell判断当前目录下的文件或目录 #!bin/sh for file in ./* doif test -f $filethenecho $file 是文件elseecho $file ...
- shell判断文件是否存在[转]
原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限#!/bin/shmyPath="/var/log/httpd ...
- case when then else多个条件_sqlserver条件分支case when使用教程
在sqlserver的条件分支case when有两种写法: 1)case 字段 when 值 then 返回值 when 值2 then 返回值2 end 2)case when 条件1 then ...
- shell如何检测linux发行版本,shell判断软件版本
shell脚本如何检查软件包是否安装成功 你的Linux是什么发行版的啊?最常用的发行版的软件包管理系统有Debian系(包括Debian,Ubuntu,Mint等)和Red Hat系(包括RHEL, ...
- Linux Shell 判断块设备节点是否存在
/************************************************************************** Linux Shell 判断块设备节点是否存在* ...
最新文章
- linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器
- 分享一套 python 试题
- C++对象确定性解析单例模式
- C++ 复制控制之复制构造函数
- Node中Exports与module.export的使用与区别
- 期权、RSU的区别与行权事宜
- JAVA的array中indexOf
- SpringCloud_Eureka集群配置
- 遍历enum(连续型)
- 【综述】Google团队发布,一文概览Transformer模型的17大高效变种
- wireshark的拆包与合并
- 是德科技成功开发广泛的测试与测量产品
- 微信公众号教程(13)公司通讯录开发 上
- springboot旅游景区景点购票系统毕业设计毕设作品开题报告开题答辩PPT
- Lumerical官方案例、FDTD时域有限差分法仿真学习(一)——闪耀光栅(Blazed grating)
- 2021年高压电工考试及高压电工考试资料
- linux下借助有道在线词典查词
- Period、Duration计算时间间隔
- 使用cmd指令生成Doc文档
- 你可能不知道的canvas烟花秀