シェル構文 "[ ${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

1行に1つの新しい構文要素しかありません。これは良いことです。

関連部分を含む各行にコメントを付けます。man bash役に立つかもしれませんし、他の答えと組み合わせることもできます。

パラメータから$10から始まる1つの文字を削除し、それが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.

(注:例ではasとは--foo=bar=bazサポートしていませんが、asとはサポートしています。) --foobar=baz--foobaz

出典:一部パラメータ拡張存在するman bash
man bash | less '+/Parameter Expansion'

(またはより短くman bash | less '+/##'

おすすめ記事