Last active
June 24, 2024 23:13
-
-
Save JTBrinkmann/dddb7d7aea0b05ea630f56da567f28f8 to your computer and use it in GitHub Desktop.
bash destructuring assignment for arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Alternative Versions | |
# only allows space separated variable name list | |
# example: destruct a b c = arr | |
destruct() { | |
for ((i=1;i < $#-1; ++i)) do | |
local value_adress="${!#}[$i-1]" | |
declare -g "${!i}=${!value_adress}" | |
done | |
} | |
# only allows comma separated variable name list | |
# example: destruct a,b,c = arr | |
destruct() { | |
local var_names=(${1//,/ }) | |
for ((i=0; i < ${#var_names[@]}; i++)) do | |
local value_adress="$3[$i]" | |
declare -g "${var_names[$i]}=${!value_adress}" | |
done | |
} | |
# only allows compact form | |
# example: destruct a,b,c=arr | |
destruct() { | |
local var_names="${1%%=*}" | |
var_names=(${var_names//,/ }) | |
for ((i=0; i < ${#var_names[@]}; i++)) do | |
local value_adress="${1#*=}[$i]" | |
declare -g "${var_names[$i]}=${!value_adress}" | |
done | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# destructuring assignment allows you to extract individual items from | |
# an array and place them into variables using a shorthand syntax | |
# | |
# allows mixed comma and space separated variable name list | |
# | |
# note: | |
# do not use consecutive commas do not skip over values! | |
# good: destruct a, _, c = arr | |
# bad: destruct a,,c = arr | |
# | |
# the following behave equally: | |
# destruct a b c d = arr | |
# destruct a, b, c, d = arr | |
# destruct a,,b c, d = arr | |
# | |
# example: | |
# connection_config=(127.0.0.1 3306) | |
# destruct host, port = connection_config | |
# | |
# if more variable names are specified than the array is long, | |
# remaining variables will be set to an empty string | |
# | |
# author: jtbrinkmann | |
## short form | |
destruct() { | |
local variable_names=${@:1:$#-2} | |
variable_names=(${variable_names//,/ }) | |
for ((i=0; i < ${#variable_names[@]}; i++)) do | |
local value_name="${!#}[$i]" | |
declare -g "${var_names[$i]}=${!value_name}" | |
done | |
} | |
## documented code | |
# adhering to the Google styleguide for shell scripts | |
# https://google.github.io/styleguide/shellguide.html | |
####################################### | |
# destructuring assignment for arrays | |
# Arguments: | |
# variable names to assign to (mixed comma/space separated or mixed) | |
# equal sign | |
# array variable name | |
# Outputs: | |
# if 2nd to last argument isn't an equal sign, | |
# an error message is written and the function will stop | |
####################################### | |
destruct() { | |
local equal_sign_parameter="${@:$#-1:1}" | |
local array_name="${!#}" | |
if [ "=" != "$equal_sign_parameter" ]; then | |
echo "incorrect usage" >&2 | |
echo "usage: destruct [variable_name]... = array_variable_name" >&2 | |
echo "" >&2 | |
echo "example: destruct a b c = sample_array" >&2 | |
echo "example: destruct a,b,c = sample_array" >&2 | |
echo "example: destruct a, b, c = sample_array" >&2 | |
false | |
else | |
# get all variable names (every parameter except the last 2) | |
local variable_names=${@:1:$#-2} | |
# split comma separated names | |
variable_names=(${variable_names//,/ }) | |
for ((i=0; i < ${#variable_names[@]}; i++)) do | |
# to get the array's value at the current index, we must first | |
# build a string with the array variable's name and the index | |
local value_adress="$array_name[$i]" | |
# and then resolve it | |
local value="${!value_adress}" | |
# and set the variable to that value | |
local variable_name="${variable_names[$i]}" | |
declare -g "$variable_name=$value" | |
done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment