A Simple Guide to a Bash Task Management Script
This article explains a basic task management script written in Bash. The script lets users create tasks, view them, mark them as completed, and delete them. Let’s go through the code step by step.
Overview of the Script
The script has several functions, each doing a specific job for managing tasks. It runs a loop that shows a menu to the user until they choose to exit.
1. Shebang Line
#! /bin/bash
This line tells the system to use the Bash shell to run the script.
2. Creating the Task File
create_file() {
if [ -f 'tasks.txt' ]; then
echo "File exists"
echo ''
else
touch tasks.txt
echo "File created"
echo ''
fi
}
-
Purpose: Checks if
tasks.txt
exists. If it does, it tells the user. If not, it creates the file. -
-f
: Checks if the file exists. -
touch
: Creates an empty file if it doesn’t already exist.
3. Displaying the Menu
display_menu() {
echo "1. Add task"
echo "2. View all tasks"
echo "3. Mark task as completed"
echo "4. Delete task"
echo "5. Exit"
echo ''
}
- Purpose: Shows a simple menu for the user to choose from.
- The menu offers options to add, view, mark, delete tasks, or exit the program.
4. Adding a Task
add_task() {
read -p "Enter task: " task
if grep -q "$task:Pending" tasks.txt; then
echo "Task already exists"
echo ''
return
elif grep -q "$task:Completed" tasks.txt; then
echo "Task already exists"
echo ''
return
fi
echo "$task:Pending" >> tasks.txt
echo "Task added"
echo ''
}
-
Purpose: Asks the user to enter a task and adds it to
tasks.txt
with the status "Pending". -
grep -q
: Searches the file quietly to check for duplicates. -
Appending: The task is added to the file with the status
Pending
.
5. Viewing All Tasks
view_tasks() {
cat tasks.txt
echo ''
}
-
Purpose: Shows all tasks in
tasks.txt
. -
cat
: Reads the file and displays its contents.
6. Marking a Task as Completed
mark_task() {
read -p "Enter task to mark as completed: " task
echo ''
if grep -q "$task:Pending" tasks.txt; then
sed -i '' "s/$task:Pending/$task:Completed/g" tasks.txt
echo "Task marked as completed"
echo ''
else
echo "Task not found or already completed"
echo ''
fi
}
- Purpose: Lets the user mark a task as completed.
-
sed -i ''
: Changes the status from "Pending" to "Completed" in the file. - Feedback: Tells the user if the task was found and updated.
7. Deleting a Task
delete_task() {
read -p "Enter task to delete: " task
echo ''
if grep -q "$task:Pending" tasks.txt; then
sed -i '' "/$task:Pending/d" tasks.txt
echo "Task deleted"
echo ''
elif grep -q "$task:Completed" tasks.txt; then
sed -i '' "/$task:Completed/d" tasks.txt
echo "Task deleted"
echo ''
else
echo "Task not found"
echo ''
fi
}
- Purpose: Deletes a task from the file.
-
/pattern/d
: Removes lines that match the pattern from the file. - Feedback: Informs the user whether the task was deleted or not found.
8. User Input Handling
user_input() {
read -p "Enter your choice: " choice
echo ''
case $choice in
1) add_task ;;
2) view_tasks ;;
3) mark_task ;;
4) delete_task ;;
5) echo "Exiting..." ;;
*) echo "Invalid choice"
esac
return $choice
}
- Purpose: Gets user input and runs the correct function based on the choice.
-
case
statement: Handles multiple options more clearly than using manyif
statements.
9. Main Program Loop
create_file
while true; do
display_menu
user_input
if [ $? -eq 5 ]; then
echo ''
break
fi
done
- Purpose: Sets up the task file and runs a loop to show the menu until the user decides to exit.
-
$?
: Gets the exit status of the last command (in this case,user_input
).
Conclusion
This Bash script is a simple task manager. It shows basic Bash scripting concepts like functions, file handling, user input, and control flow. With a few changes, this script can be improved to add more features, like saving task priorities or due dates.
Script can be found here
Top comments (1)
Insightful