Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Last active March 16, 2025 13:53
Show Gist options
  • Save romanitalian/7e2a303d6ed10668a748adb1c11901dd to your computer and use it in GitHub Desktop.
Save romanitalian/7e2a303d6ed10668a748adb1c11901dd to your computer and use it in GitHub Desktop.
Note taking script with color output and validation
#!/bin/bash
# Usage examples:
# ./note.sh # Show all notes with numbers
# ./note.sh "Buy milk" # Add new note
# ./note.sh "TODO: Fix bugs" # Add todo note
# ./note.sh -d 5 # Delete note number 5
# ./note.sh -cl # Clear all notes (with confirmation)
# ./note.sh -h # Show this help message
#
# Validation rules:
# - Note text cannot be empty
# - Maximum note length is 80 characters
# - Delete command (-d) requires a valid number
# - Note text must be in quotes if it contains spaces
# Configuration
NOTES_FILE=~/notes/all.txt
NOTES_DIR=$(dirname "$NOTES_FILE")
MAX_NOTE_LENGTH=80
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print error message in red
print_error() {
echo -e "${RED}Error: $1${NC}"
}
# Function to print success message in green
print_success() {
echo -e "${GREEN}$1${NC}"
}
# Function to print warning message in yellow
print_warning() {
echo -e "${YELLOW}$1${NC}"
}
# Function to show help message
show_help() {
cat << EOF
Usage:
./note.sh Show all notes with numbers
./note.sh "Your note" Add new note
./note.sh "TODO: something" Add todo note
./note.sh -d NUMBER Delete note by number
./note.sh -cl Clear all notes (with confirmation)
./note.sh -h Show this help message
Notes:
- Maximum note length is $MAX_NOTE_LENGTH characters
- Use quotes for notes with spaces
- Notes are stored in $NOTES_FILE
EOF
}
# Function to clear all notes
clear_notes() {
if [ ! -f "$NOTES_FILE" ]; then
print_error "No notes file exists."
exit 1
fi
local count=$(wc -l < "$NOTES_FILE")
if [ "$count" -eq 0 ]; then
print_warning "Notes file is already empty."
exit 0
fi
print_warning "Are you sure you want to delete all $count notes? (y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
: > "$NOTES_FILE"
print_success "All notes have been cleared."
else
print_warning "Operation cancelled."
fi
}
# Function to validate delete command argument
validate_delete_arg() {
local number=$1
if ! [[ "$number" =~ ^[0-9]+$ ]]; then
print_error "Delete command requires a valid number"
exit 1
fi
}
# Function to validate note text
validate_note() {
local note="$1"
if [[ -z "$note" ]]; then
print_error "Note text cannot be empty"
exit 1
fi
if [[ ${#note} -gt $MAX_NOTE_LENGTH ]]; then
print_error "Note is too long (max $MAX_NOTE_LENGTH characters)"
exit 1
fi
}
# Create notes directory if it doesn't exist
mkdir -p "$NOTES_DIR"
# Function to display notes with line numbers
display_notes() {
if [ -f "$NOTES_FILE" ]; then
nl -w2 -s'. ' "$NOTES_FILE"
else
print_error "No notes found."
fi
}
# Function to delete a note by number
delete_note() {
local line_number=$1
validate_delete_arg "$line_number"
if [ -f "$NOTES_FILE" ]; then
# Check if line exists
if [ "$line_number" -le "$(wc -l < "$NOTES_FILE")" ]; then
# Create a temporary file for the deletion
sed "${line_number}d" "$NOTES_FILE" > "$NOTES_DIR/temp.txt"
mv "$NOTES_DIR/temp.txt" "$NOTES_FILE"
print_success "Note $line_number deleted."
display_notes
else
print_error "Note number $line_number not found."
exit 1
fi
else
print_error "No notes file exists."
exit 1
fi
}
# Main logic
if [[ -z "$1" ]]; then
display_notes
elif [[ "$1" == "-h" ]]; then
show_help
elif [[ "$1" == "-cl" ]]; then
clear_notes
elif [[ "$1" == "-d" ]]; then
if [[ -z "$2" ]]; then
print_error "Delete command requires a note number"
exit 1
fi
delete_note "$2"
else
validate_note "$1"
echo "$1" >> "$NOTES_FILE"
print_success "Note added."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment