Skip to content

Variables

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/01-variables/01-variables.lab.sh).

Follow the instructions in the file to solve the lab.

# This function accepts a "string" of characters passed to the function as input
# An "argument" or "parameter" passed to a function gets a positional parameter variable
# This function should "return" with a "return code" of "2" if the string is empty
sobash_assert_variable_is_not_empty() {

  local string

  string="$1"

  # Replace "$what_goes_here" with the appropriate content
  if [[ "$what_goes_here" == "" ]]; then
    echo -e "\n[FAIL] String is empty." 1>&2
    return 2
  else
    echo -e "\n[INFO] String is not empty."
    return 0
  fi

}


# This function should "return" with a "return code" of "2" if the string is NOT empty
sobash_assert_variable_is_empty() {

  local string

  string="$1"

  # Replace "??" below with the appropriate comparison operator
  if [[ "$string" ?? "" ]]; then
    echo -e "\n[INFO] String is not empty." 1>&2
    return 2
  else
    echo -e "\n[INFO] String is empty."
    return 0
  fi

}

Test

This is the original contents of the test file (../labs/01-variables/01-variables.test.sh).

Review the test cases and your lab file.

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

#!/usr/bin/env bash

test_assert_variable_is_not_empty() {

   declare MY_NOT_EMPTY_VARIABLE
   MY_NOT_EMPTY_VARIABLE="this variable has a value"
   export MY_NOT_EMPTY_VARIABLE
   ../build/sobash assert-variable-is-not-empty MY_NOT_EMPTY_VARIABLE
   assertEquals 0 $?

}


test_assert_variable_is_empty() {

   # No $1 passed to function
   ../build/sobash assert-variable-is-empty
   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 01-variables

Solution

This is the original contents of the solution file (../labs/01-variables/01-variables.solution.sh).

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

sobash_assert_variable_is_not_empty() {

  local string

  string="$1"

  if [[ "$string" == "" ]]; then
    echo -e "\n[FAIL] String is empty." 1>&2
    return 2
  else
    echo -e "\n[INFO] String is not empty."
    return 0
  fi

}

sobash_assert_variable_is_empty() {

  local string

  string="$1"

  if [[ "$string" != "" ]]; then
    echo -e "\n[INFO] String is not empty." 1>&2
    return 2
  else
    echo -e "\n[INFO] String is empty."
    return 0
  fi

}

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

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

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

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