Linux set命令

更多资料下载、嵌入式视频教程,点击淘宝店:wanglitao.taobao.com

Linux set命令一般用来设置shell 脚本的执行方式。当我们使用bash解释器去执行一个脚本build.sh的时候:

# bash build.sh

脚本build.sh会在一个新的shell里执行,这个 Shell 就是脚本的执行环境,Bash 默认给定了这个环境的各种参数。当然,这个shell环境的执行参数也可以修改定制。set命令用来修改 Shell 环境的运行参数,也就是可以定制环境。一共有十几个参数可以定制。

# set --help
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.

几个常用的参数介绍

set -u 命令

在默认情况下,Bash 在执行脚本的时候,如果遇到不存在的变量,Bash 默认忽略它,然后接着往下继续执行。大多数情况下,这不是开发者想要的行为,遇到变量不存在,脚本应该报错,而不是一声不响地往下执行。如果你想让Bash执行脚本时,遇到不存在的变量打印报错,并停止执行,可以在脚本文件的开始加一句:set -u

#!/bin/bash
set -u
echo $MY_HOME

脚本的运行结果如下:

# ./build.sh 
./build.sh: line 3: MY_HOME: unbound variable

set -x 命令

默认的情况下,脚本执行后,屏幕只显示运行结果,没有其他内容。如果一个脚本中有多个命令,你想知道打印结果到底是哪个命令执行的结果,可以在脚本的头部加一句:set -x

#!/bin/bash
set -x
echo $MY_HOME
echo "hello, zhaixue.cc"

脚本的运行结果:

# ./build.sh 
+ echo

+ echo 'hello, zhaixue.cc'
hello, zhaixue.cc

set -x会将每一条执行的命令也打印出来,并在每个打印命令的后面,紧跟着打印出命令的执行结果。

set -e 命令

默认情况下,如果脚本里面有运行失败的命令(返回值非0),Bash 会继续执行后面的命令。

# cat build.sh 
#!/bin/bash
hello
echo "hello, zhaixue.cc"

# ./build.sh 
./build.sh: line 3: hello: command not found
hello, zhaixue.cc

如果你想让脚本执行命令错误(返回结果非0)时,就停止执行,可以加一句:set -e

# cat build.sh
#!/bin/bash
set -e
hello
echo "hello, zhaixue.cc"

# ./build.sh 
./build.sh: line 3: hello: command not found
《Linux三剑客》视频教程,从零开始快速掌握Linux开发常用的工具:Git、Makefile、vim、autotools、debug,免费赠送C语言视频教程,C语言项目实战:学生成绩管理系统。详情请点击淘宝链接:Linux三剑客