Linux下的C程序,使用函数execlp运行Shell命令

通过C程序运行Shell命令,使用execlp函数

execlp的几个参数分别为:文件名,各个参数,(char*)0

其中“各个参数”部分中的第一个参数就是文件名(相当于文件名要输入两遍)

最后一个参数写(char*)0就可以了

文件b.cpp代码:

循环地输入命令→执行命令,直到按下Ctrl+C结束

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

#define MAX_PARA_NUMS      10
#define MAX_CHAR_EACH_PARA 1000

int split(char* input, char output[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA])
{
    int counter = 0;

    int len = strlen(input);
    bool bTemp = false;
    int i, j = 0;
    for(i = 0; i < len; i++)
    {
        if(input[i] == ‘ ‘)
        {
            if(j != 0 && counter > 0)
            {
                output[counter - 1][j] = ‘\0‘;
            }
            bTemp = false;
        }
        else
        {
            if(bTemp == false)
            {
                j = 0;
                counter++;
                bTemp = true;
            }

            output[counter - 1][j] = input[i];
            j++;
        }
    }    

    return counter;
}

int main()
{
    int counter = 0;
    char params[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA];

    //使用g++编译文件
    //execlp("g++", "g++", "a.cpp", "-o", "a", (char*)0);
    
    char buf[4096];
    pid_t pid;
    int status;

    //输入PROMPT字符串
    printf("CONSOLE-X\n");
    printf("=========\n");
    while (fgets(buf, 4096, stdin) != NULL)
    {
        if (buf[strlen(buf) - 1] == ‘\n‘)
        {
            buf[strlen(buf) - 1] = ‘\0‘;
        }

        pid = fork();
        if (pid < 0)
        {
            printf("fork error");
        }
        else if (pid == 0)
        {
            counter = split(buf, params);
            if(counter != 0)
            {
                printf("---------\n");
            }
            switch(counter)
            {
                case 0: break;
                case 1: 
                    {
                        execlp(params[0], params[0], (char*)0);
                    }    
                    break;
                case 2:
                    {
                        execlp(params[0], params[0], params[1], (char*)0);
                    }
                    break;
                case 3:
                    {
                        execlp(params[0], params[0], params[1], params[2],
                            (char*)0);
                    }
                    break;
                case 4:
                    {
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], (char*)0);
                    }
                    break;
                case 5:
                    {
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], (char*)0);
                    }
                    break;
                case 6:
                    {
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], params[5], (char*)0);
                    }
                    break;
                case 7:
                    {    
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], params[5], params[6], (char*)0);
                    }
                    break;
                case 8:
                    {    
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], params[5], params[6],
                            params[7], (char*)0);
                    }
                    break;
                case 9:
                    {    
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], params[5], params[6],
                            params[7], params[8], (char*)0);
                    }
                    break;
                case 10:
                    {    
                        execlp(params[0], params[0], params[1], params[2], 
                            params[3], params[4], params[5], params[6],
                            params[7], params[8], params[9], (char*)0);
                    }
                    break;
                default:
                    {
                        printf("Illegal Input!\n");
                    }
                    break;
            }
        }
        else
        {
            //输入PROMPT字符串
            //printf("CONSOLE-X> ");
        }
    }

    return 0;
}

将b.cpp编译成可执行的文件b(输入命令:g++ b.cpp -o b)

然后打开文件b,依次输入命令

ls -ls
cat a.cpp
g++ a.cpp -o a
./a

文件a.cpp代码:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

运行结果示例:


END

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。