27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2".<br />

echo "t2 = $t2 a = $a" # t2 = 5 a = 9<br />

The comma operator finds use mainly in for loops. See Example 10−12.<br />

8.2. Numerical Constants<br />

A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation. A<br />

number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16). A<br />

number with an embedded # evaluates as BASE#NUMBER (with range and notational restrictions).<br />

Example 8−4. Representation of numerical constants<br />

#!/bin/bash<br />

# numbers.sh: Representation of numbers in different bases.<br />

# Decimal: the default<br />

let "dec = 32"<br />

echo "decimal number = $dec" # 32<br />

# Nothing out of the ordinary here.<br />

# Octal: numbers preceded by '0' (zero)<br />

let "oct = 032"<br />

echo "octal number = $oct" # 26<br />

# Expresses result in decimal.<br />

# −−−−−−−−− −−−−−− −− −−−−−−−<br />

# Hexadecimal: numbers preceded by '0x' or '0X'<br />

let "hex = 0x32"<br />

echo "hexadecimal number = $hex" # 50<br />

# Expresses result in decimal.<br />

# Other bases: BASE#NUMBER<br />

# BASE between 2 and 64.<br />

# NUMBER must use symbols within the BASE range, see below.<br />

let "bin = 2#111100111001101"<br />

echo "binary number = $bin" # 31181<br />

let "b32 = 32#77"<br />

echo "base−32 number = $b32" # 231<br />

let "b64 = 64#@_"<br />

echo "base−64 number = $b64" # 4094<br />

#<br />

# This notation only works for a limited range (2 − 64)<br />

# 10 digits + 26 lowercase characters + 26 uppercase characters + @ + _<br />

echo<br />

echo $((36#zz)) $((2#10101010)) $((16#AF16)) $((53#1aA))<br />

# 1295 170 44822 3375<br />

# Important note:<br />

# −−−−−−−−−−−−−−<br />

Chapter 8. Operations and Related Topics 61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!