Shell programming 4


基础永远值得花费90%的精力去学习加强。认识实践的重要性。


SHELL=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]

progress:[######## ] 40%


Ax Shell Basic Operators | 运算符

操作符分以下几种:

  • Arithmetic Operators | 算术运算符
  • Relational Operators | 关系运算符
  • Boolean Operators | 布尔运算符
  • String Operators | 字符串运算符
  • File Test Operators | 文件测试运算符

Bourns shell最初没有任何机制执行简单的算术运算,一般使用外部程序,awk或者expr。

如数字相加:

$ expr 2 + 2

!操作符和表达式之间必须加空格。

Basic Operators

基本语法,不用命令的方式,这两种都可以,里面的空格可有可无。

$((运算式))
$[运算式]

$ echo $[1+2]
3
$ a=$[(1+2)*4]
$ echo $a
12

放入脚本,使用参数进行赋值。

#!/bin/bash
sum=$[$1+$2]
echo sum=$sum

执行

$. sum.sh 2 3
sum=5

Arithmetic Operators

设变量a为10,b为20。所有的运算的类型是长整型

Operator Description Example
+ (Addition) 左边加右边 expr $a + $b will give 30
- (Subtraction) 左边减右边 expr $a - $b will give -10
* (Multiplication) 左右相乘 expr $a \* $b will give 200
/ (Division) 左边除以右边 expr $b / $a will give 2
% (Modulus) 左边除以右边,返回余数 expr $b % $a will give 0
= (Assignment) 将右边的值赋给左边 a = $b would assign value of b into a
== (Equality) 比较两个数,相等返回true [ $a == $b ] would return false.
!= (Not Equality) 比较两个数,不相等返回true [ $a != $b ] would return true.

值得注意的是:所有的关系表达式都应该在方括号中,且都存在空格。

例子 −

#!/bin/sh

a=10
b=20

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a is equal to b"
fi

if [ $a != $b ]
then
   echo "a is not equal to b"
fi

脚本执行 −

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b

Relational Operators

只支持数值,不支持字符串。设变量a为10,b为20。

Operator Description Example
-eq 两个值是否相等,是则为真 [ $a -eq $b ] is not true.
-ne 两个值是否相等,不是则为真 [ $a -ne $b ] is true.
-gt 左边是否大于右边,是则为真 [ $a -gt $b ] is not true.
-lt 左边是否小于右边,是则为真 [ $a -lt $b ] is true.
-ge 左边是否大于等于右边,是则为真 [ $a -ge $b ] is not true.
-le 左边是否小于等于右边,是则为真 [ $a -le $b ] is true.

例子

#!/bin/sh

a=10
b=20

if [ $a -eq $b ]
then
   echo "$a -eq $b : a is equal to b"
else
   echo "$a -eq $b: a is not equal to b"
fi

if [ $a -ne $b ]
then
   echo "$a -ne $b: a is not equal to b"
else
   echo "$a -ne $b : a is equal to b"
fi

if [ $a -gt $b ]
then
   echo "$a -gt $b: a is greater than b"
else
   echo "$a -gt $b: a is not greater than b"
fi

if [ $a -lt $b ]
then
   echo "$a -lt $b: a is less than b"
else
   echo "$a -lt $b: a is not less than b"
fi

if [ $a -ge $b ]
then
   echo "$a -ge $b: a is greater or  equal to b"
else
   echo "$a -ge $b: a is not greater or equal to b"
fi

if [ $a -le $b ]
then
   echo "$a -le $b: a is less or  equal to b"
else
   echo "$a -le $b: a is not less or equal to b"
fi

执行脚本 −

10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is not greater or equal to b
10 -le 20: a is less or equal to b

Boolean Operators

设变量a为10,b为20。

Operator Description Example
! 逻辑否定,真即为假,假即为真。 [ ! false ] is true.
-o 逻辑或,符合其一即为真。 [ $a -lt 20 -o $b -gt 100 ] is true.
-a 逻辑与,完全符合即为真。 [ $a -lt 20 -a $b -gt 100 ] is false.

例子

#!/bin/sh

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a -lt 100 -a $b -gt 15 : returns true"
else
   echo "$a -lt 100 -a $b -gt 15 : returns false"
fi

if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

执行脚本 −

10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false

String Operators

字符串操作符,设变量a=‘abc’,变量b=‘efg’

Operator Description Example
= 两个值是否相等,是则为真。 [ $a = $b ] is not true.
!= 两个值是否相等,否则为真。 [ $a != $b ] is true.
-z 值是否为0,是则为真。 [ -z $a ] is not true.
-n 值是否为0,否则为真。 [ -n $a ] is not false.
str 是否为空,否则为真。 [ $a ] is not false.

例子

#!/bin/sh

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a is equal to b"
else
   echo "$a = $b: a is not equal to b"
fi

if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi

if [ -z $a ]
then
   echo "-z $a : string length is zero"
else
   echo "-z $a : string length is not zero"
fi

if [ -n $a ]
then
   echo "-n $a : string length is not zero"
else
   echo "-n $a : string length is zero"
fi

if [ $a ]
then
   echo "$a : string is not empty"
else
   echo "$a : string is empty"
fi

执行脚本 −

abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : string length is not zero
-n abc : string length is not zero
abc : string is not empty

File Test Operators

设具有一个名为test的文件,100 bytes,具有可读、可写和可执行权限。

Operator Description Example
-b file 检查文件是否为块特殊文件,是则为真。 [ -b $file ] is false.
-c file 检查文件是否为字符特殊文件,是则为真。 [ -c $file ] is false.
-d file 检查文件是否为目录,是则为真。 [ -d $file ] is not true.
-f file 检查文件是否为普通文件,是则为真。 [ -f $file ] is true.
-g file 检查文件是否设置了组,是则为真。 [ -g $file ] is false.
-k file 检查文件是否设置了粘滞位(Stickybit),是则为真。 [ -k $file ] is false.
-p file 检查文件是否命名管道,是则为真。 [ -p $file ] is false.
-t file 检查文件描述符是否打开并与终端相关联,是则为真。 [ -t $file ] is false.
-u file 检查文件是否设置了用户id,是则为真。 [ -u $file ] is false.
-r file 检查文件是否可读,是则为真。 [ -r $file ] is true.
-w file 检查文件是否可写,是则为真。 [ -w $file ] is true.
-x file 检查文件是否可执行,是则为真。 [ -x $file ] is true.
-s file 检查文件大小是否大于0,是则为真。 [ -s $file ] is true.
-e file 检查文件是否存在,是则为真。 [ -e $file ] is true.

例子

#!/bin/sh

file="/var/www/tutorialspoint/unix/test.sh"

if [ -r $file ]
then
   echo "File has read access"
else
   echo "File does not have read access"
fi

if [ -w $file ]
then
   echo "File has write permission"
else
   echo "File does not have write permission"
fi

if [ -x $file ]
then
   echo "File has execute permission"
else
   echo "File does not have execute permission"
fi

if [ -f $file ]
then
   echo "File is an ordinary file"
else
   echo "This is sepcial file"
fi

if [ -d $file ]
then
   echo "File is a directory"
else
   echo "This is not a directory"
fi

if [ -s $file ]
then
   echo "File size is not zero"
else
   echo "File size is zero"
fi

if [ -e $file ]
then
   echo "File exists"
else
   echo "File does not exist"
fi

执行脚本 −

File does not have write permission
File does not have execute permission
This is sepcial file
This is not a directory
File size is not zero
File does not exist

文章作者: Enomothem
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Enomothem !
  目录