我编写了以下Groovy代码:
#!/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
应该返回最新RDS快照ID的名称。
当我在终端中运行脚本时,我得到的唯一输出是println todayDate
,但不是aws cli命令的输出。
编辑#1:
当我在终端中运行它时,这是命令的输出:
$ /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
编辑#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 ~]$
知道我做错了什么吗?因为我没有错误......
答案 0 :(得分:1)
您使用不当。
只需查看语句是否首先生成正确的命令。但事实并非如此。
这是生成预期命令的固定脚本。稍后你可以执行它。
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(' ')
这是在线 demo ,可以快速试用。
你可能想要执行它?然后做
def process = cmd.execute()
process.waitFor()
println process.text
编辑:根据评论(解决管道问题,虽然生成的命令没问题)
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