如何使CHDIR()留在程序完成后指定的目录? [英] How to make chdir() stay in the specified directory after the program finishes?

查看:200
本文介绍了如何使CHDIR()留在程序完成后指定的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用CHDIR()来改变CWD的程序。但是,程序完成的CWD后变回调用该程序而不是停留在一个由调用指定CHDIR()的目录。我做了一个程序来测试,如果CHDIR()实际上改变到指定目录,发现CHDIR()是做什么的我presumed:更改到指定的目录中程序的时间,然后返回到执行的目录该程序。

I have a program that calls chdir() to change the cwd. However, after the program finishes the cwd changes back to the directory that called the program instead of staying the in one specified by the call to chdir(). I made a program to test if chdir() is actually changing to the specified directory and found that chdir() is doing what I presumed: changing to the specified directory for the duration of the program then returning to the directory that executed the program.

下面是code的测试:

Here is the code for the test:

#include <stdio.h>
#include <unistd.h>

#define NAME_MAX 100

int main(int argc, char **argv)
{
    char buf[NAME_MAX];
    char *path = argv[1];

    if (chdir(path) == -1) { /* change cwd to path */   
        fprintf(stderr, "error: could not change to dir %s\n", path);
        return 1;
    }
    getcwd(buf, NAME_MAX);
    printf("CWD is: %s\n", buf); /* print cwd as obtained from getcwd() */

    return 0;
}

,这里是从我的终端输出的:

and here is the output from my terminal:

john@ubuntu:~/C/cli$ pwd
/home/john/C/cli
john@ubuntu:~/C/cli$ mkdir foobar
john@ubuntu:~/C/cli$ ./test.c foobar
CWD is: /home/john/C/cli/foobar
john@ubuntu:~/C/cli$ pwd
/home/john/C/cli

所以我的问题是,我怎么能留在,我在调用指定CHDIR(目录)的之后的该节目结束?另外,我在Ubuntu 12.04和用gcc编译。

So my question is, how can I stay in the directory that I specify in the call to chdir() after the the program finishes? Also, I am on Ubuntu 12.04 and compiling with gcc.

推荐答案

某些信息,包括环境变量的值和当前工作目录,是从父进程传递给子过程,但不回父进程。如果一个子进程(程序)调用 CHDIR 或设置或修改环境变量,影响这一进程及其任何孩子,但不能影响父。

Certain information, including values of environment variables and the current working directory, is propagated from parent processes to child processes but not back to parent processes. If a child process (your program) invokes chdir or sets or modifies an environment variable, that affects that process and any of its children, but cannot affect the parent.

这就是为什么 CHDIR 是一个内置在shell命令;它的不能的实施作为一个单独的程序。

That's why chdir is a built-in command in the shell; it can't be implemented as a separate program.

如果你想有一个程序更改你的shell的当前目录为你,你需要间接地做到这一点。例如,你的程序可以打印 CD 命令,你可以评估,在你的shell输出。 (你可以用在函数)。

If you want to have a program change your shell's current directory for you, you'll need to do it indirectly. For example, your program can print a cd command, and you can eval that output in your shell. (You can wrap that in a function.)

例如,如果你改变:

chdir(path);

printf("cd %s\n", path);

你可以有一个shell函数:

you can have a shell function:

my_func() {
    eval `your_program`
}

my_func,并将将改变你的shell的当前目录。

and my_func will change your shell's current directory.

或者你也可以直接把 CD 命令,在功能,或在一个脚本,您通过执行。脚本的名称源脚本的名称,而不是执行它。

Or you can put a cd command directly in the function, or in a script that you execute via . script-name or source script-name rather than executing it.

所有这些解决方案都需要你当前的shell执行 CD 命令本身(在内部调用 CHDIR 系统调用)。

All these solutions require your current shell to execute the cd command itself (which internally invokes the chdir system call).

这篇关于如何使CHDIR()留在程序完成后指定的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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