Current-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: use of "build.sh -B BUILDID".... with patch and more questions
RVP <rvp%SDF.ORG@localhost> writes:
> On Fri, 5 Dec 2025, Jarle Greipsland wrote:
>> val=${val#"${val%%[![:blank:]]*}"}
>> val=${val%"${val##*[![:blank:]]}"}
>
> Yes! More people should stash this in their shell tips-'n-tricks collection.
> I think you can get rid of the `"'.
For this particular operation - yes, you can. However, if you
apply the same design pattern to a problem where the result of
the inner substring processing operation contains shell pattern
meta-characters, you shall likely want the quotes to be there.
For example:
title="***Careful with that axe, Eugene!***"
# Let's chop off the leading and trailing asterisk sequences
# Here the quotation marks are neccessary!
title=${title#"${title%%[!*]*}"}
title=${title%"${title##*[!*]}"}
As for the original problem of removing blanks (or whitespace), I
generally prefer not to sprinkle my code with too much
cleverness. Instead, I stash it away in one or more functions,
and then apply them as needed. Clarity is worth a few extra
function calls. The functions below (beware! not rigorously
tested) enable me to do both:
trim_ws var " lost in space "
and
var=" lost in space "
trim_ws var
In both cases var gets set to "lost in space".
# space trimming functions
# usage: trim_XXX varname [string]
trim_leading_ws() {
if [ $# -eq 1 ]; then
eval 'set -- "$1" ${'"$1"'+"${'"$1"'}"}'
fi
if [ "${2+set}" ]; then
eval "$1=\${2#\"\${2%%[![:space:]]*}\"}"
fi
}
trim_trailing_ws() {
if [ $# -eq 1 ]; then
eval 'set -- "$1" ${'"$1"'+"${'"$1"'}"}'
fi
if [ "${2+set}" ]; then
eval "$1=\${2%\"\${2##*[![:space:]]}\"}"
fi
}
trim_ws() {
trim_leading_ws "$@"
trim_trailing_ws "$1"
}
-jarle
--
"Reading goes faster if you don't sweat comprehension."
- Hobbes
Home |
Main Index |
Thread Index |
Old Index