简体   繁体   English

在bash脚本中捕获JavaScript语法错误

[英]Catch a JavaScript syntax error in a bash script

I have a bash script in Jenkins which uses Nashorn engine to perform a little bit of JavaScript. 我在Jenkins中有一个bash脚本,它使用Nashorn引擎执行一些JavaScript。

Bash script: Bash脚本:

#!/bin/bash

"$JAVA_HOME/bin/jjs" -scripting allKeys.js | sort > resultKeys

The allKeys.js script loads another script from external location and then prints some values: allKeys.js脚本从外部位置加载另一个脚本,然后输出一些值:

bundlesUrl="https://address.to/bundles.js";
load(bundlesUrl);

for (var bundle in bundles) {
    if (bundles.hasOwnProperty(bundle)) {
        for (var key in bundles[bundle]) {
            print(bundle + "\t" + key + "\t\t\"" + bundles[bundle][key] + "\"");
        }
    }
}

However, the JavaScript file specified in bundlesUrl may sometimes contain improperly escaped special characters. 但是,bundlesUrl中指定的JavaScript文件有时可能包含不正确的转义特殊字符。 This of course produces a syntax error. 当然,这会产生语法错误。

The jjs prints that there was a Syntax error, but the bash script continues. jjs打印出语法错误,但是bash脚本继续。

What I would like to do is somehow catch that syntax error and exit the bash script with code 1 so that the Jenkins build marks as failed. 我想做的是以某种方式捕获该语法错误并使用代码1退出bash脚本,以便Jenkins构建标记为失败。

What are some good ways of achieving this? 有什么好的方法可以做到这一点?

EDIT: I found out that I can do this with a set -e 编辑:我发现我可以用set -e

jjs exits with non-zero exit code on error (see also http://hg.openjdk.java.net/jdk9/dev/nashorn/file/17cc754c8936/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/Shell.java ). jjs在错误时以非零退出代码退出(另请参见http://hg.openjdk.java.net/jdk9/dev/nashorn/file/17cc754c8936/src/jdk.scripting.nashorn/share/classes/jdk/nashorn /tools/Shell.java )。 So you should be able to check $? 因此,您应该可以检查$? in your shell script just after jjs command. 在jjs命令之后的shell脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM