c - linux 查找调用的命令

标签 c linux shell

我正在编写一个 C 程序,它确定从标准输入读取的字节数。我 发现有一些方法可以为程序提供输入

  • 管道输入
  • 重定向
  • 在程序等待输入时进入命令行

如何从 shell 中找到执行程序的确切命令。 我尝试使用命令行参数但失败了。

#include <stdio.h>

int main(int argc,char *argv[])
{
        char buffer[100];
        int n;

        for(n=1;n<argc;n++)
                printf("argument: %s\t",argv[n]);

        printf("\n");
        if(argc==1)
                printf("waiting for input :");
        else if (argc==3)
                printf("Not waiting for input . Got the source from command itself .");

        n = read(0,buffer,100);
        if(n==-1)
                printf("\nError occured in reading");
        printf("\nReading successfully done\n");

        return 0;
}

另外,

最佳答案

一般来说,您不能在程序内部执行此操作 - shell 可能不会将其中一些参数传递给您。它将扩展 glob、完成 I/O 重定向等,所有这些都在您的程序运行或获取参数之前完成。

您可以尝试调用 ps -o args,这可能对您有用。不过,据我所知,它不会提供重定向。

关于c - linux 查找调用的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19462019/

相关文章:

c++ - 在 LINUX 上读取 XML

linux - 如何执行 url 并从 bash shell 脚本解析它?

c - 如何从 int32_t 中提取字节 block 并使用 c 将其存储在 int16_t 或 int8_t 中?

c - 实际参数、格式参数、虚拟参数以及实际参数和形式参数之间有什么区别?

c - Linux 内核源代码中定义的 TTL(生存时间)值在哪里?

bash - 自删除shell脚本

linux - 使用 shell 脚本在配置文件中追加行

字符指针地址变化

linux - 如何在 ListView 中剪切 .txt 文件

python - 使用 wine 在 linux 上使用 win32com 自动化 excel