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

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

我不明白这个脚本。

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
}

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

  1. if 语句中发生了什么?
  2. 这里的“:”代表什么?

答案1

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

我将用 - 中的相关部分注释每一行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.

保留 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.

对于像 之类的参数--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.

(注意:示例情况--foo=bar=baz不支持 as --fooand bar=baz,但支持 as --fooand baz

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

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

答案2

这是子串扩展结构${parameter:offset:length}${1:0:1}从包含的字符串的第零个字符(字符串的开头)开始获取字符串长一个字符$1- 这是脚本的第一个参数的第一个字符。

有关更多详细信息,请参阅 shell 手册页中有关参数扩展的部分。

相关内容