bash条件测试及变量比较
2011-04-01 14:32
477 查看
![endif]-->!-->![endif]-->!-->![endif]-->!-->
bash条件测试及变量比较
一、 条件表达式
1. 基本的if语句if [ … ]then….Fi 2. 多个if语句if […]then…Elif […]Then…Fi 3. 嵌套if语句If […]Then… If[…] Then … FiElse…fi |
二、 字符串和数字比较
Number | String | |
大于 | -gt | /> |
小于 | -lt | /< |
等于 | -eq | == |
大于等于 | -ge | Single[] doesnot support |
小于等于 | -le | Single[] doesnot support |
不等于 | -ne | != |
三、 关键点
1. if和elif后面有then2. else if是非法的3. 单个中括号[]里面的”<”和“ >”需要转义,且不支持”>=” 和“<=”4. “z” 测试字符串是否为NULL5. “n” 测试字符串是否不为NULL四、 代码
#! /bin/bashecho "test you"a=12b=13 # number compare# -eq: equal# -ne: not equal# -gt: great# -ge: great and equal# -lt: little# -le: little and equalif [ "$a" -ne "$b" ]thenecho "$a is not equel to $b"fi if [ "$a" -eq "$b" ]then echo "$a is equal to $b"elseecho "$a is not equal to $b"fi if [ "$a" -eq "$b" ]thenecho "$a is equal to $b"elif [ "$a" -lt "$b" ]thenecho "$a is little to $b"elif [ "$a" -gt "$b" ]thenecho "$a is great to $b"elseecho "Default"fi#########Key Point#######################1."then" after "if" or "elif"#####################2."elif" is ok, not "else if"############ c=abcd=abde=#string compare if [ "$c" == "$d" ]thenecho "string $c is equal to $d"elif [ "$c" /> "$d" ]thenecho "string $c is great to $d"elif [ "$c" /< "$d" ]thenecho "string $c is little to $d"elif [ "$c" != "$d" ]thenecho "string $c is not equal to $d"fi if [ -z "$e" ]thenecho " $e is NULL"elseecho " $e is not NULL"fi if [ -n "$e" ]then echo "$e is not NULL"elseecho "$e is NULL"fi###########KEy point############################1.For string comparing, ">" "<"need escape symbol###########2."z": zero is used to test whether the string is NULL###########3."n": not zero is used to test whether the string is not NULL f=8if [ "$f" -gt 0 ]then echo "f is positive" if [ "$f" -gt 5 ] then echo "f is a big positive" else echo "f is a small positive" fielseecho "f is negative" if [ "$f" -gt -5 ] then echo "f is a big negative" else echo "f is a small negative" fifi |
相关文章推荐
- 【转】shell学习笔记(三)——引用变量、内部变量、条件测试、字符串比较、整数比较等
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- 2016-3-6 linux基础学习13——条件判断、文件测试、bash变量类型
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- linux笔记 第六天 bash编程入门之变量、条件判断、条件测试和for循环等
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- bash脚本编程之四——整数测试及特殊变量
- Linux 技巧: Bash 测试和比较函数
- bash的算术运算和条件测试语句
- Linux 技巧: Bash 测试和比较函数
- Linux exit 文件测试 测试条件、特殊变量 (笔记)
- Shell脚本基础 、 使用变量 、 条件测试及选择 、 列表式循环
- bash条件测试之空字符串的困惑
- Shell基础-Bash变量-变量测试与内容替换
- shell脚本编程之条件判断,算术运算,整数测试及特殊变量
- bash之条件判断与字符测试_学习笔记
- 谢烟客---------Linux之Bash基础特性条件测试&&自定义退出状态码(6)
- Bash 测试和比较函数
- bash shell test条件测试[[ ]]和[ ]异同小结