• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Shell Command Programming Example<a name="EN-US_TOPIC_0000001134006246"></a>
2
3-   [Example Description](#section87143612316)
4-   [Static Registration](#section1660495712314)
5-   [Static Registration Programming Example](#section1233411684113)
6-   [Dynamic Registration](#section6804126192412)
7-   [Dynamic Registration Programming Example](#section2335121613418)
8
9## Example Description<a name="section87143612316"></a>
10
11This section describes how to add a shell command  **test**  using static registration and dynamic registration, respectively.
12
13## Static Registration<a name="section1660495712314"></a>
14
15Development process:
16
171.  Define the  **cmd\_test**  function to be called to add a command.
18
192.  Use the  **SHELLCMD\_ENTRY**  function to add the new command.
20
213.  Add a parameter to the  **liteos\_tables\_ldflags.mk**  link options for linking to the new command.
22
234.  Recompile the code and run it.
24
25
26## Static Registration Programming Example<a name="section1233411684113"></a>
27
281.  Define the  **cmd\_test**  function to be called to add a command.
29
30    ```
31    #include "shell.h"
32    #include "shcmd.h"
33    int cmd_test(void)
34    {
35        printf("hello everybody!\n");
36        return 0;
37    }
38    ```
39
402.  Add the new command.
41
42    ```
43    SHELLCMD_ENTRY(test_shellcmd, CMD_TYPE_EX, "test", 0, (CMD_CBK_FUNC)cmd_test);
44    ```
45
463.  Add a parameter to the link options for linking to the new command.
47
48    Add  **-utest\_shellcmd**  to the  **LITEOS\_TABLES\_LDFLAGS**  option in the  **liteos\_tables\_ldflags.mk**  file.
49
504.  Recompile the code.
51
52    ```
53    make clean;make
54    ```
55
565.  Run the  **help**  command to view all the commands registered in the system.
57
58    The command output contains the  **test**  command \(the following command output is for reference only.\)
59
60    ```
61    OHOS # help
62    *******************shell commands:*************************
63
64    arp           cat           cd            chgrp         chmod         chown         cp            cpup
65    date          dhclient      dmesg         dns           format        free          help          hwi
66    ifconfig      ipdebug       kill          log           ls            lsfd          memcheck      mkdir
67    mount         netstat       oom           partinfo      partition     ping          ping6         pwd
68    reset         rm            rmdir         sem           statfs        su            swtmr         sync
69    tftp          touch         umount        uname         test         systeminfo    task          telnet
70    watch         writeproc
71    ```
72
73
74## Dynamic Registration<a name="section6804126192412"></a>
75
76Development process:
77
781.  Use the  **osCmdReg**  function to add a new command.
79
802.  Recompile the code and run it.
81
82
83## Dynamic Registration Programming Example<a name="section2335121613418"></a>
84
851.  Call the  **osCmdReg**  function from the  **app\_init**  function to dynamically register the command.
86
87    ```
88    #include "shell.h"
89    #include "shcmd.h"
90    int cmd_test(void)
91    {
92        printf("hello everybody!\n");
93        return 0;
94    }
95    void app_init(void)
96    {
97        ....
98        ....
99        osCmdReg(CMD_TYPE_EX, "test", 0,(CMD_CBK_FUNC)cmd_test);
100        ....
101    }
102    ```
103
1042.  Recompile the code.
105
106    ```
107    make clean;make
108    ```
109
1103.  Run the  **help**  command to view all the commands registered in the system.
111
112    The command output contains the  **test**  command.
113
114    ```
115    OHOS # help
116    *******************shell commands:*************************
117
118    arp           cat           cd            chgrp         chmod         chown         cp            cpup
119    date          dhclient      dmesg         dns           format        free          help          hwi
120    ifconfig      ipdebug       kill          log           ls            lsfd          memcheck      mkdir
121    mount         netstat       oom           partinfo      partition     ping          ping6         pwd
122    reset         rm            rmdir         sem           statfs        su            swtmr         sync
123    tftp          touch         umount        uname         test          systeminfo    task          telnet
124    watch         writeproc
125    ```
126
127
128