Re: [KSH] Assign a matched pattern to a shell variable

From: Kevin Rodgers (ihs_4664_at_yahoo.com)
Date: 08/28/03

  • Next message: Kevin Rodgers: "Re: A script that modifies other scripts"
    Date: Thu, 28 Aug 2003 11:21:07 -0600
    
    

    Barry Margolin wrote:

    > In article <3F4D3366.30707@yahoo.com>,
    > Kevin Rodgers <ihs_4664@yahoo.com> wrote:
    >
    >>Michael Wang wrote:
    >>
    >>
    >>>In article <ecf2e4b7.0308270909.634c1459@posting.google.com>,
    >>>Janis <janis_papanagnou@hotmail.com> wrote:
    >>>
    >>>
    >>>>mwang@unixlabplus.com (Michael Wang) wrote in message
    >>>>
    >>news:<NSQ2b.207$Hi2.38504@news.uswest.net>...
    >>
    >>>>>In article <ecf2e4b7.0308261006.628bacc3@posting.google.com>,
    >>>>>Janis <janis_papanagnou@hotmail.com> wrote:
    >>>>>
    >>>>>
    >>>>>>I have no shell to verify that, but since the test operator allows for
    >>>>>>patterns the following might work (assuming variable pattern has value
    >>>>>>"example"):
    >>>>>>
    >>>>>>[[ ${string} = *${pattern}* ]] && x=${pattern}
    >>>>>>
    >>>It does not work if pattern is "example_+([0-9])".
    >>>
    >>Why is that?
    >
    > It will assign the pattern to the variable, instead of assigning the
    > portion of the string that the pattern matched.

    Oh, I forgot that was the whole point. The assignment statement is so
    simple I took it at face value.

    >>I notice that it does work if the command is prefixed with eval.
    >
    > I don't see how that could happen. Eval can cause the pattern to be
    > expanded as a wildcard,

    Not expanded as file names, but matched against the string:

    /bin/ksh 1: pattern=example_+([0-9])
    /bin/ksh 2: string=example_99
    /bin/ksh 3: ls *${pattern}*
    *example_+([0-9])*: No such file or directory
    /bin/ksh 4: [[ ${string} = *${pattern}* ]] && print "${string} matches *${pattern}*"
    /bin/ksh 5: eval [[ ${string} = *${pattern}* ]] && print "${string} matches
    *${pattern}*"
    example_99 matches *example_+([0-9])*
    /bin/ksh 6:

    > but I still don't see how it could possibly cause
    > the assignment to extract a substring from ${string}.

    -- 
    Kevin Rodgers
    

  • Next message: Kevin Rodgers: "Re: A script that modifies other scripts"

    Relevant Pages

    • index function problem
      ... I tried a code to find the indices of the ocurences of a pattern in a ... Incompatible ranks 0 and 1 in assignment at ... What is wrong with the string separation? ...
      (comp.lang.fortran)
    • Re: Design: Custom Exception Usage
      ... It'd be silly to call a method that basically has to do the work of parsing the string, but which only returns a flag telling me if actually parsing the string will work. ... Beyond that, however, since sometimes performance _is_ an issue, it's not like TryParse() could have been left out of the API. ... That provides the exact assignment pattern you're asking for. ...
      (microsoft.public.dotnet.languages.csharp)
    • Sublist Pattern Matching
      ... an assignment, but I have been cracking my head for the past two days ... the facts match a particular pattern ... ... Note that this should be like a wildcard pattern match "Pattern*". ...
      (comp.lang.prolog)
    • Planned work at specifiec dates shifted when actuals arrive
      ... For the holidays we use a specific pattern for each assignment. ... assignment1 takes holiday in first week of april. ... Who knows what the reason is? ...
      (microsoft.public.project)
    • Problems with regexps
      ... but I do not want to match variable assignments like this (they are picked ... This pattern works perfectly (as long ... I really want to use the pattern below to match for zero or more spaces ... but the second pattern does match the assignment example ...
      (comp.lang.python)