dpvmtdu364462
dpvmtdu364462
2017-04-09 13:51

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

  • bash

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;
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答