当前位置: 首页 > 工具软件 > Spring Shell > 使用案例 >

07.SpringShell内置命令详解

罗源
2023-12-01

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
 类似资料: