profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2

 

by:授客 QQ1033553122

--------------------接Part 1-------------------


B.  


bashinteractive
shell
方式启动时:

如果存在~/.bashrc,bash从~/.bashrc中读取命令并执行

注意:

1.通过--rcfile 
file选项可强制bash从文件file中读取命令并执行,而不是从~/.bashrc中读取。

2.可通过使用--norc可禁止bash读取~/.bashrc中的内容


C.  


当从
login
shell
中注销登录时:

如果存在~/.bash_logout,bash会从~/.bash_logout中读取命令并执行

注意:interactive
shell中执行退出是无法直接退出的,必须先退出到login
shell

例:采用interactive
shell中执行exit退出登录(因exit只会退到上次登录界面(如有的话))

[root@localhost ~]# echo $0

-bash #login shell

[root@localhost ~]# bash

[root@localhost ~]# echo $0

bash 
#interactive shell

[root@localhost ~]# exit

exit

[root@localhost ~]# exit #需要再次执行该命令才可以退出

[root@localhost ~]# who

root    
pts/0       
2014-09-08 15:27 (172.25.75.2)

[root@localhost ~]# su -l

[root@localhost ~]# exit

logout

[root@localhost ~]# logout #需要再次执行才退出


D.  


sh script_name.sh
一般情况下,不会执行上面的文件


E.  


被远程
shell守护进程运行会去读~/.bashrc中的命令

bash会探测自己是不是被远程shell守护程序运行(通常是rshd)。如果是,它会读取并执行~/.bashrc中的命令。但是rshd一般不会用rc相关参数调用shell,也不会允许指定这些参数


5、 

实践检验理论

这里我们通过小实验来验证上述的结论是否正确


步骤
1、备份~用户主目录下的文件

[test@localhost ~]$ pwd

/home/test

[test@localhost ~]$ cp .bash_profile bak.bash_profile

[test@localhost ~]$ cp .bashrc bak.bashrc

[test@localhost ~]$ cp .bash_logout bak.bash_logout


步骤
2、新建.profile,.bash_login文件

[test@localhost ~]$ touch .bash_login

[test@localhost ~]$ touch .profile


步骤
3、修改上述文件的内容

修改.bash_profile文件内容如下

echo "shouke test from ~/.bash_profile"

修改.bash_login文件内容如下

echo "shouke test from ~/.bash_login"

修改.profile文件内容如下

echo "shouke test from ~/.profile"

修改.bashrc文件内容如下

echo "shouke test from ~/.bashrc"

修改.bash_logout文件内容如下

echo "shouke test from ~/.bash_logout"


步骤
4、修改/etc/profile/etc/bashrc文件的内容

修改/etc/profile,在文件最末尾添加如下内容

echo 'shouke test from /etc/profile'

修改/etc/bashrc,在文件最末尾添加如下内容

echo 'shouke test from /etc/bashrc'


步骤
5、运行测试


测试
1.

字符界面下,在终端tty1~tty6中任意一个tty界面下,输入帐号,密码登录


 


测试
2.

通过xshell工具远程连接系统


测试
3.

字符界面下,运行命令su
-、su
–l、su
–login

[root@localhost ~]# pwd

/root

#备份原来的配置文件并新建测试用配置文件

[root@localhost ~]# mv .bash_profile bak.bash_profile

[root@localhost ~]# mv .bashrc bak.bashrc

[root@localhost ~]# mv .bash_logout bak.bash_logout

[root@localhost ~]# cp /home/test/.bash_profile
.bash_profile

[root@localhost ~]# cp /home/test/.bashrc .bashrc

[root@localhost ~]# cp /home/test/.bash_logout
.bash_logout

[root@localhost ~]# cp /home/test/.profile .profile

[test@localhost ~]$ su -

Password:

shouke test from /etc/profile

shouke test from ~/.bash_profile

[test@localhost ~]$ su -l

shouke test from /etc/profile

shouke test from ~/.bash_profile

[test@localhost ~]$ su --login

Password:

shouke test from /etc/profile

shouke test from ~/.bash_profile


测试
4.

可视桌面下,通过新建一个终端(Applications->System
Tools->Terminal)


测试
5.

已登录的情况下,在字符界面下,运行命令bash

-bash-4.1# bash

shouke test from ~/.bashrc

bash-4.1#

测试6.

root用户身份登录,在字符界面下,运行命令su、su
username

-bash-4.1# su

shouke test from ~/.bashrc

bash-4.1# su test

shouke test from ~/.bashrc

普通用户身份登录,在字符界面下,运行命令su

-bash-4.1$ su

Password:

shouke test from ~/.bashrc

bash-4.1# echo $0

bash


测试
6.

以root身份登录,登录后不做其它操作,直接注销登录

-bash-4.1# exit

logout

shouke test from ~/.bash_logout

以root身份登录,登录后运行bash命令,然后输入exit命令

-bash-4.1# bash

shouke test from ~/.bashrc

bash-4.1# exit

exit

以普通身份登录,登录后运行su
-命令,然后输入exit命令

-bash-4.1$ su -

Password:

shouke test from /etc/profile

shouke test from ~/.bash_profile

-bash-4.1# exit

logout

shouke test from ~/.bash_logout

这里验证了上文说的,仅在login
shell中,执行注销登录,才会读取~/.bash_logout


测试
7.

新建test.sh文件,内容如下

#!/bin/bash

echo "shouke test from ~/test.sh"

login shell中

[test@localhost ~]$ sh test.sh

shouke test from ~/test.sh

interactive shell中

[test@localhost ~]$ bash

shouke test from ~/.bashrc

bash-4.1$ sh test.sh

shouke test from ~/test.sh


测试
8.

以--rcfile参数跟随的方式启动bash

interactive shell

[test@localhost ~]$ bash --rcfile .bash_profile

shouke test from ~/.bash_profile


测试
9.

以--norc参数跟随的方式启动bash

[root@localhost ~]# bash --norc

bash-4.1#


测试
10.

去掉test用户的~/.bash_profile后,以test用户身份登录

[test@localhost ~]$ mv .bash_profile
bak.last.bash_profile

登录结果:

Last login: Mon Sep  8 15:48:12 2014 from
172.25.75.2

shouke test from /etc/profile

shouke test from ~/.bash_login


测试
11.

去掉test用户的~/.bash_login后,以test用户身份登录

[test@localhost ~]$ mv .bash_login bak.last.bash_login

登录结果:

Last login: Mon Sep  8 16:16:12 2014 from
172.25.75.2

shouke test from /etc/profile

shouke test from ~/.profile


测试
12.

去掉test用户的~/.profile后,以test用户身份登录

[test@localhost ~]$ mv .profile bak.last.profile

登录结果:

Last login: Mon Sep  8 16:16:45 2014 from
172.25.75.2

shouke test from /etc/profile

说明:不同终端登录,有的还没退出,所以前缀符号不一样,有的是[test@localhost
~]$这种的,有的是-bash-4.1$这种的,但是不影响测试

profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2的更多相关文章

  1. Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1

    profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 1   by:授客 QQ:103355312 ...

  2. profile bashrc bash_profile之间的区别和联系

    profile bashrc bash_profile之间的区别和联系 博客分类: Linux   执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bas ...

  3. profile bashrc bash_profile 之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...

  4. linux下系统启动时,几个配置文件 /etc/profile、~/.bash_profile 等几个文件的执行过程,先后顺序

    1. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时, 首先启动 /etc/profile 文件, 然后再启动用户目录下的 ~/.bash_profile. ~/.bash_login ...

  5. 【转】Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程

    原文网址:http://blog.csdn.net/ithomer/article/details/6322892 在登录Linux时要执行文件的过程如下:在刚登录Linux时,首先启动 /etc/p ...

  6. Linux 中/etc/profile、~/.bash_profile 等几个环境配置文件的执行过程

    环境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的 Shell.对于UNIX,可能是CShelll.环境变量是通过 ...

  7. linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程

    关于登录linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...

  8. [转] linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程

    分类: linux 2015-03-13 16:24 1572人阅读 评论(0) 收藏 举报linuxprofile关于登录linux时,/etc/profile.~/.bash_profile等几个 ...

  9. /etc/profile、~/.bash_profile等几个文件的执行过程

    /etc/profile.~/.bash_profile等几个文件的执行过程  摘自:http://blog.chinaunix.net/uid-14735472-id-3190130.html 分类 ...

随机推荐

  1. 最短路变形 poj3615& poj2263

    问题: 牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍. 输入N代表站点数,标记为1—N,输入M代表路径数,从站点S到E之间需要跨过高度为H的障碍. 输入T代表牛要 ...

  2. Kotlin 语言作为Android新的官方语言

    苹果用 swift 作为官方语言替代 object-c  ; 谷歌 刚开完io大会 也宣布 用  kotlin 作为官方语言 替代java.工具还是xcode ,android studio. 来自于 ...

  3. 集合框架_DAY16

    1:List及其子类(掌握)     (1)List的特点:     Collection    |--List:元素有序(存入顺序和取出顺序一致),可重复.    |--Set:元素无序,唯一.   ...

  4. Elasticsearch使用filter进行匹配关系and,or,not,range查询

    RESTful接口URL的格式: http://localhost:9200/<index>/<type>/[<id>] 其中index.type是必须提供的. i ...

  5. SQL SERVICE 拆分字符串的表值函数

    SQL代码 ALTER FUNCTION [dbo].[SplitToTable]( @SplitString nvarchar(max), @Separator nvarchar(10)=' ')R ...

  6. 轻量级web富文本框——wangEditor使用手册(1)——基本应用 demo

    最新版wangEditor: 配置说明:http://www.wangeditor.com/doc.html demo演示:http://www.wangeditor.com/wangEditor/d ...

  7. Java中的构造器与垃圾回收

    构造器 在我们初始化对象时,如果希望设置一些默认值,那么就可以使用构造器,在Java中,构造器使用和类同名的名字且没有返回值,如下 class Test{ private String name; T ...

  8. 25-hadoop-hive-函数

    内置函数: 函数分类: 内置函数查看: show funcitons; 查看函数描述: DESC FUNCTION concat; 具体见: https://cwiki.apache.org/conf ...

  9. MongoDB学习3 $操作符表达式大全及实例

    from : http://blog.csdn.net/qq_16313365/article/details/58599253 1.查询和投影   1.1 比较操作符 $eq 语法:{ <fi ...

  10. MySQL 学习笔记 二

    Ø function 函数 函数的作用比较大,一般多用在select查询语句和where条件语句之后.按照函数返回的结果, 可以分为:多行函数和单行函数:所谓的单行函数就是将每条数据进行独立的计算,然 ...