【问题标题】:error: Command not found, on sending command line arguments错误:在发送命令行参数时找不到命令
【发布时间】:2011-05-11 06:21:54
【问题描述】:

我下面有一个非常简单的程序。

#include<stdio.h>
void main(int argc, char *argv[])
{
  printf("\n %s",argv[3]);
}

假设可执行文件名为 a.out 并以 $./a.out 的形式运行它 open path/to/my/file O_WRONLY|O_APPEND 给 Command no found 错误。 在哪里运行它就像运行它一样 $./a.out open path/to/my/file O_WRONLY 给出输出 O_WRONLY。

是因为|

感谢您宝贵的时间。

【问题讨论】:

  • 像这样转义:./a.out open path/to/my/file O_WRONLY\|O_APPEND 或者甚至像这样尝试:./a.out open path/to/my/file O_WRONLY'|'O_APPEND

标签: c linux


【解决方案1】:

您的 shell 将 O_APPEND 之前的 | 作为管道,并且无法识别此命令(因为它不存在)尝试 $./a.out open path/to/my/file "O_WRONLY|O_APPEND"

另外,不要使用void main,使用int main(这里的某些人如果看到它可能会心脏病发作:))

【讨论】:

  • 谢谢...:-)(是的,我猜 void main 会让许多 C 正统派心脏病发作)\
【解决方案2】:

管道字符| 对shell 有特殊含义:它创建一个管道,其中一个进程的输出通过管道传送到另一个进程的输入。当您键入foo | bar 时,shell 会使用命令行foobar 生成两个进程,并将前者的输出连接到后者的输入。

为避免这种行为,请在命令行参数周围加上引号:

$ ./a.out open path/to/my/file "O_WRONLY|O_APPEND"

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2016-04-06
    • 2015-12-21
    相关资源
    最近更新 更多