IMC!


Contents


Photos

 







Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
05 - While and until cycles in the bash

 | 

The while..do..done construct executes a group of operations while a condition is valid. The syntax is:

while [condition];
do
  code1
done

Two numbers can be compared with the following tests. The variables must be written in the form:

"$variable_name"

The numerical tests are:

ControlMeaning
x -eq y true if x is equal to y
x -ne y true if x is not equal to y
x -gt y true if x is bigger than y
x -lt y true if x is smaller than y
x -ge y true if x is bigger than or equal to y
x -le y true if x is smaller than or equal to y

Two strings can be compared with the following tests:

ControlMeaning
x = y true if x is equal to y
x != y true if x is equal to y
-n x true if x is not NULL or void
-z x true if x is NULL or void


Here is an example script:

#!/bin/bash
# The following script increments a variable
# up to 10, printing each time the result
x=0
while [ "$x" -le 10 ]; do
  echo "Valore corrente di x: $x";
  x=$(( $x + 1 ))
done
 

A construct similar to while is until, that tests the condition on the opposite way: executes some code while a condition is false (that is, until a condition becomes true). Here is an example:

#!/bin/bash
# The followinf example intrements a variable up
# to 10, printing each time the result

x=0
until [ "$x" -ge 10 ]; do
  echo "Current x value: $x";
  x=$(( $x + 1 ))
done
 


 | 







Comments

No comment present!

Write a comment

You can write here a comment to the article you've just read. Smiles, links and images are not allowed. The maximum comment length is 4000 characters. Please be polite, all the offensive messages will be deleted.

Your comment (lascia bianco!):
Uses (max 25 characters, required)
Web site (max 255 characters, optional)
e-Mail (max 255 characters, optional, will not be published) Your opinion (lascia bianco!):
Comment (max 4000 characters, required):





Valid HTML 4.01 Transitional
E-Mail - 123.72 ms

Valid HTML 4.01 Transitional