Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for i in {1..$VAR}; do echo $i; done

Tags:

bash

for-loop

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?

like image 493
Dog eat cat world Avatar asked Dec 04 '22 06:12

Dog eat cat world


1 Answers

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)
like image 152
Aif Avatar answered Jan 14 '23 12:01

Aif