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