set (Bourne shell(sh),C shell(csh)和Korn shell(ksh)的内置函数,用于定义和确定系统环境的值)

瑞兹 发表于 2020-11-28 00:56
浏览次数:
在手机上阅读

在类似Unix的操作系统上,set命令是Bourne shell(sh),C shell(csh)和Korn shell(ksh)的内置函数,用于定义和确定系统环境的值。

查看英文版

目录

1 set 运行系统环境

2 set 语法

3 set 例子

set 运行系统环境

Unix&Linux

set 语法

Bourne shell (sh):

set [--aefhkntuvx[argument]]...

C shell (csh):

set [var[=value]]
set var [n] = word

Korn shell (ksh):

set [+-aefhkmnopstuvx] [+-o option]... [+-A name] [arg...]

选项:Bourne Shell(sh)

sh中,set内置命令具有以下选项:

- 双破折号(“ - ”)的选项表示选项列表的末尾。这个选项主要是有用的,当选项后列出的值将与破折号自己开始。
-a 标记为“导出”修改或创建的变量;以这种方式设置的环境变量将被传递到任何后续命令的环境中。
-e 如果命令以非零退出状态退出,则立即退出。
-f 禁用文件名生成(globlob)。
-h 在定义功能时定位并记住功能命令(执行功能时通常会找到功能命令)。
-k 所有关键字参数都放置在命令环境中,而不仅仅是命令名称前面的参数。
-n 读取命令但不执行。
-t 读取并执行一条命令后退出。
-u 替换时,将未设置的变量视为错误。
-v 读取时打印外壳输入行。
-x 在执行命令时打印命令及其参数。

使用+而不是-导致这些标志被关闭。这些标志也可以在调用Shell本身时使用。当前的标志集可以在变量$-中找到。其余参数是位置参数,并按顺序分配给$ 1$ 2等。如果未给出参数,则将打印所有名称的值。

对于每个名称,unset命令都会删除相应的变量或函数值。不能取消设置特殊变量PATHPS1PS2MAILCHECKIF

使用内置导出命令,将给定名称标记为自动导出到随后执行的命令环境。如果未提供任何参数,则会列出在当前Shell执行期间已标记为导出的变量名称。函数名称不会导出。

选项:C Shell(csh)

csh中,如果未指定任何参数,则set显示所有shell变量的值。多字值显示为带括号的列表。仅使用var参数,set会将一个空(null)值分配给变量var。使用var value形式的参数时,set将value分配给var,其中value是以下之一:

word 一个单词(或带引号的字符串)。
wordlist 括号中用空格分隔的单词列表。

值是在分配之前扩展的命令和文件名。形式set var] =word替换n'th在一个多字个字

unset删除名称匹配(使用文件名替换)模式的变量。所有变量都用“ unset * ”删除;但是,如果您不知道自己在做什么,这是一个非常糟糕的主意。

选项:K Shell(ksh)

ksh中,set命令采用以下选项:

-A 数组分配。取消设置变量名称,并从列表arg顺序分配值。如果使用+ A,则不会先取消设置变量名。
-a 定义的所有后续变量都将自动导出。
-e 如果命令的退出状态为非零,请执行ERR 陷阱(如果已设置),然后退出。读取配置文件时禁用此模式。
-f 禁用文件名生成(globbing)。
-h 第一次遇到时,每个命令都会成为跟踪的别名。
-k 所有变量分配参数都放置在命令的环境中,而不仅仅是命令名称之前的参数。
-m 后台作业将在单独的进程组中运行,并在完成时打印一行。在完成消息中报告后台作业的退出状态。在具有作业控制的系统上,对于交互式shell,此标志会自动打开。
-n 阅读命令并检查它们是否存在语法错误,但不要执行它们。忽略交互式外壳。
-o potion

选项参数可以是下列情况之一:

allexport -a相同。
errexit -e相同。
Bgnice 所有后台作业均以较低的优先级运行,这是默认模式。
emacs 使您进入emacs风格的嵌入式编辑器以输入命令。
emacs 使您进入gmacs风格的嵌入式编辑器以输入命令。
ignoreeof Shell不会在文件末尾退出。必须使用命令出口。
keyword -k相同。
Markdirs 由文件名生成产生的所有目录名称都附加了结尾的“ / ”。
moniror -m相同。
noclobber 防止重定向“ > ”截断现有文件。要求“ > | ”以在打开文件时截断文件。
Noexec -n相同。
noglob -f相同。
nolog 不要将功能定义保存在历史文件中。
nounset -u相同。
privileged -p相同。
verbose -v相同。
Trackall -h相同。
vi 使您进入vi样式内联编辑器的插入模式,直到您单击escape为止。此选项参数使您进入控制模式。一回发线。
viraw vi模式下键入时将处理每个字符。
xtrace -x相同。
如果未提供选项名称,则将打印当前选项设置。
-p 禁用$ HOME / .profile文件的处理,并使用文件/ etc / suid_profile而不是ENV文件。只要有效uid(用户ID)不等于真实uid,或者有效gid(组ID)不等于真实gid,就会启用此模式。禁用此选项会使有效uid和gid设置为实际uid和gid。
-s 按字典顺序对位置参数进行排序。
-t 读取并执行一条命令后退出。
-u 替换时,将未设置的参数视为错误。
-v 读取时打印外壳输入行。
-X 在执行命令时打印命令及其参数。
-- 关闭-x-v标志,并停止检查标志的参数。
-- 请勿更改任何标志。在将$ 1设置-开头的值时很有用。如果没有参数跟随此标志,则未设置位置参数。

使用+而不是-导致这些标志被关闭。这些标志也可以在调用Shell时使用。当前标志集可以在$-中找到。除非指定-A,否则其余参数为位置参数,并按顺序分配给“ $ 1 $ 2 ...”。如果未提供任何参数,则所有变量的名称和值都将显示在标准输出上。

名称列表给定的变量将自动取消分配:它们的值和属性将被删除。只读变量不能被设置。如果设置了-f标志,则名称指的是函数名称。解封ERRNOLINENOMAILCHECKOPTARGOPTINDRANDOMSECONDSTMOUT_删除,即使他们随后被分配其特殊的意义。

当使用unset时,名称列表中给定的变量同样未被分配:它们的值和属性被删除。只读变量不能被设置。如果设置了-f标志,则名称指的是函数名称。解封ERRNOLINENOMAILCHECKOPTARGOPTINDRANDOMSECONDSTMOUT_删除,即使他们随后被分配其特殊的意义。

使用内置导出命令,将给定名称标记为自动导出到后续执行的命令环境。前面带有一或两个“ * ”字符(星号)的ksh命令将按以下方式进行特殊处理:

  • 命令之前的变量分配列表在命令完成时仍然有效。 
  • I / O 重定向在变量分配后进行处理。 
  • 错误会导致包含它们的脚本中止。 
  • 在以**开头的命令之后,以变量赋值格式的单词将以与变量赋值相同的规则扩展。此分配意味着在=号之后执行代字号替换,并且不执行单词拆分和文件名生成。

Bourne shell (sh):

set [--aefhkntuvx[argument]]...

C shell (csh):

set [var[=value]]
set var [n] = word

Korn shell (ksh):

set [+-aefhkmnopstuvx] [+-o option]... [+-A name] [arg...]

Options: Bourne Shell (sh)

In sh, the set built-in command has the following options:

-- An option of a double-dash ("--") signifies the end of an option list. This option is primarily useful when values listed after the options will start with a dash themselves.
-a Mark variables that are modified or created for "export"; environment variables set in this way will be passed on to the environments of any subsequent commands.
-e Exit immediately if a command exits with a non-zero exit status.
-f Disable file name generation (globbing).
-h Locate and remember function commands as functions are defined (function commands are normally located when the function is executed).
-k All keyword arguments are placed in the environment for a command, not just those that precede the command name.
-n Read commands but do not execute them.
-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.

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

For each name, the unset command removes the corresponding variable or function value. The special variables PATHPS1PS2MAILCHECK, and IF cannot be unset.

With the export built-in command, the given names are marked for automatic export to the environment of subsequently executed commands. If no arguments are given, variable names that have been marked for export during the current shell's execution are listed. Function names are not exported.

Options: C Shell (csh)

In csh, If no arguments are specified, set displays the values of all shell variables. Multiword values are displayed as a parenthesized list. With the var argument alone, set assigns an empty (null) value to the variable var. With arguments of the form var = valueset assigns value to var, where value is one of:

word A single word (or quoted string).
(wordlist) A space-separated list of words enclosed in parentheses.

Values are command and file name expanded before being assigned. The form set var[n]=word replaces the n'th word in a multiword value with word.

unset removes variables whose names match (using file name substitution) pattern. All variables are removed by "unset *"; this is a very bad idea if you don't know what you're doing, though.

Options: K Shell (ksh)

In ksh, the set command takes the following options:

-A Array assignment. Unset the variable name and assign values sequentially from the list arg. If +A is used, the variable name is not unset first.
-a All subsequent variables that are defined are automatically exported.
-e If a command has a non-zero exit status, execute the ERR trap (if set), and exit. This mode is disabled while reading profiles.
-f Disables file name generation (globbing).
-h Each command becomes a tracked alias when first encountered.
-k All variable assignment arguments are placed in the environment for a command, not just those that precede the command name.
-m Background jobs will run in a separate process group and a line will print upon completion. The exit status of background jobs is reported in a completion message. On systems with job control, this flag is turned on automatically for interactive shells.
-n Read commands and check them for syntax errors, but do not execute them. Ignored for interactive shells.
-o option

The option argument can be one of the following:

allexport Same as -a.
errexit Same as -e.
bgnice All background jobs are run at a lower priority, which is the default mode.
emacs Puts you in an emacs-style in-line editor for command entry.
emacs Puts you in a gmacs-style in-line editor for command entry.
ignoreeof The shell will not exit on end-of-file. The command exit must be used.
keyword Same as -k.
markdirs All directory names resulting from file name generation have a trailing "/" appended.
monitor Same as -m.
noclobber Prevents redirection ">" from truncating existing files. Require ">|" to truncate a file when turned on.
noexec Same as -n.
noglob Same as -f.
nolog Do not save function definitions in history file.
nounset Same as -u.
privileged Same as -p.
verbose Same as -v.
trackall Same as -h.
vi Puts you in insert mode of a vi-style in-line editor until you hit escape. This option argument puts you in control mode. A return sends the line.
viraw Each character is processed as it is typed in vi mode.
xtrace Same as -x.
If no option name is supplied then the current option settings are printed.
-p Disables processing of the $HOME/.profile file and uses the file /etc/suid_profile instead of the ENV file. This mode is on whenever the effective uid (user ID) is not equal to the real uid, or when the effective gid (group ID) is not equal to the real gid. Turning this off causes the effective uid and gid to be set to the real uid and gid.
-s Sort the positional parameters lexicographically.
-t Exit after reading and executing one command.
-u Treat unset parameters as an error when substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are executed.
- Turns off -x and -v flags and stops examining arguments for flags.
- Do not change any of the flags; useful in setting $1 to a value beginning with -. If no arguments follow this flag then the positional parameters are unset.

Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell. The current set of flags may be found in $-. Unless -A is specified, the remaining arguments are positional parameters and are assigned, in order, to "$1 $2 ...". If no arguments are given, then the names and values of all variables are printed on the standard output.

The variables given by the list of names are automatically unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f flag is set, then the names refer to function names. Unsetting ERRNOLINENOMAILCHECKOPTARGOPTINDRANDOMSECONDSTMOUT, and _ removes their special meaning even if they are subsequently assigned.

When using unset, the variables given by the list of names are similarly unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f, flag is set, then the names refer to function names. Unsetting ERRNOLINENOMAILCHECKOPTARGOPTINDRANDOMSECONDSTMOUT, and _ removes their special meaning even if they are subsequently assigned.

With the export built-in command, the given names are marked for automatic export to the environment of subsequently-executed commands. ksh commands that are preceded by one or two "*" characters (asterisks) are treated specially in the following ways:

  • Variable assignment lists preceding the command remain in effect when the command completes. 
  • I/O redirections are processed after variable assignments. 
  • Errors cause a script that contains them to abort. 
  • Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This assignment means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. 

查看英文版

查看中文版

set 例子

set PATH="/bin:/usr/bin:/usr/sbin:usr/local/bin"

在csh中,此命令设置环境变量PATH,以便外壳程序将按该顺序在/ bin,/ usr / bin,/ usr / sbin和/ usr / local / bin目录中搜索文件。

set PATH="/bin:/usr/bin:/usr/sbin:usr/local/bin"

In csh, this command sets the environment variable PATH, such that the shell will search for files in the /bin/usr/bin/usr/sbin and /usr/local/bin directories, in that order.

查看英文版

查看中文版

其他命令行

sysklogd | s2p | service | sag | setenv | setfacl | sfdisk | sha224sum | sha256sum | sha384sum | sha512sum | shutdown | sleep | stat | startx | strftime | strip | swapoff | shred |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条