There are several ways to go here, I think. Perhaps I’d go for
avail_cores=$((`lscpu --parse=core | awk 'NF==1' | sort -u | wc -l`))
(we’re trying to calculate the total number of presently-online physical cores - not logical cores, cpus, sockets, or processors)
Would that work in your case?