Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Can I test if a parameter expansion has worked




On 15/09/2022 09:24, Peter Stephenson wrote:
On 15/09/2022 08:34 zzapper <zsh@xxxxxxxxxxxxxx> 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





Messages sorted by: Reverse Date, Date, Thread, Author