linux下.bashrc的使用和个人用户家目录下的软件安装

.bashrc文件简介:环境配置

.bashrc文件主要保存个人的一些个性化设置,如命令别名替代、设置系统的环境变量(PATH)等。在公用的集群环境下,个人用户登录到该账户下的家目录,此只对这个用户的个性化设置起效。

它们是一些隐藏文件,ls -a可查看。可以看到有四个相关的文件:

  • .bashrc:当你登入shell时以及每次打开新的终端都会读取执行。
  • .bash_profile 只当你登入shell时执行。
  • .bash_logout 当你退出shell时执行的命令。
  • .bash_history 记录之前输入的命令。

这些文件是每一位用户在当前用户的权限之下对终端功能和属性设置,修改.bashrc可以改变环境变量PATH、别名alias和提示符。如果没有sudo权限,是可以修改账户家目录下的.bashrc文件使得个性化的配置生效的,无需root权限,也不会影响其他用户。

.bashrc文件的使用
PATH环境变量修改:修改.bashrc

所有环境变量名都是大写,Linux 区分大小写。变量名有时候以’’$’‘开头,但有时又不是。当设置一个变量时,直接用名称,而不需要加’’$’’,比如:

PATH=/usr/bin:/usr/local/bin:/bin

假如要获取变量值的话,就要在变量名前加’$’:

echo $PATH

处理一般变量是将新的字符串添加到原来的值中,如:

PATH=$PATH:/some/directory

要定义一个全局变量,使在以后打开的终端中都能生效,而不仅仅是当前终端生效,需要将局部变量输出(export),可以用export命令:

export PATH=$PATH:/some/directory
命令别名
alias mv='mv -i'

有了这这句话,当在终端中输入mv test.c led.c实际上输入的是mv -i test.c led.c

提示符

当打开一个终端(黑框)时,最先看到的就是提示符(prompt),如:[lusong@ln02 ~]$

在默认设置下,提示符将显示用户名、主机名、当前所在目录(在 Unix 中,’~'表示当前用户home目录)。最后一个字符可以标识普通用户($),还是root用户(#)。

可以通过$PS1变量来设置提示符。对于初学者来讲,默认设定有些不友好,因为提示符只显示当前目录的最后一部分。如果你看到象这样的提示符,如[lusong@ln02 software]$。在 man bash中的PROMPTING部分,对提示符参数有详细说明。可以加入一些小玩意,如不同格式的当前时间,命令的历史记录号,甚至不同的颜色。比如说你可以有这样的设定: export PS1="[\u: \w]\\$" ,提示符就变成:[wsf: /usr/bin]$

.bashrc的一些其他说明

注释为#号打头;

if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

判断/etc/bashrc这个文件是否存在,如果存在执行这个文件;

非root用户home目录下的软件安装

一般来说,不是root用户,所有的权限就很有限,我们只能在管理员给我们分配的目录下进行文件操作以及软件安装及其相关配置(在.bashrc中配置)等等。
所以,我们在安装软件的时候,一般要指明安装的目录,这样才不会使得安装无效。如何指明安装目录呢?一般别人在做安装包的时候,会把相关的帮助写进去,记得使用--help或者-help查看即可。比如,如果是使用形如xx.sh的shell脚本来安装的,往往运行xx.sh --help就能看到prefix是怎么设定的。安装软件前查看帮助,是一个好习惯,我们应该养成。
下面以安装C++包boost为例,做一个简单说明。

  • 下载安装包(以特定版本为例)
wget http://sourceforge.net/projects/boost/files/boost/1.54.0/boost_1_54_0.tar.gz
  • 解压安装包
tar -xzvf boost_1_54_0.tar.gz
  • 进入解压目录
cd boost_1_54_0
  • 查看用来安装的shell脚本的帮助./bootstrap.sh --help,能看到这样一些信息:
`./bootstrap.sh' prepares Boost for building on a few kinds of systems.

Usage: ./bootstrap.sh [OPTION]... 

Defaults for the options are specified in brackets.

Configuration:
  -h, --help                display this help and exit
  --with-bjam=BJAM          use existing Boost.Jam executable (bjam)
                            [automatically built]
  --with-toolset=TOOLSET    use specific Boost.Build toolset
                            [automatically detected]
  --show-libraries          show the set of libraries that require build
                            and installation steps (i.e., those libraries
                            that can be used with --with-libraries or
                            --without-libraries), then exit
  --with-libraries=list     build only a particular set of libraries,
                            describing using either a comma-separated list of
                            library names or "all"
                            [all]
  --without-libraries=list  build all libraries except the ones listed []
  --with-icu                enable Unicode/ICU support in Regex 
                            [automatically detected]
  --without-icu             disable Unicode/ICU support in Regex
  --with-icu=DIR            specify the root of the ICU library installation
                            and enable Unicode/ICU support in Regex
                            [automatically detected]
  --with-python=PYTHON      specify the Python executable [python]
  --with-python-root=DIR    specify the root of the Python installation
                            [automatically detected]
  --with-python-version=X.Y specify the Python version as X.Y
                            [automatically detected]

Installation directories:
  --prefix=PREFIX           install Boost into the given PREFIX
                            [/usr/local]
  --exec-prefix=EPREFIX     install Boost binaries into the given EPREFIX
                            [PREFIX]

More precise control over installation directories:
  --libdir=DIR              install libraries here [EPREFIX/lib]
  --includedir=DIR          install headers here [PREFIX/include]


  • 根据help提供的信息,指定安装目录等内容。
./bootstrap.sh --prefix=$HOME/usr/local
  • 查看可执行文件b2的安装帮助./b2 --help,返回结果如下:
Boost.Build 2011.12-svn

Project-specific help:

  Project has jamfile at Jamroot

Usage:

  b2 [options] [properties] [install|stage]

  Builds and installs Boost.

Targets and Related Options:

  install                 Install headers and compiled library files to the
  =======                 configured locations (below).

  --prefix=<PREFIX>       Install architecture independent files here.
                          Default; C:\Boost on Win32
                          Default; /usr/local on Unix. Linux, etc.

  --exec-prefix=<EPREFIX> Install architecture dependent files here.
                          Default; <PREFIX>

  --libdir=<DIR>          Install library files here.
                          Default; <EPREFIX>/lib

  --includedir=<HDRDIR>   Install header files here.
                          Default; <PREFIX>/include

  stage                   Build and install only compiled library files to the
  =====                   stage directory.

  --stagedir=<STAGEDIR>   Install library files here
                          Default; ./stage

Other Options:

  --build-type=<type>     Build the specified pre-defined set of variations of
                          the libraries. Note, that which variants get built
                          depends on what each library supports.

                              -- minimal -- (default) Builds a minimal set of
                              variants. On Windows, these are static
                              multithreaded libraries in debug and release
                              modes, using shared runtime. On Linux, these are
                              static and shared multithreaded libraries in
                              release mode.

                              -- complete -- Build all possible variations.

  --build-dir=DIR         Build in this location instead of building within
                          the distribution tree. Recommended!

  --show-libraries        Display the list of Boost libraries that require
                          build and installation steps, and then exit.

  --layout=<layout>       Determine whether to choose library names and header
                          locations such that multiple versions of Boost or
                          multiple compilers can be used on the same system.

                              -- versioned -- Names of boost binaries include
                              the Boost version number, name and version of
                              the compiler and encoded build properties. Boost
                              headers are installed in a subdirectory of
                              <HDRDIR> whose name contains the Boost version
                              number.

                              -- tagged -- Names of boost binaries include the
                              encoded build properties such as variant and
                              threading, but do not including compiler name
                              and version, or Boost version. This option is
                              useful if you build several variants of Boost,
                              using the same compiler.

                              -- system -- Binaries names do not include the
                              Boost version number or the name and version
                              number of the compiler. Boost headers are
                              installed directly into <HDRDIR>. This option is
                              intended for system integrators building
                              distribution packages.

                          The default value is 'versioned' on Windows, and
                          'system' on Unix.

  --buildid=ID            Add the specified ID to the name of built libraries.
                          The default is to not add anything.

  --python-buildid=ID     Add the specified ID to the name of built libraries
                          that depend on Python. The default is to not add
                          anything. This ID is added in addition to --buildid.

  --help                  This message.

  --with-<library>        Build and install the specified <library>. If this
                          option is used, only libraries specified using this
                          option will be built.

  --without-<library>     Do not build, stage, or install the specified
                          <library>. By default, all libraries are built.

Properties:

  toolset=toolset         Indicate the toolset to build with.

  variant=debug|release   Select the build variant

  link=static|shared      Whether to build static or shared libraries

  threading=single|multi  Whether to build single or multithreaded binaries

  runtime-link=static|shared
                          Whether to link to static or shared C and C++
                          runtime.


Configuration help:

  Configuration file at
  /share/home/lusong/boost_1_54_0/tools/build/v2/user-config.jam

  This file is used to configure your Boost.Build installation. You can modify
this file in place, or you can place it in a permanent location so that it
does not get overwritten should you get a new version of Boost.Build. See:

  http://www.boost.org/boost-build2/doc/html/bbv2/overview/configuration.html

for documentation about possible permanent locations.

General command line usage:

    b2 [options] [properties] [targets]

  Options, properties and targets can be specified in any order.
      
Important Options:

  * --clean Remove targets instead of building
  * -a Rebuild everything
  * -n Don't execute the commands, only print them
  * -d+2 Show commands as they are executed
  * -d0 Supress all informational messages
  * -q Stop at first error
  * --reconfigure Rerun all configuration checks
  * --debug-configuration Diagnose configuration
  * --debug-building Report which targets are built with what properties
  * --debug-generator Diagnose generator search/execution

Further Help:

  The following options can be used to obtain additional documentation.

  * --help-options Print more obscure command line options.
  * --help-internal Boost.Build implementation details.
  * --help-doc-options Implementation details doc formatting.

...found 1 target...

  • 根据帮助的信息,执行b2安装
./b2 --prefix=$HOME/usr/local install --with=all
  • 安装完后,如果使用过程中还是提示某些库或者文件找不到,但是手动到安装目录下找,确确实实是安装上了,那么就要考虑将相关目录添加到安装目录了,写到.bashrc文件中即可。
Logo

更多推荐