Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中运行的shell脚本中计算参数_Java_Spring Boot_Shell - Fatal编程技术网

在java中运行的shell脚本中计算参数

在java中运行的shell脚本中计算参数,java,spring-boot,shell,Java,Spring Boot,Shell,我在java项目中保存了一些shell脚本。我通过以下方法获得所有这些信息: public List<String> getResourceFiles() throws IOException { String path = "shellScripts"; List<String> filenames = new ArrayList<>(); try ( InputStream in = ge

我在java项目中保存了一些shell脚本。我通过以下方法获得所有这些信息:

public List<String> getResourceFiles() throws IOException {
    String path = "shellScripts";
    List<String> filenames = new ArrayList<>();
    try (
            InputStream in = getResourceAsStream(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
        String resource;

        while ((resource = br.readLine()) != null) {
            filenames.add(resource);
        }
    }
    System.out.println(filenames);
    return filenames;
}

private InputStream getResourceAsStream(String resource) {
    final InputStream in
            = getContextClassLoader().getResourceAsStream(resource);

    return in == null ? getClass().getResourceAsStream(resource) : in;
}

在本例中,它应该返回2。你知道一些最佳实践吗?我不确定是否可以在一些元数据表中定义名称和参数,我不知道它是什么

您需要解析shell脚本并提取参数引用。我不是这里的专家,但如果您知道参数总是由
$n
引用,那么您需要做的就是提取该模式并对其进行解析,以获得
n
的最高值。据我所知,即使一个脚本只使用
$5
,你也需要传递5个参数,这样你就不必知道参数的数量,只需要得到最大的数量(除非你想计算使用的参数和忽略的参数的数量)。我不确定怎么做。你能给我一些代码提示吗?我不明白你为什么要这么做。Shell脚本对变量的要求并不严格:如果未设置变量,则尝试使用它不是错误(除非
set-u
),并替换空字符串。另外,只需在文件中搜索正则表达式模式(例如
\$\{?(\d+)(\W |$)
)是不够的,因为shell函数也使用位置参数语法,而且你可以很容易地拥有一个比实际脚本接受更多参数的函数。不要这样做。这样做很疯狂。我有点需要。我有一个前端,可以在其中选择shell脚本。然后应该运行所选脚本。一些脚本需要填写的参数,例如计算器……为此,我必须“扫描”脚本并返回参数数量。我可以将参数数量声明为元数据吗?正如我所说,我对元数据不太了解。直到现在我才使用它
#!/bin/bash
echo 'First arg:' $1
echo 'Second arg:' $2