Passing date variables as dates from the CLI

Hi all,

Another problem to do with (I suspect) dates not be treated as dates.

My script has this block at the front to set some variables, so that I can eventually run it from a regular cron job:

startmonth=date +%m
startyear=date +%Y
endyear=$startyear
friendlymonth=date +%b
reportperiod=$friendlymonth" "$startyear
if [ $startmonth = 12 ];then
endyear=$(($startyear+1))
endmonth=1
else
endyear=$startyear
endmonth=$(($startmonth+1))
fi
startdate=1/$startmonth/$startyear
enddate=1/$endmonth/$endyear

Idea being to put the start & end dates into variables “startdate” and “enddate” to restrict the report range.

If I run:

           rt list "queue = 'myqueue' and created > '$startdate'"

It returns results correctly.

If I run:
rt list “queue = ‘myqueue’ and created < ‘$enddate’”

It says “No matches found”.

If I manually enter:

                          rt list "queue = 'myqueue' and created < '1/2/2011'"

Then it works correctly.

root@sirius:/ # echo $startdate $enddate
1/01/2011 1/2/2011

They’re obviously different - “startdate” has a month of “01” whilst “enddate” is “2”. But startdate works, and enddate not…

Is there some way I need to format the resulting variable differently so that it knows it’s a date and is treated as such?

Thanks,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au

yyyy-mm-dd works, but you’ll want(need?) leading zeroes for mm and dd.From: Chris Herrmann [mailto:chris.herrmann@faredge.com.au]
Sent: Wednesday, January 26, 2011 09:53 PM
To: rt-users@lists.bestpractical.com rt-users@lists.bestpractical.com
Subject: [rt-users] Passing date variables as dates from the CLI

Hi all,

Another problem to do with (I suspect) dates not be treated as dates.

My script has this block at the front to set some variables, so that I can eventually run it from a regular cron job:

startmonth=date +%m
startyear=date +%Y
endyear=$startyear
friendlymonth=date +%b
reportperiod=$friendlymonth" "$startyear
if [ $startmonth = 12 ];then
endyear=$(($startyear+1))
endmonth=1
else
endyear=$startyear
endmonth=$(($startmonth+1))
fi
startdate=1/$startmonth/$startyear
enddate=1/$endmonth/$endyear

Idea being to put the start & end dates into variables “startdate” and “enddate” to restrict the report range.

If I run:

           rt list “queue = ‘myqueue’ and created > ‘$startdate’”

It returns results correctly.

If I run:
rt list “queue = ‘myqueue’ and created < ‘$enddate’”

It says “No matches found”.

If I manually enter:

                          rt list “queue = ‘myqueue’ and created < ‘1/2/2011’”

Then it works correctly.

root@sirius:/ # echo $startdate $enddate
1/01/2011 1/2/2011

They’re obviously different – “startdate” has a month of “01” whilst “enddate” is “2”. But startdate works, and enddate not…

Is there some way I need to format the resulting variable differently so that it knows it’s a date and is treated as such?

Thanks,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au

Josh Narins
Director of Application Development

SeniorBridge
845 Third Ave
7th Floor
New York, NY 10022
Tel: (212) 994-6194
Fax: (212) 994-4260
Mobile: (917) 488-6248
jnarins@seniorbridge.com
seniorbridge.comhttp://www.seniorbridge.com/

[http://www.seniorbridge.com/images/seniorbridgedisclaimerTAG.gif]

SeniorBridge Statement of Confidentiality: The contents of this email message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. Any dissemination, distribution or copying of this email by an unintended or mistaken recipient is strictly prohibited. In said event, kindly reply to the sender and destroy all entries of this message and any attachments from your system. Thank you.

But dd/mm/yyyy also works – $startdate works for example. Or if you type “1/1/2011” this will work, which doesn’t have the leading zero.

Regards,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.auFrom: Josh Narins [mailto:jnarins@seniorbridge.com]
Sent: Thursday, 27 January 2011 14:14
To: Chris Herrmann; 'rt-users@lists.bestpractical.com’
Subject: Re: [rt-users] Passing date variables as dates from the CLI

yyyy-mm-dd works, but you’ll want(need?) leading zeroes for mm and dd.

1/1/2011 is unambiguous whether we are in Europe or America.

1/2/2011 is not.

xxxx-xx-xx is “always (everywhere I have ever seen., or hope to see)” yyyy-mm-dd.From: Chris Herrmann [mailto:chris.herrmann@faredge.com.au]
Sent: Wednesday, January 26, 2011 10:18 PM
To: Josh Narins
Cc: rt-users@lists.bestpractical.com rt-users@lists.bestpractical.com
Subject: RE: [rt-users] Passing date variables as dates from the CLI

But dd/mm/yyyy also works – $startdate works for example. Or if you type “1/1/2011” this will work, which doesn’t have the leading zero.

Regards,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au

From: Josh Narins [mailto:jnarins@seniorbridge.com]
Sent: Thursday, 27 January 2011 14:14
To: Chris Herrmann; 'rt-users@lists.bestpractical.com’
Subject: Re: [rt-users] Passing date variables as dates from the CLI

yyyy-mm-dd works, but you’ll want(need?) leading zeroes for mm and dd.

Josh Narins
Director of Application Development

SeniorBridge
845 Third Ave
7th Floor
New York, NY 10022
Tel: (212) 994-6194
Fax: (212) 994-4260
Mobile: (917) 488-6248
jnarins@seniorbridge.com
seniorbridge.comhttp://www.seniorbridge.com/

[http://www.seniorbridge.com/images/seniorbridgedisclaimerTAG.gif]

SeniorBridge Statement of Confidentiality: The contents of this email message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. Any dissemination, distribution or copying of this email by an unintended or mistaken recipient is strictly prohibited. In said event, kindly reply to the sender and destroy all entries of this message and any attachments from your system. Thank you.

Ok, some more things. I think it’s an RT bug not a bash problem… actually I think there are two (possibly separate) bugs. Actually I’m not editing the email after having done some more testing, and there is only one problem… but I’ll leave the (brief) journey in to show how I got there.

  1. If you create any arbitrary variable that contains a date string (in any format… YYYY-MM-DD, DD-MM-YYYY etc etc) and try to use this with a “<” operator it will return no results, regardless of how you create the variable. For example, I was previously trying to build a date string (which produces a correctly formatted date string for the variable) but this didn’t work. So I tried typing
    enddate=2011-02-01
    and this does not work either. If I replace the variable in the rt cli query with the literal values then it works correctly. Have also tried exporting the variables - it appears to make no difference. I also tried other variable names startdate / randomdate / thisnamecannotbereserved etc.

  2. If you are using a date variable with the > operator you get a different result - it returns a whole set of data rather than restricting by the date in question.

Actually, the problem is more with variables fullstop…

faredge@sirius:~$ rtqueryqueue=myrtqueue;export rtqueryqueue
faredge@sirius:~$ rt
rt> list "queue = ‘$rtqueryqueue’"
Query:queue = '$rtqueryqueue’
No matches found
rt>

If I run this by typing the queue name into the cli, it works OK.

So… then I guess the question becomes how do I correctly pass environment variables into the RT CLI so that they’re substituted correctly?

Regards,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au

Actually, the problem is more with variables fullstop…

faredge@sirius:~$ rtqueryqueue=myrtqueue;export rtqueryqueue
faredge@sirius:~$ rt
rt> list "queue = ‘$rtqueryqueue’"
Query:queue = '$rtqueryqueue’
No matches found
rt>

If I run this by typing the queue name into the cli, it works OK.

So… then I guess the question becomes how do I correctly pass environment variables into the RT CLI so that they’re substituted correctly?

You don’t. bin/rt doesn’t do variable substitution. If you need to use
environment variables then you need to do it without dropping into
bin/rt’s shell.

Thomas

Hi Thomas,

Thanks for your reply, I’ll try generating query files with all the variables already substituted and then executing the contents of these files instead and see how I go.

I admit I’m a bit surprised that this isn’t supported (you can do it in perl for example) - it seems like a fairly obvious use case if you’re going to bother having a CLI at all, how are you going to automate properly without variables? For example dates (as per my example) but also getting usernames from the shell, new queues / clients etc as you create them. Anyway, just my thoughts. Also not having the ability to export the results to email as an attachment (I worked around this by mutt), or to generate CSV format instead of TSV .

Do ideas for new features get submitted to the bugs list or is there somewhere else they should go?

Regards,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au

You don’t. bin/rt doesn’t do variable substitution. If you need to use environment variables then you need to do it without dropping into bin/rt’s shell.

Thomas

Thanks for your reply, I’ll try generating query files with all the variables already
substituted and then executing the contents of these files instead and see how I go.

Hi Chris

I think you may have misunderstood Tom, you were running bin/rt and
dropping into the interactive shell and expecting it to do
substitution for you. What it appears that you actually want to do
is to run rt list (your commands here).

You can absolutely substitute variables into arguments to bin/rt, it
is just a matter of ensuring that you escape things properly for the
shell or your preferred programming language, and that you add things
in the correct format.

You may find it useful to examine the test suite that ships with RT to
see examples

-kevin

Hi Kevin,

Thanks, yes I’m running it from a bash script which in turn is called from cron and the variables are substituting correctly, and I got my first automated rt queries last night on schedule :slight_smile: . Next up is creating some more scheduled jobs that run through on a daily basis or maybe more often and look for content such as SPAM and deleting these, or auto-resolving tickets that are notifications about successful backup jobs that require no attention, for example.

If anyone would like copies of the various scripts (bash wrapper + rt query + cron wrapper to run on the last day of the month) email me off list and I’ll send you a copy.

Regards,

Chris Herrmann
Far Edge Technology

p. 02 84251400
m. 0403 393309
http://www.faredge.com.au