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
To format date in DD-MM-YYYY format, use the command date +%d-%m-%Y or printf "%(%d-%m-%Y)T\n" $EPOCHSECONDS .
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.
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 # ...
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..
The equivalent on Mac OSX is date -v "+60M" to add 60 minutes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With