命令行程序的通配符入参

今天突然想到一个问题:当命令行程序的入参为一个通配符(比如./a.out *)时,会是什么样的情况? 程序得到的入参是通配符*吗?


源码:

#include <stdio.h>

int main(int argc, char *argv[])
{
        int i;

        printf("argc: %d\n", argc);
        for(i = 0; i < argc; i++)
                printf("argv[%d]: %s\n", i, argv[i]);

        return 0;
}

执行结果:

$ ls
111.txt  2byte.txt  a.out  main.c  minicom.txt  src
$ ./a.out *
argc: 7
argv[0]: ./a.out
argv[1]: 111.txt
argv[2]: 2byte.txt
argv[3]: a.out
argv[4]: main.c
argv[5]: minicom.txt
argv[6]: src
$ ./a.out *.txt
argc: 4
argv[0]: ./a.out
argv[1]: 111.txt
argv[2]: 2byte.txt
argv[3]: minicom.txt

运行环境:

$uname -a
Linux xxxxx 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:24 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

结论:当运行程序时所给的入参是通配符时,程序实际所得到的入参不是简单的通配符,而是这个通配符的实际展开。


ps.感觉上还是有点怪怪的,呵呵。

你可能感兴趣的:(c,ubuntu,gcc,2010,X86)