Skip to content

Strings

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/06-strings/06-strings.lab.sh).

Follow the instructions in the file to solve the lab.

# Assume the following string is passed to this function:
#
#   /usr/bin/cowabunga-time-my-3-amigos.sh
#
sobash_silly_string() {

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | No options passed."
    return 3
  fi

  local word
  local first_letter
  local last_letter

  word="$1"

  # Remove any directories from path, leaving only the filename
  # word="${word##*/}"

  # Remove all characters besides the letters.
  # Uncomment only one of the next two lines.
  # word="${word//[^a-zA-Z]/}"
  # word="${word//[a-zA-Z]/}"

  # Assign only the first letter of word to "first_letter" variable
  # Uncomment only one of the next three lines
  # first_letter="${word:0:1}"
  # first_letter="${word:1}"
  # first_letter="${word:1:1}"


  # Assign only the last letter of word to "last_letter" variable
  # Uncomment only one of the next three lines
  # last_letter="${word:0: -1}"
  # last_letter="${word: -1}"
  # last_letter="${word:1: -1}"


  # Make the word lowercase
  # word="${word,,}"
  # word="${word^^}"

  echo "${first_letter^^}${word:1: -1}${last_letter^^}"



}

Test

This is the original contents of the test file (../labs/06-strings/06-strings.test.sh).

Review the test cases and your lab file.

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

#!/usr/bin/env bash

test_assert_parsed_options_are_valid() {

    output="$(../build/sobash silly-string /usr/bin/cowabunga-time-my-3-amigos.sh)"
   assertEquals "$output" "CowabungatimemyamigossH"

}

source ./shunit2

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

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

Solution

This is the original contents of the solution file (../labs/06-strings/06-strings.solution.sh).

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

# Assume the following string is passed to this function:
#
#   /usr/bin/cowabunga-time-my-3-amigos.sh
#
sobash_silly_string() {

  if [[ "$#" == "0" ]]; then
    echo -e "\n[FAIL] ${FUNCNAME[0]} | No options passed."
    return 3
  fi

  local word
  local first_letter
  local last_letter

  word="$1"

  # Remove any directories from path, leaving only the filename
  word="${word##*/}"

  # Remove all characters besides the letters.
  # Uncomment only one of the next two lines.
  word="${word//[^a-zA-Z]/}"
  # word="${word//[a-zA-Z]/}"

  # Assign only the first letter of word to "first_letter" variable
  # Uncomment only one of the next three lines
  first_letter="${word:0:1}"
  # first_letter="${word:1}"
  # first_letter="${word:1:1}"


  # Assign only the last letter of word to "last_letter" variable
  # Uncomment only one of the next three lines
  # last_letter="${word:0: -1}"
  last_letter="${word: -1}"
  # last_letter="${word:1: -1}"


  # Make the word lowercase
  word="${word,,}"
  # word="${word^^}"

  echo "${first_letter^^}${word:1: -1}${last_letter^^}"



}

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

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

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

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