groovy Jenkinsfile shell脚本的语法错误

ogsagwnx  于 2023-03-16  发布在  Jenkins
关注(0)|答案(2)|浏览(227)

我尝试在我的jenkinsfile中附加一个sh脚本,但是即使“(“被正确编码,我也会得到这种语法错误。下面是我的if else块脚本:

// Display Users with permission rights
                        if(url_permission_type.contains('/api/permissions/users')){
                            userLogin = jsonGroupUserlist.users.login
                            userName = jsonGroupUserlist.users.name
                            userEmail = jsonGroupUserlist.users.email
                            userPermission = jsonGroupUserlist.users.permissions

                            index_users = 0
                            for(String usrLogin:userLogin){
                                usrName = userName.get(index_users)
                                usrEmail = userEmail.get(index_users)
                                usrPermission = userPermission.get(index_users)

                                if(usrEmail == null){
                                    usrEmail = 'N/A'
                                    if(usrPermission){
                                        sh (script: 'echo '+project+','+usrLogin+','+usrName+','+usrEmail+','+usrPermission+' >> SBI_'+params.permission_type.toString()+'Access_Rights_Matrix.csv ')
                                        echo "SBI PROJECT: ${project} | ${usrLogin} | ${usrName.toString()} | ${usrEmail.toString()} | PERMISSIONS: ${usrPermission.toString()}" 
                                    }
                                } else {
                                    if(usrPermission){
                                        sh (script: 'echo '+project+','+usrLogin+','+usrName+','+usrEmail+','+usrPermission+' >> SBI_'+params.permission_type.toString()+'Access_Rights_Matrix.csv ')
                                        echo "SBI PROJECT: ${project} | ${usrLogin} | ${usrName.toString()} | ${usrEmail.toString()} | PERMISSIONS: ${usrPermission.toString()}"
                                    }
                                }
                                index_users++
                            }
                            echo "--------------------------------------------------------------------------------------------------------"
                        }

错误:
@tmp/durable-3b 9a 6 b 0 b/script.sh:语法错误:“(“意外

pcrecxhr

pcrecxhr1#

此错误消息通常表示shell脚本中存在语法错误,很可能是由于不匹配或意外的括号造成的。
在这种情况下,问题可能与在条件语句中使用'sh'命令有关。
若要修复此错误,可以尝试将整个代码块括在双引号中,并使用“echo”命令而不是“sh”作为前缀。例如:

if(url_permission_type.contains('/api/permissions/users')){
userLogin = jsonGroupUserlist.users.login
userName = jsonGroupUserlist.users.name
userEmail = jsonGroupUserlist.users.email
userPermission = jsonGroupUserlist.users.permissions

index_users = 0
for(String usrLogin:userLogin){
    usrName = userName.get(index_users)
    usrEmail = userEmail.get(index_users)
    usrPermission = userPermission.get(index_users)

    if(usrEmail == null){
        usrEmail = 'N/A'
        if(usrPermission){
            echo "echo ${project},${usrLogin},${usrName},${usrEmail},${usrPermission} >> SBI_${params.permission_type.toString()}Access_Rights_Matrix.csv"
            echo "SBI PROJECT: ${project} | ${usrLogin} | ${usrName.toString()} | ${usrEmail.toString()} | PERMISSIONS: ${usrPermission.toString()}" 
        }
    } else {
        if(usrPermission){
            echo "echo ${project},${usrLogin},${usrName},${usrEmail},${usrPermission} >> SBI_${params.permission_type.toString()}Access_Rights_Matrix.csv"
            echo "SBI PROJECT: ${project} | ${usrLogin} | ${usrName.toString()} | ${usrEmail.toString()} | PERMISSIONS: ${usrPermission.toString()}"
        }
    }
    index_users++
}
echo "--------------------------------------------------------------------------------------------------------" }

这样可以防止在条件语句中出现与使用'sh'相关的任何语法错误。

iyr7buue

iyr7buue2#

我不认为你的代码有任何错误,但你的代码的其他地方可能有错误,你可以使用ide,如vscode,帮助你检测和删 debugging 误。语法错误可能与shell脚本代码中缺少或放错了括号有关。或者如果代码块没有包含在函数或脚本块中,这也可能导致语法错误,因为shell脚本需要顶级函数或脚本块。

相关问题