Re: Strange re-initialization of array. Bug or feature?

2012-11-30 Thread Chet Ramey
On 11/27/12 7:37 PM, Tim Friske wrote:

 If sourced at least twice from another script I get the following output
 printed:
 
 AAA
 BBB
 declare -A ONCE='()'
 CCC
 declare -A ONCE='([std.bash]=1 )'
 BBB
 declare -Ai ONCE='()'
 CCC
 declare -Ai ONCE='([std.bash]=1 )'
 
 If I remove the initialization =() from declare -gAi ONCE I get the
 following output printed:
 
 AAA
 BBB
 declare -Ai ONCE='()'
 CCC
 declare -Ai ONCE='([std.bash]=1 )'
 BBB
 declare -Ai ONCE='([std.bash]=1 )'
 
 The declare -gAi ONCE=() seems to get executed every time although
 surrounded by [[ ! -v ONCE ]] whereas echo AAA within the same if-block
 only the first time.
 
 Is this a bug or feature?

This is the same bug I described earlier today, with the same fix.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Strange re-initialization of array. Bug or feature?

2012-11-27 Thread Tim Friske
Hi folks,

I execute the following code with Bash version GNU bash, Version
4.2.39(1)-release (x86_64-redhat-linux-gnu) on Fedora 17:

# Returns 0 else 1 if the sourcing (source) script isn't keyed by its base
name in the global ONCE array yet.
#
# A script should include this script near the top with source me_once ||
return 0 in order to get sourced itself only
# once.
#
# @return 0 on success, n != 0 on failure.
#
function me_once {
  unset -f me_once
  if [[ ! -v ONCE ]]; then
echo AAA
declare -gAi ONCE=()
  fi
  echo BBB
  declare -p ONCE
  declare -r a=${BASH_SOURCE[2]##*/}
  if (( ${ONCE[$a]:+1} )); then
return 1
  else
ONCE+=([$a]=1)
echo CCC
declare -p ONCE
  fi
}
me_once

If sourced at least twice from another script I get the following output
printed:

AAA
BBB
declare -A ONCE='()'
CCC
declare -A ONCE='([std.bash]=1 )'
BBB
declare -Ai ONCE='()'
CCC
declare -Ai ONCE='([std.bash]=1 )'

If I remove the initialization =() from declare -gAi ONCE I get the
following output printed:

AAA
BBB
declare -Ai ONCE='()'
CCC
declare -Ai ONCE='([std.bash]=1 )'
BBB
declare -Ai ONCE='([std.bash]=1 )'

The declare -gAi ONCE=() seems to get executed every time although
surrounded by [[ ! -v ONCE ]] whereas echo AAA within the same if-block
only the first time.

Is this a bug or feature?

Cheers,
Tim


-- 
--
`°
C92A E44E CC19 58E2 FA35 4048 2217 3C6E 0338 83FC