注意:shell中对比字符串只能使用==、<、>、!=、-z、-n。对比字符串时,末尾一定要加上x(或者a、b等)一个字符,因为if [ $1x == "ab"x ]时如果没有了x ,并且$1是"",这个语句会翻译成if [  == "ab" ],左边相当于没有东西了,会报语法错误。或者使用[[  ]],就不需要x了。使用<或者>时,如果是用[  ],需要用转义符"\",如\>。

对比数字使用既能使用-eq、-ne、-gt、-ge、-lt、-le,也能使用==、<、>、!=。其中-eq的意思是equal,-ne是unequal,-gt是greater than,-ge是greater than or equal to,-lt是less than,-le是less than or equal to。

[zhi@centos7 sh]$ cat number_compare.sh
#!/bin/bash
#脚本名称:number_compare.sh
#用途:比较二个数字大小
echo " Please input first number:"
read x
echo "you first number x=$x"
read -p " Please input second number:" y
echo "you second number y=$y" if [ $x -eq $y ];then
echo "equal"
elif [ $x -gt $y ];then
echo "x greater than y"
else
echo "x less than y"
fi

运行测试:

[zhi@centos7 sh]$ ./number_compare.sh
Please input first number: you first number x=
Please input second number:
you second number y=
x greater than y
[zhi@centos7 sh]$ ./number_compare.sh
Please input first number: you first number x=
Please input second number:
you second number y=
equal
[zhi@centos7 sh]$ ./number_compare.sh
Please input first number: you first number x=
Please input second number:
you second number y=
x less than y

shell脚本学习(2)比较两个数字大小的更多相关文章

  1. 我的c++学习(2)比较两个数字大小

    #include "stdafx.h" #include<iostream> using namespace std; int max(int i, int j){ / ...

  2. Shell脚本学习 - 运算符

    继续shell脚本学习.上一篇是基本数据类型和语法的总结,这一篇是运算相关的操作. 运算符 bash不支持简单的数学计算,需要依赖其他命令实现. expr可以代为实现. # 表达式一般这么写 ` + ...

  3. Shell脚本学习指南笔记

    Shell脚本学习指南 作者:Danbo 2015-8-3 脚本编程语言与编译型语言的差异 许多中型.大型的程序都是用编译型语言写的,例如:C.C+.Java等.这类程序只要从源代码(Source C ...

  4. Shell 脚本学习资料搜集

    Shell文档 ChinaUnix上大神“網中人”总结的Shell十三问,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点.每一问都写得非常精彩.ChinaUnix ...

  5. 学习笔记之Shell脚本学习指南 & sed与awk & 正则表达式

    正则表达式_百度百科 http://baike.baidu.com/link?url=ybgDrN2WQQKN64_gu-diCqdeDqL8LQ-jiQ-ftzzPaNUa9CmgBRDNnyx50 ...

  6. 转 shell脚本学习指南

    shell脚本学习指南 以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bashshell 脚本的第一行,#!之后应该是什么?如果拿这个问题 ...

  7. Shell脚本学习 - 流程控制和函数

    继续Shell的学习.上两篇是关于基本数据类型,基本语法以及运算符相关,这一篇是流程控制相关(if, for, while) 流程控制 if else 流程控制不可为空,如果else没有语句执行,就不 ...

  8. shell脚本学习之6小时搞定(1)

    shell脚本学习之6小时搞定(1) 简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把 ...

  9. 笔记——shell脚本学习指南

    <shell脚本学习指南>机械工业出版 ISBN 987-7-111-25504-8 第2章 2.4 初级陷阱 1.当今的系统,对#!这一行的长度限制从63到1024个字符都有,尽量不要超 ...

  10. shell脚本学习总结02--数组

    bash同时支持普通数组个关联数组,普通数组只能使用整数作为数组的索引,关联数组可以使用字符串作为数组的索引. 数组的定义方法: 在单行中使用一列值定义一个数组 [root@new ~]# array ...

随机推荐

  1. mysql 字段类型VARCHAR转换成DECIMAL

    在我们写代码的实际业务中,有时候实体类用的是String,数据库中自然是VARCHAR类型,但是如果这个实体的属性值放的是数字类型,你查询的时候又需要对它进行排序.sql怎么写呢. 别担心mysql提 ...

  2. [转]cron语法

    最近在搞whenever时看到可以用cron语法设置定时任务.所以研究了下cron 语法. every '0 0 27-31 * *'do command "echo 'you can us ...

  3. Codeforces Round #532 (Div. 2)- B(思维)

    Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...

  4. case when null then 'xx' else 'yy' end 无效

    Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,而且语法能正常执行, 如果不注意数据内容,很容易搞错. 错误方法: CASE ...

  5. python模块之openpyxl扩展

    主要是对openpyxl扩展进行扩展,使用归类等 1. 安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow,安装文件:PIL-fork-1.1.7.win- ...

  6. 非局部均值(Nonlocal-Mean)

    转载自网站:http://www.cnblogs.com/luo-peng/p/4785922.html 非局部均值去噪(NL-means)   非局部均值(NL-means)是近年来提出的一项新型的 ...

  7. 创建第一个vue工程

    vue创建项目(npm安装→初始化项目) 第一步npm安装 首先:先从nodejs.org中下载nodejs   图1 双击安装,在安装界面一直Next   图2   图3   图4 直到Finish ...

  8. tomcat异常[0]--java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV

    自己建了一个项目,启动项目的时候,发生了java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV异常. ...

  9. Android 5.0以上heads up通知

    适用Android系统: 1) Android版本>= 5.0 2) 部分ROM是不支持 RemoteViews view=getRemoteViews(body,title, R.mipmap ...

  10. PlayMaker Int的两个数进行比较 Int Compare

    Integer 1 和 Integer 2 进行比较,Integer 1 和  Integer 2 相等的时候 执行 ChangeToGreen; Integer 1 比  Integer 2 大的时 ...