Shell命令行工具,提供了一套供用户在命令行调用的操作接口,主要用于调试或查看系统信息。在控制终端输入命令,控制终端通过某个通道(例如串口)将命令传给设备里的Shell,Shell会读取设备输入命令,解析并自动扫描内部函数表,寻找对应函数名,执行函数后输出回应,回应通过原路返回,将结果显示在控制终端上。了解其能够帮助开发者轻易调用其他语言编写的程序,本文将详细介绍Shell工具的原理并给出Shell应用示例。
-
一、Shell是什么
Shell是一种用于连接用户和内核的命令行界面,可用于运行程序、安装驱动程序和管理系统设置等,能提供用户与内核进行交互的接口。换言之,Shell是一个用户界面,可以用来访问操作系统的核心功能,如文件管理、程序运行和系统管理等。它提供了一个简单的界面,用户可以使用简单的命令来完成复杂的任务,这样可以更轻松地控制计算机。
Shell是用户和内核之间的桥梁,用户通过Shell来控制系统,而内核则是实现这些控制功能的核心部分。Shell可以接受用户输入的命令,将其转换为内核可以理解的格式,然后内核根据命令执行相应操作,并将结果返回给用户。传递命令时,Shell将命令解释为二进制形式;返回结果时,Shell将结果解释为字符形式,因此Shell又被称为命令解释器。
Shell提供了多种方式以实现用户与内核的通讯,主要有命令行模式,脚本模式和图形模式。命令行模式允许用户输入命令,而脚本模式则是用户编写脚本,然后由Shell来执行。图形模式提供了一个图形界面,用户可以通过图形界面来操作系统。
Shell编程语言简单易学,一旦掌握后它将是最得力的工具。它提供了一个简单的界面,用户可以使用简单的命令来完成复杂的任务;它可以与其他程序和服务进行交互;它可以定制脚本来自动执行任务;同时,它还可以提供一个安全的环境,可以防止误操作或恶意攻击。
-
二、Shell实现原理
OneOS 操作系统Shell模块系统结构包含3个数据区和6个逻辑处理单元。3个数据区分别为符号表只读数据区、当前命令数据区、历史命令数据区;6个逻辑处理单元分别为字符接收、Shell认证、字符处理、命令补齐、命令执行、历史命令翻阅,如下图:
-
三、Shell使用示例
系统内置shell命令
OneOS操作系统内置了一些shell命令,在控制终端中输入help,就可以看到当前系统支持的所有命令。下面我们列出一些比较有用的命令:
sh>help
OneOS shell commands:
atest_run - atest_run [-n testacse name] [-l loop count] [-p priority level] [-t] [-s] [-h]
atest_list - Display all atest testcases
dlog_glvl_ctrl - Dlog global level control
dlog_tlvl_ctrl - Dlog tag level control
dlog_gtag_ctrl - Dlog global tag control
dlog_gkw_ctrl - Dlog global keyword control
dlog_flush - Flush dlog cache
reboot - reboot
device - show device information
pinMode - set hardware pin mode
pinWrite - write value to hardware pin
pinRead - read status from hardware pin
show_event - show event information
show_mb - Show mailbox information
show_mempool - Show mempool information
show_heap - show memheap information
show_mem - show memory usage information
check_mem - check memory data
show_mq - Show message queue information
show_mutex - Show mutex information
show_sem - show semaphore information
show_task - Show task information
version - show oneos version
set_prompt - Set shell prompt
help - Obtain help of commands
配置选项
OneOS在使用Shell时提供了功能和裁剪的配置,具体配置如下图所示:
(Top) → Components→ Shell
OneOS Configuration
[*] Enable shell
(tshell) The shell task name
(20) The priority level value of shell task
(2048) The stack size for shell task
[*] Enable command history feature
(5) The command history line number
[*] Keep description in symbol table
[ ] Disable the echo mode in default
(80) The command line size for shell
(256) The prompt line size for shell
(10) The command arg num for shell
[ ] Shell support authentication
配置项 |
说明 |
Enable shell |
使能shell功能,如果不使能该功能,shell相关的源代码就不会编译,默认使能 |
The shell task name |
shell任务名字 |
The priority level value of shell task |
shell任务优先级,默认为20 |
The stack size for shell task |
shell任务栈大小,默认为2048 |
Enable command history feature |
使能shell命令缓存功能,默认使能 |
The command line size for shell |
shell命令的最大长度,默认80 |
The command arg num for shell |
shell命令的最大参数个数,默认10 |
使用示例
不带参数的shell命令使用示例
本例演示如何自定义最简单的不带参数的shell命令,代码如下:
#include <oneos_config.h>
#include <dlog.h>
#include <shell.h>
#define TEST_TAG "TEST"
void helloworld_sample(void)
{
LOG_W(TEST_TAG, "hello, world!!!");
}
SH_CMD_EXPORT(helloworld, helloworld_sample, "test for shell cmd");
编译并下载程序后,在控制终端输入help然后回车,可以看到该命令:
sh>help
OneOS shell commands:
...
helloworld - test for shell cmd
...
运行该命令,结果如下:
sh>helloworld
W/TEST: hello, world!!!
带参数的shell命令使用示例
本例演示如何自定义带参数的shell命令,代码如下:
#include <oneos_config.h>
#include <dlog.h>
#include <shell.h>
#include <string.h>
#define TEST_TAG "TEST"
static void multi_param_sample(int32_t argc, char **argv)
{
if (argc != 2)
{
LOG_W(TEST_TAG, "usage: multi_param_cmd <action1|action2>");
return;
}
if (!strcmp(argv[1], "action1"))
{
LOG_W(TEST_TAG, "do action1");
}
else if (!strcmp(argv[1], "action2"))
{
LOG_W(TEST_TAG, "do action2");
}
else
{
LOG_W(TEST_TAG, "invalid action");
}
}
SH_CMD_EXPORT(multi_param_cmd, multi_param_sample, "test for multi parameter shell cmd");
编译并下载程序后,在控制终端输入help然后回车,可以看到该命令:
sh>help
OneOS shell commands:
...
multi_param_cmd - test for multi parameter shell cmd
...
运行该命令,结果如下:
sh>multi_param_cmd action1
W/TEST: do action1
sh>multi_param_cmd action2
W/TEST: do action2
sh>multi_param_cmd action3
W/TEST: invalid action