7

I do not understand this script.

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
}

I need some help after if [ ${1:0:1} = '/' ] in the code written above and my questions are:

  1. What is happening in the if statement?
  2. What does ":" symbolises here?
1
  • It sees --foo=bar=baz as --foo and baz instead of --foo and bar=baz - that may be a bug, or intended. It is caused by the second # in ${tmp##*=}. – Volker Siegel Jul 29 '14 at 19:06
9

There's just about one new syntax element per line, nice...

I'll annotate each line with the relevant section from man bash - may be helpful as is, or in combination with another answer:

From the argument $1, cut out 1 char starting at 0 and check it's a /:

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.

Leave char 0 out and get chars from 1 to the end from $1:

tmp=${1:1} # Strip off leading '/' . . .
See section above, first case.

For arguments like --foo=bar, cut off text matching '=*' from the right, as much as possible to the left (think of handling --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.

For arguments like --foo=bar, cut off text matching '*=' from the left, as much as possible to the right (think of handling --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.

(Note: the example case --foo=bar=baz is not supported as --foo and bar=baz, but as --foo and baz)

Source: section Parameter Expansion in man bash,
man bash | less '+/Parameter Expansion'

(or, shorter man bash | less '+/##')

2
  • My pleasure, it's my most preferred man page section somehow :) (Btw, you may upvote if it was useful) – Volker Siegel Jul 29 '14 at 19:08
  • Done! Have a bful day.. Cheers!! – Atul Jul 29 '14 at 19:28
2

This is the substring expansion construct ${parameter:offset:length}. ${1:0:1} takes string long one character starting at the zeroth character (the beginning of a string) of string contained in $1 - that is the first character of the forst argument of the script.

See section about parameter expansion in your shell manual page for more details.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.