【发布时间】:2020-02-11 01:08:24
【问题描述】:
我正在尝试运行 Bash 脚本来比较两个文件以查看它们是否相同(最后几行除外)。我编写的脚本在我手动运行时运行良好,但是,因为我希望脚本在 java 程序之后立即运行,所以我已经做到了,所以 java 程序在完成其他所有操作后运行脚本,但是当这种情况发生时我收到语法错误。
有人可以解释为什么当我从 java 程序运行 bash 脚本时会发生这种情况,以及我该如何解决它。
fileChangeCheck.sh
#!/bin/bash
if [[ -f output/newfile.json ]]
then
echo "File found"
errorTest=$(tail -n 1 output/newfile.json)
if [[ $errortest=} ]]
then
diff <(head -n -3 output/current.json) <(head -n -3 output/newfile.json) > output/check.txt
if [[ -s output/check.txt ]]
then
echo "$(date "+%d-%m-%Y %H:%M:%S") - Check had Data" >> logs/log.txt
mv output/current.json output/archive/current.$(date "+%Y%m%d%H%M%S").json
mv output/newfile.json output/current.json
rm output/check.txt
else
echo "$(date "+%d-%m-%Y %H:%M:%S") - Check was empty" >> logs/log.txt
rm output/newfile.json
rm output/check.txt
fi
fi
fi
Java 代码
public static void filecheck() throws Exception {
Process p = Runtime.getRuntime().exec("sh "+directory+"fileChangeCheck.sh");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
line = "";
while ((line = errorReader.readLine()) != null) {
System.out.println(line);
}
}
错误信息
$ java -jar retrieve.jar
fileChangeCheck.sh: line 12: syntax error near unexpected token `('
fileChangeCheck.sh: line 12: `diff <(head -n -3 output/current.json) <(head -n -3 output/newfile.json) > output/check.txt'
非常感谢
【问题讨论】: