Shell script (Linux, macOS) that outputs the date of the last week

Linux working version

1week.sh


#!/bin/sh

#Loop from 7 to 1
for N in `seq 7 -1 1`
do
  #From 7 days ago to 1 day ago
  DATE=`date +%Y-%m-%d --date "${N} days ago"`
  echo $DATE
done

Operation check environment: Ubuntu 20.04 LTS (Focal Fossa) + dash 0.5.10.2 + GNU coreutils 8.30 (date, seq)

$ ./1week.sh 
2020-06-27
2020-06-28
2020-06-29
2020-06-30
2020-07-01
2020-07-02
2020-07-03

macOS working version

1week.sh


#!/bin/sh

#Loop from 7 to 1
for N in `seq 7 -1 1`
do
  # -v-From 7d-v-Up to 1d
  DATE=`date -v-${N}d +%Y-%m-%d`
  echo $DATE
done

Operation check environment: macOS Catalina 10.15 + GNU bash 3.2.57 + BSD date + BSD seq

$ ./1week.sh 
2020-06-27
2020-06-28
2020-06-29
2020-06-30
2020-07-01
2020-07-02
2020-07-03

Recommended Posts

Shell script (Linux, macOS) that outputs the date of the last week
[Linux] Learn the basics of shell commands
[Python] Get the last updated date of the website
A shell script that puts Webmin into Alpine Linux
A script that outputs a list of SoftLayer portal users
Script to get the expiration date of the SSL certificate
Script that changes the length of the sound with REAPER
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
A shell script that just emails the SQL execution result
A Python script that compares the contents of two directories
It is said that Fabric cannot get the status code in the middle of the pipe in the shell script.
I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Process the contents of the file in order with a shell script
Set the last modified date of the child file to the modified date of the parent directory
A formula that simply calculates the age from the date of birth
Conditional branch due to the existence of a shell script file
Explaining the mechanism of Linux that you do not know unexpectedly
[Infrastructure] Linux command, shell script collection
A note about the functions of the Linux standard library that handles time
Python script that outputs all records of Oracle table to CSV file
Get the return value of an external shell script (ls) with python3