3

I have a Linux command that I cannot execute from Java:

[ ! -d "/tmp/flow" ] && echo "The directory DOES NOT exist."

It works fine in command line but when I use the following code:

    String command = "[ ! -d \"/tmp/flow\" ] && echo \"The directory DOES NOT exist.\"";
    Process proc = Runtime.getRuntime().exec(command);
    Boolean successful = proc.waitFor()==0 && proc.exitValue()==0;
    System.out.println("successful:"+successful);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    String line = null;
    System.out.println("************** input");
    while ((line = stdInput.readLine()) != null) {
        System.out.println(line);
    }
    System.out.println("************** error");
    while ((line = stdError.readLine()) != null) {
        System.out.println(line);
    }

it says that

successful:false
************** input
************** error
[: missing ']'

Any idea? Is it possible that these IF bash operators cannot be called from Java...?

Thanks!

**************** UDATE ****************

vikhor@adnlt653-vm1:~> which '['
[ is a shell builtin
[ is /usr/bin/[
[ is /bin/[
9
  • are you in same box where you are trying to execute mention command. if out side try to use jsch or any other libaray Commented Feb 6, 2018 at 10:47
  • 1
    I dont think there is an executable named "[" on Linux which does what you want. And do you really want to check for the presence of some directory using an external command? Commented Feb 6, 2018 at 10:50
  • Hi @spandey15, yes, I am on the same host. Commented Feb 6, 2018 at 10:54
  • 1
    Hi @GyroGearless, basically it is an IF operator, it tries to check if the folder exists. It works fine in command line. Usually it is used in bash scripts. Commented Feb 6, 2018 at 10:54
  • Boolean successful = proc.waitFor()==0 && proc.exitValue()==0; there's no need to check exitValue() if you've just checked the return of waitFor() as they will be the same. Print the actual int value instead of creating a Boolean object. Commented Feb 6, 2018 at 11:06

1 Answer 1

1

The [ command is a shell built-in. The which command shows that it's also an executable file. It is this executable file that gets executed when you pass the command line to exec(). So then the whole command line is passed to the [ command. However, the && operator is a shell operator. The [ executable doesn't like it.

So you need to run the bash executable and pass the command as an argument:

String command = "bash -c '[ ! -d \"/tmp/flow\" ] && echo \"The directory DOES NOT exist.\"'";
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thanks, you must be right! However I cannot prove it as I get the following error when executing the command above: !: -c: line 0: unexpected EOF while looking for matching `'' !: -c: line 1: syntax error: unexpected end of file
Your error message says "unexpected EOF while looking for matching `". That's a backquote character. Make sure you're using a straight quote.
Well, the strange thing is that there is no backquote in the command but only aposthrophe (I copied the command you provided). Anyway your answer is the correct one so I will accept it.
eventually the bash -c was the solution but your answer was the key, thanks again!
Glad it helped. And thanks for pointing out what the correct command should have been. I've edited the answer for the benefit of any future readers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.