I am trying to run a shell script from a Java code. At the moment I am providing data manually in the script, but I would like to be able to provide the variables from the Java code that runs the script.
This is the Java code and the script:
public static void main(String[] args) {
try {
ProcessBuilder pb = new ProcessBuilder(
"/home/najib/upload.sh");
Process p = pb.start();
p.waitFor();
System.out.println("Script executed successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
The shell code:
#!/bin/bash
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="set AUTOCOMMIT MANUAL;"&
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="delete from DB.DBA.load_list;"&
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="SPARQL CREATE GRAPH <VAR1>;"&
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="ld_dir('/home/najib', 'VAR2', 'VAR1');"&
wait
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="rdf_loader_run();"
I would like to know where I can put the variables in the Java code, and how to then call it in run time!
Edit: Based on the solutions provided, I changed the script to this:
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="SPARQL CREATE GRAPH <$2>;"&
/usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="ld_dir('/home/najib', '$1', '$2');"&
I also added an echo at the end of the script to see if the variables are being passed correctly, and it seems that they are. The first command (the SPARQL one) seems to work as the graph is indeed created, but the second one is not processed correctly..