Shell Java 源代码中带有 vimdiff 命令的脚本在 Git bash 运行 时卡住

Shell script with vimdiff command in Java source code gets stuck when run on Git bash

我的 Java 项目中有一个带有 vimdiff 命令的 shell 脚本。

OS: Windows

在 GitBash 上,当我直接 运行 vimdiff 命令时,它工作正常并生成结果。

在 GIT BASH 终端上运行良好的命令:

vimdiff file1.log file2.log -c TOhtml -c 'w! result.html' -c 'qa!'

但是,当我尝试 运行 我在 Java 项目中的测试时,它卡住了一段时间,控制台中没有打印任何内容。不知道为什么。

我的 shell 脚本 vimDiff.sh 在 Java 项目中

#!/bin/bash

MASTERLOGFILE=""
BUILDLOGFILE=""
OUTPUTDIFFFILE=""

cat $MASTERLOGFILE | cut -d\  -f4-  > $MASTERLOGFILE.1
cat $BUILDLOGFILE | cut -d\  -f4-  > $BUILDLOGFILE.1

vimdiff $MASTERLOGFILE.1 $BUILDLOGFILE.1 -c TOhtml -c  'w! '"$OUTPUTDIFFFILE"'' -c 'qa!'

我的Java测试Class调用上面的shell脚本:

public class GenerateDiffHtml {
    @Test
    public void generateSortedLogFiles() throws Exception {
        try {

            String masterFilePath = "file1.log";
            String buildFilePath = "file2.log";
            String outputDiffFilePath = "result.html";
            String diffScriptPath = "vimDiff.sh";
            Process process = null;
            String[] cmd = {"sh", diffScriptPath, masterFilePath, buildFilePath, outputDiffFilePath};
            process = Runtime.getRuntime().exec(cmd);
            process.waitFor();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

命令 运行 测试:

mvn clean test -Dtest=GenerateDiffHtml

注意:相同的 Java 代码在 mac machine 上工作正常。所以,我想知道这是否与 Windows 上的 Git Bash 永远等待该过程完成有关。

因为您在 Windows 中使用 GitBash 运行 shell 脚本。 VimDiff 基本上是一个需要用户操作的 VI 编辑器。由于您在后台执行脚本,因此任何人都无法执行任何操作。

因此,请通过以下方式尝试一下

  1. 从您的 java 代码中删除 process.waitFor();
  2. 在 shell 脚本中添加删除临时文件(.swp 或任何其他文件)的命令。