🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of binaryphile

binaryphile's solution

to Two Fer in the Bash Track

Published at Sep 15 2018 · 0 comments
Test suite

Two-fer or 2-fer is short for two for one. One for you and one for me.

Given a name, return a string with the message:

One for name, one for me.

Where "name" is the given name.

However, if the name is missing, return the string:

One for you, one for me.

Here are some examples:

Name String to return
Alice One for Alice, one for me.
Bob One for Bob, one for me.
One for you, one for me.
Zaphod One for Zaphod, one for me.

Run the tests with:

bats two_fer_test.sh

After the first test(s) pass, continue by commenting out or removing the [[ $BATS_RUN_SKIPPED == true ]] || skip annotations prepending other tests.

To run all tests, including the ones with skip annotations, run:

BATS_RUN_SKIPPED=true bats two_fer_test.sh



External utilities

Bash is a language to write "scripts" -- programs that can call external tools, such as sed, awk, date and even programs written in other programming languages, like Python. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installation of third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure Bash, without using any external tools. Note that there are some types of problems that bash cannot solve, such as performing floating point arithmetic and manipulating dates: for those, you must call out to an external tool.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#!/usr/bin/env bash

# local version:

@test "no name given" {
  #[[ $BATS_RUN_SKIPPED == "true" ]] || skip

  # The above line controls whether to skip the test.
  # Normally, we skip every test except for the first one
  # (the first one is always commented out).  This allows for
  # a person to focus on solving a test at a time: you can
  # comment out or delete the
  # `[[ $BATS_RUN_SKIPPED == "true" ]] || skip`
  # line to run the test when you are ready.
  # You can also run all the tests by setting the
  # `$BATS_RUN_SKIPPED` environment variable, like this:
  #     $ BATS_RUN_SKIPPED=true bats two_fer_test.sh

  run bash two_fer.sh
  (( status == 0 ))
  [[ $output == "One for you, one for me." ]]

@test "a name given" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash two_fer.sh Alice
  (( status == 0 ))
  [[ $output == "One for Alice, one for me." ]]

@test "another name given" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash two_fer.sh Bob
  (( status == 0 ))
  [[ $output == "One for Bob, one for me." ]]

# bash-specific test: Focus the student's attention on the effects of
# word splitting and filename expansion:
# https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions

@test "handle arg with spaces" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash two_fer.sh "John Smith" "Mary Ann"
  (( status == 0 ))
  [[ $output == "One for John Smith, one for me." ]]

@test "handle arg with glob char" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash two_fer.sh "*"
  (( status == 0 ))
  [[ $output == "One for *, one for me." ]]
#!/usr/bin/env bash

# http://www.binaryphile.com/bash/2018/07/26/approach-bash-like-a-developer-part-1-intro.html

NL=$'\n'        # NL is newline
IFS=$NL         # don't require quotes on normal string vars
set -o noglob   # don't glob, allowing boolean? function names

shopt -s expand_aliases                   # aliases can operate within the scope of the caller, seeing its
alias args_include?='include? "$*"'       # args_include? tests if the caller's arguments include a particular value
alias fewer_args_than?='fewer_than? $#'   # fewer_args_than? tests if the caller has fewer arguments than a particular value

# get saves stdin into the variable named by the argument
# stdin is usually a heredoc.  get strips the leading indentation
# from all lines.
get () {
  local ref=$1 indent

  ! read -rd '' $ref
  printf -v $ref %s "${!ref#$indent}"
  printf -v $ref %s "${!ref//$NL$indent/$NL}"

Prog=${0##*/}   # our invocation name, from the command line

  $Prog: Output "One for NAME, one for me."


    $Prog NAME

  NAME defaults to "you".

main () {
  two_fer Result ${1:-}  # return variables such as Result are always capitalized
  echo $Result

# die ends the script
# die prints the first argument, if supplied.
# die sets the script's return code to the second argument, if supplied.
# Otherwise the return code is the status when die was called.
die () {
  local rc=$?   # aside from main, variables are always declared local

  present? ${2:-}   && rc=$2
  present? "${1:-}" && echo "$1"  # the message is allowed to have IFS (newline) in it, so it needs quotes
  exit $rc

# fewer_than? returns true if the first argument is less than the second
fewer_than? () {
  (( $1 < $2 ))

# include? returns true if the second argument is in the first
# The first argument is an IFS-delimited string of items.
# The second is matched only if there is an exactly matching item in the first arg.
include? () {
  [[ "$IFS$1$IFS" == *"$IFS$2$IFS"* ]]

# present? returns true if the argument has non-whitespace content
present? () {
  [[ -n ${1:-} ]]

# sourced? returns true if the calling script was loaded with the "source" command
sourced? () {
  [[ ${FUNCNAME[1]} == source ]]

# enabling strict_mode stops the script on errors and unset variable references
strict_mode () {
  case $1 in
    on  ) set -eu;;
    off ) set +eu;;

# two_fer sets the variable reference in the first argument to the message
# The second argument is the name used.  The name defaults to "you".
two_fer () {
  printf -v $1 "One for %s, one for me." ${2:-you}

# usage prints the USAGE message and exits
# usage takes an argument as an error message to print before USAGE.
# usage sets the exit code to 2 if an error message is supplied, otherwise to 0.
usage () {
  local rc=0

  present? ${1:-} && {
    echo "$1$NL"
  die "$USAGE" $rc

sourced? && return  # stop here so script can be sourced as a library without running
strict_mode on

# The tests below use || to indicate control flow change (exit script).
# They are guards (assertions) which must evaluate true in order to continue normally.
! args_include? --help  || usage
fewer_args_than? 2      || usage "Error: wrong number of arguments"

main $*

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?