set -x与set +x可以用于脚本调试。set 是把它下面的命令打印到屏幕。
set -x 是开启 set +x是关闭。set去track一段代码的显示情况。
执行set -x后,对整个脚本有效。
[root@charliye bin]# ls -l /bin/bash
+ ls --color=tty -l /bin/bash
-rwxr-xr-x 1 root root 722684 Jul 12 2006 /bin/bash
针对一部分script,可以选择 set -x 和 set +x配套使用。比如在一个脚本里:
set -x # activate debugging from here
w
set +x # stop debugging from here
---------------------------------------------------------------------------------------------
set指令能设置所使用shell的执行方式,可依照不同的需求来做设置
-a 标示已修改的变量,以供输出至环境变量。
-b 使被中止的后台程序立刻回报执行状态。
-C 转向所产生的文件无法覆盖已存在的文件。
-d Shell预设会用杂凑表记忆使用过的指令,以加速指令的执行。使用-d参数可取消。
-e 若指令传回值不等于0,则立即退出shell。
-f 取消使用通配符。
-h 自动记录函数的所在位置。
-H Shell 可利用"!"加<指令编号>的方式来执行history中记录的指令。
-k 指令所给的参数都会被视为此指令的环境变量。
-l 记录for循环的变量名称。
-m 使用监视模式。
-n 只读取指令,而不实际执行。
-p 启动优先顺序模式。
-P 启动-P参数后,执行指令时,会以实际的文件或目录来取代符号连接。
-t 执行完随后的指令,即退出shell。
-u 当执行时使用到未定义过的变量,则显示错误信息。
-v 显示shell所读取的输入值。
-x 执行指令后,会先显示该指令及所下的参数。
+<参数> 取消某个set曾启动的参数。
下面是man set的结果:
set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
Without options, the name and value of each shell variable are displayed in a format that can be reused
as input for setting or resetting the currently-set variables. Read-only variables cannot be reset. In
posix mode, only shell variables are listed. The output is sorted according to the current locale.
When options are specified, they set or unset shell attributes. Any arguments remaining after the
options are processed are treated as values for the positional parameters and are assigned, in order, to
$1, $2, ... $n. Options, if specified, have the following meanings:
-a Automatically mark variables and functions which are modified or created for export to the envi-
ronment of subsequent commands.
-b Report the status of terminated background jobs immediately, rather than before the next primary
prompt. This is effective only when job control is enabled.
-e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status.
The shell does not exit if the command that fails is part of the command list immediately fol-
lowing a while or until keyword, part of the test in an if statement, part of a && or || list,
or if the command's return value is being inverted via !. A trap on ERR, if set, is executed
before the shell exits. (会在命令返回值为非零的时候,立即退出。)
Without options, the name and value of each shell variable are displayed in a format that can be reused
as input for setting or resetting the currently-set variables. Read-only variables cannot be reset. In
posix mode, only shell variables are listed. The output is sorted according to the current locale.
When options are specified, they set or unset shell attributes. Any arguments remaining after the
options are processed are treated as values for the positional parameters and are assigned, in order, to
$1, $2, ... $n. Options, if specified, have the following meanings:
-a Automatically mark variables and functions which are modified or created for export to the envi-
ronment of subsequent commands.
-b Report the status of terminated background jobs immediately, rather than before the next primary
prompt. This is effective only when job control is enabled.
-e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status.
The shell does not exit if the command that fails is part of the command list immediately fol-
lowing a while or until keyword, part of the test in an if statement, part of a && or || list,
or if the command's return value is being inverted via !. A trap on ERR, if set, is executed
before the shell exits. (会在命令返回值为非零的时候,立即退出。)
-f Disable pathname expansion.
-h Remember the location of commands as they are looked up for execution. This is enabled by
default.
-k All arguments in the form of assignment statements are placed in the environment for a command,
not just those that precede the command name.
-m Monitor mode. Job control is enabled. This option is on by default for interactive shells on
systems that support it (see JOB CONTROL above). Background processes run in a separate process
group and a line containing their exit status is printed upon their completion.
-n Read commands but do not execute them. This may be used to check a shell script for syntax
errors. This is ignored by interactive shells.
-p Turn on privileged mode. In this mode, the $ENV and $BASH_ENV files are not processed, shell
functions are not inherited from the environment, and the SHELLOPTS variable, if it appears in
the environment, is ignored. If the shell is started with the effective user (group) id not
equal to the real user (group) id, and the -p option is not supplied, these actions are taken
and the effective user id is set to the real user id. If the -p option is supplied at startup,
the effective user id is not reset. Turning this option off causes the effective user and group
ids to be set to the real user and group ids.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when performing parameter expansion. If expansion is
attempted on an unset variable, the shell prints an error message, and, if not interactive,
exits with a non-zero status.
-v Print shell input lines as they are read.
-x After expanding each simple command, for command, case command, select command, or arithmetic
for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.(在执行命令后显示指令,以及参数)
-B The shell performs brace expansion (see Brace Expansion above). This is on by default.
-C If set, bash does not overwrite an existing file with the >, >&, and <> redirection operators.
This may be overridden when creating output files by using the redirection operator >| instead
of >.
-E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands
executed in a subshell environment. The ERR trap is normally not inherited in such cases.
-H Enable ! style history substitution. This option is on by default when the shell is interac-
tive.
-P If set, the shell does not follow symbolic links when executing commands such as cd that change
the current working directory. It uses the physical directory structure instead. By default,
bash follows the logical chain of directories when performing commands which change the current
directory.
-T If set, any traps on DEBUG and RETURN are inherited by shell functions, command substitutions,
and commands executed in a subshell environment. The DEBUG and RETURN traps are normally not
inherited in such cases.
-- If no arguments follow this option, then the positional parameters are unset. Otherwise, the
positional parameters are set to the args, even if some of them begin with a -.
- Signal the end of options, cause all remaining args to be assigned to the positional parameters.
The -x and -v options are turned off. If there are no args, the positional parameters remain
unchanged.
-h Remember the location of commands as they are looked up for execution. This is enabled by
default.
-k All arguments in the form of assignment statements are placed in the environment for a command,
not just those that precede the command name.
-m Monitor mode. Job control is enabled. This option is on by default for interactive shells on
systems that support it (see JOB CONTROL above). Background processes run in a separate process
group and a line containing their exit status is printed upon their completion.
-n Read commands but do not execute them. This may be used to check a shell script for syntax
errors. This is ignored by interactive shells.
-p Turn on privileged mode. In this mode, the $ENV and $BASH_ENV files are not processed, shell
functions are not inherited from the environment, and the SHELLOPTS variable, if it appears in
the environment, is ignored. If the shell is started with the effective user (group) id not
equal to the real user (group) id, and the -p option is not supplied, these actions are taken
and the effective user id is set to the real user id. If the -p option is supplied at startup,
the effective user id is not reset. Turning this option off causes the effective user and group
ids to be set to the real user and group ids.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when performing parameter expansion. If expansion is
attempted on an unset variable, the shell prints an error message, and, if not interactive,
exits with a non-zero status.
-v Print shell input lines as they are read.
-x After expanding each simple command, for command, case command, select command, or arithmetic
for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.(在执行命令后显示指令,以及参数)
-B The shell performs brace expansion (see Brace Expansion above). This is on by default.
-C If set, bash does not overwrite an existing file with the >, >&, and <> redirection operators.
This may be overridden when creating output files by using the redirection operator >| instead
of >.
-E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands
executed in a subshell environment. The ERR trap is normally not inherited in such cases.
-H Enable ! style history substitution. This option is on by default when the shell is interac-
tive.
-P If set, the shell does not follow symbolic links when executing commands such as cd that change
the current working directory. It uses the physical directory structure instead. By default,
bash follows the logical chain of directories when performing commands which change the current
directory.
-T If set, any traps on DEBUG and RETURN are inherited by shell functions, command substitutions,
and commands executed in a subshell environment. The DEBUG and RETURN traps are normally not
inherited in such cases.
-- If no arguments follow this option, then the positional parameters are unset. Otherwise, the
positional parameters are set to the args, even if some of them begin with a -.
- Signal the end of options, cause all remaining args to be assigned to the positional parameters.
The -x and -v options are turned off. If there are no args, the positional parameters remain
unchanged.
The options are off by default unless otherwise noted. Using + rather than - causes these options to be
turned off. The options can also be specified as arguments to an invocation of the shell. The current
set of options may be found in $-. The return status is always true unless an invalid option is encoun-
tered.
turned off. The options can also be specified as arguments to an invocation of the shell. The current
set of options may be found in $-. The return status is always true unless an invalid option is encoun-
tered.