[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Strange re-initialization of array. Bug or feature?
From: |
Tim Friske |
Subject: |
Strange re-initialization of array. Bug or feature? |
Date: |
Wed, 28 Nov 2012 01:37:29 +0100 |
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
- Strange re-initialization of array. Bug or feature?,
Tim Friske <=