5
Seminar #4 Summary (and not only) Looping commands in bash: for, while, until During the seminar #4 we’ve been running some examples involving looping constructs (the commands for, while and until). for command In the figure above we have a script example where we repeatedly echo on the screen the greeting message “Hello”, followed by a name from the list specified after the keyword in. Accordingly, the output of the program will be:

Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

Seminar #4Summary (and not only)

Looping commands in bash: for, while, until

During the seminar #4 we’ve been running some examples involving looping constructs (the commands for, while and until).for command

In the figure above we have a script example where we repeatedly echo on the screen the greeting message “Hello”, followed by a name from the list specified after the keyword in. Accordingly, the output of the program will be:

Page 2: Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

The for instruction may have several syntaxes. The classical Bourne shell version is the following:for name [ [in [words …] ] ; ] do commands; done(for more details please go to: https://www.gnu.org/software/bash/manual/html_node/Looping-Constructs.html)while a more modern version in Bash is:for (( expr1 ; expr2 ; expr3 )) ; do commands ; doneas in the following example:

Here we input a number n from the keyboard and we perform the sum and the product from 1 to n inside the for loop.Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax, like in the following example:

Page 3: Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

Here the step value is set to 2, so the program will output numbers from 1 to 10, incrementing the previous one by 2:

until commandThe basic syntax for until is:until test_commands; do consequent_commands; done

In this case, the consequent_commands will be executed as long as test_commands has an exit status which is not zero. The return status is the exit status of the last command executed in consequent-commands, or zero if none was executed.NOTE: The exit status of a commandThe exit status of a command is zero if the command completed successfully; if not, the exit status will have a non-zero value.

Page 4: Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

In the above example we are computing the sum from 1 to n using the until command.while commandThe basic syntax for while is:while test-commands; do consequent-commands; done

Here we have the same sum computed using the while command.

Page 5: Summary (and not only) - Prof. dr. Razvan Daniel … 4 - Looping commands.do… · Web viewSeminar #4 Summary (and not only) Looping commands in bash: for, while, until During the

Special case: select command

The break and continue builtins (see Bourne Shell Builtins) may be used to control loop execution.