Last active
May 18, 2023 18:27
-
-
Save kwkr/e786b4821e56fb1879b0228338924880 to your computer and use it in GitHub Desktop.
40 lines bash script for TODO list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
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
/usr/local/bin
astd
sudo chmod +x /usr/local/bin/td