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 网络编程学习-面向连接的编程方式
随机推荐
- jmeter自动生成测试报告
环境要求1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚本文件 基本操作 1:在你的脚本文件路径下,执行cmd命令:jmeter -n -t test ...
- mybais学习记录一——入门程序
一.传统连接数据库和执行sql的不足 1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响 数据库性能. 设想:使用数据库连接池管理数据库连接. 2. ...
- spring异常+自定义以及使用
1.首先自定义异常 DataException: package com.wbg.maven1128.exception; public class DataException extends Exc ...
- CSS实战2
1. 鼠标样式 Cursor: pointer 鼠标变成小手 Cursor: default; 小白 Cursor : move; 移动 Cursor : text ; 文本输入 网 ...
- Python基础—06-函数基础
函数基础 函数简介 定义:就是具有特定功能的一段代码 优点: 解决代码的重复书写 可以将功能的实现着和使用者分开,提高开发效率 分类: 库函数:print.input.abs等 自定义:用户自己封装的 ...
- C++创建学生类练习
/*作业,定义一个学生类*/ /*数据成员:学号.姓名.数学.英语.计算机三科成绩 *成员函数:求总成绩.求三科平均成绩.输出学生信息 *新增一个生日类 2018.4.2 */ #include &l ...
- alias,unalias命令
alias unalias 命令 =================================================[root@sambo ~]# aliasalias cp='cp ...
- vue兄弟组件传值$on多次执行的问题
首先附上如何进行兄弟组件通信的方法链接 https://segmentfault.com/a/1190000011882494 下面是$on多次执行的解决办法 https://blog.csdn.ne ...
- php数组常用函数总结
数组的创建 $arr1 = [ "姓名" => "张三", "籍贯" => "上海", "年龄&q ...
- PHP入门笔记--基础语法二
一.函数 自定义函数 任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和类定义. <?php function foo() { function bar() { echo &qu ...