Shell编程技术

目录

1 介绍

2 Bash

Bash(GNU Bourne-Again Shell)Brian J. Fox在1987年为GNU计划而写,是Linux与MAC 系统的默认Shell,也移植到了MingwCygwin。如下是主页介绍:

Bash is the GNU Project's shell. Bash is the Bourne Again SHell. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.

The improvements offered by Bash include:

  • Command line editing
  • Unlimited size command history
  • Job Control
  • Shell Functions and Aliases
  • Indexed arrays of unlimited size
  • Integer arithmetic in any base from two to sixty-four

3 调用方式与初始化/Invocation Methods and Inital Scripts

3.1 启动模式与文件

Bash启动方式包括interactive、login、run scripts,三种场景执行不同的脚本初始化。 当对应的脚本文件不存在时忽略。

3.1.1 interactive shell

Bash不带参数启动,或者带有-i参数启动。此时设置 PS1$- 包含 i

interactive模式下,默认执行/etc/bash.bashrc与~/.bashrc,bash参数–norc或–rcfile 可改变默认行为。

3.1.2 login shell

Bash以interactive login Shell,或者non-interactive shell用–login参数启动时, 默认执行/etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile。bash参数 –noprofile参数可阻止次行为。

3.1.3 run scripts

Bash以non-interactive、non-login启动时,搜寻环境变量BASH_ENV,如果环境变量存在, 则在扩展后作为文件名去执行。BASH搜索次文件不使用PATH变量,因此需要设置绝对路径, 或者在当前目录的相对路径。

3.2 调试方法

启动shell后,用命令 bash --verbose 再次启动bash,观察终端输出。附加可选参数 -i、-l、<scripts-name>,设置环境变量export BASH_ENV=<absolute-path-filename>观察 分析。e.g.:

interactive
bash or bash -i
interactive & login
bash -i -l
run-scripts
echo 'echo hello BASH_ENV' > /tmp/bash.env
export BASH_ENV=/tmp/bash.env
echo "echo hello bash scripts" > bash.scripts
bash bash.scripts

3.3 配置

4 变量

4.1 特殊变量

4.1.1 $-

Shell Flag设置,通过set配置。唯一例外的是i,i在初始化时确定,以interactive启动时 设置,不可设置不可清除。例如,set -H设置H,set +H清除。

普通启动Shell,查看结果显示:

~$ echo $-
himBs
Options Description Comments
h 查找时记录命令位置 典型地,在LFS构建系统,切换编译链时需要关闭此选项
i 交互式启动  
m 监控模式,后台进程结束时打印一行退出码 执行:set +m; sleep 1 & 观察
B 关闭打括号扩展 执行:set +B; echo /{,s}bin观察
s 不知道。可能是stdin关联意思?  

man bash 包含详细描述,搜索 abefhkmnpt 。Shell下执行 help set 查看帮助:

set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
    Set or unset values of shell options and positional parameters.

    Change the value of shell attributes and positional parameters, or
    display the names and values of shell variables.

    Options:
      -a  Mark variables which are modified or created for export.
      -b  Notify of job termination immediately.
      -e  Exit immediately if a command exits with a non-zero status.
      -f  Disable file name generation (globbing).
      -h  Remember the location of commands as they are looked up.
      -k  All assignment arguments are placed in the environment for a
          command, not just those that precede the command name.
      -m  Job control is enabled.
      -n  Read commands but do not execute them.
      -o option-name
          Set the variable corresponding to option-name:
              allexport    same as -a
              braceexpand  same as -B
              emacs        use an emacs-style line editing interface
              errexit      same as -e
              errtrace     same as -E
              functrace    same as -T
              hashall      same as -h
              histexpand   same as -H
              history      enable command history
              ignoreeof    the shell will not exit upon reading EOF
              interactive-comments
                           allow comments to appear in interactive commands
              keyword      same as -k
              monitor      same as -m
              noclobber    same as -C
              noexec       same as -n
              noglob       same as -f
              nolog        currently accepted but ignored
              notify       same as -b
              nounset      same as -u
              onecmd       same as -t
              physical     same as -P
              pipefail     the return value of a pipeline is the status of
                           the last command to exit with a non-zero status,
                           or zero if no command exited with a non-zero status
              posix        change the behavior of bash where the default
                           operation differs from the Posix standard to
                           match the standard
              privileged   same as -p
              verbose      same as -v
              vi           use a vi-style line editing interface
              xtrace       same as -x
      -p  Turned on whenever the real and effective user ids do not match.
          Disables processing of the $ENV file and importing of shell
          functions.  Turning this option off causes the effective uid and
          gid to be set to the real uid and gid.
      -t  Exit after reading and executing one command.
      -u  Treat unset variables as an error when substituting.
      -v  Print shell input lines as they are read.
      -x  Print commands and their arguments as they are executed.
      -B  the shell will perform brace expansion
      -C  If set, disallow existing regular files to be overwritten
          by redirection of output.
      -E  If set, the ERR trap is inherited by shell functions.
      -H  Enable ! style history substitution.  This flag is on
          by default when the shell is interactive.
      -P  If set, do not resolve symbolic links when executing commands
          such as cd which change the current directory.
      -T  If set, the DEBUG and RETURN traps are inherited by shell functions.
      --  Assign any remaining arguments to the positional parameters.
          If there are no remaining arguments, the positional parameters
          are unset.
      -   Assign any remaining arguments to the positional parameters.
          The -x and -v options are turned off.

    Using + rather than - causes these flags to be turned off.  The
    flags can also be used upon invocation of the shell.  The current
    set of flags may be found in $-.  The remaining n ARGs are positional
    parameters and are assigned, in order, to $1, $2, .. $n.  If no
    ARGs are given, all shell variables are printed.

    Exit Status:
    Returns success unless an invalid option is given.

5 参考资料