9

I have the following line in my code

def genList = (args[]?.size() >=4)?args[3]: "

when I run my whole code I get the following error

expecting anything but ''\n''; got it anyway at line: 9, column: 113

here I am adding the whole code so you can see what I am doing

def copyAndReplaceText(source, dest, targetText, replaceText){
    dest.write(source.text.replaceAll(targetText, replaceText))
}
def dire = new File(args[0])
def genList = (args[]?.size() >=4)?args[3]: " // check here if argument 4 is provided, and generate output if so
def outputList = ""
dire.eachFile { 
    if (it.isFile()) {
        println it.canonicalPath
        // TODO 1: copy source file to *.bak file
        copy = { File src,File dest-> 

            def input = src.newDataInputStream()
            def output = dest.newDataOutputStream()

            output << input 

            input.close()
            output.close()
        }

        //File srcFile  = new File(args[0])
        //File destFile = new File(args[1])

        //File srcFile  = new File('/geretd/resume.txt')
        //File destFile = new File('/geretd/resumebak.txt')
        File srcFile = it
        File destFile = newFile(srcFile + '~')
        copy(srcFile,destFile)

        // search and replace to temporary file named xxxx~, old text with new text. TODO 2: modify copyAndReplaceText to take 4 parameters.
        if( copyAndReplaceText(it, it+"~",   args[1], args[2]) ) {
   // TODO 3: remove old file (it)
               it.delete()
   // TODO 4: rename temporary file (it+"~") to (it)

   // If file was modified and parameter 4 was provided, add modified file name (it) to list
   if (genList != null) { 
     // add modified name to list
     outputList += it + "\n\r"
   }
  }
    }
}
// TODO 5: if outputList is not empty (""), generate to required file (args[3])
if (outputList != ""){
    def outPut = new File(genList)
    outPut.write(outputList)
 } 

Thank you

4

4 回答 4

10

只需关闭你的双引号

def genList = (args?.size() >=4)?args[3]: ""
于 2013-07-01T22:14:47.303 回答
0

为什么"在这一行的末尾有一个单曲:def genList = (args[]?.size() >=4)?args[3]: "

你需要做到:def genList = (args[]?.size() >=4)?args[3]: ""

于 2013-07-01T21:58:50.530 回答
0

具体的 OP 问题已经得到解答,但是对于那些在 Groovy 中遇到类似错误消息的人来说,例如:

  • expecting anything but '\n'; got it anyway
  • expecting '"', found '\n'

这可能是由于脚本中的多行 GString ${content} 引起的,应该用三引号(单引号或双引号)引用:

''' ${content} '''或者""" ${content} """

于 2021-11-28T20:41:32.780 回答
-2

您需要;在末尾添加一个令牌def outputList = ""

也去掉"末尾的def genList = (args[]?.size() >=4)?args[3]: "

于 2013-07-01T22:04:58.073 回答