07.SpringShell内置命令详解

SpringShell 除了允许我们自定义命令之外, 还提供了一些内置命令, 用于辅助我们操作. 笔者使用的是SpringShell 2.0版本, 内置命令只提供了五个:help, clear, stacktrace, script, exit/quite.

1.help命令-查看帮助

1.1 查看所有内置命令

help 本身就是内置命令, 不跟参数的help命令会打印所有内置命令.

shell:>help
AVAILABLE COMMANDS

Built-In Commands
        clear: Clear the shell screen.
        exit, quit: Exit the shell.
        help: Display help about available commands.
        script: Read and execute commands from a file.
        stacktrace: Display the full stacktrace of the last error.

1.2 查看命令详情

可通过help + 命令方式, 查看命令详情.

shell:>help add
NAME
    add - 计算两个整数的加法
SYNOPSYS
    add [-a] int  [-b] int
OPTIONS
    -a  int
        [Mandatory]
    -b  int
        [Mandatory]
ALSO KNOWN AS
    sum

2. clear命令-清屏

SpringShell 也提供了类似于linux shell的清屏方式, 输入clear 或使用Ctrl+L快捷键.

shell:>clear

3. stacktrace命令-查看异常堆栈信息

默认情况下, 命令执行抛出的异常, 只会输出异常的内容, 不会输出异常的堆栈信息. 当命令发生异常时, 我们可以通过stacktrace 来查看异常的堆栈信息. 需要注意的是, stacktrace 永远只保存上一次的异常的堆栈信息.

shell:>div 2 0
/ by zero
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.

shell:>stacktrace
java.lang.ArithmeticException: / by zero
    at org.zongf.learn.spring.shell.cmd.CalculatorCommands.div(CalculatorCommands.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:246)
    at org.springframework.shell.Shell.evaluate(Shell.java:169)
    at org.springframework.shell.Shell.run(Shell.java:134)
    at org.springframework.shell.jline.InteractiveShellApplicationRunner.run(InteractiveShellApplicationRunner.java:84)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:794)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at org.zongf.learn.spring.shell.SpringShellApplication.main(SpringShellApplication.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

4. script命令-执行脚本

script命令使批量执行命令成了可能, 我们可以将一组命令存入文件, 然后批量执行. 需要注意的时, 文件名需要使用绝对路径.

4.1 创建批量脚本文件

脚本存放在 /tmp/zongf/cmds

add 1 2
add 2 3
add 3 4
div 4 2
div 9 3

4.2 执行脚本

shell:>script /tmp/zongf/cmds
3
5
7
2
3

5. exit/quit命令-退出应用

exit和quite命令互为别名, 使用哪个都可以退出应用.

shell:>exit
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 6,293评论 0 10
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,683评论 0 5
  • 第 2 章 SHELL 基础知识2.1 shell脚本我们在上面简单介绍了一下什么是shell脚本,现在我们来进一...
    LiWei_9e4b阅读 1,639评论 0 0
  • 这篇文章在介绍官网的同时使用了比较多的脚本示例,示例里遇到的问题有部分在本篇文章进行了解释,还有一篇文章专门记录了...
    顾顾314阅读 13,073评论 3 32
  • 在路边摊意外遭遇武志红的巨婴,翻开扉页时就傻眼了,原来生活是如此的幼稚可笑,而我们还郑重其事的活着。 这里想说的不...
    三颗红豆豆阅读 366评论 0 1

友情链接更多精彩内容