* 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 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
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 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