The OpenNET Project / Index page

[ новости /+++ | форум | wiki | теги | ]

Поиск:  Каталог документации

3.6. Operations and Related Topics

3.6.1. Operations

=

All-purpose assignment operator, which works for both arithmetic and string assignments.

var=27
category=minerals

May also be used in a string comparison test.

if [ $string1 = $string2 ]
then
   command
fi

The following are normally used in combination with expr or let.

arithmetic operators

+

plus

-

minus

*

multiplication

/

division

%

modulo, or mod (returns the remainder of an integer division)

+=

"plus-equal" (increment variable by a constant)

`expr $var+=5` results in var being incremented by 5.

-=

"minus-equal" (decrement variable by a constant)

*=

"times-equal" (multiply variable by a constant)

`expr $var*=4` results in var being multiplied by 4.

/=

"slash-equal" (divide variable by a constant)

%=

"mod-equal" (remainder of dividing variable by a constant)

The bitwise logical operators seldom make an appearance in shell scripts. Their chief use seems to be manipulating and testing values read from ports or sockets. "Bit flipping" is more relevant to compiled languages, such as C and C++, which run fast enough to permit its use on the fly.

<<

bitwise left shift (multiplies by 2 for each shift position)

<<=

"left-shift-equal"

let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)

>>

bitwise right shift (divides by 2 for each shift position)

>>=

"right-shift-equal" (inverse of <<=)

&

bitwise and

&=

"bitwise and-equal"

|

bitwise OR

|=

"bitwise OR-equal"

~

bitwise negate

!

bitwise NOT

^

bitwise XOR

^=

"bitwise XOR-equal"

relational tests

<

less than

>

greater than

<=

less than or equal to

>=

greater than or equal to

==

equal to (test)

!=

not equal to

&&

and (logical)

if [ $condition1 ] && [ $condition2 ]
# if both condition1 and condition2 hold true...

||

or (logical)

if [ $condition1 ] || [ $condition2 ]
# if both condition1 or condition2 hold true...

Example 3-14. Compound Condition Tests Using && and ||

#!/bin/bash

a=24
b=47

if [ $a -eq 24 ] && [ $b -eq 47 ]
then
  echo "Test #1 succeeds."
else
  echo "Test #1 fails."
fi

# ERROR:
# if [ $a -eq 24 && $b -eq 47 ]


if [ $a -eq 98 ] || [ $b -eq 47 ]
then
  echo "Test #2 succeeds."
else
  echo "Test #2 fails."
fi


# The -a and -o options provide
# an alternative compound condition test.
# Thanks to Patrick Callahan for pointing this out.


if [ $a -eq 24 -a $b -eq 47 ]
then
  echo "Test #3 succeeds."
else
  echo "Test #3 fails."
fi


if [ $a -eq 98 -o $b -eq 47 ]
then
  echo "Test #4 succeeds."
else
  echo "Test #4 fails."
fi


a=rhino
b=crocodile
if [ $a = rhino ] && [ $b = crocodile ]
then
  echo "Test #5 succeeds."
else
  echo "Test #5 fails."
fi

exit 0

3.6.2. Numerical Constants

A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation. A number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16). A number with an embedded # is evaluated as BASE#NUMBER (this option is of limited usefulness because of range restrictions).

Example 3-15. Representation of numerical constants:

#!/bin/bash

# Representation of numbers.

# Decimal
let "d = 32"
echo "d = $d"
# Nothing out of the ordinary here.


# Octal: numbers preceded by '0'
let "o = 071"
echo "o = $o"
# Expresses result in decimal.

# Hexadecimal: numbers preceded by '0x' or '0X'
let "h = 0x7a"
echo "h = $h"

# Other bases: BASE#NUMBER
# BASE between 2 and 64.
let "b = 32#77"
echo "b = $b"
# This notation only works for a very limited range of numbers.
let "c = 2#47"  # Error: out of range.
echo "c = $c"


exit 0



Спонсоры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2022 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру