首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在同一行中发送多个命令

在同一行中发送多个命令
EN

Stack Overflow用户
提问于 2020-05-06 21:44:52
回答 1查看 334关注 0票数 6

有没有办法使用spring-shell在同一行上发送多个命令。例如:

代码语言:javascript
复制
shell> add 1 2; add 3 4
3
7

我注意到,如果我从Intellij运行我的应用程序,我可以复制粘贴几个命令,它将正常工作:

代码语言:javascript
复制
add 1 2
add 3 4

但是当我在bash中运行可执行jar时,它不起作用。我认为这是因为Terminal是不同的。在Intellij中它是一个DumbTerminal,当我在bash中运行它时它是一个PosixSysTerminal

EN

回答 1

Stack Overflow用户

发布于 2020-07-29 18:20:20

根据this的说法,脚本命令接受本地文件作为参数,并将重放在那里找到的命令,一次一个。从文件中读取的行为与交互式shell中的行为完全相同,因此以//开头的行将被视为注释并被忽略,而以\结尾的行将触发行continuation.it在spring-shell中不可能在一行上传递多个命令,但是您可以修改函数以接收字符串,然后拆分它或接收字符串数组,然后自己处理字符串或数组。

代码语言:javascript
复制
@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;
}

然后,您可以轻松地将其称为:

代码语言:javascript
复制
shell> add 1 2; add 3 4
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61636785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档