Skip to content

Arrays

Note: You can use the 'Copy to clipboard' icon to copy any code block to reset any file at any time.

Lab

This is the original contents of the lab file (../labs/03-arrays/03-arrays.lab.sh).

Follow the instructions in the file to solve the lab.

# Assume "LAB03A_INDEXED_ARRAY" is passed as the "$1" positional parameter
LAB03A_INDEXED_ARRAY=( "zero" "one" "two" "three" )
LAB03A_INDEXED_ARRAY+=( "four" )
LAB03A_INDEXED_ARRAY[6]="six"
LAB03A_INDEXED_ARRAY+=( "mystery" )
export LAB03A_INDEXED_ARRAY

# Assume "LAB03A_INDEX_NUMBER" is passed as the "$2" positional parameter
# Figure out which index number to set below to make the function return 0.
LAB03A_INDEX_NUMBER=""
export LAB03A_INDEX_NUMBER

# Assume "LAB03A_INDEX_VALUE" is passed as the "$3" positional parameter
LAB03A_INDEX_VALUE="mystery"
export LAB03A_INDEX_VALUE

sobash_assert_index_array_element_contains_value() {

  local -n my_indexed_array
  local -n index_number
  local -n index_value

  my_indexed_array="$1"
  index_number="$2"
  index_value="$3"

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Failed to pass arguments to function."
    return 1
  fi

  if [[ "${my_indexed_array[$index_number]}" != "${index_value}" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Value (${my_indexed_array[$index_number]}) from array element (${index_number}) does not match provided index value (${index_value})." 1>&2
    return 2
  else
    echo -e "\n[INFO] ${FUNCNAME[0]} | Value (${my_indexed_array[$index_number]}) from array element (${index_number}) does match provided index value (${index_value})."
    return 0
  fi

}

# Assume "LAB03_ASSOCIATIVE_ARRAY" is passed as the "$1" positional parameter
declare -A LAB03B_ASSOCIATIVE_ARRAY
LAB03B_ASSOCIATIVE_ARRAY=( [zero]="zero" [one]="one" [two]="two" [three]="three" )
LAB03B_ASSOCIATIVE_ARRAY+=( [four]="four" )
LAB03B_ASSOCIATIVE_ARRAY[six]="six"

# Figure out if this last type of assignment sets the element key, value, or both,
# and then update LAB03B_ELEMENT_KEY and LAB03B_ELEMENT_VALUE, as needed.
# shellcheck disable=SC2190
LAB03B_ASSOCIATIVE_ARRAY+=( "seven" )
export LAB03B_ASSOCIATIVE_ARRAY

# Assume "LAB0B_ELEMENT_KEY" is passed as the "$2" positional parameter
LAB03B_ELEMENT_KEY="seven"
export LAB03B_ELEMENT_KEY

# Assume "LAB03A_ELEMENT_VALUE" is passed as the "$3" positional parameter
LAB03B_ELEMENT_VALUE="seven"
export LAB03B_ELEMENT_VALUE

sobash_assert_associative_array_element_contains_value() {

  local -n my_associative_array
  local -n element_key
  local -n element_value

  my_associative_array="$1"
  element_key="$2"
  element_value="$3"

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Failed to pass arguments to function."
    return 1
  fi

  if [[ "${my_associative_array[$element_key]}" != "${element_value}" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Value (${my_associative_array[$element_key]}) from array element (${element_key}) does not match provided element value (${element_value})." 1>&2
    return 2
  else
    echo -e "\n[INFO] ${FUNCNAME[0]} | Value (${my_associative_array[$element_key]}) from array element (${element_key}) does match provided element value (${element_value})."
    return 0
  fi

}

Test

This is the original contents of the test file (../labs/03-arrays/03-arrays.test.sh).

Review the test cases and your lab file.

Will the lab file (../labs/03-arrays/03-arrays.lab.sh) pass all test cases?

#!/usr/bin/env bash

test_assert_indexed_array_element_contains_value() {

   ../build/sobash assert-index-array-element-contains-value LAB03A_INDEXED_ARRAY LAB03A_INDEX_NUMBER LAB03A_INDEX_VALUE
   assertEquals 0 $?

}

test_assert_associative_array_element_contains_value() {

   ../build/sobash assert-associative-array-element-contains-value LAB03B_ASSOCIATIVE_ARRAY LAB03B_ELEMENT_KEY LAB03B_ELEMENT_VALUE
   assertEquals 0 $?

}

source ./shunit2

When ready, run the following to execute the tests above against the lab file:

cd "${HOME}/bash-programming/src/" \ 
  && ./test.sh lab 03-arrays

Solution

This is the original contents of the solution file (../labs/03-arrays/03-arrays.solution.sh).

This file contains an example of a viable solution that passes all the test cases.

# Assume "LAB03A_INDEXED_ARRAY" is passed as the "$1" positional parameter
LAB03A_INDEXED_ARRAY=( "zero" "one" "two" "three" )
LAB03A_INDEXED_ARRAY+=( "four" )
LAB03A_INDEXED_ARRAY[6]="six"
LAB03A_INDEXED_ARRAY+=( "mystery" )
export LAB03A_INDEXED_ARRAY

# Assume "LAB03A_INDEX_NUMBER" is passed as the "$2" positional parameter
LAB03A_INDEX_NUMBER="7"
export LAB03A_INDEX_NUMBER

# Assume "LAB03A_INDEX_VALUE" is passed as the "$3" positional parameter
LAB03A_INDEX_VALUE="mystery"
export LAB03A_INDEX_VALUE

sobash_assert_index_array_element_contains_value() {

  local -n my_indexed_array
  local -n index_number
  local -n index_value

  my_indexed_array="$1"
  index_number="$2"
  index_value="$3"

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Failed to pass arguments to function."
    return 1
  fi

  if [[ "${my_indexed_array[$index_number]}" != "${index_value}" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Value (${my_indexed_array[$index_number]}) from array element (${index_number}) does not match provided index value (${index_value})." 1>&2
    return 2
  else
    echo -e "\n[INFO] ${FUNCNAME[0]} | Value (${my_indexed_array[$index_number]}) from array element (${index_number}) does match provided index value (${index_value})."
    return 0
  fi

}

# Assume "LAB03_ASSOCIATIVE_ARRAY" is passed as the "$1" positional parameter
declare -A LAB03B_ASSOCIATIVE_ARRAY
LAB03B_ASSOCIATIVE_ARRAY=( [zero]="zero" [one]="one" [two]="two" [three]="three" )
LAB03B_ASSOCIATIVE_ARRAY+=( [four]="four" )
LAB03B_ASSOCIATIVE_ARRAY[six]="six"

# shellcheck disable=SC2190
LAB03B_ASSOCIATIVE_ARRAY+=( "seven" )
export LAB03B_ASSOCIATIVE_ARRAY

# Assume "LAB0B_ELEMENT_KEY" is passed as the "$2" positional parameter
LAB03B_ELEMENT_KEY="seven"
export LAB03B_ELEMENT_KEY

# Assume "LAB03A_ELEMENT_VALUE" is passed as the "$3" positional parameter
LAB03B_ELEMENT_VALUE=""
export LAB03B_ELEMENT_VALUE

sobash_assert_associative_array_element_contains_value() {

  local -n my_associative_array
  local -n element_key
  local -n element_value

  my_associative_array="$1"
  element_key="$2"
  element_value="$3"

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Failed to pass arguments to function."
    return 1
  fi

  if [[ "${my_associative_array[$element_key]}" != "${element_value}" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | Value (${my_associative_array[$element_key]}) from array element (${element_key}) does not match provided element value (${element_value})." 1>&2
    return 2
  else
    echo -e "\n[INFO] ${FUNCNAME[0]} | Value (${my_associative_array[$element_key]}) from array element (${element_key}) does match provided element value (${element_value})."
    return 0
  fi

}

Run the following to execute the tests above against the solution file:

cd "${HOME}/bash-programming/src/" \ 
  && ./test.sh solution 03-arrays

Feel free to run the following to override the contents of the lab file with the solution file.

cd "${HOME}/bash-programming/labs/" \ 
  && cp ./03-arrays/03-arrays.{solution,lab}.sh