我正在尝试使用 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/