tech-toolchain 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
Date: 2025年12月04日 14:45:38 -0800
From: "Greg A. Woods" <woods%planix.ca@localhost>
Message-ID: <m1vRI4w-00Mo5f0@more.local>
I haven't really been following this, I'd never heard of BUILDID
before, but:
| + # remove any leading (and trailing) whitespace
| + val=${val##*[[:blank:]]}
| + val=${val%%*[[:blank:]]}
Those don't do what you want, the first would remove everything up
to, and including, the rightmost blank in the variable's value.
The second will set the variable to '' if the final character of its
value is a blank, otherwise (somewhat slowly, but not really measurably)
do nothing. That couldn't actually change anything here, as all blanks
would be removed by the first of the two commands.
There is no trivial way to achieve what you want, unless there are
restrictions on what is in ${val} initially.
I tend to use something like
while case "${val}" in
([[:blank:]]*) val=${val#?};;
(*) break;;
esac do :; done
to remove leading spaces, removing trailing spaces I will leave as
an exercise for the reader (hint: it can be added to the same loop).
If you expect multiple leading spaces are likely be common, you
could insert something like
([[:blank:]][[:blank:]][[:blank:]][[:blank:]]) val=${val#????};;
(and similar) ahead of the ([[:blank:]]) ... line - to be useful
the number of blanks in the pattern needs to decrease in succeeding
lines - there is no need to handle every possibility however,
this extra line just removes bursts of 4 each time around the loop,
if that is all that is added, 0 to 3 extra iterations will remove the
final (up to) 3.
You can use "false" instead of "break" if you don't trust breaking
out of the condition part of the loop - there are many ways to write
this - any way it is written will be faster (except in absurd cases
perhaps - thousands of leading blanks perhaps) than running an external
command like sed.
To avoid running the ':' command at all - not that there is much reason
to do so, a "continue" can be added after the "var=${var#?}" assignment
(and the similar ones on any additional blank removing lines added).
That is, before the ";;" - whether there is a ';' between the assignment
and the continue, should make no difference, but using it is a bit safer.
kre
ps: for a very ugly, but loop avoiding, way to do the same thing, try:
# The \n can be left in IFS, if you like
# no other chars will work, but nor would they harm anything.
IFS=$' \t' read -r val <<!
${val}
!
but that one is so obscure, and only works for ascii ' ' and '\t',
whatever you put as extra blanks in IFS, that I wouldn't recommend it.
It is also likely slower than the loop, for reasonably small volumes
of leading/trailing blanks (there is a pipe involved, so multiple
system calls, but at least no execs, or forks unless ${val} is quite long).
Home |
Main Index |
Thread Index |
Old Index