Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
if-then语句
if-then语句格式如下
if comman
then
command
fi
bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的if语句会运行if语句后面的那个命令,如果该命令的退出状态码是0 那么执行位于then部分的的命令。
代码实例
#!/bin/bash #if-then test if date
then
echo "this is the if-then test"
fi
这个脚本的功能就是,执行date命令,如果date命令执行成功,那么他的退出状态码是0就执行then后面的代码,在屏幕上面输一行文字,this is the if-then test
then后面的语句可以是一条或多条,和我们写简单的shell脚本没有区别,这里会一条一条的执行下去
测试实例代码
#if-then test if date
then
echo "this is the if-then test"
testusername=dreamlife
if grep $testusername /etc/passwd
then
echo "the bash file for user $testusername art:"
ls -a /home/$testusername/.b*
fi
fi
如果我们想要在shell和我们平常一样使用if else的功能,我们需要使用一下命令格式
if command
then
command
else
command
这里,如果 执行if后面的命令的退出状态码是0 就执行then后面的代码块,否则就执行else后面的代码块
测试代码
#!/bin/bash #if-then-else testuser=dreamlife
if grep $testuser /etc/passwd
then
echo 'the files for user $testuser are:'
ls -a /home/$testuser/ *
else
echo "the user name $testuser dose not exist on this system"
fi
在shell编程中,也是有if嵌套的,使用格式如下
if command1
then
command1-set
elif command2
then
command-set2
elif command3
then
command3-set ... fi
这个就没有实例代码了,如果有兴趣的,可以吧上面的代码改吧改吧试试看,每次只能测试一种。
好了,学习下一个命令,test
test是个好东西,他的功能之一就是可以是我们shell的if可以比较真假的,test的基本命令格式很简单
test condition
condition是test命令要测试的一系列参数和值,当用在if-then语句的时候,test命令执行,如果tets命令中列出的条件为真的时候,退出状态码为0 否则为1,这样就可以在if-then中使用了
就是下面的格式了
if test condition
then
commands
fi
不过每次这么写也挺别扭的。所以,在bash中,提供了另外一种tets的写法,那就是方括号[]
不过必须要在左方括号右面,右方括号左面各加一个空格才可以,不然报错
if [ condition ]
then
commands
fi
test命令可以判断三种类型条件
1. 数值比较
2. 字符串比较
3. 文件比较
第一类,test数值比较的基本功能
1. 检查n1是否与n2相等:n1 -eq n2
2. 检查n1是否大于或等于n2:n1 -ge n2
3. 检查n1是否大于n2:n1 -gt n2
4. 检查n1是否小于或等于n2:n1 -le n2
5. 检查n1是否小于n2:n1 -lt n2
6.检查n1是否不等于n2:n1 -ne n2
测试用例
#!/bin/bash #using numeric test comparisons var1=10
var2=11 if [ $var1 -gt 5 ]
then
echo "the test value $var1 ia greater than 5"
fi if [ $var1 -eq $var2 ]
then
echo "the values are equal"
else
echo "the calues are different"
fi这里注意的是,浮点数不能比较。下面是错误例子
#!/bin/bash #testing floating point numbers val1=`echo "scale=4;10/3" | bc`
echo "the test values is $val1"
if [ $val1 -gt 3 ]
then
echo "the result if larger than 3"
fi
这里bash提示需要整数表达式
第二类 字符串的比较
1. 检查str1是否和str2相同:str1 = str2
2. 检查str1是否和str2不同:str1 != str2
3. 检查str1是否比str2小:str1 <str2
4. 检查str1是否比str2大:str1 >str2
5. 检查str1的长度是否为非零:-n str1
6. 检查str1的长度是否为0:-z str1
下面是实例代码
#!/bin/bash #testing string equality testuser=dreamlife if [ $USER = $testuser ]
then
echo "Welcom $testuser"
fi#!/bin/bash #testing string equality testuser=dreamlife if [ $USER != $testuser ]
then
echo "this user is not $testuser"
else
echo "Welcom $testuser"
fi在字符串比较的时候,需要注意两个问题,
1. 大于小于符号必须使用转义,否则shell会把他们当做重定向符号而把字符串当做文件名
2. 大于小于顺序和sort命令所采用不同
第一个问题
#!/bin/bash #mis-using string comparisons val1=baseball
val2=hockey if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi第二个儿问题
#!/bin/bash #testing string sort order val1=testing
val2=Testing if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi字符串大小
#!/bin/bash #testing string length val1=testing
val2='' if [ -n "$val1" ]
then
echo "the string '$val1' is not empty"
else
echo "the string '$val1' is empty"
fi if [ -z "$val2" ]
then
echo "the string '$val2' is empty"
else
echo "the string '$val2' is not empty"
fi后面还有文件的比较,由于文件比较内容比较多,我会在写一个。
Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif的更多相关文章
- 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令
许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then ...
- 详细介绍Linux shell脚本基础学习
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- Linux shell脚本全面学习
Linux shell脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它 ...
- Linux shell脚本基础学习详细介绍(完整版)二
详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...
- Linux shell脚本基础学习详细介绍(完整版)一
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- Shell脚本基础学习
Shell脚本基础学习 当你在类Unix机器上编程时, 或者参与大型项目如k8s等, 某些框架和软件的安装都是使用shell脚本写的. 学会基本的shell脚本使用, 让你走上人生巅峰, 才怪. 学会 ...
- Shell脚本的学习(二)
Shell脚本的学习(二) 方法: 1) 一个计算器: 2)递归实现打印目录 3)方法调用
- Shell脚本的学习(一)
Shell脚本的学习(一) 一)代码式shell脚本简介 1.下载 Xshell 5 建一个文件夹 mkdri home/data ; 1)查看一个在data里建一个1.sh 查看是否建立成功. 2) ...
- linux shell脚本使用结构化命令
内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...
- [转]Windows网络编程学习-面向连接的编程方式
直接附上原文链接:windows 网络编程学习-面向连接的编程方式
随机推荐
- public /protected/private的作用域
作用域 当前类 同一package 子孙类 其他package public √ √ √ √ protected √ √ √ × friendly √ √ × × private √ × × ×
- Android学习笔记_73_授权过程
.需要申请App Key和App Secret.不同的开发平台有不同的接入方式,可以参考文档,然后将这两个值放进去. .通过OAuth类实现认证,它会自动跳转到认证界面,进行授权,成功之后需要处理回调 ...
- windows 安装pear & PHP_CodeSniffer
1. download https://pear.php.net/go-pear.phar 2. install pear(http://pear.php.net/manual/en/installa ...
- 数据库——MySQL——完整性约束
约束,就是用来保证数据完整性和一致性的. 常见的约束分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY (FK) 标识该字段为该表的外键 NO ...
- BFC、IFC、GFC、FFC
原文地址:https://www.xingkongbj.com/blog/css/fc.html FC FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它 ...
- C++ primer 练习9.52 适配器stack 中缀表达式
//调试环境 VS2015//本人菜鸟一枚,不喜勿喷! 谢谢!!!//主要思想引自 http://www.cnblogs.com/dolphin0520/p/3708602.html//主要代码引自 ...
- ABAP术语-EDI (Electronic Data Interchange)
EDI (Electronic Data Interchange) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/29/1057386.ht ...
- Mysql存储过程查询结果赋值到变量
# 使用的navicat 编辑的存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_student_back`()BEGIN#定义max ...
- 今天看到的一篇文章:一位资深程序员大牛给予Java初学者的学习路线建议
一位资深程序员大牛给予Java初学者的学习路线建议 持续学习!
- Python——判断
if语句简介 说明 使用if语句判断的条件表达式的结果只有两种:Ture和False,结果为True则执行if语句中的代码,否则不执行,例: name = "smith" if n ...