Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 458
Mohamad Ibrahim Avatar asked Jul 15 '12 09:07

Mohamad Ibrahim


People also ask

Can for loop be empty?

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.

What does &> mean in shell command?

& means both standard output ( 1> ) and standard error( 2> ). >> means append to end of the file.

What does $() mean in bash?

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).

What does $0 mean in shell script?

$0 Expands to the name of the shell or shell script. This is set at shell initialization.


3 Answers

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.

like image 67
Anders Lindahl Avatar answered Oct 10 '22 09:10

Anders Lindahl


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?

like image 35
Noufal Ibrahim Avatar answered Oct 10 '22 09:10

Noufal Ibrahim


#!/bin/bash
let i=0 
while [[ $i -le 1000000 ]]; do
  let i++ 
done
like image 1
perreal Avatar answered Oct 10 '22 09:10

perreal