【问题标题】:Special characters :$@ and--) and *) in bash's getoptbash 的 getopt 中的特殊字符 :$@ 和--) 和 *)
【发布时间】:2018-06-11 11:47:22
【问题描述】:
vim  mytest.sh
  1 #!/bin/bash
  2 
  3 ARGS=`getopt -o ab: -l "argv3:,help" -- "$@"`
  4 eval set -- "${ARGS}"
  5 
  6 while true;
  7 do
  8     case "$1" in
  9         -a)
 10             echo "i am a"
 11             shift
 12             ;;
 13         -b)
 14             echo "i am b, my value is $2"
 15             shift 2
 16             ;;
 17         --argv3)
 18             echo "i am argv3, my value is $2"
 19             shift 2
 20             ;;
 21         --help)
 22             echo "i am help info"
 23             exit 0
 24             ;;
 25         --)
 26             shift
 27             break
 28             ;;
 29         *)
 30             echo "Internal error!"
 31             exit 1
 32             ;;
 33     esac
 34 done
  1. "$@" 扩展为单独的参数列表。 -- "$@" 在 getopt 中是什么意思?如果我将第 3 行更改为

    ARGS=`getopt -o ab: -l "argv3:,help"`
    

test.sh? 的哪个函数会消失

  1. --) 在第 25 行

     25         --)
     26             shift
     27             break
     28             ;;
    

    编码者想表达什么意思?

  2. *) 在第 29 行

    在什么条件或什么参数下会引发“内部错误!”的输出在终端?

【问题讨论】:

  • " getopt 中-- "$@" 是什么意思?" -- 它没有任何特殊含义。 -- 是一个特殊的参数,它告诉许多程序停止在其命令行中搜索选项并将剩余的参数解释为文件(或其他东西,取决于程序)。你已经知道"$@" 的含义了。

标签: bash getopt


【解决方案1】:
  1. 只匹配包含两个减号的参数--

编辑:正如@a​​xiac 提到的,this parameter usually has a special meaning, it signify the end of command options

  1. 匹配上面的参数。

  2. Wild card,可以看作switch-case中的default,在前面没有匹配到的情况下触发。

【讨论】:

  • 为什么'./mytest.sh -d`不能触发“内部错误!”。
猜你喜欢
  • 1970-01-01
  • 2021-06-26
  • 2017-10-19
  • 1970-01-01
  • 2015-07-10
  • 2013-03-26
  • 2013-07-18
  • 1970-01-01
  • 2014-07-12
相关资源
最近更新 更多