Unix-arithmetic-operators
提供:Dev Guides
Unix/Linux-シェル算術演算子の例
次の算術演算子は、Bourne Shellでサポートされています。
変数 a が10を保持し、変数 b が20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
PLUS (Addition) | Adds values on either side of the operator | expr $a PLUS $b will give 30
|
- (Subtraction) | Subtracts right hand operand from left hand operand | expr $a - $b will give -10
|
* (Multiplication) | Multiplies values on either side of the operator | expr $a \* $b will give 200
|
/(Division) | Divides left hand operand by right hand operand | expr $b/$a will give 2
|
% (Modulus) | Divides left hand operand by right hand operand and returns remainder | expr $b % $a will give 0
|
= (Assignment) | Assigns right operand in left operand | a = $b would assign value of b into a |
== (Equality) | Compares two numbers, if both are same then returns true. | [ $a == $b ] would return false. |
!= (Not Equality) | Compares two numbers, if both are different then returns true. | [ $a != $b ] would return true. |
すべての条件式は、スペースを含む角カッコ内にある必要があることを理解することが非常に重要です。たとえば、 [$ a == $ b] は正しいのに対し、 [$ a == $ b] は正しくありません。
すべての算術計算は長整数を使用して行われます。
例
ここにすべての算術演算子を使用する例があります-
上記のスクリプトは、次の結果を生成します-
算術演算子を使用する場合、次の点を考慮する必要があります-
- 演算子と式の間にはスペースが必要です。 たとえば、2+ 2は正しくありません。 2+と書く必要があります。 2。
- 完全な式は、逆コンマと呼ばれる ’’ で囲む必要があります。
- 乗算には**記号で *\ を使用する必要があります。
- if … then … fi ステートメントは、次の章で説明されている意思決定ステートメントです。