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

Advanced Bash−Scripting Guide

exit 0

There is an alternative, and perhaps less confusing method of redirecting a function's stdin. This involves redirecting the stdin to an embedded bracketed code block within the function.

#Instead of: Function ()

{

...

}< file

#Try this: Function ()

{

{

...

}< file

}

#Similarly,

Function

()

# This works.

{

 

 

{

 

 

echo $*

 

} | tr

a b

 

}

 

 

Function

()

# This doesn't work.

{

 

 

echo $*

 

 

} | tr a

b

# A nested code block is mandatory here.

#Thanks, S.C.

23.2.Local Variables

What makes a variable "local"?

local variables

A variable declared as local is one that is visible only within the block of code in which it appears. It has local "scope". In a function, a local variable has meaning only within that function block.

Example 23−8. Local variable visibility

#!/bin/bash

 

func ()

 

{

 

local loc_var=23

# Declared local.

echo

 

echo "\"loc_var\" in function = $loc_var"

global_var=999

# Not declared local.

 

 

23.2. Local Variables

254

Advanced Bash−Scripting Guide

echo "\"global_var\" in function = $global_var"

}

func

# Now, see if local 'a' exists outside function.

echo

echo "\"loc_var\" outside function = $loc_var"

# "loc_var" outside function =

# Nope, $loc_var not visible globally. echo "\"global_var\" outside function = $global_var"

# "global_var" outside function = 999

# $global_var is visible globally.

echo

exit 0

Before a function is called, all variables declared within the function are invisible outside the body of the function, not just those explicitly declared as local.

#!/bin/bash

 

func ()

 

{

 

global_var=37

# Visible only within the function block

 

#+ before the function has been called.

}

# END OF FUNCTION

echo "global_var = $global_var" # global_var =

# Function "func" has not yet been called, #+ so $global_var is not visible here.

func

echo "global_var = $global_var" # global_var = 37

#Has been set by function call.

23.2.1.Local variables make recursion possible.

Local variables permit recursion, [52] but this practice generally involves much computational overhead and

is definitely not recommended in a shell script. [53]

Example 23−9. Recursion, using a local variable

#!/bin/bash

 

#

factorial

#

−−−−−−−−−

#Does bash permit recursion?

#Well, yes, but...

#You gotta have rocks in your head to try it.

23.2.1. Local variables make recursion possible.

255

Advanced Bash−Scripting Guide

MAX_ARG=5

E_WRONG_ARGS=65

E_RANGE_ERR=66

if [ −z "$1" ] then

echo "Usage: `basename $0` number" exit $E_WRONG_ARGS

fi

if [ "$1" −gt $MAX_ARG ] then

echo "Out of range (5 is maximum)."

#Let's get real now.

#If you want greater range than this,

#rewrite it in a real programming language. exit $E_RANGE_ERR

fi

fact ()

{

local number=$1

#Variable "number" must be declared as local,

#otherwise this doesn't work.

if [ "$number" −eq 0 ] then

 

factorial=1

# Factorial of 0 = 1.

else

 

 

let "decrnum =

number − 1"

 

fact $decrnum

# Recursive function call.

 

let "factorial

= $number * $?"

fi

 

 

return $factorial

 

}

 

 

fact

$1

 

echo

"Factorial of

$1 is $?."

exit

0

 

See also Example A−11 for an example of recursion in a script. Be aware that recursion is resource−intensive and executes slowly, and is therefore generally not appropriate to use in a script.

23.2.1. Local variables make recursion possible.

256