• shell编程:定义简单标准命令集


    shell是用户操作接口的意思,操作系统运行起来后都会给用户提供一个操作界面,这个界面就叫shell,用户可以通过shell来调用操作系统内部的复杂实现,而shell编程就是在shell层次上进行编程,如Linux中的脚本编程。

    shenll运行原理:由消息接收、解析、执行构成的死循环。

    命令行shell:该死循环包含3个模块(命令接收、命令解析、命令执行),命令行有一个标准命令集,用户输入的命令若不是标准命令,则提示用户这不是一个合法命令行,然后重新回到命令行让用户输入下一个命令。

    常见的shell:uboot、Linux终端、Windows图形界面等

    shell实例1:使用printf和scanf做输入回显

    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LINE_LENGTH        256        // 定义命令行长度,命令不能超过这个长度
    
    int main(void)
    {
        char str[MAX_LINE_LENGTH];            // 用来存放用户输入的命令内容
        
        while (1)
        {
            // 打印命令行提示符,注意不能加换行
            printf("Please input your command:#");
            // 清除str数组以存放新的字符串
            memset(str, 0, sizeof(str));
            // shell第一步:获取用户输入的命令
            scanf("%s", str);
            // shell第二步:解析用户输入命令
            
            // shell第三步:处理用户输入命令
            printf("%s
    ", str);
        }
    
        return 0;
    }    

    memset(str,0,sizeof(str))

     1 //清除数组
     2 void memset(char *p, int val, int length)
     3 {
     4     int i;
     5   
     6     for (i=0; i<length; i++)
     7     {
     8         p[i] = val;
     9     }
    10 }

    shell实例2:解析用户输入命令并回显

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define MAX_LINE_LENGTH        256            // 命令行长度,命令不能超过这个长度
     4 
     5 // 宏定义一些标准命令
     6 #define led                    "led"
     7 #define lcd                    "lcd"
     8 #define pwm                    "pwm"
     9 #define CMD_NUM                3            // 当前系统定义的命令数 
    10 
    11 char g_cmdset[CMD_NUM][MAX_LINE_LENGTH];
    12 
    13 // 初始化命令列表
    14 static void init_cmd_set(void)
    15 {
    16     memset(g_cmdset, 0, sizeof(g_cmdset));        // 先全部清零
    17     strcpy(g_cmdset[0], led);
    18     strcpy(g_cmdset[1], lcd);
    19     strcpy(g_cmdset[2], pwm);
    20 }
    21 
    22 int main(void)
    23 {
    24     int i = 0;
    25     char str[MAX_LINE_LENGTH];            // 用来存放用户输入的命令内容
    26     
    27     init_cmd_set();
    28     
    29     while (1)
    30     {
    31         // 打印命令行提示符,注意不能加换行
    32         printf("Please input your command:#");
    33         // 清除str数组以存放新的字符串
    34         memset(str, 0, sizeof(str));
    35         // shell第一步:获取用户输入的命令
    36         scanf("%s", str);
    37         // shell第二步、第三步:解析用户输入命令、处理用户输入命令
    39         for (i=0; i<CMD_NUM; i++)
    40         {
    41             if (!strcmp(str, g_cmdset[i]))
    42             {
    43                 // 相等,找到了这个命令,执行这个命令所对应的动作。
    44                 printf("您输入的命令是:%s,是合法的
    ", str);
    45                 break;
    46             }    
    47         }
    48         if (i >= CMD_NUM)
    49         {
    50             // 找遍了输入命令都没找到这个符合要求的,则输出相应指示
    51             printf("%s不是一个内部合法命令,请重新输入
    ", str);
    52         }
    53     }
    54     
    55     return 0;
    56 }

    void strcpy(char *dst, const char *src)

    1 //复制字符串常量到数组中
    2 void strcpy(char *dst, const char *src)
    3 {
    4     while (*src != '')
    5     {
    6         *dst++ = *src++;
    7     }
    8 }

    int strcmp(const char *cs, const char *ct)

     1 //比较两字符串是否相同
     2 int strcmp(const char *cs, const char *ct)
     3 {
     4     unsigned char c1, c2;
     5 
     6     while (1) {
     7         c1 = *cs++;
     8         c2 = *ct++;
     9         if (c1 != c2)
    10             return c1 < c2 ? -1 : 1;
    11         if (!c1)
    12             break;
    13     }
    14     return 0;
    15 }

     shell实例3:shell编程将用户输入的字符串命令按照空格分隔成多个字符串,依次放入cmd二维数组中并解析执行命令

     1 void cmdsplit(char cmd[][MAX_LEN_PART], const char *str)
     2 {
     3     int m = 0, n = 0;    
     4     while (*str != '')
     5     {
     6         if (*str != ' ')
     7         {
     8             cmd[m][n] = *str;
     9             n++;
    10         }
    11         else
    12         {
    13             cmd[m][n] = '';
    14             n = 0;
    15             m++;
    16         }
    17         str++;
    18     }
    19     cmd[m][n] = '';
    20 }

    解析命令:

     1 void cmd_parser(char *str)
     2 {
     3     int i;
     4     
     5     // 第一步,先将用户输入的次命令字符串分割放入cmd中
     6     cmdsplit(cmd, str);
     7     
     8     // 第二步,将cmd中的次命令第一个字符串和cmdset对比
     9     cmd_index = -1;
    10     for (i=0; i<CMD_NUM; i++)
    11     {
    12         // cmd[0]就是次命令中的第一个字符串,也就是主命令
    13         if (!strcmp(cmd[0], g_cmdset[i]))
    14         {
    15             // 相等,找到了这个命令,就去执行这个命令所对应的动作。
    17             cmd_index = i;
    18             
    19             break;
    20         }
    21     }

    执行命令

     1 void cmd_exec(void)
     2 {
     3     switch (cmd_index)
     4     {
     5         case 0:
     6             do_cmd_led();            break;
     7         case 1:
     8         case 2:
     9         default:
    10             do_cmd_notfound();        break;
    11     }
    12 }
  • 相关阅读:
    [swustoj 411] 售货员的难题
    白书P61
    白书P60
    [ZOJ 3471] Most Powerful
    [HDU 3001] Travelling
    [转] acmer必看的26个对acm态度
    [HDU 1254] 推箱子
    [POJ 3311] Hie with the Pie
    [POJ 3254] Corn Fields
    power
  • 原文地址:https://www.cnblogs.com/CYP01/p/5928211.html
Copyright © 2020-2023  润新知