Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send multiple commands in the same line

Tags:

spring-shell

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

like image 981
0x26res Avatar asked May 06 '20 13:05

0x26res


People also ask

How do you write multiple commands in one line in Linux?

Linux allows you to enter multiple commands at one time. The only requirement is that you separate the commands with a semicolon. Running the combination of commands creates the directory and moves the file in one line.

How do I run multiple commands in a single batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

How do I combine two commands in Linux?

Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.

Can 2 commands be used simultaneously in Unix?

You can use multiple operators to run multiple commands at a time. In the following example, three commands are combined with OR (||) and AND (&&) operators. After running the command, first of all, it will change the current directory to newdir if the directory exists.

How do I separate multiple commands on one command line?

Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command. && [...] Use to run the command following && only if the command preceding the symbol is successful.

How do I run multiple PowerShell commands on the same line?

If you can, it really makes sense to use PowerShell pipelines. However, if you want to do run multiple unrelated commands on the same line, you can use the firstcommand; secondcommand method. In this case, the second command gets executed even if the first command fails.

How do I send multiple line commands over SSH?

A Simple Way to Send Multiple Line Commands Over SSH. Below are three methods to send multiple line commands over SSH. The first method is a quick overview of running remote commands over SSH, the second method uses the bash command to run remote commands over SSH, and the third method uses HERE documents to run remote commands over SSH.

Can you run multiple commands at once in Linux?

But it can be even more efficient if you run multiple commands at once. Combining two or more commands on the command line is also known as “command chaining”. We’ll show you different ways you can combine commands on the command line.


1 Answers

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
like image 122
Code Demon Avatar answered Oct 22 '22 18:10

Code Demon