shell_dump_function
This is an old revision of the document!
Error loading plugin creole
Error: Class 'Doku_Handler_List' not found
More info is available in the error log.
Error: Class 'Doku_Handler_List' not found
More info is available in the error log.
To save piles of typing when debugging bash scripts, a dump() function that understands both regular and associative arrays:
is_array() { if [ -n "$BASH" ]; then declare -p ${1} 2> /dev/null | grep 'declare \-a' >/dev/null && return 0 fi return 1 } is_hash() { if [ -n "$BASH" ]; then declare -p ${1} 2> /dev/null | grep 'declare \-A' >/dev/null && return 0 fi return 1 } dump() { for var; do if is_array ${var} ; then echo ${var} = $(eval "echo \${${var}[@]}") elif is_hash ${var} ; then dump_hash ${var} else echo ${var} = $(eval "echo \$${var}") fi done } dump_hash() { h=${1} for k in $(eval "echo \${!${h}[@]}"); do echo "$h[${k}]" = $(eval "echo \${${h}[${k}]}") done }
First, it can dump regular variables:
$ dump HOME HISTFILE HISTSIZE HOME = /home/rpjday HISTFILE = /home/rpjday/.bash_history HISTSIZE = 1000 $
Next, dumping regular arrays:
$ people=(fred barney wilma betty) $ dump people people = fred barney wilma betty $
shell_dump_function.1571960585.txt.gz · Last modified: 2019/10/24 23:43 by rpjday