- 论坛徽章:
- 0
|
原帖由 FH 于 2007-1-9 19:19 发表
楼上的确很细致,佩服!
俺的是RHEL-AS4,俺也是man了以后找到的这个方法。
俺试了一下,如果直接在shell下面执行sh,是能够继承PS1的,但是进入了vi就不行了,从其它程序访问sh应该也是这样(推理),但是同样 ...
写个程式测看看不就知道了呼 ? 下面测试环境的 ENV 变数都没有指定任何内容。
- Linux:kendlee@~/tmp> cat test.c
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main()
- {
- pid_t child_p;
- printf("getenv(\"PS1\") return %s\n\n",getenv("PS1"));
- child_p = fork();
- if (child_p == 0) {
- printf("Executing sh program !\n");
- execlp("sh","sh",NULL);
- }
- else {
- waitpid(child_p,NULL,0);
- printf("end... see you !\n");
- }
- return 0;
- }
复制代码
看一下执行结果,这是有是先把 PS1 设定为 export variable:
- Linux:kendlee@~/tmp> ./test
- getenv("PS1") result: Linux:\u@\w>
- Executing sh program !
- Linux:kendlee@~/tmp>
- Linux:kendlee@~/tmp> echo $PS1
- Linux:\u@\w>
复制代码
看起来 child process 还是有继承到。
再看看另外一个执行结果,这是有是先把 PS1 设定为一般变数,也就是取消非 export variable 性质:
- Linux:kendlee@~/tmp> ./test
- getenv("PS1") result: (null)
- Executing sh program !
- sh-3.1$
复制代码
PS1 不是 export variable,所以 child 没有 PS1 变数继承下来,所以最终显示 sh$ 提示字元。
所以这边讨论来看,当初我最前面提到文章内部份的描述并没错误,这就是我会问你是否没有把 PS1 设定为 export variable 的原因点,只是你没正面回应我,只是一直告诉我搞不清楚何谓 interactive mode ... 因为实际一般测试方式就是在命令列输入执行 sh 来测试,为 export variable 时是会继承下去。
另外拿 mc 来测试一下好了...... 结果与上面也相同。
那问题拉回来看,是真的为变态的 bash 的问题吗?老实说我认为问题搞不好与 vi 本身有关系。
然后我去看了一下 vim7 的 source code :
- if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
复制代码
发现原来是使用 /bin/sh -c 方式来执行命令,来看 bash manpage:
- An interactive shell is one started without non-option arguments and
- without the -c option whose standard input and error are both connected
- to terminals (as determined by isatty(3)), or one started with the -i
- option. PS1 is set and $- includes i if bash is interactive, allowing
- a shell script or a startup file to test this state.
复制代码
所以因为有传 -c,这时候不是 interactive mode,简单说就是为 non-interactive mode。
那再看一下 bash info page 文件:
- Alternatively, startup scripts may examine the variable `PS1'; it is
- unset in non-interactive shells, and set in interactive shells. Thus:
- if [ -z "$PS1" ]; then
- echo This shell is not interactive
- else
- echo This shell is interactive
- fi
复制代码
所以说 non-interactive 时 PS1 会被 unset 而消失掉,所以不会有复制继承功能。
FH 兄你说会继承,这下真的奇怪了...... 若是真的会继承,那可能原因大概只有这点: "你系统用的 vi 程式码内容写法不一样",比方用 execlp 直接呼叫外部程式,不透过 sh -c 来达成,那我想这样 PS1 就真的会复制继承下去而不会被 unset 掉。
关于最后论点,其他人不知道有其他想法呼?
--
[ 本帖最后由 kenduest 于 2007-1-10 03:22 编辑 ] |
评分
-
查看全部评分
|