[CentOS] [OT] Bash help

Bowie Bailey Bowie_Bailey at BUC.com
Wed Oct 25 19:45:34 UTC 2017


On 10/25/2017 3:34 PM, Warren Young wrote:
> On Oct 25, 2017, at 11:28 AM, Mark Haney <mark.haney at neonova.net> wrote:
>> An associative array was the first thing I thought of, then realized BASH doesn't do those.
> But it does: in Bash 4, only.
>
> If you mean you must still use Bash 3 in places, then yeah, you’ve got a problem… one probably best solved by switching to some other language once the program grows beyond Bash 3’s natural scope.
>
> I was trying to think of which languages I know well which require even more difficult solutions than the Bash 4 one.  It’s a pretty short list: assembly, C, and MS-DOS batch files.  By “C” I’m including anything of its era and outlook: Pascal, Fortran…
>
> I think even Tcl beats Bash 4 on this score, and it’s notoriously minimal in its feature set.
>
> Here’s a brain-bender: You could probably do it with sqlite3 with fewer lines of code than my Bash 4 offering. :)
>
>> I honestly expected there to be a fairly straight forward way to do it in BASH, but I was sadly mistaken.
> Oh, I don’t know, there must be a way to do it without associative arrays, but you’d only get points for the masochism value in doing without.

Array N holds the names and array T holds the totals.  For each line in 
the file, you iterate through N to find the name and then add the number 
to the same index in T (or create a new entry in both arrays if you 
don't find it).  Then you just have to iterate through both arrays and 
print off the names from N and the totals from T.  It's a pain, but it's 
doable.

Sorry, I'm too lazy to write code for this...  :)

-- 
Bowie



More information about the CentOS mailing list