zoukankan      html  css  js  c++  java
  • 【Ubuntu 笔记】翻译 The Bash Shell Startup Files

    一、前言

         修改环境变量时对环境变量进行google,找到了这篇。读来看看。

         跟ubuntu 11.10有些不同,但是大体的配置文件的功能以及相互之间的联系是一样的。比起网上那写copy来copy去的什么.profile等文件的泛泛而谈,这篇清楚多了。还找到了几篇启动设置的,等下或者明天再一起读完来。

          小白觉得,认真读完是很有帮助的,比起在看10遍千篇一律的东西。

    二、原文

          http://www.linuxfromscratch.org/blfs/view/cvs/postlfs/profile.html

    三、翻译

         Bash 启动文件

      shell程序/bin/bash(下文中直接称为shell)有一系列的启动文件来构建运行环境。每个文件有各自的用处,它们对login以及交互式环境有不同的影响。位于/etc中的配置文件一般提供全局设置。同一变量,用户的home/user里文件的对其的设置会覆盖全局对其的设置。

           用户通过/bin/login尝试登陆,系统读取/etc/passwd,成功登陆后系统会启动交互式登陆的Shell。这个Shell启动时会读取/etc/profile和等价的用户私有的~/.bash_profile(UBUNTU里面可能是~/.profile)。

      交互式未登陆的shell

    • 交互式未登陆的shell的启动通常是通过命令行启动shell程序(e.g. $/bin/bash)或者是通过/bin/su命令行来启动。
    • 交互式未登录的shell一般在图形环境里的终端程序启动,如xterm,konsole。像这种被调用的shell会复制父进程的运行环境(parent environment),之后读取该用户的~/.bashrc文件完成此用户的特定配置

      shell调用不会读取~/.bash_logout。当user退出登陆的时候,此文件才被读取。

      很多版本用/etc/bashrc来进行系统全局的未登录shell配置。这个文件是从user的~/.bashrc中被调用读取,并没有直接写入bash.此版本(Beyond Linux® From Scratch,这个么,新手,对版本不了解,也没想去查。。。)也沿袭了这一惯例。  

      For more information see info bash -- Nodes: Bash Startup Files and Interactive Shells.

    Note:
    下面的指导都是在文件系统/etc下创建文件,需要你获得root权限。如果你选择在user主目录下创建文件,那么你以非特权用户执行命令

     

    • /etc/profile

      这是一个基文件/etc/profile(base /etc/profile).这个文件一开头就在设定一些帮助函数以及基本的参数。为了安全,它规定了一些bash的历史参数,禁止保留root用户的永久历史文件。它还负责设定了缺省的用户命令(user prompt)。之后,调用/etc/profile.d中的单一的小脚本来完成初始化。

      关于在命令中使用的转移序列(escape sequences)的更多信息,参见 info bash -- Node: Printing a Prompt

    View Code
    cat > /etc/profile << "EOF"
    # Begin /etc/profile
    # Written for Beyond Linux From Scratch
    # by James Robertson <jameswrobertson@earthlink.net>
    # modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>

    # System wide environment variables and startup programs.

    # System wide aliases and functions should go in /etc/bashrc. Personal
    # environment variables and startup programs should go into
    # ~/.bash_profile. Personal aliases and functions should go into
    # ~/.bashrc.

    # Functions to help us manage paths. Second argument is the name of the
    # path variable to be modified (default: PATH)
    pathremove () {
    local IFS=':'
    local NEWPATH
    local DIR
    local PATHVARIABLE=${2:-PATH}
    for DIR in ${!PATHVARIABLE} ; do
    if [ "$DIR" != "$1" ] ; then
    NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
    fi
    done
    export $PATHVARIABLE="$NEWPATH"
    }

    pathprepend () {
    pathremove $1 $2
    local PATHVARIABLE=${2:-PATH}
    export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
    }

    pathappend () {
    pathremove $1 $2
    local PATHVARIABLE=${2:-PATH}
    export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
    }


    # Set the initial path
    export PATH=/bin:/usr/bin

    if [ $EUID -eq 0 ] ; then
    pathappend /sbin:/usr/sbin
    unset HISTFILE
    fi

    # Setup some environment variables.
    export HISTSIZE=1000
    export HISTIGNORE="&:[bf]g:exit"

    # Setup a red prompt for root and a green one for users.
    NORMAL="\[\e[0m\]"
    RED="\[\e[1;31m\]"
    GREEN="\[\e[1;32m\]"
    if [[ $EUID == 0 ]] ; then
    PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL"
    else
    PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL"
    fi

    for script in /etc/profile.d/*.sh ; do
    if [ -r $script ] ; then
    . $script
    fi
    done

    # Now to clean up
    unset pathremove pathprepend pathappend

    # End /etc/profile
    EOF

     

    • The /etc/profile.d Directory

      现在创建/etc/profile.d目录,这里存放单个用户的初始化脚本。
    install --directory --mode=0755 --owner=root --group=root /etc/profile.d

     

    • /etc/profile.d/dircolors.sh

      此脚本使用~/.dircolors 和 /etc/dircolors 来控制目录列表下文件名字的颜色。这些设置会控制如同ls -color的输出颜色。这一章的结尾会给出初始化这些文件的指导。
    cat > /etc/profile.d/dircolors.sh << "EOF"
    # Setup for /bin/ls to support color, the alias is in /etc/bashrc.
    if [ -f "/etc/dircolors" ] ; then
    eval $(dircolors -b /etc/dircolors)

    if [ -f "$HOME/.dircolors" ] ; then
    eval $(dircolors -b $HOME/.dircolors)
    fi
    fi
    alias ls='ls --color=auto'
    EOF
    • /etc/profile.d/extrapaths.sh

      这个脚本向PATH 和 PKG_CONFIG_PATH环境变量里添加一些有用的路径。如果你希望的话,你可以取消最后章节的注释,在你的路径末尾加点(./)(If you want, you can uncomment the last section to put a dot at the end of your path.)这样就可以不用添加./来执行当前目录下的可执行文件(即将./加入环境变量中)。然而,这种做法通常被认为是有安全隐患的(security hazard).
      View Code

     

    • /etc/profile.d/readline.sh

      这个脚本建立了缺省的inputrc 配置文件。如果用户自己没有设定,则使用全局的配置文件。
      cat > /etc/profile.d/readline.sh << "EOF"
      # Setup the INPUTRC environment variable.
      if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
      INPUTRC=/etc/inputrc
      fi
      export INPUTRC
      EOF
       
    • /etc/profile.d/umask.sh

      设置文件权限掩码(umask)对系统安全是很重要的。这里系统用户的缺省读权限被关闭,且不同的用户名以及组名对应的权限也不一样。
      cat > /etc/profile.d/umask.sh << "EOF"
      # By default we want the umask to get set.
      if [ "$(id -gn)" = "$(id -un)" -a $EUID -gt 99 ]; then
      umask 002
      else
      umask 022
      fi
      EOF
       
    • /etc/profile.d/X.sh

        X安装了的话,PATH 和 PKG_CONFIG_PATH也同时更新  

    cat > /etc/profile.d/X.sh << "EOF"
    if [ -x /usr/X11R6/bin/X ]; then
    pathappend /usr/X11R6/bin
    fi
    if [ -d /usr/X11R6/lib/pkgconfig ] ; then
    pathappend /usr/X11R6/lib/pkgconfig PKG_CONFIG_PATH
    fi
    EOF
    • /etc/profile.d/i18n.sh

      这个脚本设定了本地语言支持的环境变量。对这些变量的完整讨论在 LFS Bash Shell Startup Files这里找到。

    cat > /etc/profile.d/i18n.sh << "EOF"
    # Set up i18n variables
    export LANG=<ll>_<CC>.<charmap><@modifiers>
    EOF
     
    • 其他

        其他的初始化也可以直接加相应的脚本到/etc/profile.d目录下,这样加到profile里面去

    • /etc/bashrc

      这是基本的(原始的)/etc/bashrc.看注释应该就可以懂了
      View Code
      cat > /etc/bashrc << "EOF"
      # Begin /etc/bashrc
      # Written for Beyond Linux From Scratch
      # by James Robertson <jameswrobertson@earthlink.net>
      # updated by Bruce Dubbs <bdubbs@linuxfromscratch.org>

      # System wide aliases and functions.

      # System wide environment variables and startup programs should go into
      # /etc/profile. Personal environment variables and startup programs
      # should go into ~/.bash_profile. Personal aliases and functions should
      # go into ~/.bashrc

      # Provides a colored /bin/ls command. Used in conjunction with code in
      # /etc/profile.

      alias ls='ls --color=auto'

      # Provides prompt for non-login shells, specifically shells started
      # in the X environment. [Review the LFS archive thread titled
      # PS1 Environment Variable for a great case study behind this script
      # addendum.]

      NORMAL="\[\e[0m\]"
      RED="\[\e[1;31m\]"
      GREEN="\[\e[1;32m\]"
      if [[ $EUID == 0 ]] ; then
      PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL"
      else
      PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL"
      fi

      # End /etc/bashrc
      EOF
    • ~/.bash_profile

      这是基本的~/.bash_profile.如果你希望每个新用户都自动拥有这个文件,那么将这个命令的输出导入到/etc/skel/.bash_profile里面,检查你运行完这个命令后的权限。你可以将/etc/skel/.bash_profile复制到已经存在的用户的主目录下,包括root,然后适当的设置拥有者和拥有组。
      View Code
      cat > ~/.bash_profile << "EOF"
      # Begin ~/.bash_profile
      # Written for Beyond Linux From Scratch
      # by James Robertson <jameswrobertson@earthlink.net>
      # updated by Bruce Dubbs <bdubbs@linuxfromscratch.org>

      # Personal environment variables and startup programs.

      # Personal aliases and functions should go in ~/.bashrc. System wide
      # environment variables and startup programs are in /etc/profile.
      # System wide aliases and functions are in /etc/bashrc.

      append () {
      # First remove the directory
      local IFS=':'
      local NEWPATH
      for DIR in $PATH; do
      if [ "$DIR" != "$1" ]; then
      NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
      fi
      done

      # Then append the directory
      export PATH=$NEWPATH:$1
      }

      if [ -f "$HOME/.bashrc" ] ; then
      source $HOME/.bashrc
      fi

      if [ -d "$HOME/bin" ] ; then
      append $HOME/bin
      fi

      unset append

      # End ~/.bash_profile
      EOF
    • ~/.bashrc

      下面是基本的~/.bashrc. 除了目标文件名不同外, /etc/skel如何调用.bash_profile的原理在这里也适用。
      cat > ~/.bashrc << "EOF"
      # Begin ~/.bashrc
      # Written for Beyond Linux From Scratch
      # by James Robertson <jameswrobertson@earthlink.net>

      # Personal aliases and functions.

      # Personal environment variables and startup programs should go in
      # ~/.bash_profile. System wide environment variables and startup
      # programs are in /etc/profile. System wide aliases and functions are
      # in /etc/bashrc.

      if [ -f "/etc/bashrc" ] ; then
      source /etc/bashrc
      fi

      # End ~/.bashrc
      EOF
    • ~/.bash_logout

      这是临时的~/.bash_logout文件。你会发现~/.bash_logout里没有一个明确的clear指令。这是因为clear在/etc/issue文件里控制。
      cat > ~/.bash_logout << "EOF"
      # Begin ~/.bash_logout
      # Written for Beyond Linux From Scratch
      # by James Robertson <jameswrobertson@earthlink.net>

      # Personal items to perform on logout.

      # End ~/.bash_logout
      EOF
    • /etc/dircolors

      如果你想使用dircolor的功能,那就运行下面的命令。/etc/skel的建立的步骤对一个新用户产生~/.dircolors文件同样适用。像之前一样,改变名字,确保权限,owner,group的正确性就好。
      dircolors -p > /etc/dircolors
      如果你想自定义不同类型文件的显示颜色,那就编辑/etc/dircolors文件。操作指导文件里就有注释.
       
      最后,Ian Macdonald 在设定自己的shell环境方面有一些很好的资料,建议。移步 http://www.caliban.org/bash/index.shtml
  • 相关阅读:
    晨考总结第二天
    晨考总结第一天
    常用的设计模式总结
    AOP底层原理剖析
    Spring AOP
    jstat统计输出说明
    zabbix 触发器匹配字符串告警
    hive部分常用函数
    nginx日志说明
    windwos文件句柄数限制
  • 原文地址:https://www.cnblogs.com/hundan/p/2243637.html
Copyright © 2011-2022 走看看