Itai Ganot
Itai Ganot

Reputation: 6305

Output not printed to screen when command chain is executed from Groovy Script

I wrote the following Groovy code:

#!/opt/groovy-2.4.12/bin/groovy
String todayDate = new Date().format( 'yy-MM-dd' )
def p = ['/usr/bin/aws', 'rds', 'describe-db-snapshots', '--db-instance-identifier dev-rds-2017-10-02', '--snapshot-type automated', '--query "DBSnapshots[?SnapshotCreateTime>=' + todayDate +'.DBSnapshotIdentifier"'].execute() | 'grep rds'.execute() | ['tr', '-d', '\'\"|[:space:]\''].execute()
p.waitFor()
//p.waitFor()
println todayDate
println p.text

Which is supposed to return the name of the latest RDS snapshot id.

When I run the script in terminal, the only output that I get is the println todayDate but not the output of the aws cli command.

Edit #1:

This is the output of the command when I run it in terminal:

$ /usr/bin/aws rds describe-db-snapshots --db-instance-identifier dev-rds-2017-10-02 --snapshot-type automated --query "DBSnapshots[?SnapshotCreateTime>='$todaydate'].DBSnapshotIdentifier" | grep rds | tr -d '\"'
    rds:dev-rds-2017-10-02-2017-11-22-00-05

Edit #2:

[jenkins@ip-X-X-X-X ~]$ /usr/bin/aws rds describe-db-snapshots --db-instance-identifier dev-rds-2017-10-02 --snapshot-type automated --query "DBSnapshots[?SnapshotCreateTime>='2017-11-23'].DBSnapshotIdentifier" | grep rds | tr -d '\"'
    rds:dev-rds-2017-10-02-2017-11-23-00-05
[jenkins@ip-X-X-X-X ~]$ groovy -d rdssnapshotid.groovy
/usr/bin/aws rds describe-db-snapshots --db-instance-identifier dev-rds-2017-10-02 --snapshot-type automated --query "DBSnapshots[?SnapshotCreateTime>='2017-11-23'].DBSnapshotIdentifier" | grep rds | tr -d '\"'

[jenkins@ip-X-X-X-X ~]$

Any idea what I'm doing wrong? Cause I get no errors...

Upvotes: 0

Views: 258

Answers (1)

Rao
Rao

Reputation: 21349

You are using it incorrectly.

Just see if the statement generate the right command first. But it does not.

Here is the fixed script which generates the expected command. Later you can execute it.

def todaydate = new Date().format( 'yy-MM-dd' )
def cmd = ['/usr/bin/aws', 'rds', 'describe-db-snapshots', '--db-instance-identifier', 'dev-rds-2017-10-02', '--snapshot-type', 'automated', '--query', "\"DBSnapshots[?SnapshotCreateTime>='${todaydate}'].DBSnapshotIdentifier\"", '|', 'grep', 'rds', '|', 'tr', '-d', "'\\\"'"]
println cmd.join(' ')

Here is online demo for quick try.

You may want to execute it? Then do

def process = cmd.execute()
process.waitFor()
println process.text

EDIT: Based on the comments (to address the pipe issue, though generated command is ok)

def todaydate = new Date().format( 'yy-MM-dd' )
def process = ['/usr/bin/aws', 'rds', 'describe-db-snapshots', '--db-instance-identifier', 'dev-rds-2017-10-02', '--snapshot-type', 'automated', '--query', "\"DBSnapshots[?SnapshotCreateTime>='${todaydate}'].DBSnapshotIdentifier\""].execute() | ['grep', 'rds'].execute() | ['tr', '-d', "'\\\"'"].execute()
process.waitFor()
println process.text

Upvotes: 1

Related Questions