How to get the current date in a script using bamboo - bash

How can you get the current date in YYYYMMDD formate in a Atlassian Bamboo script?

DATE=$(date +%Y%m%d)
It doesn't work for, but
DATE=$(date +%%Y%%m%%d)
works fine :)
I have Bamboo 3.4.1

If it's in bash, it's:
$ date +Y%m%d
20140815
If you want that in a variable, these ways are essentially equivalent:
DATE=`date +%Y%m%d`
DATE=$(date +%Y%m%d)

Related

How to get the number of days since a specific date in bash

I need to find the number of days for today since a specific date (Lets say its 01011980). Wanted to write a shell scrip which will be running on solaris.
Ex: uname -a -->SunOS test-1 5.11 11.3 sun4v sparc sun4v
So far I have been able to get the current date formatted in to a desired format.
But could not find a way to convert a given date to a format which can be used in to subtract from the current date to get the number of days.
I tried date +%s and passing in the desired dates but every time it showed the crrent time.
AG1> date +%s
1495427876
AG1> (date +%s -ud '2003-08-02 17:24:33' )
1495427877
AG1>
I don't want to use another language like perl as this is going to execute different machines and additional programming language will be a impediment.
Can someone help.
Use arithmetic evaluation:
echo $(( ($(date +%s) - $(date +%s -ud '2003-08-02 17:24:33')) / 3600 / 24 ))
Answer: 5041.
edit: since the question was about SunOS, date utility in this system differs from GNU date, and the key -d is not working. GNU version in SunOS is /usr/gnu/bin/date.

Using date to get tomorrows date in Bash

I want to write a bash script that will run on a given but process data with next days date, My current approach is to get the unix time stamp and add a days worth of seconds to it, but I cant get it working, and haven't yet found what I'm looking for online.
Here's what I've tried, I feel like the problem is that its a string an not a number, but I dont know enough about bash to be sure, is this correct? and how do I resolve this?
today="$(date +'%s')"
tomorrow="$($today + 86400)"
echo "$today"
echo "$tomorrow"
If you have gnu-date then to get next day you can just do:
date -d '+1 day'
Some of the answers for this question depend on having GNU date installed. If you don't have GNU date, you can use the built-in date command with the -v option.
The command
$ date -v+1d
returns tomorrow's date.
You can use it with all the standard date formatting options, so
$ date -v+1d +%Y-%m-%d
returns tomorrow's date in the format YYYY-MM-DD.
$(...) is command substitution. You're trying to run $today + 86400 as a command.
$((...)) is arithmetic expansion. This is what you want to use.
tomorrow=$(( today + 86400 ))
Also see http://mywiki.wooledge.org/ArithmeticExpression for more on doing arithmetics in the shell.
I hope that this will solve your problem here.
date --date 'next day'
Set your timezone, then run date.
E.g.
TZ=UTC-24 date
Alternatively, I'd use perl:
perl -e 'print localtime(time+84600)."\n"'
echo $(date --date="next day" +%Y%m%d)
This will output
20170623
I like
input_date=$(date '+%Y-%m-%d')
tomorrow_date=$(date '+%Y-%m-%d' -d "$input_date + 1 day")
echo "$tomorrow_date"
It returns the date of the day after $input_date, in format YYYY-MM-DD
You can try below
#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"

Getting date of X days ago in bash script, using argument variable

I'm trying to calculate the date for a dynamic number of days ago in a bash script.
This is what I've done -
#!/bin/bash
STAMP=`date --date='$1 day ago' +%y%m%d`
but when running myscript 2, it says -
date: invalid date `$1 day ago'
How can I use my argument value in this formula?
It works if ' is replaced with " into this command on the script -
STAMP=`date --date="$1 day ago" +%y%m%d`
The clue was the two different character ` and ' used in the error response -
date: invalid date `$1 day ago'
An expert in bash scripting (not me) can probably explain why this has happen.
It's because variable substitution wouldn't happen in single quotes, i.e. '$1' wouldn't expand but "$1" would.
As such, saying
STAMP=`date --date="$1 day ago" +%y%m%d`
or
STAMP=$(date --date="$1 day ago" +%y%m%d)
would work.

How can I detect BSD vs. GNU version of date in shell script

I am writing a shell script that needs to do some date string manipulation. The script should work across as many *nix variants as possible, so I need to handle situations where the machine might have the BSD or the GNU version of date.
What would be the most elegant way to test for the OS type, so I can send the correct date flags?
EDIT:
To clarify, my goal is to use date's relative date calculation tools which seem distinct in BSD and GNU.
BSD example
date -v -1d
GNU example
date --date="1 day ago"
You want to detect what version of the date command you're using, not necessarily the OS version.
The GNU Coreutils date command accepts the --version option; other versions do not:
if date --version >/dev/null 2>&1 ; then
echo Using GNU date
else
echo Not using GNU date
fi
But as William Pursell suggests, if at all possible you should just use functionality common to both.
(I think the options available for GNU date are pretty much a superset of those available for the BSD version; if that's the case, then code that assumes the BSD version should work with the GNU version.)
Use portable flags. The standard is available here
For the particular problem of printing a relative date, it is probably easier to use perl than date:
perl -E 'say scalar localtime(time - 86400)'
(note that this solution utterly fails on 23 or 25 hour days, but many perl solutions are available to address that problem. See the perl faq.)
but you could certainly use a variation of Keith's idea and do:
if date -v -1d > /dev/null 2>&1; then
DATE='date -v -1d'
else
DATE='date --date="1 day ago"'
fi
eval "$DATE"
or just
DATE=$(date -v -1d 2> /dev/null) || DATE=$(date --date="1 day ago")
Another idea is to define a function to use:
if date -v -1d > /dev/null 2>&1; then
date1d() { date -v -1d; }
else
date1d() { date --date='1 day ago'; }
fi
It is recommended to use feature-based defection rather than the os-based or platform-based detection.
Following sample code show how it works:
if is-compatible-date-v; then
date -v -1d
elif is-compatible-date-d; then
date --date="1 day ago"
fi
function is-compatible () {
"$#" >/dev/null 2>&1
}
function is-compatible-date-v () {
is-compatible date -v +1S
}
function is-compatible-date-d () {
is-compatible date -d '1 second'
}
I was having the exact need to writing some shellcode to deal with date, and hoping the code be able to run on both BSD and GNU version of date.
I end up with a set of scripts that provides a uniform interface for both BSD and GNU version of date.
Example:
Follow command will output a date that is 21 days later than 2008-10-10, and it works with both BSD and GNU version of date.
$ xsh x/date/adjust +21d 2008-10-10
2008-10-31
The scripts can be found at:
https://github.com/xsh-lib/core/tree/master/functions/date
It's a part of a library called xsh-lib/core. To use them you need both repos xsh and xsh-lib/core, I list them below:
https://github.com/alexzhangs/xsh
https://github.com/xsh-lib/core
Hope this will help people with the same need.
To just answer your question, probably "uname -o" is what you want. In my case it's "GNU/Linux". You can decide for yourself if detecting the os type is worthless or not.

Shell script datetime function?

I am running the following script:
#!/bin/ksh
./clear_old
./rooms_xls.pl 2/23/09
cd doors
./doors_xls.pl 2/23/09
How can I get the date to be today's date based upon the system/server time?
This should work fine (and be almost compliant with your format):
#!/bin/ksh
./clear_old
./rooms_xls.pl `date +%D`
cd doors
./doors_xls.pl `date +%D`
$(date)
works
Use backticks with the date command:
./rooms_xls.pl `date +%m/%d/%Y`

Resources