shell 语法“[ ${1:0:1} = '/' ]”(参数扩展)有什么作用?

Atu*_*tul 7 shell parameter

我不明白这个脚本。

getopt_simple()
{
    echo "getopt_simple()"
    echo "Parameters are '$*'"
    until [ -z "$1" ]
    do
        echo "Processing parameter of: '$1'"
        if [ ${1:0:1} = '/' ]
        then
            tmp=${1:1} # Strip off leading '/' . . .
            parameter=${tmp%%=*} # Extract name.
            value=${tmp##*=} # Extract value.
            echo "Parameter: '$parameter', value: '$value'"
            eval $parameter=$value
         fi
         shift
    done
}
Run Code Online (Sandbox Code Playgroud)

if [ ${1:0:1} = '/' ]在上面编写的代码之后我需要一些帮助,我的问题是:

  1. if 语句中发生了什么?
  2. “:”在这里象征什么?

Vol*_*gel 10

每行只有一个新的语法元素,很好......

我将用来自man bash-的相关部分注释每一行- 可能会有所帮助,或者与另一个答案结合使用:

从参数中$1,从 0 开始切出 1 个字符并检查它是一个/

if [ ${1:0:1} = '/' ]

    ${parameter:offset}
    ${parameter:offset:length}
           Substring Expansion.  Expands to up to length characters of  the
           value  of  parameter starting at the character specified by off?
           set.  If parameter is @, an indexed array subscripted by @ or *,
           or  an  associative  array name, the results differ as described
           below.  If length is omitted, expands to the  substring  of  the
           value of parameter starting at the character specified by offset
           and extending to the end of the value.  length  and  offset  are
           arithmetic expressions (see ARITHMETIC EVALUATION below).

           If  offset  evaluates  to  a number less than zero, the value is
           used as an offset in characters from the end  of  the  value  of
           parameter.   If  length evaluates to a number less than zero, it
           is interpreted as an offset in characters from the  end  of  the
           value  of  parameter rather than a number of characters, and the
           expansion is the characters  between  offset  and  that  result.
           Note  that a negative offset must be separated from the colon by
           at least one space to avoid being confused with  the  :-  expan?
           sion.
Run Code Online (Sandbox Code Playgroud)

将 char 0 排除在外,并从 1 到末尾获取字符$1

tmp=${1:1} # Strip off leading '/' . . .
见上一节,第一种情况。

对于诸如 之类的参数--foo=bar,从右侧尽可能地向左侧切断与 '=*' 匹配的文本(想想处理--foo=bar=baz):

parameter=${tmp%%=*} # Extract name.

   ${parameter%word}
   ${parameter%%word}
           Remove matching suffix pattern.  The word is expanded to produce
           a pattern just as in pathname expansion.  If the pattern matches
           a  trailing portion of the expanded value of parameter, then the
           result of the expansion is the expanded value of parameter  with
           the  shortest  matching  pattern (the ``%'' case) or the longest
           matching pattern (the ``%%'' case) deleted.  If parameter  is  @
           or  *,  the  pattern  removal operation is applied to each posi?
           tional parameter in turn, and the  expansion  is  the  resultant
           list.   If  parameter is an array variable subscripted with @ or
           *, the pattern removal operation is applied to  each  member  of
           the array in turn, and the expansion is the resultant list.
Run Code Online (Sandbox Code Playgroud)

对于诸如 之类的参数--foo=bar,从左侧尽可能多地向右截断与 '*=' 匹配的文本(想想处理--foo=bar=baz):

value=${tmp##*=} # Extract value.

    ${parameter#word}
    ${parameter##word}
           Remove matching prefix pattern.  The word is expanded to produce
           a pattern just as in pathname expansion.  If the pattern matches
           the  beginning of the value of parameter, then the result of the
           expansion is the expanded value of parameter with  the  shortest
           matching  pattern  (the ``#'' case) or the longest matching pat?
           tern (the ``##'' case) deleted.  If parameter is  @  or  *,  the
           pattern  removal operation is applied to each positional parame?
           ter in turn, and the expansion is the resultant list.  If param?
           eter  is  an array variable subscripted with @ or *, the pattern
           removal operation is applied to each  member  of  the  array  in
           turn, and the expansion is the resultant list.
Run Code Online (Sandbox Code Playgroud)

(注意:示例案例--foo=bar=baz不支持为--fooand bar=baz,但支持为--fooand baz

资料来源:部分参数扩展man bash
man bash | less '+/Parameter Expansion'

(或者,更短man bash | less '+/##'