开发者

Handling empty argument to CommandLine in Commons Exec

开发者 https://www.devze.com 2023-02-21 17:21 出处:网络
I am trying to execute a bash script that takes 3 parameters from Java, following is the code where I create the command line

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 paramete开发者_如何学Gors 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.

  1. Absolute
  2. 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?


You can try handling the quotation yourself:

if (Param.isEmpty()) {
    command.addArgument("\"\"", false);
} else {
    command.addArgument(Param2);
}

or just:

command.addArgument("\"" + Param2 + "\"", false);

I never used it, but learning [:-)

Update: This is what worked for me (Salman):

CommandLine command = new CommandLine(/bin/bash);
command.addArgument(ScriptName);
command.addArgument(Param1);

if (Param2== null || Param2.trim().length() == 0) {
    command.addArgument("\"\"", false);
} else {
    command.addArgument(Param2);
}

command.addArgument(Param3);


Check the argument length before starting evaluation

Here is something from the top of my head :

public static void main(String[] args) {
        if (args.length != 3) {
            //print usage message throw exception or whatever System.exit afterwards
        }
        //individual agrument evaluation
        for (int i = 0; i < args.length; i++) {
            //if length less then one, or whatever you prefer
            if(args[i].length() < 1){
                //print usage message
                //throw exception exit the program 
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号