6

Is there are way to send several commands on the same line using the spring-shell. For example:

shell> add 1 2; add 3 4
3
7

I've noticed that if I run my application from Intellij, I can copy paste several commands and it will work correctly:

add 1 2
add 3 4

But it doesn't work when I run the executable jar in bash. I think it's because the Terminal is different. In Intellij it's a DumbTerminal, when I run it in bash it's a PosixSysTerminal

0x26res
  • 7,536
  • 9
  • 48
  • 90
  • 1
    I've been through it and this helped me. https://www.robinhowlett.com/blog/2015/03/19/supporting-multi-step-commands-with-spring-shell/ – Lhew Jul 22 '20 at 12:20
  • This is interesting, but not exactly what I'm looking for. – 0x26res Jul 22 '20 at 13:10
  • Hi, did you consider using some sort a batch file? and maybe you'll find this useful https://mkyong.com/java/how-to-execute-shell-command-from-java/ – Xrio Jul 28 '20 at 18:42
  • out of curiosity... did you try using && ? like command 1 && command 2 – d3javu999 Jul 29 '20 at 06:35

1 Answers1

1

According to this The script command accepts a local file as an argument and will replay commands found there, one at a time. Reading from the file behaves exactly like inside the interactive shell, so lines starting with // will be considered as comments and ignored, while lines ending with \ will trigger line continuation.it is not possible to pass multiple commands on one line in spring-shell ,however you can modify the function to receive a string then split it or receive a string array and afterwards process the string or array yourself.

@ShellMethod("Add or subtract ,two integers together.")
    public static String add(String args) {
     args=args.trim();
    String output="";
    if(args.contains(";")){
        for(String arg:args.split(";"))
        {
              arg=arg.trim();
   
            if(arg.split(" ").length==3)
            {
                int first_fig=0;
                int second_fig=0;
                try{
                    first_fig=Integer.parseInt(arg.split(" ")[1]);
                    second_fig=Integer.parseInt(arg.split(" ")[2]);
                }catch (Exception ex){
                  //  System.out.println("Invalid Argument");
                    output+="Invalid Argument\n";
                    continue;
               
                    

                }
                if(arg.split(" ")[0].equalsIgnoreCase("add"))
                {
                    output+= (first_fig+second_fig)+"\n";
                    continue;
                  
                    

                }else  if(arg.split(" ")[0].equalsIgnoreCase("subtract"))
                {
                    output+= (first_fig-second_fig)+"\n";
                    continue;
                }else{
                    output+="Invalid Argument\n";
                    continue;

                }

            }else{
                output+="Invalid Argument\n";
                continue;
            }

        }
    }else{
        if(args.split(" ").length==3) {
            int first_fig = 0;
            int second_fig = 0;
            try {
                first_fig = Integer.parseInt(args.split(" ")[1]);
                second_fig = Integer.parseInt(args.split(" ")[2]);
            } catch (Exception ex) {
                output+="Invalid Argument\n";
          
            }
            if (args.split(" ")[0].equalsIgnoreCase("add")) {
               
                output+= (first_fig+second_fig)+"\n";

            } else if (args.split(" ")[0].equalsIgnoreCase("subtract")) {
            
                output+= (first_fig-second_fig)+"\n";
            } else {
              //  System.out.println("Invalid Argument");
              
                output+="Invalid Argument\n";

            }
        }else{
           // System.out.println("Invalid Argument");
         
            output+="Invalid Argument\n";
        }
    }

    return output;
}

Then you can comfortably call it like :

shell> add 1 2; add 3 4
Code Demon
  • 894
  • 1
  • 5
  • 9
  • Thanks for your answer, it's interesting. But I'd like to avoid having to implement the CLI logic my self (parsing arguments etc). – 0x26res Jul 30 '20 at 09:31