Executing unix command in java using semicolon

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
LVL 1
thimerionAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
aman123_123Connect With a Mentor Commented:
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
 
CEHJCommented:
You need to invoke the shell intepreter, especially since the first command is an alias

Runtime.exec("/bin/bash ...................
0
 
CEHJCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
thimerionAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJConnect With a Mentor Commented:
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
 
objectsConnect With a Mentor Commented:
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
 
thimerionAuthor Commented:
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
 
objectsCommented:
try:

bash -c 'll;cd'

0
 
objectsConnect With a Mentor Commented:
or as a array of strings as {"bash", "-c", "ll;cd"}
0
 
CEHJConnect With a Mentor Commented:
>>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
 
gheistCommented:
Java has its own file access facilities.
0
 
thimerionAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.