$_ CLI In Depth
Learning progress 0%
Build · Scripting Basics & Exit Codes

Build: A Command-Line Todo App

Build todo.sh in three steps: add and list, then done, then usage and error statuses.

~40 min Advanced 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Add tasks and list them

Build a todo tool in one script, todo.sh, in /workspace. This step covers add and list. The file that stores tasks comes from the TODO_FILE environment variable, and it defaults to todo.txt. add TEXT... appends the words joined by single spaces as one line. list prints each task as N. TEXT, numbered from 1. When the file does not exist, list prints nothing and exits with status 0.

Test it with a temporary TODO_FILE, adding buy milk and call mom, and then listing the tasks.

Step 2: Remove a task with done

Add the done command to todo.sh. done N removes task number N from the file and prints removed: followed by the task text. Tasks are numbered as list shows them, so after task 1 is removed, the task that was number 2 becomes number 1.

Test it by adding two tasks, running done 1, and then listing the file.

Step 3: Report usage and bad task numbers

Finish todo.sh with its error behavior. A missing or unknown command prints usage: todo.sh add TEXT... | list | done N to standard error and exits with status 2. done with a value that is not a positive whole number is a usage error with status 2. done with a number that has no task prints no task N to standard error and exits with status 1.

Test each case: no arguments, the unknown command frob, done 9 when only two tasks exist, and done x. Also confirm that done 1 still works.

Files

  • todo.sh: the finished script.
  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

todo.sh

BASH26 lines
#!/bin/bash
# Usage: todo.sh add TEXT... | list | done N
set -u
file=${TODO_FILE:-todo.txt}
usage() { echo "usage: todo.sh add TEXT... | list | done N" >&2; exit 2; }
cmd=${1:-}
[ $# -gt 0 ] && shift
case $cmd in
  add)
    [ $# -gt 0 ] || usage
    printf '%s\n' "$*" >> "$file" ;;
  list)
    [ -f "$file" ] || exit 0
    n=0
    while IFS= read -r line; do
      n=$((n + 1))
      printf '%d. %s\n' "$n" "$line"
    done < "$file" ;;
  done)
    [[ ${1:-} =~ ^[1-9][0-9]*$ ]] || usage
    text=$(sed -n "${1}p" "$file" 2>/dev/null)
    [ -n "$text" ] || { echo "no task $1" >&2; exit 1; }
    sed -i "${1}d" "$file"
    echo "removed: $text" ;;
  *) usage ;;
esac

walkthrough.sh

BASH70 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Add tasks and list them
cat > /workspace/todo.sh <<&#039;EOF&#039;
file=${TODO_FILE:-todo.txt}
case ${1:-} in
  add)
    shift
    printf &#039;%s\n&#039; "$*" >> "$file" ;;
  list)
    [ -f "$file" ] || exit 0
    n=0
    while IFS= read -r line; do
      n=$((n + 1))
      printf &#039;%d. %s\n&#039; "$n" "$line"
    done < "$file" ;;
esac
EOF

# Step 2: Remove a task with done
cat > /workspace/todo.sh <<&#039;EOF&#039;
file=${TODO_FILE:-todo.txt}
case ${1:-} in
  add)
    shift
    printf &#039;%s\n&#039; "$*" >> "$file" ;;
  list)
    [ -f "$file" ] || exit 0
    n=0
    while IFS= read -r line; do
      n=$((n + 1))
      printf &#039;%d. %s\n&#039; "$n" "$line"
    done < "$file" ;;
  done)
    text=$(sed -n "${2}p" "$file")
    sed -i "${2}d" "$file"
    echo "removed: $text" ;;
esac
EOF

# Step 3: Report usage and bad task numbers
cat > /workspace/todo.sh <<&#039;EOF&#039;
#!/bin/bash
# Usage: todo.sh add TEXT... | list | done N
set -u
file=${TODO_FILE:-todo.txt}
usage() { echo "usage: todo.sh add TEXT... | list | done N" >&2; exit 2; }
cmd=${1:-}
[ $# -gt 0 ] && shift
case $cmd in
  add)
    [ $# -gt 0 ] || usage
    printf &#039;%s\n&#039; "$*" >> "$file" ;;
  list)
    [ -f "$file" ] || exit 0
    n=0
    while IFS= read -r line; do
      n=$((n + 1))
      printf &#039;%d. %s\n&#039; "$n" "$line"
    done < "$file" ;;
  done)
    [[ ${1:-} =~ ^[1-9][0-9]*$ ]] || usage
    text=$(sed -n "${1}p" "$file" 2>/dev/null)
    [ -n "$text" ] || { echo "no task $1" >&2; exit 1; }
    sed -i "${1}d" "$file"
    echo "removed: $text" ;;
  *) usage ;;
esac
EOF

Lessons this build uses