Solved

Executing unix command in java using semicolon

Posted on 2008-06-23
13
1,040 Views
Last Modified: 2013-12-06
Hi,

I want to execute 2 commands in one go, in unix this works like this:
ll ; cd ..

But when I call unix from out of java :
Process p = Runtime.getRuntime().exec("ll ; cd ..");

It threads the ; not as a delimiter for the previous command, but just like a character.
this results in ll semicolon, which gives of course the error message:
"; not found"
instead of executing ll and then cd ..

Any idea?

Tim
0
Comment
Question by:thimerion
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21845813
You need to invoke the shell intepreter, especially since the first command is an alias

Runtime.exec("/bin/bash ...................
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21845829
Furthermore, cd is a shell built-in, not a programme. Make sure that you're aware of the issues here too:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 
LVL 1

Author Comment

by:thimerion
ID: 21845917
Hi,
Sorry to have mislead you ...
I made the commands simpler for the example , but what I really try to execute is:
String cmd = new String("mv " + sourceDir + "/" + Message.getInputFname() + " " + targetDir
           + " ; ln " + targetDir + "/" + tsMessage.getInputFname() + " " + temparchInDir + "/" + Message.getInputFname());
0
 
LVL 4

Accepted Solution

by:
aman123_123 earned 50 total points
ID: 21846111
Hi Thimerion,
You can achieve this by passing a array of Strings to the exec() function. Each element of the array will contain one command.

Hope this helps,
Aman

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21846337
>>but what I really try to execute is

That's OK. It's probably better to invoke the interpreter anyway, just in case. Using an array is always better than using a String since escaping is handled better. Each element would contain an argument
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 21847090
Personally i'd split the commands up into two invocations. Redirecting, you might be able avoid handling the streams by doing something like:
String[] cmd1 = new String[] { "/bin/bash", "mv", sourceDir + "/" + Message.getInputFname(), targetDir, "2>&1 >/dev/null" };

Runtime.getRuntime().exec(cmd1);
 

// Same principle for the ln command

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 21851153
easiest would be to put two in a script and call scriipt using Runtime.exec.
Simpler code, and easier to mainain allowing you to change the commands without recompiling

0
 
LVL 1

Author Comment

by:thimerion
ID: 21853176
Hi,

I agree it is easier to just execute them one by one, but I want to save execution time.

Tried passing a array of Strings but that didn't work for me.  Also I guess that using that array of Strings is going to invoke the command interpreter twice.

I was looking for a way to escape the ; character so that it is back to the right meaning at unix level.

Regards,
Tim
0
 
LVL 92

Expert Comment

by:objects
ID: 21853242
try:

bash -c 'll;cd'

0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 21853248
or as a array of strings as {"bash", "-c", "ll;cd"}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 21853519
>>Also I guess that using that array of Strings is going to invoke the command interpreter twice.

No - just once

>>I was looking for a way to escape the ; character so that it is back to the right meaning at unix level.

You don't escape it. If you want to use it, just pass it as one of the elements of the array.

If you're having a problem, be sure to read the link i posted. The stream redirection i showed you is a kludge - if there is an error at the shell, you'll never see it
0
 
LVL 61

Expert Comment

by:gheist
ID: 21873508
Java has its own file access facilities.
0
 
LVL 1

Author Comment

by:thimerion
ID: 21945765
Sorry haven't had time yet to review it because I am put on another project, I'll accept the solutions that I think that will work,
thanks guys!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now