别名命令在c中的系统函数下不起作用

时间:2012-10-13 13:26:38

标签: c alias

我正在尝试使用C系统函数编写别名,这是代码

if (argc <= 3) {
    printf("Program Usage: ./executable alias > or < or >> or &\n");
}
else {
    if (strcmp(argv[1],"alias") == 0) {
        if (strcmp(argv[2], "redirect") == 0) {
            char y[] = "=\">\"";
            char *xs = strcat(argv[1], " ");
            char *x = strcat( xs, strcat(argv[3], y));
            printf("%s\n",x);
            int status = system(x);
            printf("%d\n", status);
        }
        else {
            printf("You've not entered proper symbol\n");
        }
    }
    else {
        printf("You've not entered the shell property as alias\n");
    }
}

运行程序的一般方法是

./a.out alias redirect custom_alias_name

此外系统函数返回0,但是当我使用alias命令检查时,它不会显示当前的别名。

2 个答案:

答案 0 :(得分:1)

这是因为,对system()的调用不会修改父环境的状态。执行时的程序会继承父进程的环境。此继承的环境副本是子进程的本地副本。一旦此子进程存在,就会丢弃对此本地环境的任何更改。

答案 1 :(得分:-1)

返回的值是命令的状态,0表示成功,-1表示错误;在这种情况下,system()返回0!