Skip to content

Instantly share code, notes, and snippets.

@fadookie
Last active August 2, 2017 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadookie/4a08de38784dfdaa1f31952c1792d4dd to your computer and use it in GitHub Desktop.
Save fadookie/4a08de38784dfdaa1f31952c1792d4dd to your computer and use it in GitHub Desktop.
Example of bash array to pipe-delimited string de/serialization, for discussion at https://discuss.bitrise.io/t/output-from-multiple-instances-of-the-same-step/538
# WARNING! The following is not as safe as I originally thought and I'm not sure yet the right way to do this.
# See https://unix.stackexchange.com/questions/383541/how-to-save-restore-all-shell-options-including-errexit
enable_safety() {
BITRISE_CLI_PREVIOUS_SHELL_OPTIONS=$(set +o)
set -o nounset
set -o errexit
set -o pipefail
}
disable_safety() {
eval "$BITRISE_CLI_PREVIOUS_SHELL_OPTIONS"
unset BITRISE_CLI_PREVIOUS_SHELL_OPTIONS
}
##
# Takes a pipe-delimited string and returns a bash array.
# Sets the array to global varaible BITRISE_CLI_LAST_PARSED_LIST
# Make a copy between calls so it doesn't get overwritten
# ex. list_to_array "some|list"
# list1=("${BITRISE_CLI_LAST_PARSED_LIST[@]}")
##
list_to_array () {
enable_safety
IFS='|' read -a BITRISE_CLI_LAST_PARSED_LIST <<< "$1"
disable_safety
}
##
# Converts arguments (such as an array) to a pipe-delimited string
# ex. output_list=$(to_list "${list1[@]}")
##
to_list () {
enable_safety
local IFS='|'
echo "$*"
disable_safety
}
#!/bin/bash
source ./bash_list_library.sh
list_to_array "hello| this is|a serialized |list"
list1=("${BITRISE_CLI_LAST_PARSED_LIST[@]}")
list_to_array "this|is|another|list"
list2=("${BITRISE_CLI_LAST_PARSED_LIST[@]}")
for i in "${list1[@]}"; do
echo "list1:'$i'"
done
echo
for i in "${list2[@]}"; do
echo "list2:'$i'"
done
echo
output_array=(output array "foo 1" "bar two")
output_list=$(to_list "${output_array[@]}")
echo "output_list='$output_list'"
Copyright 2017 Eliot Lash
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment