《Shell学习笔记》set命令

  1. set –help
  2. set 简介
  3. 常用方式
    1. echo “$-“
    2. set -u
    3. set -x
    4. set -o pipefail
    5. set +<参数>
    6. set -o
  4. 附录

set –help

root@DESKTOP-6MVDBD1:~/go/src# 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.
      --  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.

set 简介

set指令能设置所使用shell的执行方式,可依照不同的需求来做设置。

常用方式

echo “$-“

root@DESKTOP-6MVDBD1:~/go/src# echo "$-"
himBHs                # 显示当前shell开启的所有模式开关
root@DESKTOP-6MVDBD1:~/go/src# set -e
root@DESKTOP-6MVDBD1:~/go/src# echo "$-"
ehimBHs               # 显示当前shell开启的所有模式开关

set -u

对于一些变量(如:APP)若忘记设置值,可能会导致 rm -rf /${APP} 变为 rm -rf /.。这是非常危险的操作。
开启 set -u 后执行脚本时,遇到不存在变量,bash报错并停止

root@DESKTOP-6MVDBD1:~/go/src# rm -rf ${APP}
root@DESKTOP-6MVDBD1:~/go/src# set -u
root@DESKTOP-6MVDBD1:~/go/src# rm -rf ${APP}
-bash: APP: unbound variable

set -x

在执行时打印命令及其参数。在调试 shell 脚本的时候很常用。

root@DESKTOP-6MVDBD1:~/go/src# set -x
root@DESKTOP-6MVDBD1:~/go/src# ls
+ ls --color=auto
github.com  k8s.io  mosn.io  volcano.sh

set -o pipefail

set -e有一个例外情况,就是不适用于管道命令。

所谓管道命令,就是多个子命令通过管道运算符(|)组合成为一个大的命令。Bash 会把最后一个子命令的返回值,作为整个命令的返回值。也就是说,只要最后一个子命令不失败,管道命令总是会执行成功,因此它后面命令依然会执行,set -e就失效了。

请看下面这个例子

#!/bin/bash

set -e

foo | echo a

echo bar

执行结果如下。

$ bash script.sh

a

script.sh:行4: foo: 未找到命令

bar

上面代码中,foo是一个不存在的命令,但是foo | echo a这个管道命令会执行成功,导致后面的echo bar会继续执行。

set -o pipefail用来解决这种情况,只要一个子命令失败,整个管道命令就失败,脚本就会终止执行。

set -eo pipefail

foo | echo a

echo bar

运行后,结果如下

$ bash script.sh

a

script.sh:行4: foo: 未找到命令

可以看到,echo bar没有执行。

set +<参数>

使用 + 来替代 - 则代表关闭某个已经开启的选项

root@DESKTOP-6MVDBD1:~/go/src# set -x
root@DESKTOP-6MVDBD1:~/go/src# ls
+ ls --color=auto
github.com  k8s.io  mosn.io  volcano.sh
root@DESKTOP-6MVDBD1:~/go/src# set +x
+ set +x
root@DESKTOP-6MVDBD1:~/go/src# ls
github.com  k8s.io  mosn.io  volcano.sh
root@DESKTOP-6MVDBD1:~/go/src#

set -o

  • 如果命令以非零状态退出,则立即退出。
  • 替换时将未设置的变量视为错误。
  • 管道的返回值是状态,最后一个以非零状态退出的命令,如果没有命令以非零状态退出,则为零。
set -o errexit
set -o nounset
set -o pipefail

附录


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nz_nuaa@163.com
github