# Variable initialization

**URL:** <https://community.unix.com/t/variable-initialization/343889>\
**Category:** Shell Programming and Scripting\
**Created:** [June 23, 2014, 11:08am UTC](https://community.unix.com/t/variable-initialization/343889 "2014-06-23T11:08:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kekanap](https://community.unix.com/user_avatar/community.unix.com/kekanap/32/100_2.png) [@kekanap](https://community.unix.com/u/kekanap)\
**Post date:** [June 23, 2014, 11:08am UTC](https://community.unix.com/t/variable-initialization/343889/1 "2014-06-23T11:08:08Z")

</div>

Hallo Team,

I have a simple request and i would appreciate your help.  
I would like to use two dates in my script lets:

`A=$(date +"%d %B %Y")`

```nohighlight
echo $A
23 June 2014

```

That's awesome now we cooking. Now i want B to be on the previous month  
eg:

```nohighlight
 echo $B Should give me 
23 May 2014 

```

I would like to use these two arguments in my script below:

```nohighlight
../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate between '20 MAY 2014' and '19 JUN 2014' and destination in (select distinct accountani from asani where (accountani between '0124264000' and '0124264611'))" MHG_Bitts_Only_Billing_201406 Y Y N;

../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate between '20 MAY 2014' and '19 JUN 2014' and destination in (select distinct accountani from asani where (accountani between '0116712000' and '0116712999'))" MHG_MedScheme_Only_Billing_201406_1 Y Y N;

```

My question is how do i set B as a variable to give me the desired output?

Regards,

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 23, 2014, 11:35am UTC](https://community.unix.com/t/variable-initialization/343889/2 "2014-06-23T11:35:30Z")

</div>

What's your system? On Linux you can do things like

```plaintext
date -d "2014/01/01 - 1 month"

```

to get the last month.

If you don't, it may take thing like [this](http://www.unix.com/tips-and-tutorials/239167-general-purpose-date-script.html) to get handy date math.

---

<div class="post-metadata">

**Author:** ![kekanap](https://community.unix.com/user_avatar/community.unix.com/kekanap/32/100_2.png) [@kekanap](https://community.unix.com/u/kekanap)\
**Post date:** [June 23, 2014, 11:39am UTC](https://community.unix.com/t/variable-initialization/343889/3 "2014-06-23T11:39:58Z")

</div>

Thank you Corona,

I am using Redhat Linux and your suggestion works but how do I initialize variable B?

---

<div class="post-metadata">

**Author:** ![Makarand\_Dodmis](https://community.unix.com/letter_avatar/makarand_dodmis/32/6_449c533f586939b6e5f246632a46421f.png) [@Makarand\_Dodmis](https://community.unix.com/u/Makarand_Dodmis)\
**Post date:** [June 23, 2014, 11:48am UTC](https://community.unix.com/t/variable-initialization/343889/4 "2014-06-23T11:48:58Z")

</div>

Hi You can get like "23 5 2014" using below script

```nohighlight
echo $(date +"%d") `expr $(date +"%m") - 1` $(date +"%Y")

```

..not able to print word "May" though..working on it

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 23, 2014, 11:57am UTC](https://community.unix.com/t/variable-initialization/343889/5 "2014-06-23T11:57:35Z")

</div>

Ugh, that is not how you do it...

Have you read the link I linked? Have you read the example I showed you? They show you how.

---

<div class="post-metadata">

**Author:** ![kekanap](https://community.unix.com/user_avatar/community.unix.com/kekanap/32/100_2.png) [@kekanap](https://community.unix.com/u/kekanap)\
**Post date:** [June 23, 2014, 12:08pm UTC](https://community.unix.com/t/variable-initialization/343889/6 "2014-06-23T12:08:59Z")

</div>

Hallo Corona,

I saw the your link its awesome but its not giving me my format.

Makarand awesome feedfack only if i can get :

```nohighlight
23052014 not 2352014

```

that would be great.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 23, 2014, 12:13pm UTC](https://community.unix.com/t/variable-initialization/343889/7 "2014-06-23T12:13:43Z")

</div>

In what way does it "not work"? date supports your format:

```nohighlight
$ date.pl +"%d %B %Y"

23 June 2014

$

```

What did you do? In what way did it "not work"?

Try getting the date in YYYY/MM/DD format, then using GNU date or the Perl script to convert it into the format you like in both -d "DDDD/MM/YY" and -d "DDDD/MM/YY - 1 month".

---

<div class="post-metadata">

**Author:** ![kekanap](https://community.unix.com/user_avatar/community.unix.com/kekanap/32/100_2.png) [@kekanap](https://community.unix.com/u/kekanap)\
**Post date:** [June 23, 2014, 3:24pm UTC](https://community.unix.com/t/variable-initialization/343889/8 "2014-06-23T15:24:06Z")

</div>

Hallo Corona,

```nohighlight
[mind@util1-pkl REPORTS]$ date.pl +"%d %B %Y"
-bash: date.pl: command not found
[mind@util1-pkl REPORTS]$ date -d "2014/06/23 - 1 month"
Fri May 23 00:00:00 SAST 2014
[mind@util1-pkl REPORTS]$

```

There are 2 Options on my script which i can use:

Option A:

```nohighlight
../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate between '20 MAY 2014' and '19 JUN 2014' and destination in (select distinct accountani from asani where (accountani between '0124264000' and '0124264611'))" MHG_Bitts_Only_Billing_201406 Y Y N;

../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate between '20 MAY 2014' and '19 JUN 2014' and destination in (select distinct accountani from asani where (accountani between '0116712000' and '0116712999'))" MHG_MedScheme_Only_Billing_201406_1 Y Y N;

```

Or Option B:

```nohighlight
../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate >= to_date('20052014', 'ddmmyyyy') AND calldate <= to_date('19062014', 'ddmmyyyy')and destination in (select distinct accountani from asani where (accountani between '0124264000' and '0124264611'))" MHG_Bitts_Only_Billing_201406 Y Y N;

../getUsageDetailsFromMind_Where.pl "where accountid in (select id from accounts where parent = 77) and calldate >= to_date('20052014', 'ddmmyyyy') AND calldate <= to_date('19062014', 'ddmmyyyy') and destination in (select distinct accountani from asani where (accountani between '0116712000' and '0116712999'))" MHG_MedScheme_Only_Billing_201406_1 Y Y N;

```

This script has to be run every month on the 20th.

On Option A:

```nohighlight
variableA='20 MAY 2014'
variableB='19 JUN 2014'

```

Or Option B:

```nohighlight
VariableA='20052014'
VariableB='19062014'

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 23, 2014, 6:22pm UTC](https://community.unix.com/t/variable-initialization/343889/9 "2014-06-23T18:22:00Z")

</div>

First off, you don't need my date.pl because you have GNU date. It says in the thread that it's a minimal GNU date imitation, if you have GNU date you don't need it.

Second off, you clearly can't run date.pl if you didn't make the file.

Third off, you clearly can't run it just by typing in 'date.pl', but by typing in './date.pl' as my posts clearly illustrate.

Fourth off... You don't need my date.pl because you have GNU date.

Lastly: You haven't tried any of my or anyone else's suggestions at all, preferring to wait for a whole answer. I refuse to accept that you're that helpless, but:

```nohighlight
DAY=$(date +%Y-%m-%d)
CURDAY=$(date -d "$DAY" +"%d %B %Y")
PREV=$(date -d "$DAY - 1 month" +"%d %B %Y")

```

---

<div class="post-metadata">

**Author:** ![kekanap](https://community.unix.com/user_avatar/community.unix.com/kekanap/32/100_2.png) [@kekanap](https://community.unix.com/u/kekanap)\
**Post date:** [June 24, 2014, 3:12am UTC](https://community.unix.com/t/variable-initialization/343889/10 "2014-06-24T03:12:38Z")

</div>

Thank you everyone.
