LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: lofeng410

请教几个小问题

[复制链接]
 楼主| 发表于 2008-11-12 23:38:50 | 显示全部楼层
interactive login shell:
An interactive login shell is started after a successful login, using /bin/login, by reading the /etc/passwd file.
通过bash -login或者su -也能实现login shell登录
在BLFS手册中,其读取的初始化文件为/etc/profile 和 ~/.bash_profile
-----------------------------------------
interactive non-login shell :
An interactive non-login shell is normally started at the command-line using a shell program (e.g., [prompt]$/bin/bash) or by the /bin/su command. An interactive non-login shell is also started with a terminal program such as xterm or konsole from within a graphical environment.
从上可知,在X环境下打开的终端,第一个一定是non-login,但是在这个打开的终端中,可以通过bash -login或者su 来得到一个login shell,当然,这是子shell。
BLFS手册给出:non-login shell除了继承父进程的环境(normally copies the parent environment )外,还要读取~/.bashrc文件
。但是,在X环境中,他的环境变量之类的又是从哪个文件读取的呢?这个parent有怎么理解呢?xterm是子进程么?

另外一般都有个/etc/bashrc文件,这个文件又是什么时候读的呢?
有人说是先读/etc/bashrc,再读~/.bashrc,是这样的么?
-----------------------------------------
non-interactive shell
这个很简单,就不说了
回复 支持 反对

使用道具 举报

发表于 2008-11-13 09:45:46 | 显示全部楼层
这个就要你自己去试了。

比如在我的机器上,在开机进入字符界面时,读取的仅仅是
/etc/profile和~/.bash_profile,并不读取bashrc;到了X下,打开一个终端时,这个终端读取的是~/.bashrc,它不读取前面的两个文件。而/etc/bashrc没有任何一个shell去读,我是在profile文件中加了一句
source /etc/bashrc
来实现的。这个情况我也非常费解,估计是程序的实现问题。

你把每个文件的PS1环境变量改一下就知道它到底读了哪个文件了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 10:27:01 | 显示全部楼层
Post by ti8er;1907863
这个就要你自己去试了。

比如在我的机器上,在开机进入字符界面时,读取的仅仅是
/etc/profile和~/.bash_profile,并不读取bashrc;到了X下,打开一个终端时,这个终端读取的是~/.bashrc,它不读取前面的两个文件。而/etc/bashrc没有任何一个shell去读,我是在profile文件中加了一句
source /etc/bashrc
来实现的。这个情况我也非常费解,估计是程序的实现问题。

你把每个文件的PS1环境变量改一下就知道它到底读了哪个文件了。

这个好试出来,但是是通过什么来控制login shell一定会去读取profile的呢?而不是其他的?
在X环境中,也是需要一些环境变量之类的,这时是通过读取哪个文件读取的呢?X环境本身也是一个应用程序,是否就相当于字符模式登陆是的shell?
回复 支持 反对

使用道具 举报

发表于 2008-11-13 11:14:50 | 显示全部楼层
Post by lofeng410;1907784
interactive login shell:
An interactive login shell is started after a successful login, using /bin/login, by reading the /etc/passwd file.
通过bash -login或者su -也能实现login shell登录
在BLFS手册中,其读取的初始化文件为/etc/profile 和 ~/.bash_profile
-----------------------------------------
interactive non-login shell :
An interactive non-login shell is normally started at the command-line using a shell program (e.g., [prompt]$/bin/bash) or by the /bin/su command. An interactive non-login shell is also started with a terminal program such as xterm or konsole from within a graphical environment.
从上可知,在X环境下打开的终端,第一个一定是non-login,但是在这个打开的终端中,可以通过bash -login或者su 来得到一个login shell,当然,这是子shell。
BLFS手册给出:non-login shell除了继承父进程的环境(normally copies the parent environment )外,还要读取~/.bashrc文件
。但是,在X环境中,他的环境变量之类的又是从哪个文件读取的呢?这个parent有怎么理解呢?xterm是子进程么?

另外一般都有个/etc/bashrc文件,这个文件又是什么时候读的呢?
有人说是先读/etc/bashrc,再读~/.bashrc,是这样的么?
-----------------------------------------
non-interactive shell
这个很简单,就不说了


去看/etc/rc.d,初始化的工作是在这里完成的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 11:32:54 | 显示全部楼层
Post by hritian;1907918
去看/etc/rc.d,初始化的工作是在这里完成的。

我知道初始化工作是在这里完成的,但是这个bash的调用的初始化并没有包含在这里面的吧?
而且我的rc5.d目录下有个很奇怪的现象,竟然有多个S99*

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 11:35:31 | 显示全部楼层
另外可否有non-interactive login shell,若有,能否举一例?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 17:19:58 | 显示全部楼层
已经明白为什么interactive login shell是按照那样的顺序去读取初始化文件了,下面是bash源码中的一段:
  sourced_login = 0;

  /* A shell begun with the --login (or -l) flag that is not in posix mode[color="Red"]//这段注释没有看懂是什么意思,下面的都是根据其他信息
     runs the login shell startup files, no matter whether or not it is                                 [color="Red"]来判断的
     interactive.  If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
     startup files if argv[0][0] == '-' as well. */
#if defined (NON_INTERACTIVE_LOGIN_SHELLS)
  if (login_shell && posixly_correct == 0)
#else
  if (login_shell < 0 && posixly_correct == 0)
#endif
    {
[color="urple"]      /* We don't execute .bashrc for login shells. */
      no_rc++;


      /* Execute /etc/profile and one of the personal login shell
         initialization files. */
      [color="Red"]if (no_profile == 0)
        {
          maybe_execute_file (SYS_PROFILE, 1);

          if (act_like_sh)        /* sh */
            maybe_execute_file ("~/.profile", 1);
          else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
                   (maybe_execute_file ("~/.bash_login", 1) == 0))        /* bash */
            maybe_execute_file ("~/.profile", 1);

        }

      sourced_login = 1;
    }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 17:25:22 | 显示全部楼层
interactive login shell:
如果是以sh运行,则先读取全局配置文件,一般都是/etc/profile,然后再读取~/.profile
如果是以bash运行,则先读取全局配置文件,一般都是/etc/profile,然后再读取~/.profile_profile,如果该文件不存在,则去寻找~/.profile_login文件,若存在,则读取之,若也不存在,才会去读取~/.profile。这也是网上常说的~/.profile_profile 、~/.profile_login、/.profile_profile读取优先级的由来

在这些文件里面,还可以控制是否读取/etc/bashrc、~/.bashrc等
不过,在上面的代码中有如下所示部分:
[color="urple"]      /* We don't execute .bashrc for login shells. */
      no_rc++;
意为不读取~/.bashrc,不知其为何意
回复 支持 反对

使用道具 举报

发表于 2008-11-13 17:33:46 | 显示全部楼层
不错啊,已经开始看源代码了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-13 18:37:35 | 显示全部楼层
Post by ti8er;1908093
不错啊,已经开始看源代码了。
感觉在源码中能解决我的问题
但是目前只解决了上面说的一点点问题,其他的困惑仍然存在。
太菜,源码看得一知半解的。呵呵~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表