0

I am writing a java program that takes command and run it in unix shell. I have managed to get the output, but I need the program to detect if the given command is invalid, now, if I put in an invalid command, it gives me an error. Otherwise, it's working fine. Can anybody help me with this?? Here is my code:

public class TestCommand {

    public static void main(String[] args) {

        TestCommand obj = new TestCommand();
        String sentence ="";
        Scanner scn = new Scanner(System.in);

         while (sentence != " ")
        {
             if (sentence !="exit")
             {   
                 System.out.println("> ");
                 sentence = scn.nextLine();

                 String outPut = obj.executeCommand(sentence);
                 System.out.println(outPut + "\n");
             }
             else
             {
                 System.exit(2);
             }
        }




    }

    private String executeCommand(String sentence)
    {
        StringBuffer output = new StringBuffer();


        Process p;

        try
        {
            p = Runtime.getRuntime().exec(sentence);
            p.waitFor();

            BufferedReader bfrd = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = bfrd.readLine())!= null)
            {
                output.append(line);

            }
            bfrd.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }

}
3
  • Not directly on point, but don't test String equality with == or !=. You should be using something like !sentence.equals(" ") and !sentence.equals("exit") Commented Oct 8, 2014 at 3:19
  • You are trying to determine if the shell command you are given is a syntactically valid shell command? So echo foo bar is ok but echo 'foo is not? Commented Oct 8, 2014 at 3:39
  • Try checking the exitValue? docs.oracle.com/javase/7/docs/api/java/lang/… Commented Oct 8, 2014 at 3:59

1 Answer 1

4

Unix shell language is extraordinarily complex. Re-implementing enough of it in java to test for correctness would be a large undertaking. If you just want to find out if some shell code is syntactically correct, you can use bash with the -n option:

bash -n file_with_code_to_test

Or:

bash -n -c string_with_code_to_test

The key thing here is the -n option. It tells bash to read the commands and check their syntax without executing them. Thus, this is safe to run.

You can run these command from java just as you would other bash commands.

bash will return an exit code of 0 if the code is syntactically correct. If it isn't, it will print error messages and return an exit code of 1.

Just to be clear, this checks syntax. It will, for example, not test for the existence of needed files or commands, only that the code is would run if they did exist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.