1e41f4b71Sopenharmony_ci# Shell Command Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can perform the following operations to add shell commands: 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci1. Include header files. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci ``` 9e41f4b71Sopenharmony_ci #include "shell.h" 10e41f4b71Sopenharmony_ci #include "shcmd.h" 11e41f4b71Sopenharmony_ci ``` 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci2. Register commands. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci You can register commands either statically or dynamically (with the system running). Generally, common system commands are registered statically, and user commands are registered dynamically. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci - Static registration 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 1. Register a command using a macro. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci The prototype of the macro is as follows: 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci SHELLCMD_ENTRY(l, cmdType, cmdKey, paraNum, cmdHook) 25e41f4b71Sopenharmony_ci ``` 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci **Table 1** SHELLCMD_ENTRY parameters 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci | Parameter| Description| 30e41f4b71Sopenharmony_ci | -------- | -------- | 31e41f4b71Sopenharmony_ci | l | Specifies the global variable name passed in static registration. Note that the name cannot be the same as other symbol names in the system.| 32e41f4b71Sopenharmony_ci | cmdType | Specifies the command type, which can be any of the following:<br>**CMD_TYPE_EX**: does not support standard command parameters and will mask the command keywords you entered. For example, if you enter **ls /ramfs**, only **/ramfs** will be passed to the registration function and **ls** will be masked.<br>**CMD_TYPE_STD**: supports standard command parameters. All the characters you entered will be passed to the registration function after being parsed. | 33e41f4b71Sopenharmony_ci | cmdKey | Specifies the command keyword, which is the name used to access a shell function.| 34e41f4b71Sopenharmony_ci | paraNum | Specifies the maximum number of input parameters in the execution function to be called. This parameter is not supported currently.| 35e41f4b71Sopenharmony_ci | cmdHook | Specifies the address of the execution function, that is, the function executed by the command.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci Example: 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci ``` 40e41f4b71Sopenharmony_ci SHELLCMD_ENTRY(ls_shellcmd, CMD_TYPE_EX, "ls", XARGS, (CMD_CBK_FUNC)osShellCmdLs) 41e41f4b71Sopenharmony_ci ``` 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 2. Add options to the **build/mk/liteos_tables_ldflags.mk** file. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci For example, when registering the **ls** command, add **-uls_shellcmd** to the **build/mk/liteos_tables_ldflags.mk** file. **-u** is followed by the first parameter of **SHELLCMD_ENTRY**. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci - Dynamic registration 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci The prototype of the function to register is as follows: 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci ``` 53e41f4b71Sopenharmony_ci UINT32 osCmdReg(CmdT ype cmdType, CHAR *cmdKey, UINT32 paraNum, CmdCallBackFunc cmdProc) 54e41f4b71Sopenharmony_ci ``` 55e41f4b71Sopenharmony_ci **Table 2** UINT32 osCmdReg parameters 56e41f4b71Sopenharmony_ci | Parameter| Description| 57e41f4b71Sopenharmony_ci | -------- | -------- | 58e41f4b71Sopenharmony_ci | cmdType | Specifies the command type, which can be any of the following:<br>**CMD_TYPE_EX**: does not support standard command parameters and will mask the command keywords you entered. For example, if you enter **ls /ramfs**, only **/ramfs** will be passed to the registration function, and **ls** will be masked.<br>**CMD_TYPE_STD**: supports standard command parameters. All the characters you entered will be passed to the registration function after being parsed.| 59e41f4b71Sopenharmony_ci | cmdKey | Specifies the command keyword, which is the name used to access a shell function.| 60e41f4b71Sopenharmony_ci | paraNum | Specifies the maximum number of input parameters in the execution function to be called. This parameter is not supported currently. The default value is **XARGS(0xFFFFFFFF)**.| 61e41f4b71Sopenharmony_ci | cmdHook | Specifies the address of the execution function, that is, the function executed by the command.| 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci Example: 64e41f4b71Sopenharmony_ci ``` 65e41f4b71Sopenharmony_ci osCmdReg(CMD_TYPE_EX, "ls", XARGS, (CMD_CBK_FUNC)osShellCmdLs) 66e41f4b71Sopenharmony_ci ``` 67e41f4b71Sopenharmony_ci  NOTE<br> 68e41f4b71Sopenharmony_ci > The command keyword must be unique. That is, two different commands cannot share the same command keyword. Otherwise, only one command is executed. When executing user commands sharing the same keyword, the shell executes only the first command in the **help** commands. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci3. Use the following function prototype to add built-in commands: 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci ``` 74e41f4b71Sopenharmony_ci UINT32 osShellCmdLs(UINT32 argc, CHAR **argv) 75e41f4b71Sopenharmony_ci ``` 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci **Table 3** osShellCmdLs parameters 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci | Parameter| Description| 80e41f4b71Sopenharmony_ci | -------- | -------- | 81e41f4b71Sopenharmony_ci | argc | Specifies the number of parameters in the shell command.| 82e41f4b71Sopenharmony_ci | argv | Specifies a pointer array, where each element points to a string. You can determine whether to pass the command keyword to the registration function by specifying the command type.| 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci4. Enter the shell command in either of the following methods: 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci - Enter the shell command using a serial port tool. 87e41f4b71Sopenharmony_ci - Enter the shell command using the Telnet tool. For details about how to use Telnet, see [telnet](../kernel/kernel-small-debug-shell-net-telnet.md). 88