LFS 学习日记(第4章环境设置)和 bash 及其内建 set 命令学习小节

2010年06月08日 星期二
1、4.4. Setting Up the Environment http://www.linuxfromscratch.org/lfs/view/6.6/chapter04/settingenvironment.html
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash 这个命令不能得到一个完全干净的 LFS 编译环境的问题,我通过用LFS6.6提供的bash覆盖Ubuntu10.04中自带的bash解决,由于LFS6.6 BASH 不会调用/etc/bash.bashrc,在非lfs用户中的 .bashrc中显式调用它:source /etc/bash.bashrc ( the issue of environment value $PS1 set under Ubuntu 10.04 http://linuxfromscratch.org/pipermail/lfs-support/2010-June/038825.html )

2、在4.5. About SBUs http://www.linuxfromscratch.org/lfs/view/6.6/chapter04/aboutsbus.html 中,“set MAKEFLAGS='-j 2'”的用法是错的,正确的应该是用"export MAKEFLAGS='-j 2'" ( the issue of setting an environment variable with "set" command under Ubuntu 10.04 host http://linuxfromscratch.org/pipermail/lfs-dev/2010-June/063888.html )
todo: set 命令的全面准确用法(参考资源: help set;Advanced Bash-Scripting Guide: Chapter 15. Internal Commands and Builtins http://tldp.org/LDP/abs/html/internal.html#EX34 Using set with positional parameters)

到目前为此,相对于 LFS BOOK 6.6 改变的做法有:在 ~lfs/.bashrc 中设置了
<code>
 MAKEFLAGS='-j 2'
 export MAKEFLAGS
</code>

在/etc/fstab 中自动添加了 /mnt/lfs 的挂载:
<code>
/dev/sdb1       /mnt/lfs        ext3    defaults                 0       0
</code>

2010年06月10日 星期四
bash 及其内建 set 命令学习小节

前面已经初步学习了一下 bash 的登录shell和非登录shell的区别( http://www.learndiary.com/home/space-1-do-blog-id-3780.html )以及一些bash的基础知识( http://www.learndiary.com/home/space-1-do-blog-id-3781.html ),在解决上述问题的时候,又学习了一点bash的相关知识:
1、随 LFS 6.6提供的 bash 源码编译后的man page中并没有 /etc/bash.bashrc之类的全局 .bashrc 文件。Ubuntu 10.04 的 bash 指定了 --rcfile 仍会调用 /etc/bash.bashrc,跟其 man page 说的不一致,是一个 bug( https://bugs.launchpad.net/ubuntu/+source/bash/+bug/589496 );

       --rcfile file
              Execute commands from file instead of the system  wide  initial‐
              ization file /etc/bash.bashrc and the standard personal initial‐
              ization file ~/.bashrc if the shell is interactive (see  INVOCA‐
              TION below).

2、"bash --posix" 或 以 sh的名字来启动bash(sh软链接到bash)将启动 bash 的posix模式,在这种模式下启动的交互式bash读取ENV环境变量中指定的配置文件,而不是/etc/bash.bashrc或~ /.bashrc,在这种模式下,Ubuntu 10.04 的bash也工作如 man page 所说的一样;
       When  bash  is  started in posix mode, as with the --posix command line
       option, it follows the POSIX standard for startup files.  In this mode,
       interactive  shells  expand  the ENV variable and commands are read and
       executed from the file whose name is  the  expanded  value.   No  other
       startup files are read.

 When invoked as an interactive shell  with  the
       name  sh,  bash  looks for the variable ENV, expands its value if it is
       defined, and uses the expanded value as the name of a file to read  and
       execute.  Since a shell invoked as sh does not attempt to read and exe‐
       cute commands from any other startup files, the --rcfile option has  no
       effect.   A  non-interactive  shell  invoked  with the name sh does not
       attempt to read any other startup files.   When  invoked  as  sh,  bash
       enters posix mode after the startup files are read.

3、在bash脚本文件中启动的shell是非交互式的shell,读取在环境变量BASH_ENV中指定的配置文件,而不是 /etc/bash.bashrc 或 ~/.bashrc。

如文件test2.sh
#!/bin/bash
export BASH_ENV=/home/mdx/bashenvrc
bash /home/mdx/test.sh

文件bashenvrc
echo "I am bashenvrc."
export PATH=/GGG

文件 test.sh
#!/bin/bash
echo "PATH is: "$PATH
echo "end of bashenvtest"

这时候,在test.sh 中输出的 PATH 值就会是 /GGG。但是,这个PATH值不会作为查找命令的路径。请看 man bash中:

       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  com‐
       mand 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.

bash 内建set命令针对bash的选项可以直接跟在bash后面达到同样的效果,如: set +h 的效果可用 bash +h 实现

OPTIONS
       In  addition  to  the  single-character shell options documented in the
       description of the set builtin command, bash interprets  the  following
       options when it is invoked:

Bash内建set命令的用法:(“ help set ” 和 http://ss64.com/bash/set.html )
正如前面所写,set命令不是用来设置环境变量的,它的主要作用有3个:
1、设置shell属性值
如:“ set +h ”将使shell不记住命令执行路径,跟“ bash +h ”效果是一样的
2、设置位置参数
如:“ set a b c ”将使 $1=a $2=b $3=c
“ set +h - a b c ”执行设置shell属性值后再设置位置参数(不过发现 set +h a b c 也是一样的效果)
“ set -- ”将清空位置参数,“ set -- d e f ”将重新设置位置参数(发现 set d e f 也可重新设置位置参数)
3、单独不带参数的set命令显示shell变量的名称及值和shell的函数

另外,其它 shell (如:ubuntu 10.04 中的 dash)也有内建 set 命令,如 Ubuntu 10.04 dash中的内建 set 命令:
     set [{ -options | +options | -- }] arg ...
            The set command performs three different functions.

            With no arguments, it lists the values of all shell variables.

            If options are given, it sets the specified option flags, or
            clears them as described in the section called Argument List
            Processing.  As a special case, if the option is -o or +o and no
            argument is supplied, the shell prints the settings of all its
            options.  If the option is -o, the settings are printed in a
            human-readable format; if the option is +o, the settings are
            printed in a format suitable for reinput to the shell to affect
            the same option settings.

            The third use of the set command is to set the values of the
            shell's positional parameters to the specified args.  To change
            the positional parameters without changing any options, use “--”
            as the first argument to set.  If no args are present, the set
            command will clear all the positional parameters (equivalent to
            executing “shift $#”.)