如何更改将影响终端的C程序中的目录。如何从C文件更改终端中的目录
其实不要告诉系统(“”)功能或chdir(“”)功能。这些工作只是在C的过程或子壳。
说我正在执行一个C程序从一个进程ID为10223的bash shell。 那么,我可以写一个C代码,将影响10223进程是一个bash shell,其路径将被我的C程序改变。
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include<dirent.h>
#include <unistd.h>
using namespace std ;
int main(){
system("pwd") ;
chdir("test");
system("pwd") ;
return 0 ;
}
此更改仅在C代码运行之前进行。从C代码返回后,我该如何保持改变当前的环境。
UPD 1:
在终端如果我写PS然后我得到以下输出。现在我可以有任何功能来通过10991进程执行命令。我认为这解决了这个问题。
[email protected]:~$ ps
PID TTY TIME CMD
10991 pts/14 00:00:00 bash
11005 pts/14 00:00:00 ps
您不能更改另一个进程的当前工作目录。你为什么要这么做? –
好吧我正在写一个C代码,我可以通过inode值更改目录。可能吗 ? – Kryptonyte
毕竟,你为什么需要通过inode光盘?你知道inode而不是路径的情况是怎样的? –