Hello guys;
The bellow code will help you to write the Java code to pass argument in shell script and execute shell script on ubuntu.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyScript {
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException {
try{
System.out.println("start excution......");
String[] command = {"/bin/bash", "/home/bhagwan/shellscript/myscript.sh", "Bhagwan"};
ProcessBuilder p = new ProcessBuilder(command);
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("ending excution......");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
myscript.sh
#! /bin/bash
echo "Hello , Lets be friend! $@"
Thank You
0 Comment(s)