From 2c8ab3825d321561c22e4759ed7d70d9bc50877e Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Tue, 9 Jun 2015 13:28:35 -0400 Subject: [PATCH] added support for showing arrays and aliases to the var method, but array elements are not distinguished yet by internal quoting (so it can be confusing if array had elements with spaces). but it looks pretty good and basically works. --- scripts/core/functions.sh | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/scripts/core/functions.sh b/scripts/core/functions.sh index c38bc7f4..d098f8e7 100644 --- a/scripts/core/functions.sh +++ b/scripts/core/functions.sh @@ -34,19 +34,48 @@ if [ -z "$skip_all" ]; then cd "$1" } + function is_array() { + [[ "$(declare -p $1)" =~ "declare -a" ]] + } + + function is_alias() { + alias $1 &>/dev/null + return $? + } + # displays the value of a variable in bash friendly format. function var() { + HOLDIFS="$IFS" + IFS="" while true; do local varname="$1"; shift if [ -z "$varname" ]; then break fi - if [ -z "${!varname}" ]; then + + if is_alias "$varname"; then +#echo found $varname is alias + local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)" + alias $varname | sed -e 's/.*=//' >$tmpfile + echo "alias $varname=$(cat $tmpfile)" + \rm $tmpfile + elif [ -z "${!varname}" ]; then echo "$varname undefined" else - echo "$varname=${!varname}" + if is_array "$varname"; then +#echo found $varname is array var + local temparray + eval temparray="(\${$varname[@]})" + echo "$varname=(${temparray[@]})" +#hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly +# see ones that have spaces in them. + else +#echo found $varname is simple + echo "$varname=${!varname}" + fi fi done + IFS="$HOLDIFS" } function success_sound() -- 2.34.1