zsh-users
 help / color / mirror / code / Atom feed
* Can I test if a parameter expansion has worked
@ 2022-09-14 10:03 zzapper
  2022-09-14 18:36 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: zzapper @ 2022-09-14 10:03 UTC (permalink / raw)
  To: Zsh-Users List

Hi

Q1) Can I test if a parameter expansion has 'fired'?

other than by testing if input == output

zzapper






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-14 10:03 Can I test if a parameter expansion has worked zzapper
@ 2022-09-14 18:36 ` Bart Schaefer
  2022-09-15  0:08 ` Alex Satrapa
  2022-09-15  7:34 ` zzapper
  2 siblings, 0 replies; 12+ messages in thread
From: Bart Schaefer @ 2022-09-14 18:36 UTC (permalink / raw)
  To: zzapper; +Cc: Zsh-Users List

On Wed, Sep 14, 2022 at 9:52 AM zzapper <zsh@rayninfo.co.uk> wrote:
>
> Q1) Can I test if a parameter expansion has 'fired'?

I'm not sure what that means.  Does 'fired' mean expanded to a
non-empty string, as in, the parameter has a value?  And, test when,
relative to the placement of the parameter reference?  And, what do
you want to do based on the result of the test?

The most obvious answer would be that ${param:?} will cause a fatal
error if the parameter is either unset or empty, but that doesn't
provide any avenue for recovery unless you use an "always" block
around it.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-14 10:03 Can I test if a parameter expansion has worked zzapper
  2022-09-14 18:36 ` Bart Schaefer
@ 2022-09-15  0:08 ` Alex Satrapa
  2022-09-15  7:34 ` zzapper
  2 siblings, 0 replies; 12+ messages in thread
From: Alex Satrapa @ 2022-09-15  0:08 UTC (permalink / raw)
  To: zzapper; +Cc: Zsh-Users List

On 14 Sep 2022, at 20:03, zzapper <zsh@rayninfo.co.uk> wrote:
> 
> Q1) Can I test if a parameter expansion has ‘fired’?

Are you able to describe the scenario where parameter expansion is not behaving as you expected?

What is ZSH doing that is different to what you expected?



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-14 10:03 Can I test if a parameter expansion has worked zzapper
  2022-09-14 18:36 ` Bart Schaefer
  2022-09-15  0:08 ` Alex Satrapa
@ 2022-09-15  7:34 ` zzapper
  2022-09-15  8:24   ` Peter Stephenson
                     ` (4 more replies)
  2 siblings, 5 replies; 12+ messages in thread
From: zzapper @ 2022-09-15  7:34 UTC (permalink / raw)
  To: zsh-users

On 14/09/2022 11:03, zzapper wrote:
> Hi
>
> Q1) Can I test if a parameter expansion has 'fired'?
>
> other than by testing if input == output
>
Bit embarrassed I didn't provide an example :(

so here it is

# adding a prefix '_s' to an image name

f=stuff.jpg;

print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}

stuff_s.jpg  # good this is what i want

# now pass 'bad' input

f=stuff.txt;

print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
stuff.txt  #  expansion has obviously left the input unchanged but can I 
test for that???

I suppose I should be testing the filename before passing it to the 
expansion

if [[ $f =~ '.jpg|gif|png|jpeg' ]] ;then echo 'ok' ;else echo 'nok' ;fi





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  7:34 ` zzapper
@ 2022-09-15  8:24   ` Peter Stephenson
  2022-09-15  8:38     ` zzapper
  2022-09-15 16:48   ` Bart Schaefer
                     ` (3 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Peter Stephenson @ 2022-09-15  8:24 UTC (permalink / raw)
  To: zsh-users

> On 15/09/2022 08:34 zzapper <zsh@rayninfo.co.uk> wrote:
> # adding a prefix '_s' to an image name
> 
> f=stuff.jpg;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> 
> stuff_s.jpg  # good this is what i want
> 
> # now pass 'bad' input
> 
> f=stuff.txt;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> stuff.txt  #  expansion has obviously left the input unchanged but can I 
> test for that???

No, there's no way of telling if some expansion actually changed an
expression without actually testing the final result.  The differences
between the cases (changed / not changed) are buried deep within the
shell and don't leave an obvious trace above --- and in any case given
how complicated zsh expressions can get finding a useful binary way of
testing what the whole expression did would be incredibly hairy.

Just test the resulting expression.

pws


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  8:24   ` Peter Stephenson
@ 2022-09-15  8:38     ` zzapper
  0 siblings, 0 replies; 12+ messages in thread
From: zzapper @ 2022-09-15  8:38 UTC (permalink / raw)
  To: zsh-users


On 15/09/2022 09:24, Peter Stephenson wrote:
>> On 15/09/2022 08:34 zzapper <zsh@rayninfo.co.uk> wrote:
>> # adding a prefix '_s' to an image name
>>
>> f=stuff.jpg;
>>
>> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
>>
>> stuff_s.jpg  # good this is what i want
>>
>> # now pass 'bad' input
>>
>> f=stuff.txt;
>>
>> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
>> stuff.txt  #  expansion has obviously left the input unchanged but can I
>> test for that???
> No, there's no way of telling if some expansion actually changed an
> expression without actually testing the final result.  The differences
> between the cases (changed / not changed) are buried deep within the
> shell and don't leave an obvious trace above --- and in any case given
> how complicated zsh expressions can get finding a useful binary way of
> testing what the whole expression did would be incredibly hairy.
>
> Just test the resulting expression.
>
> pws
>

I now realise that it would horribly subjective to decide whether or not 
an expansion had succeeded in any case :)

f=pws

echo ${f/%(#m)pws/thanks $MATCH}


zzapper



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  7:34 ` zzapper
  2022-09-15  8:24   ` Peter Stephenson
@ 2022-09-15 16:48   ` Bart Schaefer
  2022-09-20  7:14     ` david rayner
  2022-09-16  0:41   ` Alex Satrapa
                     ` (2 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2022-09-15 16:48 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users

On Thu, Sep 15, 2022 at 12:34 AM zzapper <zsh@rayninfo.co.uk> wrote:
>
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> stuff.txt  #  expansion has obviously left the input unchanged but can I
> test for that???

In the general case, no, as PWS has already addressed.

In this specific case, tho, if you "unset MATCH" before doing the
substitution, you can test $+MATCH after.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  7:34 ` zzapper
  2022-09-15  8:24   ` Peter Stephenson
  2022-09-15 16:48   ` Bart Schaefer
@ 2022-09-16  0:41   ` Alex Satrapa
  2022-09-20 12:49   ` Stephane Chazelas
  2022-09-20 13:25   ` Stephane Chazelas
  4 siblings, 0 replies; 12+ messages in thread
From: Alex Satrapa @ 2022-09-16  0:41 UTC (permalink / raw)
  To: zzapper; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 755 bytes --]

On 15 Sep 2022, at 17:34, zzapper <zsh@rayninfo.co.uk> wrote:
> 
> I suppose I should be testing the filename before passing it to the expansion
> 
> if [[ $f =~ '.jpg|gif|png|jpeg' ]] ;then echo 'ok' ;else echo 'nok' ;fi

That’s how I’d approach it.

Also make sure to check that the new file name doesn’t exist. A common workaround to trying to name a file the same as an existing file is to add a numerical suffix. Then you need to check if there’s already a file/some files with numerical suffixes and figure out what the next suffix needs to be. One example I saw was to start with N=1, then keep incrementing that counter until file_s_${N} doesn’t exist.

Anyway, if you need help overthinking stuff I’ll be around.

Alex


[-- Attachment #2: Type: text/html, Size: 2758 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15 16:48   ` Bart Schaefer
@ 2022-09-20  7:14     ` david rayner
  0 siblings, 0 replies; 12+ messages in thread
From: david rayner @ 2022-09-20  7:14 UTC (permalink / raw)
  To: zsh-users


On 15/09/2022 17:48, Bart Schaefer wrote:
> On Thu, Sep 15, 2022 at 12:34 AM zzapper <zsh@rayninfo.co.uk> wrote:
>> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
>> stuff.txt  #  expansion has obviously left the input unchanged but can I
>> test for that???
> In the general case, no, as PWS has already addressed.
>
> In this specific case, tho, if you "unset MATCH" before doing the
> substitution, you can test $+MATCH after.
>

Bart

Generically useful thanks (sorry late reply)

f=fred.jpg

print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}

echo $+MATCH

1

zzapper



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  7:34 ` zzapper
                     ` (2 preceding siblings ...)
  2022-09-16  0:41   ` Alex Satrapa
@ 2022-09-20 12:49   ` Stephane Chazelas
  2022-09-20 13:25   ` Stephane Chazelas
  4 siblings, 0 replies; 12+ messages in thread
From: Stephane Chazelas @ 2022-09-20 12:49 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users

2022-09-15 08:34:34 +0100, zzapper:
[...]
> f=stuff.jpg;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> 
> stuff_s.jpg  # good this is what i want
> 
> # now pass 'bad' input
> 
> f=stuff.txt;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> stuff.txt  #  expansion has obviously left the input unchanged but can I
> test for that???
[...]

You could always do:

unset flag
print -r -- ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH${flag=}}
if (( $+flag )) echo it did substitute

Note that "print $var" without the -- is a command injection
vulnerability and without -r generally not what you want.

-- 
Stephane


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-15  7:34 ` zzapper
                     ` (3 preceding siblings ...)
  2022-09-20 12:49   ` Stephane Chazelas
@ 2022-09-20 13:25   ` Stephane Chazelas
  2022-09-20 13:31     ` Stephane Chazelas
  4 siblings, 1 reply; 12+ messages in thread
From: Stephane Chazelas @ 2022-09-20 13:25 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users

2022-09-15 08:34:34 +0100, zzapper:
[...]
> if [[ $f =~ '.jpg|gif|png|jpeg' ]] ;then echo 'ok' ;else echo 'nok' ;fi
[...]

ITYM

if [[ $f =~ '\.(jpe?g|gif|png)$' ]]

Or (better as regexps can often not be used with non-text while
zsh globs can):

if [[ $f = *.(jp(e|)g|gif|png) ]]

-- 
Stephane


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Can I test if a parameter expansion has worked
  2022-09-20 13:25   ` Stephane Chazelas
@ 2022-09-20 13:31     ` Stephane Chazelas
  0 siblings, 0 replies; 12+ messages in thread
From: Stephane Chazelas @ 2022-09-20 13:31 UTC (permalink / raw)
  To: zzapper, zsh-users

2022-09-20 14:25:51 +0100, Stephane Chazelas:
[...]
> ITYM
> 
> if [[ $f =~ '\.(jpe?g|gif|png)$' ]]
[...]

Which, btw, if the rematchpcre option is on must be changed to:

if [[ $f =~ '\.(jpe?g|gif|png)\z' ]]

As otherwise, that would match on f=$'foo.gif\n' for instance.

With rematchpcre, see also:

$ f=$'St\xe9phane.jpg'
$ [[ $f =~ '\.(jpe?g|gif|png)\z' ]]
zsh: pcre_exec() error [-10]

Not fixed with:

$ LC_ALL=C [ "$f" '=~' '\.(jpe?g|gif|png)\z' ]
zsh: pcre_exec() error [-10]

(bug?)

-- 
Stephane


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-09-20 13:38 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-14 10:03 Can I test if a parameter expansion has worked zzapper
2022-09-14 18:36 ` Bart Schaefer
2022-09-15  0:08 ` Alex Satrapa
2022-09-15  7:34 ` zzapper
2022-09-15  8:24   ` Peter Stephenson
2022-09-15  8:38     ` zzapper
2022-09-15 16:48   ` Bart Schaefer
2022-09-20  7:14     ` david rayner
2022-09-16  0:41   ` Alex Satrapa
2022-09-20 12:49   ` Stephane Chazelas
2022-09-20 13:25   ` Stephane Chazelas
2022-09-20 13:31     ` Stephane Chazelas

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).