We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › using open() to execute a bash script
Page Index Toggle Pages: 1
using open() to execute a bash script (Read 1354 times)
using open() to execute a bash script
Dec 31st, 2009, 1:01pm
 
I am trying to use the open() function to execute a script, however when I call open on "/home/user/script" the script is open in a text editor instead of being executed, I have used chmod to make the script executable for all users but I'm not sure what I'm missing.

Code:

open("/home/user/script")

//also tried using exec:

String[] params = { "exec", "/home/user/script" };
open(params);



Thanks for any suggestions
Re: using open() to execute a bash script
Reply #1 - Dec 31st, 2009, 1:32pm
 
Try to give it a .sh extension.

Or maybe run as something like
Process p = Runtime.getRuntime().exec("/home/user/script");
Re: using open() to execute a bash script
Reply #2 - Dec 31st, 2009, 2:14pm
 
Thanks jeffg,
I already tried adding .sh, and I'm not sure how to utilize:

Code:
Process p = Runtime.getRuntime().exec("/home/user/script");  



Could you elaborate or point me to some documentation?

Thanks again
Re: using open() to execute a bash script
Reply #3 - Dec 31st, 2009, 2:23pm
 
open page says:
"On Linux, it first tries gnome-open, then kde-open, but if neither are available, it sends the command to the shell without any alterations."

what does the file bash command say for the file?
file /home/user/script

try adding #!/bin/bash as the first line and make sure it's executable. (.sh isn't enough, unix doesn't use extensions to determine filetypes)
Re: using open() to execute a bash script
Reply #4 - Dec 31st, 2009, 2:29pm
 
file outputs:
Bourne-Again shell script text executable

I have #!/bin/bash at the top, the script runs fine directly from the shell.

I'm running XFCE4 but that shouldn't make a difference
Re: using open() to execute a bash script
Reply #5 - Dec 31st, 2009, 3:27pm
 
you could also try
Code:

Open("bash /home/user/script");


Code:

String line;
Process p = Runtime.getRuntime().exec("/home/user/script");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {
println(line);
}

input.close();
Re: using open() to execute a bash script
Reply #6 - Jan 1st, 2010, 7:03pm
 
thanks jeffg,
I'm going to try out your getRuntime().exec method.


I have had success by giving the script a unique extension such as
Code:
script.myext 


then right clicking and having bash as the default application for ".myext"

this doesn't feel like the best way to go though
Re: using open() to execute a bash script
Reply #7 - Jan 2nd, 2010, 2:48am
 
Perhaps searching what other people tried before can help (or not...):
Execute shell script xubuntu
At least, cross-referencing is good! Wink
Page Index Toggle Pages: 1