get_next_array_index
Function description
The get_next_array_index function, as its name would suggest, may be
used to retrieve the next index of an array.
Function parameters
$1 - The name of the array variable.
$2 - The (optional) starting index. If no starting index is
supplied then the first index shall be returned.
Function output
The get_next_array_index function will send the next array index, if
any, to stdout. If no next index is available then nothing will be sent to
stdout.
A successful call to the get_next_array_index function will return in a
zero return value. Errors, including no index available, will be indicated by a non-zero exit status.
Example use
Listing 1
#! /bin/bash
# We rely on some functions from hacking-bash.sh
[[ -r ${HACKING_BASH_LIB_PATH:=/usr/lib/hacking-bash.sh} ]] && \
source ${HACKING_BASH_LIB_PATH} || \
{ echo "Unable to find ${HACKING_BASH_LIB_PATH}"; exit 1; }
# Create an example array
ARRAY[0]="zero"
ARRAY[1]="one"
ARRAY[4]="four"
# Get the first array index (0)
get_next_array_index ARRAY
# Get the third array index (4)
get_next_array_index ARRAY 1
# Test that the last index is less than or equal to 4
get_next_array_index ARRAY 4 || echo "No index after 4"
max@lisa ~ $ ./get_next_array_index.sh0
4
No index after 4