Avatar of binaryphile

binaryphile's solution

to Two Fer in the Bash Track

Published at Sep 15 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

"One for X, one for me."

When X is a name or "you".

If the given name is "Alice", the result should be "One for Alice, one for me." If no name is given, the result should be "One for you, 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 skip annotations prepending other tests.

Source

https://en.wikipedia.org/wiki/Two-fer

External utilities

Bash is a language to write scripts that works closely with various system utilities, like sed, awk, date and even 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 installing 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.

Submitting Incomplete Solutions

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

two_fer_test.sh

#!/usr/bin/env bash

@test "no name given" {
  #skip
  run bash two_fer.sh
  [ "$status" -eq 0 ]
  [ "$output" == "One for you, one for me." ]
}

@test "a name given" {
  skip
  run bash two_fer.sh Alice
  [ "$status" -eq 0 ]
  [ "$output" == "One for Alice, one for me." ]
}

@test "another name given" {
  skip
  run bash two_fer.sh Bob
  [ "$status" -eq 0 ]
  [ "$output" == "One for Bob, one for me." ]
}
#!/usr/bin/env bash

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

IFS=$'\n'
set -o noglob
shopt -s expand_aliases
alias args_include?='include? "$*"'
alias fewer_args_than?='fewer_than? $#'

get () {
  local ref_=$1 indent_

  ! read -rd '' $ref_
  indent_=${!ref_%%[^[:space:]]*}
  printf -v $ref_ %s "${!ref_#$indent_}"
  printf -v $ref_ %s "${!ref_//$'\n'$indent_/$'\n'}"
}

Prog=${0##*/}

get Usage <<END
  $Prog: Output "One for NAME, one for me."

  Usage:

    $Prog NAME

  NAME defaults to "you".
END

main () {
  two_fer result ${1:-}
  echo $result
}

die () {
  local rc=$?

  present? ${2:-}   && rc=$2
  present? "${1:-}" && echo "$1"
  exit $rc
}

fewer_than? () {
  (( $1 < $2 ))
}

include? () {
  [[ $IFS$1$IFS == *"$IFS$2$IFS"* ]]
}

present? () {
  [[ -n ${1:-} ]]
}

sourced? () {
  [[ ${FUNCNAME[1]} == source ]]
}

strict_mode () {
  case $1 in
    on  ) set -euo pipefail;;
    off ) set +euo pipefail;;
  esac
}

two_fer () {
  printf -v $1 "One for %s, one for me." ${2:-you}
}

usage () {
  local rc=0

  present? ${1:-} && {
    echo "$1$IFS"
    rc=2
  }
  die "$Usage" $rc
}

sourced? && return
strict_mode on

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?