Skip to content

Example

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/00-example/00-example.lab.sh).

Follow the instructions in the file to solve the lab.

sobash_example_lab() {

  number=5

  # Replace the value of the "number" variable with a number that makes the statement below true
  if [[ "2" == "$number" ]]; then
    echo "this statement is true."
  else
    echo "this statement is false."
  fi

}

Test

This is the original contents of the test file (../labs/00-example/00-example.test.sh).

Review the test cases and your lab file.

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

test_statement_is_true() {
  output="$(../build/sobash example-lab)"
  assertEquals "this statement is true." "$output"
}

source ./shunit2

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

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

Solution

This is the original contents of the solution file (../labs/00-example/00-example.solution.sh).

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

sobash_example_lab() {

  number=2

  # Replace the value of the "number" variable with a number that makes the statement below true
  if [[ "2" == "$number" ]]; then
    echo "this statement is true."
  else
    echo "this statement is false."
  fi

}

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

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

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

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