# deb http://download.opensuse.org/repositories/home:/rzrfreefr/Debian_7.0/ /
sudo apt-get install bash=0.0.20140411-0~rzr20140515
SOURCE
- bash_test.sh
#!/bin/bash
generate_file_to_be_sourced()
{
local file_to_be_sourced
file_to_be_sourced="$(mktemp)"
cat << "EOF" > "${file_to_be_sourced}"
declare -a tmp='([0]="1" [1]="2")'
EOF
echo "${file_to_be_sourced}"
}
foo()
{
source "${g_file_to_be_sourced}"
if declare -p tmp &> /dev/null && [ "${#tmp[@]}" = "2" ]
then
echo "test #2: OK"
else
echo "test #2: NOK"
fi
unset tmp
}
echo "bash version: ${BASH_VERSION}"
echo
declare -r g_file_to_be_sourced="$(generate_file_to_be_sourced)"
echo "content of file to be sourced:"
cat "${g_file_to_be_sourced}"
echo
source "${g_file_to_be_sourced}"
if declare -p tmp &> /dev/null && [ "${#tmp[@]}" = "2" ]
then
echo "test #1: OK"
else
echo "test #1: NOK"
fi
unset tmp
foo
rm "${g_file_to_be_sourced}"
OK
bash -x /tmp/t.sh
+ echo 'bash version: 4.2.37(1)-release'
bash version: 4.2.37(1)-release
+ echo
++ generate_file_to_be_sourced
++ local file_to_be_sourced
+++ mktemp
++ file_to_be_sourced=/tmp/tmp.LtpCQJkxug
++ cat
++ echo /tmp/tmp.LtpCQJkxug
+ declare -r g_file_to_be_sourced=/tmp/tmp.LtpCQJkxug
+ echo 'content of file to be sourced:'
content of file to be sourced:
+ cat /tmp/tmp.LtpCQJkxug
declare -a tmp='([0]="1" [1]="2")'
+ echo
+ source /tmp/tmp.LtpCQJkxug
++ declare -a 'tmp=([0]="1" [1]="2")'
+ declare -p tmp
+ '[' 2 = 2 ']'
+ echo 'test #1: OK'
test #1: OK
+ unset tmp
+ foo
+ source /tmp/tmp.LtpCQJkxug
++ declare -a 'tmp=([0]="1" [1]="2")'
+ declare -p tmp
+ '[' 2 = 2 ']'
+ echo 'test #2: OK'
test #2: OK
+ unset tmp
+ rm /tmp/tmp.LtpCQJkxug
KO
bash -x t.bash
+ echo 'bash version: 4.3.11(1)-release'
bash version: 4.3.11(1)-release
+ echo
++ generate_file_to_be_sourced
++ local file_to_be_sourced
+++ mktemp
++ file_to_be_sourced=/tmp/tmp.IUBziKiNg4
++ cat
++ echo /tmp/tmp.IUBziKiNg4
+ declare -r g_file_to_be_sourced=/tmp/tmp.IUBziKiNg4
+ echo 'content of file to be sourced:'
content of file to be sourced:
+ cat /tmp/tmp.IUBziKiNg4
declare -a tmp='([0]="1" [1]="2")'
+ echo
+ source /tmp/tmp.IUBziKiNg4
++ declare -a 'tmp=([0]="1" [1]="2")'
+ declare -p tmp
+ '[' 2 = 2 ']'
+ echo 'test #1: OK'
test #1: OK
+ unset tmp
+ foo
+ source /tmp/tmp.IUBziKiNg4
++ declare -a 'tmp=([0]="1" [1]="2")'
+ declare -p tmp
+ echo 'test #2: NOK'
test #2: NOK
+ unset tmp
+ rm /tmp/tmp.IUBziKiNg4
ARRAYS
initialize_struct_framework()
{
g_struct_container=()
generic_struct_builder()
{
local p_struct_name="$1"
local p_builder_name="$2"
declare -A "${p_struct_name}" || error_handler || return
eval "${p_struct_name}"'[builder]="${p_builder_name}"' || error_handler || return
shift 2 || error_handler || return
while [ "0" -lt "$#" ]
do
local p_key_name="$1"
local p_key_value="$2"
shift 2 || error_handler || return
eval "${p_struct_name}"'["${p_key_name}"]="${p_key_value}"' || error_handler || return
done
g_struct_container+=("$(declare -p "${p_struct_name}")") || error_handler || return
}
filesystem_item_struct_builder()
{
local p_struct_name="$1"
local name_field_key="name"
local p_name_field_value="$2"
local child_set_field_key="child_set"
local p_child_set_field_value="$3"
local type_field_key="type"
local p_type_field_value="$4"
generic_struct_builder \
"${p_struct_name}" \
"${FUNCNAME}" \
"${name_field_key}" "${p_name_field_value}" \
"${child_set_field_key}" "${p_child_set_field_value}" \
"${type_field_key}" "${p_type_field_value}" || error_handler || return
}
}
add_item()
{
local p_item="$1"
filesystem_item_struct_builder "struct" "${p_item}" "" ""
local struct_index="$((${#g_struct_container[@]}-1))"
struct[child_set]+="${struct_index} "
eval "${g_struct_container[struct_index]}"
if [ -d "${p_item}" ]
then
struct[type]="directory"
cd "${p_item}"
local item
for item in *
do
add_item "${item}"
done
cd ..
else
struct[type]="file"
fi
g_struct_container[struct_index]="$(declare -p struct)"
}
display_rec()
{
local p_path="$1"
[ "${p_path}" != "" ] && p_path+="/"
echo "${p_path}${struct[name]}"
p_path+="${struct[name]}"
local struct_index
for struct_index in ${struct[child_set]}
do
eval "${g_struct_container[struct_index]}"
display_rec "${p_path}"
done
}
display()
{
eval "${g_struct_container}"
display_rec ""
}
initialize_struct_framework
add_item "."
display
declare -p g_struct_container
MORE