The specification
The script is named todo.sh. It keeps its tasks in one plain text file, one task per line. The file is named by the TODO_FILE environment variable, and it defaults to todo.txt in the current directory. The script takes one subcommand as its first argument.
The add subcommand takes one or more words and appends them to the file as a single line, with the words joined by single spaces. The list subcommand prints each task as a number, a period, a space, and the task text, numbered from 1. When the file does not exist, list prints nothing and exits with status 0. The done subcommand takes a task number, removes that task from the file, and prints removed: followed by the task text.
Errors follow the conventions from the exit codes unit. A missing or unknown subcommand, or a bad argument to done, prints a usage line to standard error and exits with status 2. A done with a number that has no task prints a message to standard error and exits with status 1.
Design decisions
The first argument is read into a variable, and then shifted off, so that the rest of the arguments are the subcommand arguments. A case statement dispatches on the subcommand, and its default branch prints usage. This is the same structure as the classify example in the if and case unit, and it keeps every subcommand in one place.
The file is read with a while loop that uses read with the -r option and an empty IFS, so each line comes back unchanged. The loop numbers the lines as it prints them. Removing a task uses sed with the line number, which deletes exactly that line. Because the task numbers in list come from the file itself, the numbers stay consistent with what done removes.
The number given to done is checked with a regular expression that accepts only a positive whole number. Anything else is a usage error. Asking sed for a line past the end prints nothing, so an empty result means that no such task exists.
The reference solution
The script below implements the specification in about two dozen lines. Read it from top to bottom, and match each part to the specification. It is one possible design, and you can rewrite it in other ways as long as the behavior and the exit statuses stay the same.
#!/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 ;;
esacThe top of the script sets the file name with a default and defines a usage function that exits with status 2. The first argument is saved in cmd, and the shift removes it when there are other arguments. The case statement then handles add, list, and done, and the last branch sends everything else to usage.
Why the file format is plain text
A plain text file with one task per line works with the tools from earlier modules. A user can open it in an editor, search it with grep, or count the tasks with wc. The script needs no database and no special parser. The cost is that the file has no ids, so the number a task has depends on its position. For a small list that trade is fine, and the specification makes the behavior explicit.
Testing the script
Test the script with a temporary file, so that your real list is not affected. Set TODO_FILE to a path in /tmp, add two tasks, list them, remove one, and list again. Then run each error case and check its status with echo $?. The no-argument case, an unknown subcommand, done with a number that does not exist, and done with a word should each produce a message on standard error and a status that matches the specification.
The lab for this build asks you to write the script in three steps. The first step covers add and list, the second adds done, and the third adds the error behavior. Each step has its own check, so you can see which part of the specification is still missing.
Extensions to try
Once the basic tool works, add a subcommand that prints only the count of tasks, or make list accept a substring and print only the matching lines. Each extension reuses a unit of this module: arguments, tests, loops, read, and exit statuses. Treat the build as a place to combine what you have learned, and keep each change small enough that you can test it on its own.