shell 比较判断
引用自:http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/abs-3.9.1_cn/html/comparison-ops.html
#! /bin/shvar1=20var2=21if [ $var1 -ne $var2 ];thenecho "1. -ne means NOT equal true"fixyz="11"if [ -n "$xyz" ];thenecho "2. -n means init true(MUST to use \"\")"fiif [ -z "$1" ];thenecho "3. -z means NOT command-line arguments true"fifile="/etc/mtab"if [ -e $file ];thenecho "4. -e means file exists true"fihome="/root"if [ -d $home ];thenecho "5. -d means dir exists true"fi[ "true" ] && [ "false" ] && [ 0 ] && [ 1 ] && [ -1 ] && echo "6. string and integer means true"
二元比较操作符用来比较两个变量或数字. 注意整数比较与字符串比较的区别.
<div class="VARIABLELIST">整数比较
-eq
等于
if [ "$a" -eq "$b" ]
-ne不等于
if [ "$a" -ne "$b" ]
-gt大于
if [ "$a" -gt "$b" ]
-ge大于等于
if [ "$a" -ge "$b" ]
-lt小于
if [ "$a" -lt "$b" ]
-le小于等于
if [ "$a" -le "$b" ]
<小于(在双括号中使用)
(("$a" < "$b"))
<=小于等于(在双括号中使用)
(("$a" <= "$b"))
>大于(在双括号中使用)
(("$a" > "$b"))
>=大于等于(在双括号中使用)
(("$a" >= "$b"))
页:
[1]