procmail
[Top] [All Lists]

Re: how to drop last N characters from string?

1997-12-04 14:36:22
Thu 97-12-04 "David W. Tamkin" <dattier(_at_)miso(_dot_)wwa(_dot_)com> 
list.procmail
| When I suggested this code to remove the last two characters from a variable,
| 
| | |   savemetas=$SHELLMETAS
| | |   SHELLMETAS # Get your asterisk out of there!
| | |   VAR=`expr "$VAR" : '\(.*\)..'`
| | |   SHELLMETAS=$savemetas
| 
| Jari Aalto expressed a preference for this:
| 
| |            VAR = ` awk 'BEGIN{ v = ARGV[1];                       \
| |                    print substr(v,1,length(v)-2); exit }'          \
| |                    "$VAR" `
| 
| I disagree.  expr is sure to be a smaller binary than awk for procmail to
| fork, and it needs much less command-line code to do this job.

True. It's just that expr is a quite limited what comes to string handling.
Anything more complex would be job for awk. In this case, yes; expr
is the best.

| Note also that one still has to fiddle with SHELLMETAS to avoid a shell,
| because the awk code contains brackets; thus it doesn't replace all four
| lines of my suggestion but only the third (though it changes the comment
| on line 2).

I'm lost here. How does the procmail actually call external programs?
I have only used fork for own C/Perl binaries, but how does
procmail "sh" or "fork" an awk when the code "has" or "has not" shell metas,
ie. Braces in this case.

Why would we need to avoid "shell metas" so hard? Eagerly learning new...
VAR is expanded by Procmail before program sees it.

jari