如何设置父进程的工作目录? [英] How do I set the working directory of the parent process?

查看:117
本文介绍了如何设置父进程的工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所揭示的,我们正在编写一个Unix风格的shell应用程序 U ,它应该在bash中调用(大多数情况下)。



U 究竟如何改变bash的工作目录(或一般的父目录)?



PS shell实用程序 chdir 成功地执行完全相同的操作,因此必须采用编程方式来实现该效果。 不要这样做。

  FILE * p; 
char cmd [32];
p = fopen(/ tmp / gdb_cmds,w);
fprintf(p,call chdir(\.. \)\\\
detach\\\
quit\\\
);
fclose(p);
sprintf(cmd,gdb -p%d -batch -x / tmp / gdb_cmds,getppid());
系统(cmd);

它可能会 工作,但请注意Bash的 pwd 命令被缓存,不会注意。


As the title reveals it, we are writing a Unix-style shell utility U that is supposed to be invoked (in most cases) from bash.

How exactly could U change the working directory of bash (or parent in general)?

P.S. The shell utility chdir succeeds in doing exactly the same, thus there must be a programmatic way of achieving the effect.

解决方案

Don't do this.

FILE *p;
char cmd[32];
p = fopen("/tmp/gdb_cmds", "w");
fprintf(p, "call chdir(\"..\")\ndetach\nquit\n");
fclose(p);
sprintf(cmd, "gdb -p %d -batch -x /tmp/gdb_cmds", getppid());
system(cmd);

It will probably work, though note that Bash's pwd command is cached and won't notice.

这篇关于如何设置父进程的工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆