Re: [KSH] Assign a matched pattern to a shell variable
From: Kevin Rodgers (ihs_4664_at_yahoo.com)
Date: 08/28/03
- Previous message: BBQ: "How to alias "n1" to "n 1" and "n2" to "n 2" and so on (with style)"
- In reply to: Barry Margolin: "Re: [KSH] Assign a matched pattern to a shell variable"
- Next in thread: Chris F.A. Johnson: "Re: [KSH] Assign a matched pattern to a shell variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: BBQ: "How to alias "n1" to "n 1" and "n2" to "n 2" and so on (with style)"
- In reply to: Barry Margolin: "Re: [KSH] Assign a matched pattern to a shell variable"
- Next in thread: Chris F.A. Johnson: "Re: [KSH] Assign a matched pattern to a shell variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|