Pass a variable from Java to a Shell script

后端 未结 2 1577
一生所求
一生所求 2021-01-25 22:36

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

2条回答
  •  日久生厌
    2021-01-25 22:55

    Use shell variables in the script, set them in Java through the environment:

    import java.util.Map;
    
    class so1 {
    
        public static void main(String[] args) {
    
            try {
                ProcessBuilder pb = new ProcessBuilder("/home/...sh");
                Map env = pb.environment();
                env.put("VAR1", "something");
                Process p = pb.start();    
                p.waitFor();              
                System.out.println("Script executed successfully");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    In the script:

    /usr/local/virtuoso-opensource/bin/isql 1111 dba dba exec="SPARQL CREATE GRAPH $VAR1;"&
    

提交回复
热议问题