9.3. Arithmetic

9.3.1. Integers (declare and let Commands)

The declare Command.

Variables can be declared as integers with the declare -i command. If you attempt to assign any string value, bash assigns 0 to the variable. Arithmetic can be performed on variables that have been declared as integers. (If the variable has not been declared as an integer, the built-in let command allows arithmetic operations. See "The let Command" on page 394.) If you attempt to assign a floating point number, bash reports a syntax error. Numbers can also be represented in different bases such as binary, octal, and hex.

Example 9.6.
1 $ declare –i num

2 $ num=hello
  $ echo $num
  0

3 $ num=5 + 5
							bash: +: command not found

4 $ num=5+5
  $ echo $num
  10

5 $ num=4*6
  $ echo $num
  24

6 $ num="4 * 6"
  $ echo $num
  24

7 $ num=6.5
							bash: num: 6.5: sytax error in expression (remainder of
							expression is ".5")
						

Explanation

  1. The declare command with the -i option creates an integer variable num.

  2. Trying to assign the string hello to the integer variable num causes the string to be stored as zero.

  3. The white space must be quoted or removed unless the let and is used.

  4. The white space is removed and arithmetic is performed.

  5. Multiplication is performed and the result assigned to num.

  6. The white space is quoted so that the multiplication can be performed and to keep the shell from expanding the wildcard (*).

  7. Because the variable is set to integer, adding a fractional part causes a bash syntax error.

Listing Integers

The declare command with only the -i argument will list all preset integers and their values, as shown in the following display.

$ declare –i
							declare -ir EUID="15"
							# effective user id
							declare -ir PPID="235"
							# parent process id
							declare -ir UID="15"
							# user id
						

Representing and Using Different Bases

Numbers can be represented in decimal (base 10 and the default), octal (base 8), hexadecimal (base 16), ranging from base 2 to 36.

Format

variable=base#number-in-that-base

Example 9.7.
							n=2#101
							Base is 2; number 101 is in base 2
						

Example 9.8.
(The Command Line)
1  $ declare -i x=017
   $ echo $x
   15
2  $ x=2#101
   $ echo $x
   5
3  $ x=8#17
   $ echo $x
   15
4  $ x=16#b
   $ echo $x
   11
						

Explanation

  1. The declare function is used to assign an integer variable x the octal value 017. Octal numbers must start with a leading zero. 15, the decimal value of 017, is printed.

  2. The variable, x, is assigned the value of 101 (binary). 2 represents the base, separated by a #, and the number in that base, 101. The value of x is printed as decimal, 5.

  3. The variable, x, is assigned the value of 17 (octal).x. The value of x is printed as decimal, 15.

  4. The variable, x, is assigned the value of b (hexadecimal). The value of x is decimal 11.

The let Command

The let command is a bash shell built-in command that is used to perform integer arithmetic and numeric expression testing. To see what let operators your version of bash supports, type at the prompt:

             help let

A list of the operators is also found in Table 9.4 on page 407.

Example 9.9.
1 $ i=5
							or
							let i=5

2 $ let i=i+1
  $ echo $i
							6

3 $ let "i = i + 2"
  $ echo $i
							8

4 $ let "i+=1"
  $  echo $i
							9

5 $ i=3

6 $ (( i+=4))
  $ echo $i
							7

7 $ (( i=i-2 ))
  $ echo $i
							5
						

Explanation

  1. The variable i is assigned the value 5.

  2. The let command will add 1 to the value of i. The $ (dollar sign) is not required for variable substitution when performing arithmetic.

  3. The quotes are needed if the arguments contain white space.

  4. The shortcut operator, +=, is used to add 1 to the value of i.

  5. The variable i is assigned the value 5.

  6. The double parentheses can be used to replace let.[a] 4 is added and assigned to i.

    [a] Double parentheses (()) are used to replace let on versions of Bash 2.x.

  7. 2 is subtracted from i. We could have also written: i–=2

9.3.2. Floating Point Arithmetic

Bash supports only integer arithmetic, but the bc, awk, and nawk utilities are useful if you need to perform more complex calculations.

Example 9.10.
(The Command Line)
1  $ n=`echo "scale=3; 13 / 2" | bc`
   $ echo $n
						6.500

2  product=`gawk -v x=2.45 -v y=3.123'BEGIN{printf    "%.2f
",x*y}''
   $ echo $product
						7.65
					

Explanation

  1. The output of the echo command is piped to the bc program. The scale is set to 3, which is the number of significant digits to the right of the decimal point that will be printed. The calculation is to divide 13 by 2. The entire pipeline is enclosed in back quotes. Command substitution will be performed and the output assigned to the variable n.

  2. The gawk program gets its values from the argument list passed in at the command line, x=2.45 y=3.123. After the numbers are multiplied, the printf function formats and prints the result with a precision of two places to the right of the decimal point. The output is assigned to the variable product.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.185