1 Star 0 Fork 4.9K

丛林 / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kernel-small-debug-shell-build.md 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
Annie_wang 提交于 2022-05-19 22:02 . update docs

Shell Command Programming Example

Example Description

This section describes how to add a shell command test using static registration and dynamic registration.

Static Registration

Development process:

  1. Define the cmd_test function to be called to add a command.

  2. Use the SHELLCMD_ENTRY function to add the new command.

  3. Add a parameter to the liteos_tables_ldflags.mk link options for linking to the new command.

  4. Recompile the code and run it.

Static Registration Programming Example

  1. Define the cmd_test function to be called to add a command.

    #include "shell.h" 
    #include "shcmd.h"  
    int cmd_test(void) 
    {     
        printf("hello everybody!\n");     
        return 0; 
    }
  2. Add the new command.

    SHELLCMD_ENTRY(test_shellcmd, CMD_TYPE_EX, "test", 0, (CMD_CBK_FUNC)cmd_test);
  3. Add a parameter to the link options for linking to the new command.

    Add -utest_shellcmd to the LITEOS_TABLES_LDFLAGS option in the liteos_tables_ldflags.mk file.

  4. Recompile the code.

    make clean;make
  5. Run the help command to view all the commands registered in the system.

    The command output contains the test command (the following command output is for reference only.)

    OHOS # help
    *******************shell commands:*************************
    
    arp           cat           cd            chgrp         chmod         chown         cp            cpup          
    date          dhclient      dmesg         dns           format        free          help          hwi           
    ifconfig      ipdebug       kill          log           ls            lsfd          memcheck      mkdir         
    mount         netstat       oom           partinfo      partition     ping          ping6         pwd           
    reset         rm            rmdir         sem           statfs        su            swtmr         sync          
    tftp          touch         umount        uname         test         systeminfo    task          telnet        
    watch         writeproc     

Dynamic Registration

Development process:

  1. Use the osCmdReg function to add a new command.

  2. Recompile the code and run it.

Dynamic Registration Programming Example

  1. Call the osCmdReg function from the app_init function to dynamically register the command.

    #include "shell.h" 
    #include "shcmd.h"  
    int cmd_test(void) 
    {     
        printf("hello everybody!\n");     
        return 0; 
    }  
    void app_init(void) 
    {      
        ....      
        ....      
        osCmdReg(CMD_TYPE_EX, "test", 0,(CMD_CBK_FUNC)cmd_test);      
        .... 
    }
  2. Recompile the code.

    make clean;make
  3. Run the help command to view all the commands registered in the system.

    The command output contains the test command.

    OHOS # help
    *******************shell commands:*************************
    
    arp           cat           cd            chgrp         chmod         chown         cp            cpup          
    date          dhclient      dmesg         dns           format        free          help          hwi           
    ifconfig      ipdebug       kill          log           ls            lsfd          memcheck      mkdir         
    mount         netstat       oom           partinfo      partition     ping          ping6         pwd           
    reset         rm            rmdir         sem           statfs        su            swtmr         sync          
    tftp          touch         umount        uname         test          systeminfo    task          telnet        
    watch         writeproc     
1
https://gitee.com/jungle8023/docs.git
git@gitee.com:jungle8023/docs.git
jungle8023
docs
docs
master

搜索帮助