dimanche 1 mars 2015

Detecting function circular references in bash


Why does the following idiom not work in bash 4.1.0?



if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]


Here it is in context...



function isCircularRef_test () {
#
### Seems like this should work but it does not.
### if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]; then ...
### It appears to fail silently and exit the script so neither 'then' nor
### 'else' branch executes.
### Storing the array into a temporary string variable works. Why necessary?
#
printf "%s\n" "VERSION #1"
local _fna="${FUNCNAME[*]:1}"
if [[ "${_fna/$FUNCNAME/}" != "${_fna}" ]]
then
printf "%s\n" "IS circular reference"
else
printf "%s\n" "IS not circular reference"
fi
#
printf "%s\n" "VERSION #2"
if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]
then
printf "%s\n" "IS circular reference"
else
printf "%s\n" "IS not circular reference"
fi
}


Output is...



VERSION #1
IS not circular reference
VERSION #2


Aucun commentaire:

Enregistrer un commentaire