我正在编写一个 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/