Home > Enterprise >  Run all lines of a file at the same time
Run all lines of a file at the same time

Time:10-20

The while loop works fine.

But wondering if there is a way of running all lines from a file at the same time and then run the variables through the same set of commands instead of waiting each interface for 3 seconds ?

while read line; do
before=$(snmpwalk -v2c -c public 1.1.1.1 .1.3.6.1.2.1.2.2.1.16.$line | awk '{print $4}')
sleep 3
after=$(snmpwalk -v2c -c public 1.1.1.1 .1.3.6.1.2.1.2.2.1.16.$line | awk '{print $4}')
res=$(bc <<< "$after-$before")
zxc=$(echo "$res * 8 * 100 / (1000000000 * 3) "  | bc)
per=$(echo "scale=1; 100 * $zxc / (1000) "  | bc)
done < intfile.txt

Thnaks

CodePudding user response:

Can use GNU's parallel

Moving commands to a separate file command.sh:

before=$(snmpwalk -v2c -c public 1.1.1.1 .1.3.6.1.2.1.2.2.1.16.$1 | awk '{print $4}')
sleep 3
after=$(snmpwalk -v2c -c public 1.1.1.1 .1.3.6.1.2.1.2.2.1.16.$1 | awk '{print $4}')
res=$(bc <<< "$after-$before")
zxc=$(echo "$res * 8 * 100 / (1000000000 * 3) "  | bc)
per=$(echo "scale=1; 100 * $zxc / (1000) "  | bc)

Then execute parallel with intfile.txt as input:

 cat intfile.txt | parallel -j 5 'bash command.sh {}'
  • : j flag sets how many jobs will run concurrently
  • Related