/home/alex/dev/lib/array.sh (1)

From RaySoft
# ------------------------------------------------------------------------------
# array.sh
# ========
#
# Project   Library
# Scope     Native
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------
#
# WARNING: This library uses functions from the 'nf.sh' library!
#
# ------------------------------------------------------------------------------

# Exit if the library is run as a script and not sourced as a library
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  echo "Don't run this script! Just source it."
  exit 1
fi

# Return if the library is already loaded
if declare -F 'array' >'/dev/null' 2>&1; then
  return 0
fi

# Returns if a needed library is not loaded
for lib in 'nf'; do
  if ! declare -F "${lib}" >'/dev/null' 2>&1; then
    echo "The needed library '${lib}.sh' is not loaded!"
    return 1
  fi
done

# ------------------------------------------------------------------------------

array() {
  # Shows all function of this library.
  #
  # Arguments:
  #   none
  #
  # Returns:
  #   0:  Success
  #   >0: Error

  nf::print_heading '-' 'ba' "My 'Array' Library"

  compgen -c 'array::'

  echo

  return 0
}

# ------------------------------------------------------------------------------

array::insert() {
  # Insert one or many elements into a Bash array.
  #
  # Arguments:
  #   $1: Name of array
  #   $2: Position to insert the new element(s)
  #   $@: New element(s)
  #
  # Returns:
  #   0:  Success
  #   >0: Error

  local -n array="$1"; shift
  local pos="$1"; shift

  if [[ -z "${array}" ]]; then
    nf::carp 'Error finding array!'
    return 1
  fi

  if [[ $# -lt 1 || ${pos} -lt 0 || ${pos} -gt ${#array[@]} ]]; then
    echo "Usage: ${FUNCNAME[0]} ARRAY POSITION(0-${#array[@]}) VALUE [VALUE ...]"
    return 1
  fi

  array=("${array[@]:0:${pos}}" "$@" "${array[@]:${pos}}")

  return 0
}

# ------------------------------------------------------------------------------

array::remove() {
  # Remove one element from a Bash array.
  #
  # Arguments:
  #   $1: Name of array
  #   $2: Position of the element to be removed
  #
  # Returns:
  #   0:  Success
  #   >0: Error

  local -n array="$1"; shift
  local pos="$1"; shift

  if [[ -z "${array}" ]]; then
    nf::carp 'Error finding array!'
    return 1
  fi

  if [[ $# -gt 0 || ${pos} -lt 0 || ${pos} -ge ${#array[@]} ]]; then
    echo "Usage: ${FUNCNAME[0]} ARRAY POSITION(0-$((${#array[@]} - 1)))"
    return 1
  fi

  array=("${array[@]:0:$pos}" "${array[@]:$((pos + 1))}")

  return 0
}

# ------------------------------------------------------------------------------

return 0

Usage

NOTE:
This script uses functions from the libraries ~/dev/lib/nf.sh.
Load library
# Path to the global libraries
XX_DE_GLOBAL_LIB="${HOME}/dev/lib"

# Global libraries to be loaded
XX_GLOBAL_LIBS=('array' 'nf')
# Test if library path is available
if [[ ! -d "${XX_DE_GLOBAL_LIB}" ]]; then
  echo "Error finding directory: ${XX_DE_GLOBAL_LIB}!"
  exit 1
fi

# Load libraries
for lib in "${XX_GLOBAL_LIBS[@]}"; do
  lib="${XX_DE_GLOBAL_LIB}/${lib}.sh"

  if [[ ! -f "${lib}" ]]; then
    echo "Error finding library: ${lib}!"
    exit 1
  fi

  if ! source "${lib}"; then
    exit 1
  fi
done
Insert into array (array::insert)
NOTE:
Array indexing always start with 0.
test=('alpha' 'bravo' 'charlie' 'delta' 'echo' 'foxtrot')

echo "${test[@]}"

array::insert 'test' 3 'yankee' 'zulu'

echo "${test[@]}"

Output:

alpha bravo charlie delta echo foxtrot
alpha bravo charlie yankee zulu delta echo foxtrot
Remove from array (array::remove)
NOTE:
Array indexing always start with 0.
test=('alpha' 'bravo' 'charlie' 'delta' 'echo' 'foxtrot')
 
echo "${test[@]}"
 
array::remove 'test' 3
 
echo "${test[@]}"

Output:

alpha bravo charlie delta echo foxtrot
alpha bravo charlie echo foxtrot