Find the answer to your Linux question:
Results 1 to 6 of 6
Hi pro's, I'm busy getting started with some scripting and was wondering if someone could explain me in details how I should interpertate the following code: The name of the ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Location
    Brasil
    Posts
    4

    Thumbs up Please explain details - ${0##*/}



    Hi pro's,

    I'm busy getting started with some scripting and was wondering if someone could explain me in details how I should interpertate the following code:

    The name of the script is test.ksh

    echo "This is test 1"
    echo "The name of the executed script is ${0##*/}"
    typeset -r script=${0##*/}
    typeset -r basicname=${script%.*}
    echo "This is test 2"
    echo "The name of the executed script is ${basicname}"

    output:
    This is test 1
    The name of the executed script is test.ksh
    This is test 2
    The name of the executed script is test

    I understand that ${0##*/} extracts the name of the executed script and that ${script%.*} extracts the name before the extension from the variabele script.

    My question is why and how.
    Could somone please explain this to me, or (better) give me some keywords to search for at google or this forum that explains me the options that were used with this code....

    I've looked for typeset operators, but couldn't find any usefull informatie.
    Thx in advance.
    Camy

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    I don't have experience with ksh, as far as bash is concerned, you may have a look at Bash Reference Manual
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Location
    Brasil
    Posts
    4
    Thx 4 your reply.
    This isn't necessarily for ksh...I think that is is some unix thing.

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    In the ksh manpage.

    For ${0##*/}:

    ${parameter#pattern}
    ${parameter##pattern}
    If the shell pattern matches the beginning of the value of parameter,
    then the value of this expansion is the value of the parameter with the
    matched portion deleted; otherwise the value of this parameter is sub-
    stituted. In the first form the smallest matching pattern is deleted
    and in the second form the largest matching pattern is deleted. When
    parameter is @, *, or an array variable with subscript @ or *, the sub-
    string operation is applied to each element in turn.
    In this case the '*/' is the pattern and means "any characters followed by a slash" which is then deleted.

    For ${script%.*}:

    ${parameter%pattern}
    ${parameter%%pattern}
    If the shell pattern matches the end of the value of parameter, then
    the value of this expansion is the value of the parameter with the
    matched part deleted; otherwise substitute the value of parameter. In
    the first form the smallest matching pattern is deleted and in the sec-
    ond form the largest matching pattern is deleted. When parameter is @,
    *, or an array variable with subscript @ or *, the substring operation
    is applied to each element in turn.
    In this case the ".*" is the pattern and means "a period followed by any characters" which us then deleted.

  5. #5
    Just Joined!
    Join Date
    Feb 2010
    Location
    Brasil
    Posts
    4

    Red face

    Lomcevak,

    Thanks for your detailed explain. I must say that I still find it much complicated to adjust your comments to the result I'm getting by excuting the code inside a script (eg. test.ksh).

    ${0##*/} , extracts the name of the script that is executed --> in this case it outputs test.ksh

    ${script%.*}, extracts the value of the variabele script, but without the extension --> in this case it outputs test.

    With this in mind and the explaination, I still find it verry hard to see the logic...
    I'll take another look at it.

  6. #6
    Just Joined!
    Join Date
    Feb 2010
    Location
    Brasil
    Posts
    4

    ${0##*/} AND ${var1%.*}

    Hi,

    I'm new with Linux (working on an AIX machine) and busy getting started with some scripting and was wondering if someone could explain me in details how I should interpertate the following code:

    The name of the script is test.ksh

    echo "This is test 1"
    echo "The name of the executed script is ${0##*/}"
    typeset -r var1=${0##*/}
    typeset -r basicname=${var1%.*}
    echo "This is test 2"
    echo "The name of the executed script is ${basicname}"

    output:
    This is test 1
    The name of the executed script is test.ksh
    This is test 2
    The name of the executed script is test

    I understand that ${0##*/} extracts the name of the executed script (in this case test.ksh) and that ${script%.*} extracts the name before the extension from the variabele script (in this case test).

    My question is why and how.
    Could somone please explain this to me, or (better) give me some keywords to search for at google or this forum that explains me the options that were used with this code....

    I've looked for typeset operators, but couldn't find any usefull informatie.
    Thx in advance.
    Camy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •