I am trying to execute a bash script that takes 3 parameters from Java, following is the code where I create the command line
CommandLine command = new CommandLine(/bin/bash);
command.addArgument(ScriptName);
command.addArgument(Param1);
command.addArgument(Param2);
command.addArgument(Param3);
This works like a charm when I have non empty parameters being passed. But does not work as supposed to the one of the parameter is empty (i.e. "")
To elaborate, in some cases Param2 = "". Now when the bash script is executed, instead of considering Param2 = "", it takes Param2 = Param 3 (value) and Param3 = undefined.
How do I stop this from happening?
Edit: Param1, Param2 & Param3 are actually files names that are passed to the bash script. Where, Param1 => File1, Param2 => File 2 & Param3 => Output File
Bash script is actually calling and generating some metrics on the File 1 and storing it in Output File. When generating the metrics there are 2 categories of metrics that are being generated.
- Absolute
- Relative
Absolute are generate on File 1 while the relative are generated after comparing File 1 & File 2. In the bash script I have condition there if the File 2 is not passed in as an argument, do not generate relative metrics.
This is what the design has been. Now problems comes in when there is no File 2 present for comparison in which case I want the absolute metrics to be generated and Relative ones to be left out.
But right now whats happening with me is Param 3 (i.e. is the output file) is considered as the File 2 (which is completely undesired) and my relative metric generation goes for a toss.
When using Runtime.getRuntime().exec i was able to do this by quoting it. i.e. /bin/bash Script Param1 "" Param3 this worked like a charm then.
In my attempt to reduce the code and have good cross-platform support I introduced commons-exec and and the problem started appearing.
So, My question still is, How do I pass and empty value for Param2 to bash script using commons-exec CommandLine?