Skip to content

Instantly share code, notes, and snippets.

@danitfk
Last active May 15, 2020 09:00
Show Gist options
  • Save danitfk/8b8c44eaba330bddcfb2c168dbc5e5ee to your computer and use it in GitHub Desktop.
Save danitfk/8b8c44eaba330bddcfb2c168dbc5e5ee to your computer and use it in GitHub Desktop.
#!/bin/bash -x
# Global variables
taskFile="/tmp/.task.txt"
function taskAdd {
echo "Task Add"
checkTaskfile
# Check if task name is not empty
checkString=$(echo $@ | sed 's/add//g')
if [[ "$checkString" == "" ]]; then
echo "[Error] Task name is missing"
exit 1
fi
if grep -iq "(important)" <<< $@ ; then
taskPriority="M"
taskString=$(echo $@ | sed 's/ (important)/,M/g'| sed 's/^add //g')
fi
if grep -iq "(very important)" <<< $@ ; then
taskPriority="H"
taskString=$(echo $@ | sed 's/ (very important)/,H/g' | sed 's/^add //g')
fi
# If priority has not been set, then use default priority
if [ -z ${taskPriority+x} ]; then
taskPriority="L"
taskString=$(echo $@ | sed 's/$/,L/' | sed 's/^add //g')
fi
# Check if task duplicated or not
if grep -iq "$taskString" $taskFile; then
echo "[Error] Task duplicated."
exit 1
fi
echo "$taskString" >> $taskFile
taskId=$(grep -n "$taskString" "$taskFile" | cut -d":" -f1)
echo "Added task $taskId with priority $taskPriority"
}
function taskList {
echo "Task List"
checkTaskfile
taskCount=$(wc -l $taskFile | awk {'print $1'})
for (( i = 1 ; i <= $taskCount ; i++ ));
do
task=$(head -n$i $taskFile | tail -n1)
priority=$(echo $task | cut -d"," -f2)
if [[ "$priority" == "M" ]]; then
prioritySymbol="*** "
fi
if [[ "$priority" == "H" ]]; then
prioritySymbol="****** "
fi
if [[ "$priority" == "L" ]]; then
prioritySymbol="* "
fi
taskString=$(echo $task | cut -d"," -f1)
echo "$i $prioritySymbol" $taskString
done
}
function taskDone {
echo "Task Done"
checkTaskfile
# Check ID Provided
taskId=$(echo $@ | sed 's/done //g')
if grep -q "^[0-9]" <<< $taskId; then
echo "[Error] Task ID is invalid"
exit 1
fi
taskString=$(head -n $taskId $taskFile | tail -n1)
if [[ "$taskString" == "" ]]; then
echo "[Error] Task ID not found"
exit 1
fi
sed -i "/$taskString/d" $taskFile
echo "Completed task $taskId: $taskString"
}
function check {
if [[ "$1" == "" ]]; then
echo "[Error] This command needs an argument"
exit 1
fi
case $1 in
add)
taskAdd $@
;;
list)
taskList
;;
done)
taskDone
;;
*)
echo "[Error] Invalid command"
;;
esac
}
function checkTaskfile {
if [[ ! -f "$taskFile" ]]; then
touch $taskFile
fi
}
function printPriority {
echo "Print priority"
}
check $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment