This is an exercise in the shell script section of the Linux Primer, "New Linux Textbooks".
After running the shell, a file will be created to create today's diary.
diary.sh
#!/bin/bash
directory="${HOME}/diary"
#Create a data storage directory if it does not exist
if [ ! -d "$directory" ]; then
mkdir "$directory"
fi
#Assembling the diary file path
diaryfile="${directory}/$(date '+%Y-%m-%d').txt"
#If you don't have a diary file (if you're writing for the first time today), insert the date at the beginning
if [ ! -e "$diaryfile" ]; then
date '+%Y-%m-%d' > "$diaryfile"
fi
vim "$diaryfile"
meaning | |
---|---|
-d file | The file exists and is a directory |
-e file | File exists |
New Linux textbook
Recommended Posts