* CONDITIONAL EXPRESSIONS, string == pattern
@ 2021-07-25 2:14 scowles
2021-07-25 3:30 ` Eric Cook
0 siblings, 1 reply; 3+ messages in thread
From: scowles @ 2021-07-25 2:14 UTC (permalink / raw)
To: Zsh Users
I would like to execute test on patterns that are dynamically generated and that
change between invocations depending on other data. When I generate a scalar
parameter containing the pattern, construct the test statement, then invoke it,
the test fails. However, if I force an eval on the test statement, the test
succeeds. I do not understand zsh parsing in this case. Could someone please
let me know the mechanics I am missing?
thanks very much.
zsh version: 5.8, patch 460
platform: x64, ubu 21.04, current patches
# test code begin:
unset vs vp r
local -a r
vs=' str1 a2'
vp=
vp+=[[:blank:]]##
vp+=str1
vp+=[[:blank:]]##
vp+=[[:alnum:]]
vp+=[[:alnum:]]
r=( ${(f)"$( eval echo 'test ${vs} == ${vp} && echo hi || echo lo' )"} )
echo ${r[-1]}
test ${vs} == ${vp} && echo hi || echo lo
# test code end.
first test output: hi
second test output: lo
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: CONDITIONAL EXPRESSIONS, string == pattern
2021-07-25 2:14 CONDITIONAL EXPRESSIONS, string == pattern scowles
@ 2021-07-25 3:30 ` Eric Cook
2021-07-25 3:48 ` Lawrence Velázquez
0 siblings, 1 reply; 3+ messages in thread
From: Eric Cook @ 2021-07-25 3:30 UTC (permalink / raw)
To: zsh-users
On 7/24/21 10:14 PM, scowles@ckhb.org wrote:
> I would like to execute test on patterns that are dynamically generated and that change between invocations depending on other data. When I generate a scalar parameter containing the pattern, construct the test statement, then invoke it, the test fails.
> However, if I force an eval on the test statement, the test succeeds. I do not understand zsh parsing in this case. Could someone please let me know the mechanics I am missing?
>
> thanks very much.
>
> zsh version: 5.8, patch 460
> platform: x64, ubu 21.04, current patches
>
> # test code begin:
>
> unset vs vp r
> local -a r
>
> vs=' str1 a2'
> vp=
> vp+=[[:blank:]]##
> vp+=str1
> vp+=[[:blank:]]##
> vp+=[[:alnum:]]
> vp+=[[:alnum:]]
>
> r=( ${(f)"$( eval echo 'test ${vs} == ${vp} && echo hi || echo lo' )"} )
> echo ${r[-1]}
>
> test ${vs} == ${vp} && echo hi || echo lo
>
> # test code end.
>
> first test output: hi
> second test output: lo
>
Your code assumes EQUALS is disabled which allowed test == to not error with a command not found error.
so in:
eval echo 'test ${vs} == ${vp} && echo hi || echo lo'
zsh does the normal expansions of the line and passes the arguments to eval, since the single quotes prevented
any possible expansions the result is:
echo test ${vs} == ${vp} && echo hi || echo lo
so the above line goes through the steps of expansions and ends up like so:
echo test ' str1 a2' '==' '[[:blank:]]##str1[[:blank:]]##[[:alnum:]][[:alnum:]]' && echo hi || echo lo
so /echo/ prints out the strings given to it, which succeeds, causing echo hi to run.
Nothing related to pattern matching happened since test doesn't perform pattern matching.
CONDITIONAL EXPRESSIONS
A conditional expression is used with the [[ compound command to test attributes of files and to com‐
pare strings. Each expression can be constructed from one or more of the following unary or binary
expressions
[[ does perform pattern matching on its right hand operand but when said pattern is inside a parameter you have
to perform an expansion that allows for it to be treated as a pattern since the results of a parameter expansion
are treated literal unlike other shells.
ultimately, the script boils down to:
setopt extendedglob
unset vs vp r
vs=' str1 a2'
vp=[[:blank:]]##str1[[:blank:]]##[[:alnum:]][[:alnum:]]
if [[ ${vs} == ${~vp} ]]; then
echo hi
else
echo lo
fi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: CONDITIONAL EXPRESSIONS, string == pattern
2021-07-25 3:30 ` Eric Cook
@ 2021-07-25 3:48 ` Lawrence Velázquez
0 siblings, 0 replies; 3+ messages in thread
From: Lawrence Velázquez @ 2021-07-25 3:48 UTC (permalink / raw)
To: zsh-users
> On Jul 24, 2021, at 11:30 PM, Eric Cook <llua@gmx.com> wrote:
>
> so in:
>
> eval echo 'test ${vs} == ${vp} && echo hi || echo lo'
>
> zsh does the normal expansions of the line and passes the arguments to eval, since the single quotes prevented
> any possible expansions the result is:
>
> echo test ${vs} == ${vp} && echo hi || echo lo
>
> so the above line goes through the steps of expansions and ends up like so:
>
> echo test ' str1 a2' '==' '[[:blank:]]##str1[[:blank:]]##[[:alnum:]][[:alnum:]]' && echo hi || echo lo
>
> so /echo/ prints out the strings given to it, which succeeds, causing echo hi to run.
I'll just add that examining the full contents of 'r' would have
provided a big hint that 'test' wasn't being run at all.
% cat /tmp/testing2.zsh
setopt EXTENDED_GLOB NO_EQUALS
vs=' str1 a2'
vp='[[:blank:]]##str1[[:blank:]]##[[:alnum:]][[:alnum:]]'
r=( ${(f)"$( eval echo 'test ${vs} == ${vp} && echo hi || echo lo' )"} )
typeset -p r
% zsh -f /tmp/testing2.zsh
typeset -a r=( 'test str1 a2 == [[:blank:]]##str1[[:blank:]]##[[:alnum:]][[:alnum:]]' hi )
--
vq
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-07-25 3:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-25 2:14 CONDITIONAL EXPRESSIONS, string == pattern scowles
2021-07-25 3:30 ` Eric Cook
2021-07-25 3:48 ` Lawrence Velázquez
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).