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

Advanced Bash−Scripting Guide

The /dev/urandom device−file provides a means of generating much more

"random" pseudorandom numbers than the $RANDOM variable. dd if=/dev/urandom of=targetfile bs=1 count=XX creates a file of well−scattered pseudorandom numbers. However, assigning these numbers to a variable in a script requires a workaround, such as filtering through od (as in above example) or using dd (see Example 12−34).

There are also other means of generating pseudorandom numbers in a script. Awk provides a convenient means of doing this.

Example 9−23. Pseudorandom numbers, using awk

#!/bin/bash

#random2.sh: Returns a pseudorandom number in the range 0 − 1.

#Uses the awk rand() function.

AWKSCRIPT=' { srand(); print rand() } '

#Command(s) / parameters passed to awk

#Note that srand() reseeds awk's random number generator.

echo −n "Random number between 0 and 1 = " echo | awk "$AWKSCRIPT"

exit 0

#Exercises for the reader:

#−−−−−−−−−−−−−−−−−−−−−−−−−

#1] Using a loop construct, print out 10 different random numbers.

#(Hint: you must reseed the "srand()" function with a different seed

#in each pass through the loop. What happens if you fail to do this?)

#2] Using an integer multiplier as a scaling factor, generate random numbers

#in the range between 10 and 100.

#3] Same as exercise #2, above, but generate random integers this time.

9.7.The Double Parentheses Construct

Similar to the let command, the ((...)) construct permits arithmetic expansion and evaluation. In its simplest form, a=$(( 5 + 3 )) would set "a" to "5 + 3", or 8. However, this double parentheses construct is also a mechanism for allowing C−type manipulation of variables in Bash.

Example 9−24. C−type manipulation of variables

#!/bin/bash

# Manipulating a variable, C−style, using the ((...)) construct.

echo

9.7. The Double Parentheses Construct

96

 

 

 

Advanced Bash−Scripting Guide

(( a = 23 ))

# Setting

a value, C−style, with spaces on both sides of the "=".

echo "a (initial value)

= $a"

(( a++ ))

# Post−increment 'a', C−style.

echo "a (after a++) =

$a"

(( a−− ))

# Post−decrement 'a', C−style.

echo "a (after a−−) =

$a"

(( ++a ))

# Pre−increment 'a', C−style.

echo "a (after ++a) =

$a"

(( −−a ))

# Pre−decrement 'a', C−style.

echo "a (after −−a) =

$a"

echo

 

 

 

(( t = a<45?7:11 ))

# C−style trinary operator.

echo "If a < 45, then

t = 7, else t = 11."

echo "t = $t "

 

# Yes!

echo

#−−−−−−−−−−−−−−−−−

#Easter Egg alert!

#−−−−−−−−−−−−−−−−−

#Chet Ramey apparently snuck a bunch of undocumented C−style constructs #+ into Bash (actually adapted from ksh, pretty much).

#In the Bash docs, Ramey calls ((...)) shell arithmetic,

#+ but it goes far beyond that.

#Sorry, Chet, the secret is now out.

#See also "for" and "while" loops using the ((...)) construct.

#These work only with Bash, version 2.04 or later.

exit 0

See also Example 10−11.

9.7. The Double Parentheses Construct

97