免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3547 | 回复: 6
打印 上一主题 下一主题

$- 显示shell使用的当前选项 是什么意思 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-11 10:11 |只看该作者 |倒序浏览
预定义变量 $- 显示shell使用的当前选项 是什么意思? 我显示的是 hB

论坛徽章:
0
2 [报告]
发表于 2012-05-11 10:17 |只看该作者
我显示的是ism和himBH

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2012-05-11 10:40 |只看该作者
就是set的参数,没什么用。
  1. case $- in
  2. *i*)    # interactive shell
  3. ;;
  4. *)      # non-interactive shell
  5. ;;
  6. # (Courtesy of "UNIX F.A.Q.," 1993)
复制代码

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
4 [报告]
发表于 2012-05-11 10:49 |只看该作者
本帖最后由 rdcwayx 于 2012-05-11 10:55 编辑

很好的一个问题。
  1. $-     The current shell flags, such as -x and -v.
  2.        -      Expands to the current option flags as specified upon invocation, by the set builtin command,  or  those set by the shell itself (such as the -i option).
复制代码
set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]

给些例子。
  1. $ echo $-
  2. himBH
  3. $ set -x
  4. $ echo $-
  5. himxBH
  6. $ set +x
  7. $ echo $-
  8. himBH
  9. $ bash -r # start another shell with -r set
  10. $ echo $-
  11. himrBH
  12. $ exit
  13. $ echo $-
  14. himBH
复制代码
再看一下后面的红色部分,可以帮助理解。
              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  option
              processing  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 envia
                      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 pipeline (which may consist of a single simple command),  a subshell  coma
                      mand enclosed in parentheses, or one of the commands executed as part of a command list enclosed
                      by braces (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 following a while or until keya
                      word, part of the test following the if or elif reserved words, part of any command executed  in
                      a  &&  or aaaa list except the command following the final && or aaaa, any command in a pipeline but
                      the last, or if the command's return value is being inverted with !.  A trap on ERR, if set,  is
                      executed before the shell exits.  This option applies to the shell environment and each subshell
                      environment separately (see COMMAND EXECUTION ENVIRONMENT above), and  may  cause  subshells  to
                      exit before executing all the commands in the subshell.
              -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.
              -o option-name
                      The option-name can be one of the following:
                      allexport
                              Same as -a.
                      braceexpand
                              Same as -B.
                      emacs   Use an emacs-style command line editing interface.  This is enabled by default when  the
                              shell  is  interactive,  unless  the shell is started with the --noediting option.  This
                              also affects the editing interface used for read -e.
                      errexit Same as -e.
                      errtrace
                              Same as -E.
                      functrace
                              Same as -T.
                      hashall Same as -h.
                      histexpand
                              Same as -H.
                      history Enable command history, as described above under HISTORY.  This option is on by  default
                              in interactive shells.
                      ignoreeof
                              The  effect  is  as  if  the shell command ``IGNOREEOF=10'' had been executed (see Shell
                              Variables above).
                      keyword Same as -k.
                      monitor Same as -m.
                      noclobber
                              Same as -C.
                      noexec  Same as -n.
                      noglob  Same as -f.
                      nolog   Currently ignored.
                      notify  Same as -b.
                      nounset Same as -u.
                      onecmd  Same as -t.
                      physical
                              Same as -P.
                      pipefail

                              If set, the return value of a pipeline is the value of the last (rightmost)  command  to
                              exit  with a non-zero status, or zero if all commands in the pipeline exit successfully.
                              This option is disabled by default.
                      posix   Change the behavior of bash where the default operation differs from the POSIX  standard
                              to match the standard (posix mode).
                      privileged
                              Same as -p.
                      verbose Same as -v.
                      vi      Use  a vi-style command line editing interface.  This also affects the editing interface
                              used for read -e.
                      xtrace  Same as -x.
                      If -o is supplied with no option-name, the values of the current options are printed.  If +o  is
                      supplied  with  no option-name, a series of set commands to recreate the current option settings
                      is displayed on the standard output.
              -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, BASHOPTS, CDPATH, and GLOa
                      BIGNORE variables, if they appear in the environment, are 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 and parameters other than the special parameters "@" and "*" as  an  error
                      when  performing parameter expansion.  If expansion is attempted on an unset variable or paramea
                      ter, 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 argua
                      ments 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 interaca
                      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 encouna
              tered.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2012-05-11 11:08 |只看该作者
rdcwayx 发表于 2012-05-11 10:49
很好的一个问题。set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]

给些例子。再看一下后面的红色部分 ...
用的不是bash?
This was originally a ksh construct adopted into Bash, and unfortunately it does not seem to work reliably in Bash scripts. One possible use for it is to have a script self-test whether it is interactive.

论坛徽章:
0
6 [报告]
发表于 2012-05-11 14:01 |只看该作者
  1. 6.3.2 Is this Shell Interactive?

  2. To determine within a startup script whether or not Bash is running interactively, test the value of the ‘-’ special parameter. It contains i when the shell is interactive. For example:

  3.      case "$-" in
  4.      *i*)        echo This shell is interactive ;;
  5.      *)        echo This shell is not interactive ;;
  6.      esac

  7. Alternatively, startup scripts may examine the variable PS1; it is unset in non-interactive shells, and set in interactive shells. Thus:

  8.      if [ -z "$PS1" ]; then
  9.              echo This shell is not interactive
  10.      else
  11.              echo This shell is interactive
  12.      fi
复制代码
回复 1# coolung


   

论坛徽章:
1
双子座
日期:2013-10-17 00:46:45
7 [报告]
发表于 2012-05-12 20:39 |只看该作者
回复 4# rdcwayx


    问下这个在哪找到的:
    $-     The current shell flags, such as -x and -v.
           -      Expands to the current option flags as specified upon invocation, by the set builtin command,  or  those set by the shell itself (such as the -i option).

man bash/info bash
没找到...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP