2

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.

  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?

Salman A. Kagzi
  • 3,833
  • 13
  • 45
  • 64

2 Answers2

4

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);
Salman A. Kagzi
  • 3,833
  • 13
  • 45
  • 64
user85421
  • 28,957
  • 10
  • 64
  • 87
  • This really did work. Updating your answer to show snippet that I have used. I wonder if there is a better way to do this. Seems like a hack right now. – Salman A. Kagzi Mar 31 '11 at 11:53
  • +1@Carlos Heuberger this is correct, I see that the question was updated – ant Mar 31 '11 at 14:30
2

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 
            }
        }
    }
ant
  • 22,634
  • 36
  • 132
  • 182
  • Do you mean length of argument value or number of arguments? I also suppose you mean to do this in bash script? I dont want to do that. I want to pass an empty argument for Param2. how do I achieve that? – Salman A. Kagzi Mar 31 '11 at 10:25
  • no I mean check argument length in your java class, not their values if length != 3 throw exception or however you handle. – ant Mar 31 '11 at 10:29
  • Can I not pass an empty string for Param2? This is a valid condition in my code. When using Runtime.getRuntime().exec i was able to do this by quoting it. i.e. /bin/bash Script Param1 "" Param3 this worked then. How do I do with commons-exec? – Salman A. Kagzi Mar 31 '11 at 10:33
  • @Salman Can I not pass an empty string for Param2? - yes you may, that is what I said, check the arguments length, then evaluate them all trough for loop or something(again check length) for each argument and you should be good. – ant Mar 31 '11 at 10:56
  • Please check the updates on my question. I have elaborated it to give more context to my question. I think we are not on the same page with the question here. – Salman A. Kagzi Mar 31 '11 at 11:16