Empty variable detection (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Variables

# Initialisation
########################################
#
echo ""; echo ""; echo "### rename_slugs_and_titles_image_wizard_pages()..."; echo ""
if [ -z "$path" ]; then echo "	Variable 'path' not provided. Exiting"; exit; fi
cd $path

Arrays

With #, the length of the array is returned, like in echo ${#myarray[@]}. That's how these examples work:

#
# Test at start of script
########################################
#
if (( ${#tr_f[@]} == 0 )); then
	echo "Before unset - Empty"
else
	echo "Before unset - Not empty"
fi


# Unset + test
########################################
#
unset tr_f
#
#
if (( ${#tr_f[@]} == 0 )); then
	echo "After unset, before declare - Empty"
else
	echo "After unset, before declare - Not empty"
fi



# Declare + test
########################################
#
declare -A tr_f
i=0


# Fill array
########################################
#
((i++))
	tr_f[$i,1]="One"
	tr_f[$i,2]="Einz"
((i++))
	tr_f[$i,1]="Two"
	tr_f[$i,2]="Zwei"


# Last test
########################################
#
if (( ${#tr_f[@]} == 0 )); then
	echo "After assigning values - Empty"
else
	echo "After assigning values - Not empty"
fi

Sources