Namerefs
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/02-namerefs/02-namerefs.lab.sh).
Follow the instructions in the file to solve the lab.
sobash_assert_nameref_variable_is_not_empty() {
# The "-n" option of the "local" builtin acts similar a "pointer" in C
# Rather than expecting a string and storing a string,
# it expects the name of another variable,
# and it "points to" the value of that variable without restoring it.
# In Bash, this is called a "name reference" to another variable.
local -n variable_value
local variable_name
# Based on your understanding of name references,
# what goes in place of "$_" below to make the "if" statement return 0?
variable_value="$_"
variable_name="$1"
if [[ "$variable_value" == "" ]]; then
echo -e "\n[FAIL] Variable ($variable_name) is empty." 1>&2
return 2
else
echo -e "\n[INFO] Variable ($variable_name) is not empty."
return 0
fi
}
# What line is missing from this function to make it work as intended?
# Hint: this function does the opposite of the one above it, but works similarly.
sobash_assert_nameref_variable_is_empty() {
local -n variable_value
local variable_name
variable_name="$1"
if [[ "$variable_value" != "" ]]; then
echo -e "\n[INFO] Variable ($variable_name) is not empty." 1>&2
return 2
else
echo -e "\n[INFO] Variable ($variable_name) is empty."
return 0
fi
}
Test
This is the original contents of the test file (../labs/02-namerefs/02-namerefs.test.sh).
Review the test cases and your lab file.
Will the lab file (../labs/02-namerefs/02-namerefs.lab.sh) pass all test cases?
#!/usr/bin/env bash
test_assert_nameref_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-nameref-variable-is-not-empty MY_NOT_EMPTY_VARIABLE
assertEquals 0 $?
}
test_assert_nameref_variable_is_empty() {
declare MY_EMPTY_VARIABLE
MY_EMPTY_VARIABLE=""
export MY_EMPTY_VARIABLE
../build/sobash assert-nameref-variable-is-empty MY_EMPTY_VARIABLE
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 02-namerefs
Solution
This is the original contents of the solution file (../labs/02-namerefs/02-namerefs.solution.sh).
This file contains an example of a viable solution that passes all the test cases.
sobash_assert_nameref_variable_is_not_empty() {
local -n variable_value
local variable_name
variable_value="$1"
variable_name="$1"
if [[ "$variable_value" == "" ]]; then
echo -e "\n[FAIL] ${FUNCNAME[0]} | Variable ($variable_name) is empty." 1>&2
return 2
else
echo -e "\n[INFO] ${FUNCNAME[0]} | Variable ($variable_name) is not empty."
return 0
fi
}
sobash_assert_nameref_variable_is_empty() {
local -n variable_value
local variable_name
variable_value="$1"
variable_name="$1"
if [[ "$variable_value" != "" ]]; then
echo -e "\n[INFO] ${FUNCNAME[0]} | Variable ($variable_name) is not empty." 1>&2
return 2
else
echo -e "\n[INFO] ${FUNCNAME[0]} | Variable ($variable_name) 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 02-namerefs
Feel free to run the following to override the contents of the lab file with the solution file.
cd "${HOME}/bash-programming/labs/" \
&& cp ./02-namerefs/02-namerefs.{solution,lab}.sh