简体   繁体   中英

Running Bash script using Java

I am attempting to run a Bash script to compare two files to see if they are identical (except for the last few lines). The script I have written works fine when I run it manually, however, because I want the script to run immediately after a java program, I have made it so the java program runs the script after it has done everything else, but when this happens I am getting a syntax error.

Could someone please explain why this is happening when I run the bash script from the java program and how I could fix it.

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 code

    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);
        }
    }

Error Message

$ 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'

Many Thanks

You're invoking sh, which will just run this script; the top 'comment' that tells the system to run it with bash is ignored by sh, and sh may not be bash, so that's something to look into. This is also rather dubious and is likely to fail on many systems; I'd advise invoking explicitly '/bin/bash' at the very least.

That will likely fix it. If not, probably an issue with current working directory being different.

EDIT: Applied commenter dave thompson's suggestion.

Processbuilder has many options you can set:

try {
        final Process pr = new ProcessBuilder("fileChangeCheck.sh").directory(new File(yourDirectory)).redirectOutput(new File(logfile))
                        .start();
        pr.waitFor();
    } catch (IOException | InterruptedException e) {
        System.out.println(e.getMessage());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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