Getting Started with Shell Scripting-6

(1) Shell, like other programming languages, supports a variety of operators, including:

  • arithmetic operators
  • relational operator
  • boolean operator
  • string operator
  • file test operator

Native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr is the most commonly used .

expr is an expression evaluation tool that can be used to evaluate expressions .

For example, to add two numbers ( note the use of backticks ` instead of single quotes' ):

#!/bin/bash

val=`expr 2 + 2`  (注意: 2和'+'号 之间要有空格,不然结果为 2+2, 而不是4)
echo "两数之和为 : $val"

Execute the script and the output is as follows:

The sum of the two numbers is: 4

Two points of note:

  • There should be spaces between expressions and operators , for example 2+2 is wrong, it must be written as 2 + 2, which is different from most programming languages ​​we are familiar with.
  • The complete expression should be enclosed by ` `, note that this character is not the usual single quote , below the Esc key.

(2) Arithmetic operators 

The following table lists common arithmetic operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
+ addition `expr $a + $b` results in 30.
- subtraction `expr $a - $b` results in -10.
* multiplication `expr $a \ * $b` results in 200.
/ division `expr $b / $a` results in 2.
% remainder `expr $b % $a` results in 0.
= assign a=$b will assign the value of variable b to a.
== equal. Used to compare two numbers, returns true if they are the same. [ $a == $b ] returns false.
!= not equal. Used to compare two numbers, returns true if they are not the same. [ $a != $b ] returns true.

Note: Conditional expressions should be placed between square brackets and spaces . For example:  [$a==$b]  is wrong and must be written as  [ $a == $b ] .

example

Examples of arithmetic operators are as follows:

#!/bin/bash
# author:ethan

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 ]   (注意:$a前面和后面, $b前面和后面都需要加空格)
then
   echo "a 等于 b"
fi
if [ $a != $b ]
then
   echo "a 不等于 b"
fi


执行脚本,输出结果如下所示:
a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b

Notice:

  • The multiplication sign (*) must be preceded by a backslash (\) to achieve multiplication ;
  • if...then...fi is a conditional statement, which will be explained later.
  • The expr syntax of the shell in MAC is: $((expression)), where the "*" in the expression does not need the escape symbol "\" .

(3) Relational operators

Relational operators only support numbers, not strings, unless the value of the string is a number .

The following table lists commonly used relational operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
-eq Checks whether two numbers are equal, and returns true if they are equal. [ $a -eq $b ] returns false.
-born Check if two numbers are equal, return true if they are not equal. [ $a -ne $b ] returns true.
-gt Checks whether the number on the left is greater than the number on the right, and if so, returns true. [ $a -gt $b ] returns false.
-lt Checks whether the number on the left is less than the number on the right, and if so, returns true. [ $a -lt $b ] returns true.
-give Check if the number on the left is greater than or equal to the number on the right, and if so, return true. [ $a -ge $b ] returns false.
-the Checks whether the number on the left is less than or equal to the number on the right, and if so, returns true. [ $a -le $b ] returns true.

example

Examples of relational operators are as follows:

#!/bin/bash
# author:ethan

a=10
b=20

if [ $a -eq $b ]
then
   echo "$a -eq $b : a 等于 b"
else
   echo "$a -eq $b: a 不等于 b"
fi
if [ $a -ne $b ]
then
   echo "$a -ne $b: a 不等于 b"
else
   echo "$a -ne $b : a 等于 b"
fi
if [ $a -gt $b ]
then
   echo "$a -gt $b: a 大于 b"
else
   echo "$a -gt $b: a 不大于 b"
fi
if [ $a -lt $b ]
then
   echo "$a -lt $b: a 小于 b"
else
   echo "$a -lt $b: a 不小于 b"
fi
if [ $a -ge $b ]
then
   echo "$a -ge $b: a 大于或等于 b"
else
   echo "$a -ge $b: a 小于 b"
fi
if [ $a -le $b ]
then
   echo "$a -le $b: a 小于或等于 b"
else
   echo "$a -le $b: a 大于 b"
fi


执行脚本,输出结果如下所示:
10 -eq 20: a 不等于 b
10 -ne 20: a 不等于 b
10 -gt 20: a 不大于 b
10 -lt 20: a 小于 b
10 -ge 20: a 小于 b
10 -le 20: a 小于或等于 b

(4) Boolean operators

The following table lists common Boolean operators, assuming variable a is 10 and variable b is 20:

operator illustrate Example
! NOT operation, returns false if the expression is true, otherwise returns true. [ ! false ] returns true.
-O OR operation, returns true if one of the expressions is true. [ $a -lt 20 -o $b -gt 100 ] returns true.
-a AND operation returns true when both expressions are true. [ $a -lt 20 -a $b -gt 100 ] returns false.

example

Examples of Boolean operators are as follows:

#!/bin/bash
# author:ethan

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a 小于 100 且 $b 大于 15 : 返回 true"
else
   echo "$a 小于 100 且 $b 大于 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a 小于 100 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 100 或 $b 大于 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a 小于 5 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 5 或 $b 大于 100 : 返回 false"
fi

执行脚本,输出结果如下所示:
10 != 20 : a 不等于 b
10 小于 100 且 20 大于 15 : 返回 true
10 小于 100 或 20 大于 100 : 返回 true
10 小于 5 或 20 大于 100 : 返回 false

(5) Logical operators

The following describes Shell's logical operators, assuming that variable a is 10 and variable b is 20:

operator illustrate Example
&& logical AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| logical OR [[ $a -lt 100 || $b -gt 100 ]] returns true

example

Examples of logical operators are as follows:

#!/bin/bash
# author:ethan

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

if [[ $a -lt 100 || $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

执行脚本,输出结果如下所示:
返回 false
返回 true

(6) String operators

The following table lists common string operators, assuming that variable a is "abc" and variable b is "efg":

operator illustrate Example
= Checks if two strings are equal, returns true for equality. [ $a = $b ] returns false.
!= Checks whether two strings are equal, and returns true if they are not equal. [ $a != $b ] returns true.
-with Check if the string length is 0, if 0 returns true . [ -z $a ] returns false.
-n Check if the string length is 0, if not return true . [ -n $a ] returns true.
str Check if the string is empty, return true if not . [ $a ] returns true.

example

Examples of string operators are as follows:

#!/bin/bash
# author:ethan

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ -n $a ]
then
   echo "-n $a : 字符串长度不为 0"
else
   echo "-n $a : 字符串长度为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

执行脚本,输出结果如下所示:
abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空

(七)文件测试运算符

文件测试运算符用于检测 Unix 文件的各种属性。

属性检测描述如下:

操作符 说明 举例
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。

实例

变量 file 表示文件"/Users/youzan-ethan/documents/shell/shell_5.sh",它的大小为100字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:

#!/bin/bash
# author:ethan

file="/Users/youzan-ethan/documents/shell/shell_5.sh"
if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi
if [ -w $file ]
then
   echo "文件可写"
else
   echo "文件不可写"
fi
if [ -x $file ]
then
   echo "文件可执行"
else
   echo "文件不可执行"
fi
if [ -f $file ]
then
   echo "文件为普通文件"
else
   echo "文件为特殊文件"
fi
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi
if [ -s $file ]
then
   echo "文件不为空"
else
   echo "文件为空"
fi
if [ -e $file ]
then
   echo "文件存在"
else
   echo "文件不存在"
fi

执行脚本,输出结果如下所示:
文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473770&siteId=291194637