我编写了以下Groovy代码:
#!/usr/local/bin/groovy
String todayDate = new Date().format( 'yyyy-MM-dd' )
def rdbs = "aws rds describe-db-snapshots --db-instance-identifier $dbname --snapshot-type automated --query DBSnapshots[?SnapshotCreateTime>='$todayDate'].DBSnapshotIdentifier".execute().text.eachLine {
retval.add(it.subSequence(4,it.length() ))
}
return retval
这个想法是输出将被添加到一个数组但是当我运行它时我得到以下错误:
Caught: java.lang.StringIndexOutOfBoundsException: String index out of range: -3
java.lang.StringIndexOutOfBoundsException: String index out of range: -3
at java_lang_CharSequence$subSequence$0.call(Unknown Source)
at rds$_run_closure1.doCall(rds.groovy:8)
at rds.run(rds.groovy:7)
所以我改变了脚本只是为了看看输出是否正常:
#!/usr/local/bin/groovy
String todayDate = new Date().format( 'yyyy-MM-dd' )
def dbname = 'dev-rds-2017-10-02'
retval=[]
def rdbs = "aws rds describe-db-snapshots --db-instance-identifier $dbname --snapshot-type automated --query DBSnapshots[?SnapshotCreateTime>='$todayDate'].DBSnapshotIdentifier".execute().text
println rdbs
当我运行它时,我得到以下正确的输出:
[
"rds:dev-rds-2017-10-02-2017-11-26-00-05"
]
但我希望它只返回" rds:dev-rds-2017-10-02-2017-11-26-00-05"行,怎么办呢?
答案 0 :(得分:0)
以下代码解决了我的问题:
#!/usr/local/bin/groovy
String todayDate = new Date().format( 'yyyy-MM-dd' )
def dbname = 'dev-rds-2017-10-02'
retval=[]
def rdbs = "aws rds describe-db-snapshots --db-instance-identifier $dbname --snapshot-type automated --query DBSnapshots[?SnapshotCreateTime>='$todayDate'].DBSnapshotIdentifier".execute().text.eachLine {
if (!it.contains(']') && (!it.contains('[') ) )
retval.add(it.replaceAll('\"','').trim())
}
retval.each {
println it
}