Linux编程  变量表达式
变量表达式是编程期间使用非常频繁的编程元素之一。对变量表达式进行判断或者比较是在shell程序中使用逻辑完成任务的主要部分。在不同的shell中,两个逻辑比较操作符(数值和串)执行的方式稍有不同。在bash中,命令test可以用来完成表达式比较。test命令的语法如下所示:
test expression
[ expression ]
同其他的编程语言一样,test后面表达式中的操作符包括字符串操作符、数值操作符和逻辑操作符。由于shell编程并非真正意义上的计算机语言编程,所以,它没有像C语言那样具有大量可用的函数,在对文件进行判断时也不能像C语言那样先定义一个文件变量,然后用有关文件的函数对这个文件进行操作。shell里的变量都是字符串,为了能够对文件进行操作,shell提供了一种特殊的操作符,即文件操作符。
test命令支持如下几种比较类型:
字符串比较
数值比较
文件操作
逻辑操作
1.字符串比较
字符串表达式可以测试字符串是否相等、字符串长度是否为0或者字符串是否为空。bash shell是区分0长度字符串和空字符串的。字符串的比较操作符如表2所示。
表2  字符串操作符
操作符
说明
=
比较两个字符串是否相等
!=
比较两个字符串是否不相等
-n
判断字符串的长度是否大于0
-z
判断字符串的长度是否等于0
下面的shell程序compareStr利用这些操作符比较两个名为str1和str2的字符串。
#!/bin/sh
str1="opq"
str2="opr"
if [ $str1 = $str2 ]; then
    echo "str1 equal to str2"
else
    echo "str1 not equal to str2"
fi
if  [ $str2 != $str1 ]; then
    echo "str2 not equal to str1"
else
    echo "str2 equal to str1"
fi
if [ $str1 ]; then
    echo "str1 is not empty"
else
    echo "str1 is empty"
fi
if [ -n $str2 ]; then
    echo  "str2 has a length greater than zero"
else
    echo "str2 has  lenth equal to zero"
fi
if [ -z $str1 ]; then
    echo "str1 has a lenth equal to zero"
else
    echo "str1 has a lenth greater than zero"
fi
该程序的执行结果如图13所示。
程序第一行的“#!/bin/sh”用于指定使用bash shell解释该程序,该行也等价于“#!/bin/bash”。如果两个字符串长度不相等,系统会在短字符串尾部添加空格,然后再进行比较。另外,读者要注意shell程序书写时的格式,例如第4行中的“[ $str1 = $str2 ];”,在输入时就应当注意要有适当的空格,否则,程序就不能正常执行。
2.数值比较
bash shell不使用诸如>、<和>=等符号来表示大于和小于等关系的比较,而是用整数表达式来表示。表3中的操作符可用于比较两个数值。
表3  数值比较操作符
操作符
说明
-eq
比较两个数值是否相等(equal
-ge
比较一个数是否大于或者等于另一个数(greater or equal
-le
比较一个数是否小于或者等于另一个数(less or equal
-ne
比较两个数是否不相等(not equal
-gt
比较一个数是否大于另一个数(greater than
-lt
比较一个数是否小于另一个数(less than
下面的shell程序compareNum用于对3个数num1、num2和num3进行比较。
#!/bin/bash
num1=20
num2=30
num3=20
if [ $num1 -eq $num3 ]; then
    echo "num1 is equal to num3"
else
    echo "num1 is not equal to num3"
fi
if  [ $num2 -ne $num1 ]; then
    echo "num2 is not equal to num1"
else
    echo "num2 is equal to num1"
fi
if [ $num1 -gt $num2 ]; then
    echo "num1 is greater than num2"
else
    echo "num1 is not greater than num2"
fi
if [ $num1 -ge $num3 ]; then
    echo  "num1 is greater than or equal  to num3"
else
    echo "num1 is not greater than or equal to num3"
fi
if [ $num1 -lt $num2 ]; then
    echo "num1 is less than num2"
else
    echo "num1 is not less than num2"
fi
if [ $num1 -le $num3 ]; then
    echo "num1 is less than or equal to num3"
else
    echo "num1 is not less than or equal to num3"
fi
该程序的执行结果如图所示。
 
图11  使用数值比较符  图14  使用文件操作符字符串长度判断
该程序首先定义了3个变量,并对它们赋以数值,然后,使用多个数值比较符对其进行比较,最后输出了比较的结果。
3.文件操作
文件测试表达式通常用来测试文件的信息,一般由脚本来决定文件是否应该备份、复制或者删除,文件测试表达式有很多种,表4列出了比较常用的几种。
表4  文件操作符
操作符
说明
-d
判断文件是否为目录
-f
判断文件是否为普通文件
-r
判断文件是否可读
-s
判断文件是否存在且长度大于0
-w
判断文件是否可写
-x
判断文件是否可执行
下面的shell程序fileOp位于当前用户目录中,在该目录中还有文件fileTest和子目录dirTest。假设fileTest的权限为读和写,dirTest的权限为读和执行。
#!/bin/bash
if [ -d $dirTest ]; then
    echo "dirTest is a directory"
else
    echo "dirTest is not a directory"
fi
if  [ -f $dirTest]; then
    echo "dirTest is a regular file"
else
    echo "dirTest is not a regular file"
fi
if [ -r $fileTest ]; then
    echo "fileTest has read permission"
else
    echo "fileTest does not have read permission"
fi
if [ -w $fileTest ]; then
    echo  "fileTest has write permission"
else
    echo "fileTest does not have write permission"
fi
if [ -x $dirTest ]; then
    echo "dirTest has execute permission"
else
    echo "dirTest does not have permission"
fi
该程序的执行结果如图14所示。
该程序使用文件操作符对当前用户目录中的子目录和文件进行判断,然后输出了结果。
4.逻辑操作
逻辑操作是对逻辑值进行的操作,逻辑值只有“是”和“否”两个。表4列出了shell中的3个逻辑操作符。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。