I'm trying to do the following:
CPU_COUNT=$(cat /proc/stat | grep -E "^cpu[[:digit:]]+ " | wc -l)
let CPU_COUNT=CPU_COUNT-1
for core in {0..$CPU_COUNT}; do
echo $core
done
On a system with 4 cores, I would expect the bash script to loop 4 times, incrementing core from 0 to 3.
The output I receive is however:
{0..3}
What I'm doing is clearly wrong, but how do I make it work as intended?
You are looking for seq
.
for core in $(seq 0 $CPU_COUNT); do
Edit: You can use getconf(1) to get the number of CPU available:
CPU_COUNT=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With