intmain(int argc, char *argv[]){ //get the name of the executable char *path = strdup(argv[0]); char *bname = basename(path);
if(!strcmp(bname, "happy")){ printf("I'm happy!\n"); } elseif(!strcmp(bname, "unhappy")){ printf("I'm not happy!\n"); } else{ printf("To be, or not to be, that is the question.\n"); } return0; }
编译运行:
1 2 3 4 5 6 7 8 9
$ gcc 3.c -o happy $ ./happy I'm happy! $ mv happy unhappy $ ./unhappy I'm not happy! $ mv unhappy x $ ./x To be, or not to be, that is the question.
Shell脚本也可以玩这一招
3.sh
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/sh
bname=`basename $0` if [ $bname = "happy" ] then echo"I'm happy!" elif [ $bname = "unhappy" ] then echo"I'm not happy!" else echo"To be, or not to be, that is the question." fi