قالب وردپرس درنا توس
Home / Tips and Tricks / for, while and until – CloudSavvy IT

for, while and until – CloudSavvy IT

Shutterstock / jeep5d

Most programming languages ​​support different forms of repeating lines of code. Bash natively supports ̵

6;while’ loops, ’till’ loops and the most famous ‘for’ loops. This article introduces and discusses all three.

What are Bash loops?

To define this a little better, let’s start with the question what are in heat. Loops are a programming language construction, which allows a developer to repeat (ie repeat) some or all of the code within such a loop definition. It is now easy to define Bash loops like any loop programming language construct used in Bash!

Bash natively supports ‘for’, ‘to’ and ‘while’ based loops. Each of these have their own advantages, but you can already get a sense of their meaning just by looking at the main word language. For example, ‘to’ causes one to naturally think about ‘do something to’ and this is indeed what a Bash ‘to’ loops do; it repeats a certain amount (or all) of code until a certain condition is met.

Likewise, while loops remain active until a condition is no longer true. Finally, for example, ‘for’ loops loop a defined number of times, similar to how we would write ‘for 70 times, do…’. This helps us to logically understand the unique functions that each loop offers us and to implement more readable code.

in front of Based on Bash Loops

For the purposes of this article we will look at the newer form of defining Bash for loops. A somewhat older, less modern definition of in front of loops in Bash might look something like this: for i in $(seq 1 5); do echo $i; done. Let’s compare this to a cleaner, more structured, modern look in front of loop:

for ((i=1;i<=5;i++)); do echo $i; done

A bash for a loop based

So simple in front of based Bash one-liner (a term often used in Linux / Bash circles to conceptualize a mini script written on a single line) will print the numbers 1 to 5 in sequential order. We have set a starting value for the i variable ($i) by assigning the value 1 same, as the first part of our in front of loop definition, terminated by a ; separator.

Next we specify that we are only one to go to 'less than or equal to five' by using i<=5. We then specify what should happen at the end of each round, which is to increase the variable i by one, or, in a commonly used (including in the C ++ language for example) coding steno, this is represented as i++.

Finally, we specify the beginning of our loop code with do, that is, after our termination for loop definition, just like any other statement termination, with ; for the do. We also specify the end of our loop code with done and we echo (print) the value of our variable I between the do and done.

Also note specifically that the do clause itself is not terminated with ;, and that would lead to an error. Keep in mind the do a prefix for what should be done, and it makes more sense. This may be one reason why it is sometimes cleaner to put things in a multi-line script like the do can just be the last word on a line.

Even when defining other types of loops, we still keep the do and done clauses, and we will always make sure to end our loop definition (i.e. before do) with ;, as well as ending every individual statement within us do...done loop definition with ;.

Let's put this in a little script to see more clearly how things can work that way:


for ((i=1;i<=10;i+=2)); do 
  echo $i

A bash for a based loop script

After the script is made executable with chmod +x test.sh, we do the same. A few minor changes have been made to the script. Notice how we increase the variable this time i each time with two. This is done with the help of another encoding abbreviation, namely i+=2 which can be read as increase i by two. One can also write i=i+2 in the same place, and it works exactly the same.

We see that we start at 1, and increase by 2 every time we go through the loop, ending in 9. The reason it ends in 9 is that is the maximum value 10. So after 9 the next value would be 11, which is then larger 10 and so it is not displayed / looped through.

Also note how the ; was removed after the echo line. This is because it is not necessary to terminate the end of a statement if there is an end-of-line situation / character instead. This is the case here; we have no other command after the echo $i and the line ends immediately (spaces at the end of the line would be fine too, the principle is just to have no other command unless that new command is preceded (and the previous one ended) by ;).

while Based on Bash Loops

Now let's look at a Bash loop - with the same do...done loop definition, we can create a while based Bash loop it will run as long as a certain condition is true.

i=1; while [ $i -le 5 ]; do echo $i; i=$[ i + 1 ]; done

A Bash while-based loop

In this example, we do the same as our first in front of based loop example. Although the definition looks more complex (and thus a in front of loop might be better suited for this particular use), it's interesting to see how we create a while loop the same way.

Here we put our first i variable, manually, in a separate command terminated by ;, to 1. Then we start one while loop where we set a condition, in a similar way to defining a if statement (there is a link at the end of this article to an article about if statements if you want to know more), where we check if the i variable is lower or equal (-le) thereafter 5.

After this, we have our usual do...done block in which we echo our variable, and then, in a new statement, manually increment our value i variable per one in a mathematical calculation as defined by the $[...] Bash arithmetic words. Let's have one next time up to based on Bash loop

until Based on Bash Loops

Armed with what we've learned so far, we can now more easily evaluate the following until based bash loop:

i=1; until [ $i -gt 5 ]; do echo $i; i=$[ i + 1 ]; done

A Bash to based loop

Here we look for the condition I greater than 5 to become true. Until then (i.e. a until based loop), we will print (with echo) our variable I and increase the same by one, equal to our previous one while based example.

We can see how the syntax for the until command is very similar to it while order. Also note that unlike the for based command, these commands look for one true condition to continue to exist (with while), or one by one true condition to start (with until). This also allows someone to use other commands that can return a true / false like output like for example grep -q:

echo 1 > a
while grep -q '1' ./a; do echo 'yes'; sleep 5; done

Checking for a real state with grep -q in a time-based Bash loop

Here we add the number 1 to a file named aand check if the number exists 1 within that file using a grep -q (a quiet grep). We will continue to do this (eg while) until it is no longer true. While it is true, we will print the text Yes and pause the loop for five seconds with sleep 5. Notice how each command ends with ; again.

After about 17 seconds, we pause our script with CTRL + c, which is the universal method to stop a running process in Bash (in addition to the stronger and more effective CTRL + z that immediately pauses a process, but that's for another article !)

Shut down

In this article, we've discussed the 'for', 'while' and 'until' loops available by default in Bash. If you're interested in more Bash, check out Conditional Testing in Bash: if, then, else, elif and Bash Functions and Local Variables.

To enjoy!

Source link