Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Escape Char on linux Bash Process

Thread view: 
lsguotti@gmail.com - 21 Mar 2008 13:28 GMT
Hello,

we want to try execute a bash command from java but, I need to insert
in command a double quotes  example:

Process p = rt.exec( "echo \"Te st\" " );

but the bash refuse a " value.

I have already test any char I thinked  (' ''  \ \/ )

Do you any idea to escape a " char?

Thank you for any help.
Roedy Green - 21 Mar 2008 13:51 GMT
>Process p = rt.exec( "echo \"Te st\" " );

exec does not understand bash commands directly.  See
http://mindprod.com/jgloss/exec.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Wayne - 21 Mar 2008 16:02 GMT
> Hello,
>
[quoted text clipped - 4 lines]
>
> but the bash refuse a " value.

That command runs the "echo" command, not bash!

Also you need to pass the arguments to the command differently.
Use a shell like bash to use quoting:

   Process p = rt.exec(
       new String[]{"bash", "-c", "echo \"hello, world!\""} );

should do it.

-Wayne
Andreas Leitgeb - 21 Mar 2008 21:28 GMT
>> we want to try execute a bash command from java but, I need to insert
>> in command a double quotes  example:
[quoted text clipped - 7 lines]
> should do it.
> -Wayne

Echo  is also a separate command, but as such it needs no special
argument escaping:
 Process p = rt.exec(new String[]{"echo", "hello, world!"} );

If you use any newer than stone aged java versions (1.5 and up), you
can also make it shorter:
 Process p = rt.exec("echo", "hello, world!");
Wayne - 21 Mar 2008 22:54 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes  example:
[quoted text clipped - 15 lines]
> can also make it shorter:
>   Process p = rt.exec("echo", "hello, world!");

If you don't need to process backslash escapes, quotes, and
so on, using echo is easier.  However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.

Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted.  You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc.  Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:

   String arg = "Hello, \"World\"!";
   Process p = rt.exec( new String[]{"bash", "-c",
      "printf \"%s\\n\"" + arg} );

However that is uglier.

-Wayne
Wayne - 21 Mar 2008 23:00 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes  example:
[quoted text clipped - 15 lines]
> can also make it shorter:
>   Process p = rt.exec("echo", "hello, world!");

If you don't need to process backslash escapes, quotes, and
so on, using echo is easier.  However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.

Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted.  You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc.  Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:

   String arg = "Hello, \"World\"!";
   Process p = rt.exec( new String[]{"bash", "-c",
      "printf \"%s\\n\" " + arg} );

However that is uglier.

-Wayne
Wayne - 21 Mar 2008 23:08 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes  example:
[quoted text clipped - 15 lines]
> can also make it shorter:
>   Process p = rt.exec("echo", "hello, world!");

If you don't need to process backslash escapes, quotes, and
so on, using echo is easier.  However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.

Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted.  You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc.  Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:

   Process p = rt.exec( new String[]{"bash", "-c",
      "printf \"%s\\n\" '" + arg + "'"} );

However that is uglier (took me three tries to get the quoting right).

-Wayne
Andreas Leitgeb - 22 Mar 2008 08:48 GMT
>> Echo  is also a separate command, but as such it needs no special
>> argument escaping:
[quoted text clipped - 3 lines]
>> can also make it shorter:
>>   Process p = rt.exec("echo", "hello, world!");

> However I don't think that
> your "modern stone aged" version will work, did you try it?
> javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
> don't list that as a legal version of Runtime.exec() either.

Damn, you're right here.  Typical case of: posted too fast.
I (blindly) assumed that they did it like PrintStream's
printf with  "..."-ified argument, but obviously they
omitted that.  And there isn't even any other two(or more)-
String version of exec, that could have caused ambiguities.

Sorry for confusion.

>     Process p = rt.exec( new String[]{"bash", "-c",
>        "printf \"%s\\n\" '" + arg + "'"} );
> However that is uglier (took me three tries to get the quoting right).

It's still far away from right.  Just think about arg values like:
  "hello'`rm -rf /`'world"
(don't try that at home, folks :-)
The single quotes don't make it "secure" - unless you preprocess
the string and replace every "'" by this: "'\\''"  and even then you
need to worry about how many backslashes you really need.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.