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'
If we want to create a for-loop or while loop that has an empty body, we can use an empty statement. Let's start with for-loop. The semicolon immediately after the for loop shows that the body of the for loop is empty.
& means both standard output ( 1> ) and standard error( 2> ). >> means append to end of the file.
Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
$0 Expands to the name of the shell or shell script. This is set at shell initialization.
You must specify at least one command in a loop body.
The best comand fo such purposes ia a colon :
, commonly used as a no-op shell command.
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?
#!/bin/bash
let i=0
while [[ $i -le 1000000 ]]; do
let i++
done
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