shell脚本学习(2)比较两个数字大小

注意: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。

[[email protected] 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

运行测试:

[[email protected] sh]$ ./number_compare.sh
 Please input first number:
34
you first number x=34
 Please input second number:23
you second number y=23
x greater than y
[[email protected] sh]$ ./number_compare.sh
 Please input first number:
22
you first number x=22
 Please input second number:22
you second number y=22
equal
[[email protected] sh]$ ./number_compare.sh
 Please input first number:
23
you first number x=23
 Please input second number:56
you second number y=56
x less than y
时间: 2024-08-26 10:04:21

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

shell脚本学习与总结

shell脚本学习总结,东西很多,供初学者参考. shell脚本是区分大小写的. 2.Unix特殊字符有: ( ; $ ? & * () [] ` ' " + 使用其时要进行转义() 3.Shell的注释以#开头 4.函数的定义Function fuction_name(){ Command to execute} 调用时直接用function_name. 5.控制结构 1)If...then语句 If [ test_command ]    Then    Commandsfi2)If

shell脚本学习指南

以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bash shell 脚本的第一行,#!之后应该是什么? 如果拿这个问题去问别人,不同的人的回答可能各不相同.我见过/usr/bin/env bash,也见过/bin/bash,还有/usr/bin/bash,还有/bin/sh,还有/usr/bin/env sh.这算是编程界的“’茴’字四种写法”了. 在多数情况下,以上五种写法都是等价的.但是,写过程序的人都知道:“少数情况”里往往隐藏

笔记——shell脚本学习指南

<shell脚本学习指南>机械工业出版 ISBN 987-7-111-25504-8 第2章 2.4 初级陷阱 1.当今的系统,对#!这一行的长度限制从63到1024个字符都有,尽量不要超过64个字符. 2.在某些系统上,命令行部分包含了命令的完整路径名称.不过有些系统却不是这样:命令行的部分会原封不动地传递给被引用的程序. 3.别在选项之后放置任何空白,因为空白也会跟着选项一起传递给被引用的程序. 4.你需要知道解释其的完整路径名称.这可以用来规避可移植问题,因为不同的厂商可能将同样的东西放

shell脚本学习笔记系列--1

一.学好shell编程的知识储备 1.相关Linux系统命令应用: 2.Vi/vim 编辑器的熟练使用,相关客户端软件的设置: 3.基础的服务,系统服务ntp,crond,网络服务:nfs,rsync,inotify,sersync,ssh,lanmp等. 补充:清空日志的三种方法: 1)echo  " " > filename.log 2)>filename.log 3)cat  /dev/null > filename.log 注:工作中有的时候不能删除(日志)文

linux下shell脚本学习

在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shell特别擅长系统管理任务,尤其适合那些易用性.可维护性和便携性比效率更重要的任务. 下面,让我们一起来看看shell是如何工作的: 1. 建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行shell编程,因为bash是免费的并且

shell脚本学习-语法篇

一. 条件测试:test [命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测 试结果为假,则命令的Exit Status为1(注意与C语言的逻辑表示正好相反). 二.if/then/elif/else/fi和C语言类似,在Shell中用if.then.elif.else.fi这几条命令实现分支控制.这种流程控制语句本质上也是由若干条Shell命令组成的,其实是三条命令,if [ -f ~/.bashrc ]是第一条,then . ~/.ba

shell脚本学习笔记 (sed的高级用法----模式空间和保持空间)

前段时间在学习shell脚本,上次有提到sed的模式空间和保持空间概念,但是一直没有研究好,这两天研究了一下,所以将它发出来,不是很全面,仅仅供大家参考一下. 保持空间sed在正常情况下,将处理的行读入模式空间,脚本中的"sed command(sed命令)"就一条接着一条进行处理,直到脚本执行完毕.然后该行被输出,模式被清空:接着,在重复执行刚才的动作,文件中的新的一行被读入,直到文件处理完毕. 模式空间可以比喻为一个生产线,而保持空间则可以被比喻为仓库,这个比喻希望可以帮助大家理解

Shell脚本学习第二课&#183;

Shell文件包含 shell也可以包含外部脚本,语法格式如下: . filename 或 source filename 例如创建两个shell脚本. 脚本1:test1.sh url = "www.baidu.com" 脚本2:test2.sh . ./test1.sh echo "$url" 执行test2.sh,即可看到结果. Shell输入输出重定向 命令 说明 command>file 将输出重定向到file command<file 将输入

shell脚本学习第一课

shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell(/bin/bash) C Shell(/usr/bin/csh) K Shell(/usr/bin/ksh) Shell for Root(/sbin/sh) Shell脚本执行的两种方法 ./shell.sh 根据shell脚本第一行指定的shell执行 /bin/sh test.sh 根据命令