Empty variable detection (Bash): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(Nieuwe pagina aangemaakt met 'https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/') |
|||
(2 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 1: | Regel 1: | ||
− | https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/ | + | == Variables == |
+ | |||
+ | <pre> | ||
+ | # 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 | ||
+ | </pre> | ||
+ | |||
+ | == Arrays == | ||
+ | |||
+ | With <code>#</code>, the length of the array is returned, like in <code>echo ${#myarray[@]}</code>. That's how these examples work: | ||
+ | |||
+ | <pre> | ||
+ | # | ||
+ | # 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 | ||
+ | </pre> | ||
+ | |||
+ | == Sources == | ||
+ | |||
+ | * https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/ |
Huidige versie van 29 sep 2022 om 07:44
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