C语言定制shell命令

#include 
#include 
#include 
int main(int argc, char* argv[]){
    char* cmd = "pwd";
    char buf[4096] = {0};
    FILE* result = popen(cmd, "r");
    if (result == NULL) {   
        printf("popen faild, (%d, %s)\n", errno, strerror(errno));
        return -1;
    }
    fread(buf, 1, sizeof(buf), result);
    printf("%s", buf);
    pclose(result);
    return 0;
}

编译命令:
gcc -o pwd_diy main.c

拷贝到/usr/local/bin
cp ./pwd_diy /usr/local/bin

执行命令:
pwd_diy

你可能感兴趣的:(Linux,c语言,开发语言)