InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Supplying a Default Date in bash
Bash

Supplying a Default Date in bash

InfinityCoder February 20, 2017

You want your script to provide a useful default date, and perhaps prompt the user to verify it.

Using the GNU date command, assign the most likely date to a variable, then allow the user to change it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash
# cookbook filename: default_date
 
# Use Noon time to prevent a script running around midnight and a clock a
# few seconds off from causing off by one day errors.
START_DATE=$(date -d 'last week Monday 12:00:00' '+%Y-%m-%d')
while [ 1 ]; do
   printf "%b" "The starting date is $START_DATE, is that correct? (Y/new date) "
   read answer
 
   # Anything other than ENTER, "Y" or "y" is validated as a new date
   # could use "[Yy]*" to allow the user to spell out "yes"...
   # validate the new date format as: CCYY-MM-DD
   case "$answer" in
     [Yy]) break
         ;;
     [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])
         printf "%b" "Overriding $START_DATE with $answer\n"
         START_DATE="$answer"
         ;;
     *)   printf "%b" "Invalid date, please try again...\n"
         ;;
   esac
done
 
END_DATE=$(date -d "$START_DATE +7 days" '+%Y-%m-%d')
 
echo "START_DATE: $START_DATE"
echo "END_DATE:   $END_DATE"

Not all date commands support the -d option, but the GNU version does. Our advice is to obtain and use the GNU date command if at all possible.
Leave out the user verification code if your script is running unattended or at a known time (e.g., from cron).

We use code like this in scripts that generate SQL queries. The script runs at a given time and creates a SQL query for a specific date range to generate a report.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Doing More with less in bash
You’d like to take better advantage of the features of …

Doing More with less in bash

Finding All Your MP3 Files in bash
You have MP3 audio files scattered all over your filesystem. …

Finding All Your MP3 Files in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap