【问题标题】:C++ input redirection from shell来自 shell 的 C++ 输入重定向
【发布时间】:2012-05-30 22:11:47
【问题描述】:

我确信这是一个简单的问题,但是在用 C++ 创建模拟 shell 时,我不太了解项目的一个方面。

基本上,我们正在创建一个名为 myShell 的程序,使用命令“./myShell”调用。这将打开自定义 shell,但我想要做的是直接从命令中调用外部函数,并使用令牌“-c”。

例如,命令:“./myShell -c ls -l”将调用linux ls 函数。我可以在实际调用程序后执行此操作,但不是在此之前(即打开 ./myShell,然后键入 ls -l"。

我是流程新手,如有任何帮助,我们将不胜感激。

【问题讨论】:

  • 目前这个太模糊了。阻碍您前进的具体技术问题是什么?

标签: c++ linux shell input io-redirection


【解决方案1】:

您的代码等待用户输入,解释从用户那里读取的命令,运行它们,然后在循环中返回等待用户输入,就像下面的伪代码:

int main(int argc, char *argv[]) {
    bool must_exit = false;
    while (!must_exit) {
        string input = read_user_input();
        must_exit = interpret_and_run(input);
    }
    return 0;
}

修改你的函数以连接argvs 并在进入循环之前运行它们:

int main(int argc, char *argv[]) {
    bool must_exit = false;
    if (argc > 2 && !strcmp(argv[1], "-c")) {
        string input = concatenate(argv); // From 1 to N, not from 0 to N
        must_exit = interpret_and_run(input);
    }
    while (!must_exit) {
        string input = read_user_input();
        must_exit = interpret_and_run(input);
    }
    return 0;
}

【讨论】:

  • 完美。正是我需要的。
猜你喜欢
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 2016-11-16
相关资源
最近更新 更多