Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cooper M.Advanced bash-scripting guide.2002.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
916.67 Кб
Скачать

Advanced Bash−Scripting Guide

8.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 8−3. Representation of numerical constants:

#!/bin/bash

#numbers.sh: Representation of numbers.

#Decimal

let "d = 32" echo "d = $d"

#Nothing out of the ordinary here.

#Octal: numbers preceded by '0' (zero) let "o = 071"

echo "o = $o"

#Expresses result in decimal.

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

echo "h = $h"

#Expresses result in decimal.

#Other bases: BASE#NUMBER

#BASE between 2 and 36.

let "b = 32#77" echo "b = $b"

#

# This notation only works for a limited range (2 − 36)

# ... 10 digits + 26 alpha characters = 36. let "c = 2#47" # Out of range error:

# numbers.sh: let: c = 2#47: value too great for base (error token is "2#47") echo "c = $c"

echo

echo $((36#zz)) $((2#10101010)) $((16#AF16))

exit 0

# Thanks, S.C., for clarification.

Part 3. Beyond the Basics

Table of Contents

9.Variables Revisited

9.1.Internal Variables

9.2.Manipulating Strings

9.3.Parameter Substitution

9.4.Typing variables: declare or typeset

9.5.Indirect References to Variables

8.2. Numerical Constants

58

Advanced Bash−Scripting Guide

9.6.$RANDOM: generate random integer

9.7.The Double Parentheses Construct

10.Loops and Branches

10.1.Loops

10.2.Nested Loops

10.3.Loop Control

10.4.Testing and Branching

11.Internal Commands and Builtins

11.1.Job Control Commands

12.External Filters, Programs and Commands

12.1.Basic Commands

12.2.Complex Commands

12.3.Time / Date Commands

12.4.Text Processing Commands

12.5.File and Archiving Commands

12.6.Communications Commands

12.7.Terminal Control Commands

12.8.Math Commands

12.9.Miscellaneous Commands

13.System and Administrative Commands

14.Command Substitution

15.Arithmetic Expansion

16.I/O Redirection

16.1.Using exec

16.2.Redirecting Code Blocks

16.3.Applications

17.Here Documents

18.Recess Time

8.2. Numerical Constants

59