Skip to content

Instantly share code, notes, and snippets.

@sridatta
Forked from anonymous/task
Last active August 29, 2015 13:56
Show Gist options
  • Save sridatta/9027573 to your computer and use it in GitHub Desktop.
Save sridatta/9027573 to your computer and use it in GitHub Desktop.
A simple file-based task manager in Bash
#!/bin/sh
if [ -z $TASK_DIR ]; then
TASK_DIR=$HOME/tasks
fi
help () {
echo "Valid subcommands: create, ls, rm, cat, edit"
}
nth () {
ls $TASK_DIR | sed "$1q;d"
}
taskcreate () {
ESCAPE=$(printf "%q" $1)
FILENAME="${*}.txt"
touch "$TASK_DIR/$FILENAME"
}
taskls () {
ls $TASK_DIR | cat -n | sed "s/\..*//g"
}
taskrm () {
N=$1
if [ -z $N ] ; then
echo "** TASKS **"
taskls
read -p "Select a task number: " N
fi
F=$(nth $N)
rm "$TASK_DIR/$F"
}
taskcat () {
N=$1
if [ -z $N ] ; then
echo "** TASKS **"
taskls
read -p "Select a task number: " N
fi
F=$(nth $N)
cat "$TASK_DIR/$F"
}
taskedit () {
N=$1
if [ -z $N ] ; then
echo "** TASKS **"
taskls
read -p "Select a task number: " N
fi
F=$(nth $N)
$EDITOR "$TASK_DIR/$F"
}
case $1 in
cat) taskcat $2
;;
rm) taskrm $2
;;
ls) taskls
;;
edit) taskedit $2
;;
create) taskcreate $2
;;
*) help
;;
esac
@laughinghan
Copy link

Have you considered replacing lines 58 onward with:

case "$1" in
  cat|rm|ls|edit|create) "task$1" "$2" ;;
  *) help ;;
esac

Variables in command names are pretty universally supported by shells; I learned this dispatching trick from git-submodule.sh, and git's coding guidelines are explicitly very stringent about cross-compat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment