免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7156 | 回复: 6

不知为什么~/.bash_profile文件不执行! [复制链接]

论坛徽章:
0
发表于 2006-09-16 21:19 |显示全部楼层
新安装的debian系统,发现在~/.bash_profile文件就是不执行。
查了以前的贴子,和 man,其中在man bash的手册中写到:
When  bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes  commands  from  the file /etc/profile, if that file exists.  After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,in  that order, and reads and executes commands from the first one that exists and is readable.

对系统进行了检查,发现在首先/etc/profile文件是存在的,并且在其中对变量设置也是可以的;然后~/.bash_profile文件也是存在的,并且对它的权限为644,但是在其中加入设置就是不行,怀疑其在系统启动的时候没有执行。

但是,发现在一个怪现象,就是如果按ctrl+alt+f1打开一个新的终端,发现在~/.bash_profile是可以执行的,因为我在其中加入了一条 echo。

到底为啥,有人遇到过吗?

论坛徽章:
0
发表于 2006-09-16 21:36 |显示全部楼层
原帖由 a-ki 于 2006-9-16 21:19 发表
新安装的debian系统,发现在~/.bash_profile文件就是不执行。
查了以前的贴子,和 man,其中在man bash的手册中写到:
When  bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes  commands  from  the file /etc/profile, if that file exists.  After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,in  that order, and reads and executes commands from the first one that exists and is readable.

对系统进行了检查,发现在首先/etc/profile文件是存在的,并且在其中对变量设置也是可以的;然后~/.bash_profile文件也是存在的,并且对它的权限为644,但是在其中加入设置就是不行,怀疑其在系统启动的时候没有执行。

但是,发现在一个怪现象,就是如果按ctrl+alt+f1打开一个新的终端,发现在~/.bash_profile是可以执行的,因为我在其中加入了一条 echo。

到底为啥,有人遇到过吗?


你的问题好像有点矛盾。

放在 ~/.bash_profile 当然是你输入帐号密码后启动该 shell 这类 "login shell mode" 时才会读取的。所以你说按下 ctrl-alt-f1 于 console 登入,这是属于 login shell mode,这时候 ~/.bash_profile 会读取。

若是你说的是 X 下开终端机要跑的叙述,那你应该是放在 ~/.bashrc 才对。

==

论坛徽章:
0
发表于 2006-09-16 21:59 |显示全部楼层
对,我是要是X下开终端的时候.
之前在FC5的时候,我都是把设置加入~/.bash_profile中的,并没有出现现在的这种情况。

刚把它们的关系搞明白了一点点,你这么一说,我现在又有点不太明白了。如果按你说的,在X上打开终端只运行~/.bashrc,那在文件 ~/.bash_profile中的设置是什么执行呢。是不是说,如果我只在X下打开终端,那所有在~/.bash_profile文件中的设置都不可能会被执行了?
并且一般在~/.bashrc文件都会在~/.bash_profile中调用,这又是为什么呢?

论坛徽章:
0
发表于 2006-09-17 01:40 |显示全部楼层
原帖由 a-ki 于 2006-9-16 21:59 发表
对,我是要是X下开终端的时候.
之前在FC5的时候,我都是把设置加入~/.bash_profile中的,并没有出现现在的这种情况。


除非你的终端机程式是使用 "bash --login" 方式来以 login shell mode 方式来运行 bash,要不然应该是不会出现读取 ~/.bash_profile 这类情况。

刚把它们的关系搞明白了一点点,你这么一说,我现在又有点不太明白了。如果按你说的,在X上打开终端只运行~/.bashrc,那在文件 ~/.bash_profile中的设置是什么执行呢。


前面说过了:

  1. kenduest said:

  2. 放在 ~/.bash_profile 当然是你输入帐号密码后启动该 shell 这类 "login shell mode" 时才会读取的。所以你说按下 ctrl-alt-f1 于 console 登入,这是属于 login shell mode,这时候 ~/.bash_profile 会读取。
复制代码

是不是说,如果我只在X下打开终端,那所有在~/.bash_profile文件中的设置都不可能会被执行了?


同刚刚前面的解释。

并且一般在~/.bashrc文件都会在~/.bash_profile中调用,这又是为什么呢?


大部分 linux distro 的 bash 配置档案会安排就算是 login shell mode 时还是会去读取 .bashrc,这应该是简化配置设定的考量。

比方你把 alias 设定放 ~/.bash_profile,那你用 console login 时可以执行到 alias 设定,但是设定好后于 X 开终端机执行 bash 时,因为执行不到 ~/.bash_profile 内的 alias 设定,所以你在 X 下无法享受到 ~/.bash_profile 内 alias 设定成果。

基于该原因,所以一般 linux distro 就安排就算是 login shell mode 时还是会读取 .bashrc,那不管是 console login 还是 X 开终端机启动了 bash,都可以带入 .bashrc 内容设定。而基于该点安排,通常 alias、function 等等项目都可以放在该档案内让配置能够简化。

但是你要瞭解的是,比方 rhel 等系统,login shell mode 会读取 .bashrc,都是 ~/.bash_profile 的内容关系。

  1. # .bash_profile

  2. # Get the aliases and functions
  3. if [ -f ~/.bashrc ]; then
  4.         . ~/.bashrc
  5. fi
复制代码


而 suse linux 则是于 /etc/profile 提供该功能:

  1. test -r $HOME/.bashrc  && . $HOME/.bashrc
  2. fi
复制代码


最后结论就是,一般除非你有改过相关的配置,要不然 X 开终端机时通常终端机不会传入 --login 方式来启动 bash shell,所以这时后不可能有机会让 bash 使用 login shell mode 启动,所以 /etc/profile and ~/.bash_profile 当然就不会执行了。

==

[ 本帖最后由 kenduest 于 2006-9-17 01:47 编辑 ]

论坛徽章:
0
发表于 2006-09-17 15:29 |显示全部楼层
呵呵,非常感谢 kenduest 不厌其烦的解释!

现在算是明白是怎么一回事了,看来各个发行版之前还是有不少细微的差别呀!以后要加什么path之类的,看来最好是加在~/.bashrc里比较可靠了。

论坛徽章:
0
发表于 2006-09-18 01:02 |显示全部楼层
原帖由 a-ki 于 2006-9-17 15:29 发表
呵呵,非常感谢 kenduest 不厌其烦的解释!

现在算是明白是怎么一回事了,看来各个发行版之前还是有不少细微的差别呀!以后要加什么path之类的,看来最好是加在~/.bashrc里比较可靠了。


看 .bashrc ? PATH 这类通常不适合放 .bashrc,若是你要放要想清楚配置的使用方式。比方常见有人这样用:

  1. PATH=$PATH:/sbin:/usr/sbin
复制代码


这个配置方式可能会有问题,你开终端机后多跑几次 bash 再看 echo $PATH 就会知道实际的意思。

==

论坛徽章:
0
发表于 2006-09-18 13:21 |显示全部楼层
刚才试了一下,你说的没错!

那象只是给某个用户设置PATH之类的,放在哪个文件里才是正解呀!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP