Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a shell script where to add days month and year to current date

I am new to shell script.

I want to write a script where I add days month and years to current day. for example it take current date and every time addd 3 to day 4 to months and 2 to years. all the three things given as arguments.

I would really appreciate it.

Br

like image 830
Sara Avatar asked Jun 19 '12 10:06

Sara


People also ask

How do you display 2 days back date in dd mm yyyy format in Linux?

To format date in DD-MM-YYYY format, use the command date +%d-%m-%Y or printf "%(%d-%m-%Y)T\n" $EPOCHSECONDS .

How do I display the current month in a shell script?

Method 1: Using cal command If a user wants a quick view of the calendar in the Linux terminal, cal is the command for you. By default, the cal command shows the current month calendar as output. The cal command in Linux is used to view the calendar for a specific month or for the entire year.

How do I get today's date in shell?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...


2 Answers

Tip: fire up a terminal(in my case bash terminal)

the help is a good starting point

date --help

or man page

man date

A lot of information and examples.

Date manipulation in bash (copy paste the example run in your terminal):

add 10 days to the current date:

date -d "10 day" +"%Y %m %d"

or remove 10 days to the current date

date -d "-10 day" +"%Y %m %d"

add 2 months to the current date:

date -d "2 month" +"%Y %m %d"

remove 2 months from the current date:

date -d "-2 month" +"%Y %m %d"

add 1 year to the current date

date -d "1 year" +"%Y %m %d"

remove 1 year to the current date

date -d "-1 year" +"%Y %m %d"

mixing add 1 year month and day

date -d "1 year 1 month 1 day" +"%Y %m %d"

in a script (in my case bash)

foobaa=`date -d "1 year 1 month 1 day" +"%Y %m %d"`
echo $foobaa

I hope it helps a little..

like image 120
theforce Avatar answered Nov 15 '22 05:11

theforce


The equivalent on Mac OSX is date -v "+60M" to add 60 minutes.

like image 1
joeyreid Avatar answered Nov 15 '22 03:11

joeyreid