[LINUX] Creating a shell script to write a diary

Introduction

This is an exercise in the shell script section of the Linux Primer, "New Linux Textbooks".

Contents

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

reference

New Linux textbook

Recommended Posts

Creating a shell script to write a diary
I'll never forget how to write a shell script, don't forget! !!
[Ubuntu] How to execute a shell script
Write a script to convert a MySQL dump to TSV
Try to write a ping confirmation script appropriately
Let's try a shell script
A shell script that numbers duplicate names when creating files
How to run a Python program from within a shell script
[Linux] Copy data from Linux to Windows with a shell script
[Linux] Write a deployment tool using rsync with a shell script
How to write a Python class
Write standard output to a file
Write a batch script with Python3.5 ~
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
How to determine if a shell script was started in bash
Write a script in Shell and Python to notify you in Slack when the process is finished
I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux
Qiita (1) How to write a code name
Execute a script from Jupyter to process
A simple IDAPython script to name a function
I made a script to display emoji
Manage logrotate generations with a shell script.
Script to create a Mac dictionary file
How to run a Maya Python script
Shell script basics # 2
View today's weather forecast with a shell script
Why does Python have to write a colon?
How to write a ShellScript Bash for statement
I wrote a script to upload a WordPress plugin
How to write a named tuple document in 2020
[Go] How to write or call a function
I want to write to a file with Python
Try creating a FizzBuzz problem with a shell program
How to write a ShellScript bash case statement
Let's understand the standard input / output of bash together and write a shell script.
[Ubuntu] How to execute a shell script
[Grasshopper] When creating a data tree on Python script
How to write a GUI using the maya command
I want to write in Python! (2) Let's write a test
I made a script to put a snippet in README.md
How to write a list / dictionary type of Python3
A memorandum to run a python script in a bat file
A shell script that puts Webmin into Alpine Linux
[Python] How to write a docstring that conforms to PEP8
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
How to create a simple TCP server / client script
Write code to Unit Test a Python web app
Python script to create a JSON file from a CSV file
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Generate a bash script to add Datadog monitor settings
Diary to create a site called Harmony Engine (irregular) 1
Creating a Home screen
4. Creating a structured program
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Shell script command replacement
Creating a scraping tool
Shell script special variables
Shell script @ study memo
Creating a dataset loader