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
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.
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.
Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With