Pass a variable from Java to a Shell script

后端 未结 2 1554
一生所求
一生所求 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 23:02

    You can pass params to your shell script and use them inside de script, The Class ProcessBuilder have a constructor that take the command to execute and the list of parameters to pass to the executable :

    ProcessBuilder(String... command)
    

    you have to pass params like this :

     ProcessBuilder pb = new ProcessBuilder("/home/najib/upload.sh",param1,param2,param3);
    

    inside the script param1 is $1 , param2 is $2 , and param3 is $3 (you can pass in the constructor as many arguments as you want )

提交回复
热议问题