码迷,mamicode.com
首页 > 系统相关 > 详细

bash的配置文件(linux学习之八)

时间:2017-02-11 22:57:11      阅读:569      评论:0      收藏:0      [点我收藏+]

标签:bash配置文件 login shell no-login shell

一、什么是shell


广义的shell指,能够操作应用程序的接口都成为shell,(包括linux和windows的图形界面)

狭义的shell指,命令行方面的程序,包括zsh,bash,csh,等等


查看当前系统的可用shell,文件/etc/shells 中所列的shell,称为当前系统上安全的shell列表。默认shell如果非文件/etc/shells中的shell,很可能拒绝登入系统。


[root@localhost ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh


为甚么/etc/shells 文件中的shell列表称为合法的shell列表,这是因为系统某些服务在运行的时候,会去检查用户能够使用的shells,而检查的标准就是根据/etc/shells 文件中的shell列表。


二、bash命令与sh命令的区别


Linux 操作系统缺省的 shell 是Bourne Again shell,它是 Bourne shell 的扩展,简称 Bash,与 Bourne shell 完全向后兼容,并且在Bourne shell 的基础上增加、增强了很多特性。


GNU/Linux 操作系统中的 /bin/sh 本是 bash (Bourne-Again Shell) 的符号链接,但鉴于 bash 过于复杂,有人把 ash 从 NetBSD 移植到 Linux 并更名为 dash (Debian Almquist Shell),并建议将 /bin/sh 指向它,以获得更快的脚本执行速度。Dash Shell 比 Bash Shell 小的多,符合POSIX标准。


debian和Ubuntu中,/bin/sh默认已经指向dash,这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它速度更快,但功能相比bash要少很多,语法严格遵守POSIX标准。


三、bash的四种模式


在man bash的INVOCATION一节讲述了bash的四种模式,bash会依据这四种模式而选择加载不同的配置文件,而且加载的顺序也有所不同。


Bash是shell的一种,运行中的Bash有两种属性(状态/模式),一种,是否interactive shell(交互式Shell),另一种,是否login shell(登录Shell)。两种组合成四种模式。


  • login shell(交互式shell):


定义:A login shell is one whose first character of argument zero is a -, or one started with the --login option.


大概意思是:一个登入式shell,它的第零个参数的第一个字符是  -,或者 它是一个以 --login 选项启动的shell。


解释一下这个定义


1、第零个参数的第一个字符是 - 的shell是登入式shell


比如: tty1-tty6 终端登入系统(系统默认shell是bash)是登入式shell

tty1-tty6 登入系统后


echo $0
-bash


第零个参数的第一个字符是 -,所以这是一个登入式shell。


2、以 --login 选项启动的bash是登入式shell

比如:bash --login[-l]

系统登入以后,在当前shell中登入子shell,加上--login选项,表示登入的这个子shell是登入式shell


此外。除定义外,我们判断这两种模式的方法。


登入式shell与非登入式shell的区别我们可以从字面上理解,登入式shell要求用户输入用户名,密码。包含登入一个shell的全过程。(这个说法不一定严谨准确,比如bash --login[- l]  这种方式就不需要输入用户名密码,因为它登入的是当前shell的子shell,用户名,密码与当前shell相同,所以省去)


登入式shell和非登入式shell的退出机制不同。

非登入式shell,退出时,使用exit命令退出,不能使用logout命令退出

登入式shell,退出时,使用logout命令退出,能够使用exit命令退出,但是此时的exit命令扔是调用logout命令。


常见的login shell与no-login shell


下列为login shell 统计不完整。(no-loginshell及说明待更新)

tty1-tty6                              

su -[l] UserName                                           

ssh UserName@HostName                            

ssh UserName@HostName "command"       

bash --login[-l]                                              

su -[l] UserName -c "command"

bash --login[-l] -c "command"

bash -l[--login] script.sh


  • interactive shell(登入式shell):


定义: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.


(解释以及常见的非交互式shell 待更新


四、种模式配置文件文件加载过程


配置文件的作用: 持久保存用户的配置,只在登入时读取一次


运行中的Shell排列组合有这么几种


  • 登录交互式Shell

  • 登录非交互式Shell


这两种模式,加载配置文件的顺序和过程相同

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.  The --noprofile option may be used when the shell is started to inhibit this behavior.


When a login shell exits, bash reads and executes commands from the files~/.bash_logout and /etc/bash.bash_logout, if the files exists.


  • 非登录交互式Shell

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc,  if  that  file  exists.This  may  be  inhibited  by  using  the  --norc option.  The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.


  • 非登录非交互式Shell

When bash is started non-interactively, to run a shell script, for example, it looks  for  the  variable  BASH_ENV  in  the  environment, expands  its  value  if  it appears there, and uses the expanded value as the name of a file to read and execute.  Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the file name.


(英文解释待续,非登录非交互式Shell加载 BASH_ENV变量测试过程中有点问题,问题链接 待续。各个配置文件的说明未完待续


五、补充一下,以其他用户运行command命令时,是否加-或-l 选项 ,环境变量加载都不成功得原因


以下是好久之前网上的找的,没有原文章链接。


 sudo command

   sudo命令是以root的身份执行command命令,但是环境变量还是当前用户的,执行目录也仍然是当前目录

  即环境变量和执行目录都不会切换到root

sudo command1 | command2     这种命令只会是command1有root权限,但是管道符后面的command则还是没有root权限。Sudo只会提升紧跟其后的那个命令的权限

 

 su - username -c "command1;command2"

  su命令是切换到另一个用户,环境变量会切换到username,执行目录会切换到目标用户username的家目录

  提醒:

   假设当前用户为普通用户lx(该用户没有ORACLE_SID这个环境变量),以lx用户执行命令

   su - oracle -c "echo $ORACLE_SID"

   输出会是空

   su - username -c环境变量会切换到username,为什么没打印出oracle用户的ORACLE_SID环境变量呢?

   因为双引号是不屏蔽$这个特殊字符的,在执行su - oracle -c "echo $ORACLE_SID"命令,

   将先在当前用户下替换变量ORACLE_SID(当前用户的ORACLE_SID变量为空),然后发送给oracle执行的命令就成了 echo ""

   

   我们的意图是将echo $ORACLE_SID这个命令发送给oracle用户,打印出oracle用户用户的环境变量ORACLE_SID,有以下解决方式:


   1、su - oracle -c ‘echo $ORACLE_SID‘   (单引号会屏蔽所有的特殊字符)

   2、su - oracle -c "echo \$ORACLE_SID"


命令行命令“

 su 与 su - 命令的却别:

  su 命令仅切换用户身份,例如从A切换到B,执行whoami命令,显示的是用户B,但当前目录不会切换,

  环境变量也仍未切换,仍为A用户的环境变量

  

  su - 命令切换用户,A切换到B,会以登录B的流程执行,不仅会切换用户还会执行.profile文件,

  更换成B用户的环境变量,目录切换到B的家目录




待更新文章

笔记本太烂,虚拟机不能运行,周六元宵节

回公司 一些列子待更新



参考:man bash 的帮助文档。

本文出自 “xiaoliuer” 博客,请务必保留此出处http://xiaoliuer.blog.51cto.com/11859529/1896907

bash的配置文件(linux学习之八)

标签:bash配置文件 login shell no-login shell

原文地址:http://xiaoliuer.blog.51cto.com/11859529/1896907

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!