Bash While Dead Loop
I need to run a windows command n times within a bat script file. I know how to do this in various programming languages but cannot manage to get it right on the windows command line :-(
What is the best way to emulate a do-while loop in Bash? I could check for the condition before entering the while loop, and then continue re-checking the condition in the loop, but that's duplica. ECHO Start of Loop FOR /L%i IN (1,1,5) DO ( ECHO%i ) The 1,1,5 is decoded as: (start,step,end) Also note, if you are embedding this in a batch file, you will need to use the double percent sign (%%) to prefix your variables, otherwise the command interpreter will try to evaluate the variable%i prior to running the loop.
I would expect something like either
or even this (though not entirely seriously)
Thanks!
EDIT
I can write a program in java, perl, c or whatever that will generate a bat script that looks like this
and so on. Or even 'better':
and then execute it... But the thing is that I need a concise way to specify a range of numbers to iterate through within the script.
Thanks!
3 Answers
You can do it similarly like this:
The 1,1,5 is decoded as:
Also note, if you are embedding this in a batch file, you will need to use the double percent sign (%%) to prefix your variables, otherwise the command interpreter will try to evaluate the variable %i prior to running the loop.
GoyuixGoyuixDirectly from the command line:
Using a batch file:
Displays:
jscottprotected by Michael Hampton♦Mar 28 '14 at 0:15
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged windowsbatchscripting or ask your own question.
On Unix-like operating systems, until and while are functions of the shell that define a block of code, and a conditional statement.
If while is used, the block loops as long as the condition is true. When the condition is false, the loop terminates.
Bash Read User Input While Loop
If until is used, the block loops as long as the condition is false. When the condition is true, the loop terminates.
This document covers the bashbuilt-in versions of until and while.
Syntax
Examples
User is repeated prompted for a name of a file to be located, until the user chooses to finish execution.
Related commands
Bash While Dead Loop Song
break — Break out of a while, for, foreach, or until loop.
csh — The C shell command interpreter.
ksh — The Korn shell command interpreter.
sh — The Bourne shell command interpreter.