【问题标题】:C: using the system() commandC: 使用 system() 命令
【发布时间】:2014-03-10 19:14:23
【问题描述】:

我正在编写一个充当简单外壳的程序。用户从命令行调用程序并提示输入发送到操作系统以完成的命令。它应该一直运行到用户输入“完成”,此时程序应该中断。我在输入 done 时遇到问题 - 程序按原样退出,但打印出来

sh: -c: line 0: syntax error near unexpected token `done'
sh: -c: line 0: `done'

在完成执行之前到终端。这是我编写的适用的代码:

char isDone[] = "done\n"; //fgets stores the new line character from the command line

do {
    printf(">>"); //print the prompt each time through the loop
    fgets(command, 50, stdin); //get a line from the terminal
    system(command);
} while (strcmp(command, isDone) != 0); //break the loop if the user types in done

我认为该错误与“完成”不是有效的 UNIX 命令这一事实有关,但我不确定如何处理此错误。我尝试通过以下修复解决此问题:

if(system(command) == -1){
    printf("The OS doesn't recognize this command");
}
else
    system(command);

但这并没有解决问题或将错误打印到屏幕上,并产生了第二个问题,即两次打印命令/错误 - 一次在 if 条件块中,一次在 else 块中。我该如何解决这个问题?

编辑 这是一个家庭作业问题,需要使用 do-while。有没有使用 do-while 的解决方案?

【问题讨论】:

    标签: c error-handling system


    【解决方案1】:

    do...while 构造在检查循环条件之前执行其主体。因此,当循环“意识到”用户输入了done 时,它已经尝试将该输入作为循环体内的命令执行。

    解决此问题的最简单方法是使用break

    while (1)
    {
        fgets(command, 50, stdin);
        if (!strcmp(command, isDone)) break;
        system(command);
    }
    

    以这种方式构造它的原因是每次迭代都包含应该在条件之前完成的操作(读取用户输入)和应该在条件之后完成的操作(使用system() 执行命令) .因此,do...while 或简单的while 都不允许您直观地构建代码。 break 关键字提供了一种将循环的终止条件放在循环体中间的方法。

    【讨论】:

    • 由于这是家庭作业,我们需要使用do-while。你能想出一个使用 do-while 的解决方案吗? ——
    • 有可能,但不干净或没有代码重复。由于我所说的在终止条件之前和之后都有动作,do...while 是一个非常糟糕的控制流结构。我会告诉我的教授(我从来都不是一个非常优秀或受欢迎的学生)。
    • 哈哈好吧我会告诉他的。
    【解决方案2】:

    执行顺序为:

    fgets(command, 50, stdin); //get a line from the terminal
    system(command);
    strcmp(command, isDone) != 0
    

    因此“完成”行被读取,发送到系统(尝试将其作为 shell 命令执行,打印错误),然后才对其进行检查。

    你可以试试这样的:

    for(;;){
        printf(">>"); //print the prompt each time through the loop
        fgets(command, 50, stdin); //get a line from the terminal
        if(!strcmp(command, isDone)) break; //break the loop
        system(command);
    }
    

    编辑:如果你想保留do-while:

    printf(">>"); //print the prompt each time through the loop
    fgets(command, 50, stdin); //get a line from the terminal
    do {
        system(command);
        printf(">>"); //print the prompt each time through the loop
        fgets(command, 50, stdin); //get a line from the terminal
    } while (strcmp(command, isDone) != 0); //break the loop if the user types in done
    

    break 版本显然更具可读性。

    【讨论】:

    • 因为这是家庭作业,我们需要使用do-while。你能想出一个使用 do-while 的解决方案吗?
    • @itscharlieb 如果您稍微考虑一下,我认为您可以想出一种方法将这个概念应用到您自己的代码中,仅基于您粘贴和 pod 回答的内容.
    • 中断有效,但这意味着比较 do-while 没有任何用处,这就是为什么我不愿将其用作解决方案的原因。
    【解决方案3】:

    在 fgets() 之后,在 if 语句中执行 system() 调用:

    if ( strcmp( isDone, command) != 0 ) {
        system( command );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 2010-12-02
      • 1970-01-01
      • 2011-10-04
      • 2021-02-27
      相关资源
      最近更新 更多