Shell - set命令

set命令简介

使用内置命令set可以调试Shell脚本的指定部分。
set命令通过选项开关来设置shell的不同特性,每个特性都对应一个选项。

  • set -<Options> 直接启用指定选项
  • set +<Options> 直接停用指定选项
  • set -o <option-name> 通过选项名启用对应的选项
  • set +o <option-name> 通过选项名停用对应的选项

常用命令

  • set -o 输出当前set选项的配置情况

  • set +o 以set命令形式输出当前set选项的配置情况

  • set -x 或者 set -o xtrace 启用跟踪(调试)模式,识别语法错误和逻辑错误,显示所有执行的命令、参数和结果

  • set -v 或者 set -o verbose 启用详细模式,将所有执行过的脚本命令打印到标准输出

  • set -n 或者 set -o noexec 语法检查模式,读取脚本并检查语法错误,但不执行

  • set -e 或者 set -o errexit 如果命令运行失败,脚本立即退出执行

  • set -u 或者 set -o nounset 如果存在未声明(赋值)的变量,脚本立即退出执行(交互式shell不退出)

  • set -o 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
    简而言之,就是如果命令执行失败就返回非0值,如果所有命令都成功则返回0值。

命令帮助

[root@anliven ~]# 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 follow symbolic links when executing commands
          such as cd which change the current directory.
      -T  If set, the DEBUG trap is 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.
[root@anliven ~]#

命令示例

[root@anliven ~]# set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off
[root@anliven ~]#
[root@anliven ~]# set +o
set +o allexport
set -o braceexpand
set -o emacs
set +o errexit
set +o errtrace
set +o functrace
set -o hashall
set -o histexpand
set -o history
set +o ignoreeof
set -o interactive-comments
set +o keyword
set -o monitor
set +o noclobber
set +o noexec
set +o noglob
set +o nolog
set +o notify
set +o nounset
set +o onecmd
set +o physical
set +o pipefail
set +o posix
set +o privileged
set +o verbose
set +o vi
set +o xtrace
[root@anliven ~]#

其他执行shell脚本调试的方法

  1. 在命令行提供参数,调试整个脚本,例如$bash -x script.sh
  2. 脚本开头提供参数,调试整个脚本,例如#!/bin/bash -x

参考信息

posted @ 2020-07-09 00:12  Anliven  阅读(6388)  评论(0编辑  收藏  举报