[cups.development] [RFE] STR #2193: A bash library for parsing template files

Kurt Pfeifle kpfeifle at danka.de
Mon Jan 22 13:31:20 PST 2007


DO NOT REPLY TO THIS MESSAGE.  INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.

[STR New]

Very nice!  :-)

I admit, I haven't looked very closely at it yet, but here is a tipp you
may want to consider: 

One of the things I myself learned only very recently was to use the Bash
way of manipulationg strings and substrings contained in variables. (I
used to resort to a lot of sed and awk as well -- and still like it for
better readability, because i'm not yet very fluent with the Bash way --
but sometimes it is not good to have 3 or more external processes started
for every little script, when Bash could do it all itself. And since you
are not caring much for portability, and doing an explicit Bash
thingie...) 

See the man page, look for "parameter expansion". It may help you getting
rid of your "sed" calls...

Examples:

parameter="bash manpage"

${#parameters} - return length of the parameter value.
EX.: ${#parameter} = 12

${parameter#word} - cut shortest match from start of parameter.
EX.: ${parameter#*n} = uensis

${parameter##word} - cut longest match from start of parameter.
EX.: ${parameter#*n} = page

${parameter%word} - cut shortest match from end of parameter.
EX.: ${parameter%n*} = sh manpage

${parameter%%word} - cut longest match from end of parameter.
EX.: ${parameter%%n*} = bash ma

${parameter:offset} - return parameter starting at 'offset'.
EX.: ${parameter:5} = manpage

${parameter:offset:length} - return 'length' characters of parameter
starting at 'offset'.
EX.: ${parameter:4:8} = manpage

${parameter/pattern/string} - replace single match.
EX.: ${parameter/manpage/infosheet} = bash infosheet

${parameter//pattern/string} - replace all matches.
EX.: ${parameter//a/A} = bAsh mAnpAge

Link: http://www.cups.org/str.php?L2193
Version:  -feature





More information about the cups mailing list