Skip to content

Instantly share code, notes, and snippets.

@kwkr
Last active May 18, 2023 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwkr/e786b4821e56fb1879b0228338924880 to your computer and use it in GitHub Desktop.
Save kwkr/e786b4821e56fb1879b0228338924880 to your computer and use it in GitHub Desktop.
40 lines bash script for TODO list
#!/bin/bash
if [ ! -d "$HOME/.td" ]
then
mkdir "$HOME/.td"
fi
if [ $1 = 'a' ]
then
to_add=${@: 2}
echo "Added: \"$to_add\""
if [ -s ~/.td/todo.txt ]
then
echo "$to_add" >> ~/.td/todo.txt
else
echo "$to_add" > ~/.td/todo.txt
fi
fi
if [ $1 = 'l' ]
then
cat ~/.td/todo.txt 2> /dev/null
fi
if [ $1 = 'p' ]
then
finished=$(head -1 ~/.td/todo.txt)
todo=$(tail -n +2 ~/.td/todo.txt)
if [ -z "$todo" ]
then
echo -n "" > ~/.td/todo.txt
else
echo "$todo" > ~/.td/todo.txt
fi
echo "Done: \"$finished\""
fi
if [ $1 = 'c' ]
then
head -1 ~/.td/todo.txt
fi
@kwkr
Copy link
Author

kwkr commented May 16, 2023

  1. Put the script in /usr/local/bin as td
  2. Run sudo chmod +x /usr/local/bin/td

@gabrielsroka
Copy link

gabrielsroka commented May 17, 2023

i added some comments, usage, shrank it a bit (26 lines) and factored out $file. also, >> will create a file if it doesn't exist

#!/bin/bash
file=~/.td/todo.txt
if [ ! -d "$HOME/.td" ]; then
	mkdir "$HOME/.td"
fi

if [ "$1" = a ]; then # add
	to_add=${@: 2}
	echo "Added: \"$to_add\""
	echo "$to_add" >> $file
elif [ "$1" = l ]; then # list
	cat $file  2> /dev/null
elif [ "$1" = p ]; then # pop
	finished=$(head -1 $file)
	todo=$(tail -n +2 $file)
	if [ -z "$todo" ]; then
		echo -n "" > $file
	else
		echo "$todo" > $file
	fi
	echo "Done: \"$finished\""
elif [ "$1" = c ]; then # current
	head -1 $file
else # usage
	echo 'Usage: td [alpc] # add/list/pop/current'
fi

@gabrielsroka
Copy link

can also use case ... esac (but it's now 31 lines)

#!/bin/bash
file=~/.td/todo.txt
if [ ! -d "$HOME/.td" ]; then
	mkdir "$HOME/.td"
fi

case "$1" in
	a) # add
		to_add=${@: 2}
		echo "Added: \"$to_add\""
		echo "$to_add" >> $file
		;;
	l) # list
		cat $file  2> /dev/null
		;;
	p) # pop
		finished=$(head -1 $file)
		todo=$(tail -n +2 $file)
		if [ -z "$todo" ]; then
			echo -n "" > $file
		else
			echo "$todo" > $file
		fi
		echo "Done: \"$finished\""
		;;
	c) # current
		head -1 $file
		;;
	*) # usage
		echo 'Usage: td [alpc] # add/list/pop/current'
esac

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