Created
January 3, 2020 17:27
-
-
Save rohitkg98/cd0e795936f7882d0a0d4e734b7b7597 to your computer and use it in GitHub Desktop.
A Script to maintain logs for everyday.
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 | |
# Check if DAILYLOG_DIR exists. | |
if [[ ! -z $DAILYLOG_DIR ]]; then | |
echo "Creating a new log at" "$DAILYLOG_DIR" | |
else | |
echo "Please enter directory to store logs:" | |
# tput is used to set colour and bold font - experimental turns all text to bold red | |
# trap 'echo -e $(tput bold)$(tput setaf 196)"NOTE: Path should be relative from ~/ i.e. home"' DEBUG | |
echo -e "NOTE: Path should be relative from ~/ i.e. home" | |
read -r logdir | |
logdir=~/$logdir | |
echo "$logdir" | |
if ! mkdir -p $logdir; then | |
printf "Please enter a non existent directory\nor\nset DAILYLOG_DIR environment variable to your preferred directory" | |
exit | |
fi | |
echo "export DAILYLOG_DIR=$logdir" >> ~/.profile | |
DAILYLOG_DIR="$logdir" | |
months=($(locale mon | tr ';' '\n')) | |
for num in ${!months[@]}; do | |
mkdir -p ${logdir}/$(expr "$num" + "1")_${months[num]} | |
done | |
fi | |
# Create a new log file for the day | |
month=$(date +"%-m_%B") | |
file_name=$(date +"%d_%b_%Y.md") | |
if [ -f $DAILYLOG_DIR/$month/$file_name ]; then | |
echo "Today's log has already been created!" | |
exit | |
fi | |
init_comment=$(date +"%A %d %b %Yq") | |
printf '<!-- Daily Log for %s -->\n' "$init_comment" >> $DAILYLOG_DIR/$month/$file_name | |
scaffold=$(date +"%A %d %B") | |
echo "# $scaffold" >> $DAILYLOG_DIR/$month/$file_name | |
echo "Created new logfile at $DAILYLOG_DIR/$month/$file_name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source ~/.profile after using it for the first time. Thanks!