On 100501 12:40, Pierre Gaston wrote:
> On Sat, May 1, 2010 at 12:26 PM, Dennis Williamson wrote:
> > As Chet said, use internal variables that are unlikely to conflict.
> You can use workarounds like:
> printf -v $a "%s" foobar
> read $a <<< "%s"
The problem with obfucscated internal variables I think is that my
library code becomes unnecessary obfuscated. That's why I'm thinking an
additional call-layer (workaround 2 in my original mail) is an
improvement in that the private library code "_blackbox()" can use
simple, readable variables without restrictions/conflicts, while the
public layer "blackbox()" checks conflicts, which - unlikely as they
might be - I'd like to return to my library users as a known error
instead of silent failing.
Revised example, replacing eval with printf -v, thanks:
# Param: $1 variable name to return value to
# Private library function. Do not call directly. See blackbox()
_blackbox() {
# Just don't declare "local __1"
local a b c d e f g h i j
# ...
# Lots of complicated library code here
# ...
# Return value
printf -v $1 %s b
}
# Param: $1 variable name to return value to
# Public library function
blackbox() {
local __1
_blackbox __1
[[ $1 == __1 ]] && echo "ERROR: variable name conflicts"\
"with local variable: $1"
printf -v $1 %s "$__1"
}
blackbox a; echo $a # Outputs "b" all right
blackbox __1 # Outputs error
d='ls /;true'; blackbox "$d" # No more oops
Freddy Vulto
http://fvue.nl/wiki/Bash:_passing_variables_by_reference