Mohamad Ibrahim
Mohamad Ibrahim

Reputation: 5555

Empty Body For Loop Linux Shell

Hi I want to write and empty body loop. I just want the loop counter to increment so I want the cpu to stay busy without any IO operation. Here is what I have written but it gives me an error:

#!/bin/bash
for ((  i = 0 ;  i <= 1000000;  i++  ))
do
done


root@ubuntu:~# ./forLoop
./forLoop: line 4: syntax error near unexpected token `done'
./forLoop: line 4: `done'

Upvotes: 16

Views: 12204

Answers (4)

Anders Lindahl
Anders Lindahl

Reputation: 42870

You must specify at least one command in a loop body.

The best command for such a purposes is a colon :, commonly used as a no-op shell command.

Upvotes: 32

Michael
Michael

Reputation: 58447

You could use sleep x if you want to delay for x number of seconds.

Upvotes: 0

perreal
perreal

Reputation: 97948

#!/bin/bash
let i=0 
while [[ $i -le 1000000 ]]; do
  let i++ 
done

Upvotes: 1

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

You could put a no op command inside the loop like true or false (do nothing successfully or unsuccessfully respectively).

This will be a tight loop and will burn CPU. Unless you want to warm up your computer on a cold morning, you can simply say i=1000000 and have the same effect as the loop.

What is it that you're trying to achieve?

Upvotes: 4

Related Questions