AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 387935
Accepted
kylex
kylex
Asked: 2012-05-11 06:27:55 +0800 CST2012-05-11 06:27:55 +0800 CST 2012-05-11 06:27:55 +0800 CST

shell 命令上的单破折号和双破折号标志有什么区别?

  • 772

我是 shell 的新手,这些命令的使用似乎很随意。有没有一个标志有一个破折号而另一个可能有两个破折号的原因?

shell
  • 5 5 个回答
  • 65521 Views

5 个回答

  • Voted
  1. Best Answer
    psusi
    2012-05-11T06:32:57+08:002012-05-11T06:32:57+08:00

    单个连字符后可以跟多个单字符标志。双连字符作为单个多字符选项的前缀。

    考虑这个例子:

    tar -czf
    

    在此示例中,-czf指定三个单字符标志:c、z和f。

    现在考虑另一个例子:

    tar --exclude
    

    在这种情况下,--exclude指定一个名为 的单个多字符选项exclude。双连字符消除了命令行参数的歧义,确保将其tar解释为, , , , , , 和exclude的组合。exclude

    • 151
  2. mulaz
    2012-05-11T06:35:55+08:002012-05-11T06:35:55+08:00

    这完全取决于程序。通常“-”用于“短”选项(一个字母,-h),“--”用于“长”(er)选项(--help)。

    短选项通常可以组合(所以“-h -a”与“-ha”相同)

    在类 Unix 系统中,ASCII 连字符减号通常用于指定选项。该字符通常后跟一个或多个字母。一个参数本身是一个连字符减号,没有任何字母,通常指定程序应该处理来自标准输入的数据或将数据发送到标准输出。一些程序使用两个连字符减号 (--) 来指定使用更具描述性的选项名称的“长选项”。这是 GNU 软件的一个共同特征。

    资源

    • 27
  3. ssice
    2012-05-11T10:56:20+08:002012-05-11T10:56:20+08:00

    这真的是一个约定。但是,它可以帮助解析器更有效地了解传递给程序的选项。此外,还有一些简洁的实用程序可以帮助解析这些命令,例如帮助解析程序参数的getopt(3)非标准实用getopt_long(3)程序。

    这很好,因为我们可以组合多个短选项,就像其他答案所说的那样,比如tar -xzf myfile.tar.gz.

    如果 有一个“lisa”参数ls,那么键入的含义可能与ls -lisa不同ls --lisa。前者是l, i, s, 和a参数,而不是单词。

    事实上,您可以写ls -l -i -s -a, 与 的含义完全相同ls -lisa,但这取决于程序。

    也有不遵守此约定的程序。最值得注意的是我的视力,dd和gcc。

    • 12
  4. Premraj
    2017-08-29T19:48:32+08:002017-08-29T19:48:32+08:00

    带有单破折号的短选项与带有双破折号的长选项

    短选项可以组合成一个参数;

    for example: ls -lrt #instead of ls -l -r -t
    

    如果我们允许带有单个破折号的长选项,则会导致歧义。为了解决这个问题,我们对长选项使用双破折号。

    • 7
  5. smalers
    2021-07-31T02:11:56+08:002021-07-31T02:11:56+08:00

    另一种情况是当脚本调用另一个程序时,可能需要将第一个脚本的选项与传递给第二个程序的选项分开。例如,您可以编写一个 bash 函数来解析命令行,类似于下面的函数。然后使用类似于以下的命令行。在这种情况下,双破折号将每个程序的选项分开,并允许内置解析器的错误处理按预期工作。当然,可能会有特殊情况需要处理。

     firstscript --firstScriptOption -- --optForSecondProgram
    
    # Parse the command line and set variables to control logic.
    parseCommandLine() {
      local additionalOpts exitCode optstring optstringLong
      # Indicate specification for single character options:
      # - 1 colon after an option indicates that an argument is required
      # - 2 colons after an option indicates that an argument is optional, must use -o=argument syntax
      optstring="h"
      # Indicate specification for long options:
      # - 1 colon after an option indicates that an argument is required
      # - 2 colons after an option indicates that an argument is optional, must use --option=argument syntax
      optstringLong="help"
      # Parse the options using getopt command:
      # - the -- is a separator between getopt options and parameters to be parsed
      # - output is simple space-delimited command line
      # - error message will be printed if unrecognized option or missing parameter but status will be 0
      # - if an optional argument is not specified, output will include empty string ''
      GETOPT_OUT=$(getopt --options ${optstring} --longoptions ${optstringLong} -- "$@")
      exitCode=$?
      if [ ${exitCode} -ne 0 ]; then
        # Call a separate function to print usage.
        printUsage
        exit 1
      fi
      # The following constructs the command by concatenating arguments:
      # - the $1, $2, etc. variables are set as if typed on the command line
      # - special cases like --option=value and missing optional arguments are generically handled
      #   as separate parameters so shift can be done below
      eval set -- "${GETOPT_OUT}"
      # Loop over the options:
      # - the error handling will catch cases were argument is missing
      # - shift over the known number of options/arguments
      while true; do
        #echo "Command line option is ${opt}"
        case "$1" in
          -h|--help) # Print usage of this script
            printUsage
            shift
            ;;
          --) # No more arguments - following arguments are passed to the second program.
            shift
            break
            ;;
          *) # Unknown option - will never get here because getopt catches up front
            # and remaining options are after --
            echo "Invalid option $1." >&2
            printUsage
            exit 1
            ;;
        esac
      done
      # Get a list of all command line options that do not correspond to dash options:
      # - These are "non-option" arguments after --
      # - For example, one or more file or folder names that need to be processed.
      # - If multiple values, they will be delimited by spaces.
      # - Command line * will result in expansion to matching files and folders.
      shift $((OPTIND-1))
      additionalOpts=$*
      echo "Additional options: ${additionalOpts}"
      # The additional options would be passed to the second program.
    }
    
    • 0

相关问题

  • 如何在 Mac OS X 上的 shell 脚本中检索绝对文件名?

  • 强制 rsync 进入非交互模式

  • ssh 中类似 Bash 的命令替换或如何从 ssh *client* 请求内容?

  • unlink 和 rm 有什么区别?

  • 如何将 VAR 从子 shell 导出到父 shell?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve