Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
shell_dump_function [2019/10/24 09:27] – rpjday | shell_dump_function [2019/10/24 23:44] (current) – rpjday | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | To save piles of typing when debugging bash scripts, a dump() function that understands arrays: | + | To save piles of typing when debugging bash scripts, a dump() function that understands |
< | < | ||
Line 5: | Line 5: | ||
if [ -n " | if [ -n " | ||
declare -p ${1} 2> /dev/null | grep ' | declare -p ${1} 2> /dev/null | grep ' | ||
+ | fi | ||
+ | return 1 | ||
+ | } | ||
+ | |||
+ | is_hash() { | ||
+ | if [ -n " | ||
+ | declare -p ${1} 2> /dev/null | grep ' | ||
fi | fi | ||
return 1 | return 1 | ||
Line 10: | Line 17: | ||
dump() { | dump() { | ||
- | | + | |
- | if is_array ${var} ; then | + | if is_array ${var} ; then |
- | echo "This is an array" | + | echo ${var} = $(eval " |
- | | + | elif is_hash ${var} ; then |
- | else | + | dump_hash ${var} |
- | echo "This is not an array" | + | |
- | | + | echo ${var} = $(eval "echo \$${var}" |
- | fi | + | fi |
- | done | + | done |
} | } | ||
+ | dump_hash() { | ||
+ | h=${1} | ||
+ | for k in $(eval "echo \${!${h}[@]}" | ||
+ | echo " | ||
+ | done | ||
+ | } | ||
</ | </ | ||
- | Then: | + | First, it can dump regular variables: |
< | < | ||
- | $ dump PATH HOME | + | $ dump HOME HISTFILE HISTSIZE |
- | PATH = / | + | |
HOME = / | HOME = / | ||
+ | HISTFILE = / | ||
+ | HISTSIZE = 1000 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | Next, dumping regular arrays: | ||
+ | |||
+ | < | ||
+ | $ people=(fred barney wilma betty) | ||
+ | $ dump people | ||
+ | people = fred barney wilma betty | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | Finally, dump a bash associative array: | ||
+ | |||
+ | < | ||
+ | $ declare -A wife | ||
+ | $ wife[fred]=wilma | ||
+ | $ wife[barney]=betty | ||
+ | $ dump wife | ||
+ | wife[fred] = wilma | ||
+ | wife[barney] = betty | ||
+ | $ | ||
</ | </ |