views:

3464

answers:

7

Hi,

I'd like to invoke bash using a string as input. Something like:

sh -l -c "./foo"

I'd like to do this from Java. Unfortunately, when I try to invoke the command using getRuntime().exec, I get the following error:

      foo": -c: line 0: unexpected EOF while looking for matching `"'

      foo": -c: line 1: syntax error: unexpected end of file

It seems to be related to my string not being terminated with an EOF.

Is there a way to insert a platform specific EOF into a Java string? Or should I be looking for another approach, like writing to a temp script before invoking "sh" ?

+4  A: 

EOF is NOT a character, so there's no way to write an EOF. You've forgotten to close a quoted string.

Lev
A: 

The cause for this error is most likely a missing syntax token that bash expects but the string you pass ends before bash encountered it. Look for ifs, fors etc. that have no closing fi or done.

unbeknown
+8  A: 

Use this:

Runtime.getRuntime().exec(new String[] {"sh", "-l", "-c", "./foo"});

Main point: don't put the double quotes in. That's only used when writing a command-line in the shell!

e.g., echo "Hello, world!" (as typed in the shell) gets translated to:

Runtime.getRuntime().exec(new String[] {"echo", "Hello, world!"});

(Just forget for the moment that the shell normally has a builtin for echo, and is calling /bin/echo instead. :-))

Chris Jester-Young
A: 

Quotes need to be escaped when inside a string. Instead of writing " write \".

E.g.

strcpy(c, "This is a string \"with\" quotes");

diciu
A: 

Thanks for the input everyone. I'm already escaping the quotes; I should have perhaps mentioned that.

I just want to use a complicated sequence of bash commands (a small script really) that I pass via a string which I can build up programatically depending on some set of inputs.

On Windows this works fine. However, on OSX/Linux I get the above error. It's really weird. For example, this fails:

String cmd = "sh -l -c \"echo foo; echo bar;\"";
Runtime.getRuntime().exec(cmd);
Daniel
+1  A: 

Windows command lines differently from UNIX, Mac OS X and GNU/Linux.

On Windows the process receives the input text as is after the executeable name (and space). It's then up to the program to parse the command line (which is usually done implicitly and the programmer is often clueless).

In GNU/Linux the shell processes the command line and gnereates the familiar array of strings passed to C's main. You don't have that shell. The best approach (even on Windows) is to use the form of exec where you pass each command line argument individually in its own String.

You can get a shell to the parsing for you if you really wanted. Which would make you example look something like (untested):

Runtime.getRuntime().exec(new String[] {
    "sh", "-c", "sh -l -c \"echo foo; echo bar;\""
});
Tom Hawtin - tackline
A: 

if I were you, I would write the contents of the string to a temp bashfile and see if bash executes that without any error. If that executes without an error, then I would consider debugging further;

anjanb