shell脚本中的逻辑判断
shell脚本中也可以实现逻辑判断。
案例4:shell脚本中的逻辑判断
如果你学过C或者其他语言,相信你不会对if 陌生,在shell脚本中我们同样可以使用if逻辑判断。在shell中if判断的基本语法为:
1)不带else
if 判断语句; then
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
fi
在if1.sh中出现了 ((a<60))这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,请记住这个格式,即可。执行结果为:

2)带有else
if 判断语句 ; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
else
echo "you pass this test"
fi
执行结果为:

3)带有elif
if 判断语句一 ; then
command
elif 判断语句二; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
elif ((a>=60)) && ((a<85));then
echo "you pass this test"
else
echo "Verry Good!"
fi
这里的 && 表示“并且”的意思,当然你也可以使用 || 表示“或者”,执行结果:

以上只是简单的介绍了if语句的结构。在判断数值大小除了可以用”(( ))”的形式外,还可以使用”[ ]” 注意方括号里面需要有空格。但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if [ $a -lt 60 ];then
echo "$a lt than 60";
elif [ $a -ge 60 ] && [ $a -lt 85 ];then
echo "you pass this test"
elif [ $a -eq 100 ];then
echo "$a eq than 100. Very Good!"
elif [ $a -ne 0 ];then
echo "$a ne than 100. Your Input error!"
elif [ $a -gt 100 ];then
echo "$a gt than 100. Your Input error!"
else
echo "Your Input error!"
fi
运行结果如图:

案例5:shell脚本中判断档案属性
shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:
-e :判断文件或目录是否存在
-d :判断是不是目录,并是否存在
-f :判断是否是普通文件,并存在
-r :判断文档是否有读权限
-w :判断是否有写权限
-x :判断是否可执行
使用if判断时,具体格式为: if [ -e filename ] ; then
#! /bin/bash
## author:Xiong Xuehao
## File properties. read -p "Please input a file or directory: " a
###判断文件或目录是否存在
if [ -e $a ];then
echo "$a The file or directory already exists."
else
echo "$a The file or directory does not exist!"
fi ###判断文件或目录
if [ -d $a ];then
echo "$a The path is a directory and already exists."
elif [ -f $a ];then
echo "$a The path is a file and already exists."
else
echo "$a I don't know!"
fi ###判断权限
if [ -r $a ];then
echo "$a The document has read permission."
else
echo "$a The document does not have read permission!"
fi if [ -w $a ];then
echo "$a The document has write permission."
else
echo "$a The document does not have write permission!"
fi if [ -x $a ];then
echo "$a The document has execution permission."
else
echo "$a The document does not have execution permission!"
fi
执行如图:

案例6:shell脚本中用case逻辑判断
在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:
case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
上面的结构中,不限制value的个数,*则代表除了上面的value外的其他值。下面笔者写一个判断输入数值是奇数或者偶数的脚本。
#! /bin/bash
## author:Xiong Xuehao
## Use case in this script. read -p "Please input a number: " n
###判断奇数或者偶数
a=$[$n % 2] case $a in
1)
echo "The remainder is $a. $n The num is odd."
;;
0)
echo "The remainder is $a. $n The num is even."
;;
esac
输入任意一个自然数,除以2得余数 $a 的值或为1或为0,执行结果为:

case脚本常用于编写系统服务的启动脚本,例如/etc/init.d/iptables中就用到了,不妨去查看一下。
shell脚本中的逻辑判断的更多相关文章
- Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断
1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if …; then … ;elif …; then ...
- shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
7月12日任务 20.5 shell脚本中的逻辑判断20.6 文件目录属性判断20.7 if特殊用法20.8/20.9 case判断 20.5 shell脚本中的逻辑判断 逻辑判断在shell中随处可 ...
- Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...
- shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
case判断 • 格式 case 变量名 in value1) command ...
- shell脚本中常见的一些特殊符号和作用详解
这篇文章主要介绍了shell脚本中常见的一些特殊符号和它的作用详解,总结的很简洁,容易看懂,需要的朋友可以参考下 在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我 ...
- shell脚本中的整数测试
shell脚本中的整数测试 author:headsen chen 2017-10-17 13:58:12 个人原创,转载请注明作者,出处,否则依法追究法律责任 1,test用法:tes ...
- shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...
- Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- shell脚本中的反引号使用 `
反引号是~的英文切换 在shell脚本中,反引号允许将shell命令的输出赋值给变量. test=`date` 这样shell会执行反引号中的命令.并将执行的结果赋值给变量tests.
随机推荐
- Linux再学!
第三篇Linux入门 一.linux基本指令 1.Linux根目录为/,后续路径用/分隔,如/home/admin 2.Linux命令 基础格式: command: 命令本身 -options:[可选 ...
- 一款.NET开源的屏幕实时翻译工具
前言 今天大姚给大家推荐一款.NET开源的屏幕实时翻译工具:Translumo. 工具介绍 Translumo是一个.NET开源的高级屏幕翻译工具,能够实时检测和翻译选定区域内的文本(如字幕).Tra ...
- PostgreSQL中将对象oid和对象名相互转换
PostgreSQL中将对象oid转为对象名 使用pg的内部数据类型将对象oid转为对象名,可以简化一些系统视图的关联查询. 数据库类型转换对应类型的oid 可以用以下数据库类型转换对应类型的oid( ...
- delphi Image32 图像采样
图像数据采样 代码: 1 unit uFrmImageResampling; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, Win ...
- 2025年前端面试准备js篇
1.js的基本数据类型有哪些 undefined,null,bo0lean,number,string,object,Symbol,bigInt 分为原始类型和引用类型 原始类型:undefined, ...
- dotnet学习笔记-专题06-过滤器和中间件-01
1. 基本概念 在ASP.NET Core中,中间件和过滤器都是处理HTTP请求的重要组件,但它们在应用中的位置.作用范围以及使用方式有所不同. 1.1 中间件和过滤器的区别 1.1.1 中间件 位置 ...
- 优秀的 Java 程序员所应该知道的 Java 知识
JDK 相关知识 JDK 的使用 JDK 源代码 JDK 相应技术背后的原理 JVM 相关知识 服务器端开发需要重点熟悉的 Java 技术 Java 并发 Java IO 开源框架 Java 之外的知 ...
- Mysql的整体架构设计
整体分层 连接层 服务层 存储引擎层 连接层 客户端要连接到服务器 3306 端口,必须要跟服务端建立连接,那么 管理所有的连接,验证客户端的身份和权限,这些功能就在连接层完成. 服务层 连接层会把 ...
- flutter安装过程中 flutter doctor 出现错误 cmdline-tools component is missing
进入Android studio的settings添加tool工具
- PM-软件最难的不是开发程序,而是需求
最近几个月,关于人工智能的惊人文章在互联网泛滥.这也引发了很多人的担心--软件开发人员可能很快就会失业,被人工智能取代.他们想象所有的企业高管和产品研究人员将绕过大多数或所有的软件开发人员,直接要求人 ...