【问题标题】:Understanding the meaning of set - - "$@" "-h" [duplicate]理解set--“$@”“-h”的含义[重复]
【发布时间】:2021-10-28 17:27:54
【问题描述】:

我想了解以下命令的作用

set -- "$@" "-h"

gnu 手册说

--

  If no arguments follow this option, then the positional parameters are unset.    
  Otherwise, the positional parameters are set to the arguments, even if some
  of them begin with a ‘-’.

不过,我无法从这样的描述中获取太多有用的信息。

据我了解,以下将-h 附加到函数参数列表中。

set -- "$@" "-h"

但是,下面的内容实际上是如何将--help替换为-h等的呢?

 printf '%s\n' "$*"
 for arg in "$@"; do
   shift
   printf '%s\n' "--> arg: $arg"
   case "$arg" in
     "--Version")   set -- "$@" "-V"   ;;
     "--usage")     set -- "$@" "-u"   ;;
     "--help")      set -- "$@" "-h"   ;;
     "--verbosity") set -- "$@" "-v"   ;;
     *)             set -- "$@" "$arg" ;;
   esac
 done

【问题讨论】:

标签: bash set positional-parameter


【解决方案1】:

简短回答:它将-h 添加到当前参数列表(也称为位置参数)的末尾。

长答案:"$@" 扩展为当前参数列表(即传递给当前脚本、函数或任何相关上下文的参数列表)。 set -- whatever -- 之后的任何内容替换当前参数列表。因此,它将当前参数列表替换为...当前参数列表后跟-h

例如,假设我们在使用./scriptname foo bar baz 运行的脚本中。那么参数列表是foobarbaz(即$1是“foo”,$2是“bar”,$3是“baz”)。在脚本中,

set -- "$@" "-h"

扩展为:

set -- "foo" "bar" "baz" "-h"

...将参数列表设置为foobarbaz-h

【讨论】:

    猜你喜欢
    • 2017-05-02
    • 2017-07-17
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2014-01-25
    • 2018-05-20
    相关资源
    最近更新 更多