ThinkThen

Beatles Bench pages
Scripts
What Jev knows
Tune your bar
Beatles Bench · Scripts

Bash runs on judgment.

The talk's slide: Bash runs on judgment.
Open the slide at full size.

The script reads one song per line on standard input and asks whether a Beatle wrote each one. It passes the question and its flags on the command line. --true and --false say what yes and no mean. --threshold 0.2:0.8 sets the band. A probability of 0.8 or more is yes, and one under 0.2 is no. Between them is not sure. The two --field pointers send the title and the album as one JSON object. Without them, decide sends the whole line as text. The previous page keeps the same question in a file. The script plays a yes, skips a no, and hands a not sure to a person.

decide --quiet prints nothing. Its exit code is the answer: 0 for yes, 1 for no, and 3 for not sure. The function is_original names what the code means. code=$? names the code, and case picks the step. Any other code is a failure, and the script stops. Handle not sure teaches the same form.

The numbers come from the bench's songwriter list.

setlist.sh, the script
is_original() {
  thinkthen decide 'Did a Beatle write this song?' \
    --true 'John, Paul, George, or Ringo wrote it.' \
    --false 'Someone else wrote it. It is a cover.' \
    --threshold 0.2:0.8 --quiet \
    --field /title --field /album
}
while read -r song; do
  title=$(printf '%s\n' "$song" | jq -r .title)
  printf '%s\n' "$song" | is_original
  code=$?
  case $code in
    0) echo "play   $title" ;;
    1) echo "skip   $title" ;;
    3) echo "check  $title" ;;
    *) exit "$code" ;;
  esac
done
setlist.jsonl, the songs, one JSON record per line
{"title":"Something","album":"Abbey Road"}
{"title":"Act Naturally","album":"Help!"}
{"title":"Words of Love","album":"Beatles for Sale"}
{"title":"Mr. Moonlight","album":"Beatles for Sale"}

Run it

The run feeds setlist.jsonl to the script. Two songs play, one is skipped, and one goes to a person.
bash setlist.sh < setlist.jsonl
Output
play   Something
skip   Act Naturally
play   Words of Love
check  Mr. Moonlight
exit 0

The lesson

Two of the three sure answers are right. Jev says a Beatle wrote Words of Love. Buddy Holly wrote it. Mr. Moonlight falls inside the band, and a person checks it. Roy Lee Johnson wrote it.

The exit code is the answer, and an ordinary case acts on it.