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

标签 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,但是当我使用别名命令检查时,它没有显示当前别名。

最佳答案

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

关于c - 别名命令在c中的系统函数下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873548/

相关文章:

c++ - 如何将 'int' 值放入 'const char*'

git - 如何使用 Git 别名启动 Bash 函数

php - Drush 9 别名文件应该位于 Drupal 8 中的什么位置?

c - 按钮处理(伪代码/c)

c - 对于大多数数据包,我的 TCP 校验和函数返回 0

c - 使用字符指针复制字符串

c - 如何将规范传递给 gcc 链接器?

sql - 连接列别名时从表更新表

c# - 使用别名 = 类;与泛型

sql-server - 将 SQL Server 实例重定向到新实例?