将命令作为输入并在终端中运行的程序

I kind of know how to do this in C, but how to do it in Go?

This is the code I'm using:

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

#define MAX_CMN_LEN 100

int main(int argc, char *argv[])
{
    char cmd[MAX_CMN_LEN] = "", **p;

    if (argc < 2) /*no command specified*/
    {
        fprintf(stderr, "Usage: ./program_name terminal_command ...");
        exit(EXIT_FAILURE);
    }
    else
    {
        strcat(cmd, argv[1]);
        for (p = &argv[2]; *p; p++)
        {
            strcat(cmd, " ");
            strcat(cmd, *p);
        }
        system(cmd);
    }

    return 0;
}
查看全部
dpvmtdu364462
dpvmtdu364462
2017/04/09 13:51
  • bash
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复