第一部分


An interactive login shell is started after a successful login, using /bin/login, by reading the /etc/passwd file. This shell invocation normally reads /etc/profile and its private equivalent ~/.bash_profile upon startup.


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. This type of shell invocation normally copies the parent environment and then reads the user's ~/.bashrc file for additional startup configuration instructions.


A non-interactive shell is usually present when a shell script is running. It is non-interactive because it is processing a script and not waiting for user input between commands. For these shell invocations, only the environment inherited from the parent shell is used.

The file ~/.bash_logout is not used for an invocation of the shell. It is read and executed when a user exits from an interactive login shell


第二部分


export ABC="123"
su lfs
echo $ABC

export ABC="123"
su - lfs
echo $ABC

实际上,当用su 用户 的时候,会使用当前用户的环境变量;如果当前用户是root,su后还是使用root的环境变量,而 su - 用户 的时候,会使用该用户登录时获取的环境变量,就是用户lfs的环境变量。


第三部分  shell变量   (环境变量、预定义变量、位置变量、用户自定义变量)

一、环境变量   (可以较好的了解到用户当前状态信息)

    1、查看环境变量
     #set  |  head -3   (查看多个环境变量)
     #echo  $PATH     ( 查询某个环境变量)

     2、常用环境变量  
     #echo  $USER    ( 当前用户的登陆名称)
     #echo  $UID    ( 当前用户的用户号)
     #echo $SHELL    ( 当前用户登陆的shell)
     #echo  $HOME   (当前登陆用户的主目录)
     #echo  $PWD     (用户当前所在目录)
     #echo  $PATH    (当前用户的命令搜索路径)
     #echo  $PS1    (当前用户的主提示符)
     #echo  $PS2    (当前用户的辅助提示符)

    3、环境变量配置文件  (分全局配置文件、用户配置文件)
       环境变量全局配置文件:
         /etc/profile      ( 负责设置全局环境,并应用与所有的用户登陆shell)
         /etc/bashrc      ( 定义全局的函数和别名,除为系统中所有登陆用户提供设置外,

                                   也使用与非交互shell和非登陆shell)

       用户环境变量配置文件:
         /home/sto1/.bash_profile   (存在于用户的主目录内,作用于当前用户)
         /home/sto1/.bash_bashrc   (存在于用户的主目录内,作用于当前用户)
-------------------------------------------------------------
二、位置变量   (与shell脚本程序执行时所使用的命令参数相对应,命令行中的参数按照从左到右的顺序给位置变量赋值)

      $0     预定义变量
      $n     n="1"~9之间的一个数

      引号的作用:
      单引号('……') 对引号中的字符串不进行任何变量替换,保持字符串的原始值;
      双引号("……")对引号中的变量会以变量的值来替换,并作为字符串的一部分;
      反引号(`……`) 用于进行命令替换,引号中命令执行的结果将替换命令本身作为字符串
----------------------------------------------------------
三、预定义变量  (是系统中已定义好的变量,用户只能使用不能改变或创建)

     $#     表示参数的数量
     $*      表示所有位置参数的内容
     $?      表示上一个命令执行后退出时的状态,为0表示正确执行,非0执行错误
     $$      表示当前进程的进程号
     $0      表示当前执行的进程名
     $!      表示后台运行的最后一个进程号
------------------------------------------------------------
四、用户自定义变量  (又称本地变量,用户自己定义的变量,在用户自己的shell中有效)

     1、自定义变量的设置
          # DAY="sunday"

     2、自定义变量的查看与引用
          # echo $DAY

     3、自定义变量的输出   (输出变量到全局变量,在所有用户的子shell中使用)
          # export DAY

     4、自定义变量的清除
          # unset DAY
------------------------------------------------------------------------
五、shell脚本

      1、创建shell脚本:
           # vi  hello.sh     (文件名用后缀.sh结尾,用来表明这是个shell脚本)

       2、shell脚本的首行(固定格式):
           #!/bin/shell

       3、注释行以#开头:
           # This  is  my  first  shell  program.

       4、脚本运行:
           # chmod  u+x  hello.sh   (给脚本添加可执行属性)
           # bash  hello.sh    (使用shell命令程序执行脚本,需要设置可执行属性)
           # ./hello.sh  (执行当前目录下的hello.sh,需要设置可执行属性)
           # /home/user1/hello.sh  (需要设置可执行属性)

           # . hello.sh  (用 . 命令执行,通常在脚本文件中调用其他脚本文件时使用,不需要hello.sh有可执行属性)  


第四部分  让Linux系统的程序在开机时自动运行

【摘 要】 一个交互的登陆shell会在 /bin/login 成功登陆之后运行。一个交互的非登陆shell是通过命令行来运行的,如[prompt]$/bin/bash。一般一个非交互的shell出现在运行 shell脚本的时候。之所以叫非交互的  shell,是因为它不在命令行上等待输入而只是执行脚本程序。


Linux有自己一套完整的启动体系,抓住了linux启动的脉络,linux的启动过程将不再神秘。

阅读之前建议先看一下附图。

本文中假设inittab中设置的init tree为:

/etc/rc.d/rc0.d

/etc/rc.d/rc1.d

/etc/rc.d/rc2.d

/etc/rc.d/rc3.d

/etc/rc.d/rc4.d

/etc/rc.d/rc5.d

/etc/rc.d/rc6.d

/etc/rc.d/init.d

1. 关于linux的启动

init是所有进程之父

init读取/etc/inittab,执行rc.sysinit脚本

(注意文件名是不一定的,有些unix甚至会将语句直接写在inittab中)

rc.sysinit脚本作了很多工作:

init $PATH
config network
start swap function
set hostname
check root file system, repair if needed
check root space
....

rc.sysinit根据inittab执行rc?.d脚本

linux是多用户系统,getty是多用户与单用户的分水岭

在getty之前运行的是系统脚本

2. 关于rc.d

所有启动脚本放置在 /etc/rc.d/init.d下

rc?.d中放置的是init.d中脚本的链接,命名格式是:

S{number}{name}

K{number}{name}

S开始的文件向脚本传递start参数

K开始的文件向脚本传递stop参数

number决定执行的顺序

3. 启动脚本示例

这是一个用来启动httpd的 /etc/rc.d/init.d/apache 脚本:

#!/bin/bash
source /etc/sysconfig/rc
source $rc_functions

case "$1" in
   start)
         echo "Starting Apache daemon..."
         /usr/local/apache2/bin/apachectl -k start
         evaluate_retval
         ;;
   stop)
         echo "Stopping Apache daemon..."
         /usr/local/apache2/bin/apachectl -k stop
         evaluate_retval
         ;;
   restart)
         echo "Restarting Apache daemon..."
         /usr/local/apache2/bin/apachectl -k restart
         evaluate_retval
         ;;
   status)
         statusproc /usr/local/apache2/bin/httpd
         ;;
   *)
         echo "Usage: $0 {start|stop|restart|status}"
         exit 1
         ;;
esac

可以看出他接受start,stop,restart,status参数


然后可以这样建立rc?.d的链接:

cd /etc/rc.d/init.d &&
ln -sf ../init.d/apache ../rc0.d/K28apache &&
ln -sf ../init.d/apache ../rc1.d/K28apache &&
ln -sf ../init.d/apache ../rc2.d/K28apache &&
ln -sf ../init.d/apache ../rc3.d/S32apache &&
ln -sf ../init.d/apache ../rc4.d/S32apache &&
ln -sf ../init.d/apache ../rc5.d/S32apache &&
ln -sf ../init.d/apache ../rc6.d/K28apache

4. 关于rc.local

经常使用的 rc.local 则完全是习惯问题,不是标准。

各个发行版有不同的实现方法,可以这样实现:

touch /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc1.d/S999rc.local &&
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc2.d/S999rc.local &&
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc3.d/S999rc.local &&
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc4.d/S999rc.local &&
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc5.d/S999rc.local &&
ln -sf /etc/rc.d/rc.local /etc/rc.d/rc6.d/S999rc.local

5. 关于bash启动脚本


/etc/profile

/etc/bashrc

~/.bash_profile

~/.bashrc

是bash的启动脚本

一般用来设置单用户的启动环境,也可以实现开机单用户的程序,但要明确他们都是属于bash范畴而不是系统范畴。

他们的具体作用介绍如下:

/bin/bash这个命令解释程序(后面简称shell)使用了一系列启动文件来建立一个运行环境:


/etc/profile

/etc/bashrc

~/.bash_profile

~/.bashrc

~/.bash_logout

每一个文件都有特殊的功用并对登陆和交互环境有不同的影响。

/etc/profile 和 ~/.bash_profile 是在启动一个交互登陆shell的时候被调用。

/etc/bashrc 和 ~/.bashrc 是在一个交互的非登陆shell启动的时候被调用。

~/.bash_logout 在用户注销登陆的时候被读取

一个交互的登陆shell会在 /bin/login 成功登陆之后运行。一个交互的非登陆shell是通过命令行来运行的,如[prompt]$/bin/bash。一般一个非交互的shell出现在运行 shell脚本的时候。之所以叫非交互的shell,是因为它不在命令行上等待输入而只是执行脚本程序。


6. 关于开机程序的自动启动


系统脚本可以放置在/etc/rc.d/init.d中并建立/etc/rc.d/rc?.d链接,也可以直接放置在/etc/rc.d/rc.local中。

init.d脚本包含完整的start,stop,status,reload等参数,是标准做法,推荐使用。

为特定用户使用的程序(如有的用户需要使用中文输入法而有的不需要)放置在~/中的bash启动脚本中