C2110 UNIX and programming Lesson 7 / Module 2 -1C2110 UNIX and programming Petr Kulhanek kulhanek@chemi.muni.cz National Center for Biomolecular Research, Faculty of Science Masaryk University, Kamenice 5, CZ-62500 Brno PS / 2020 Distance form of teaching: Rev2 Lesson 7 / Module 2 C2110 UNIX and programming Lesson 7 / Module 2 -2- Loops state ? Yes No block 1 change initialization Loop block execution counter (variable) C2110 UNIX and programming Lesson 7 / Module 2 -3Loop Using while/until ... state ? Yes No block 1 change initialization state ? Yes No block 1 change initialization evaluates conditions at the end loop evaluates conditions in the beginning of the loop C2110 UNIX and programming Lesson 7 / Module 2 -4Loop using while/until ... This algorithm has no direct support in the control structures of bash, its implementation is possible, but at cost of poorer readability of resulting code. complicated implementation in bash state ? Yes No block 1 change initialization state ? Yes No block 1 change initialization evaluates conditions at the end loop evaluates conditions in the beginning of the loop C2110 UNIX and programming Lesson 7 / Module 2 -5Loop using while/until ... easy implementation in bash state ? Yes No block 1 change initialization state ? Yes No block 1 change initialization evaluates conditions at the end loop evaluates conditions in the beginning of the loop This algorithm has no direct support in the control structures of bash, its implementation is possible, but at cost of poorer readability of resulting code. C2110 UNIX and programming Lesson 7 / Module 2 -6Loop Using while/until while command1 do command2 ... done while command1; do command2 ... done Compact notation: the loop is in progress while command1 returns return value 0 (no error) A loop is a control structure that repeatedly executes a sequence of commands. Repetition and end of the loop is controlled by a condition. until command1; do command2 ... done the loop is in progress until command1 will not return a return value of 0 C2110 UNIX and programming Lesson 7 / Module 2 -7Practical Example - Loop I < N Yes No I = I + 1 N = 10 I= 0 writestr "X" counter (variable) N=10 I=0 while [[ I -lt N ]]; do echo "X" ((I = I + 1)) done C2110 UNIX and programming Lesson 7 / Module 2 -8Practical Example - Loop N=10 I=0 while test "$I" -lt "$N"; do echo "X" ((I = I + 1)) done N=10 I=0 while [[ I -lt N ]]; do echo "X" ((I = I + 1)) done $ must be used $ optional, if [[ ]] or (( )) block is used I < N Yes No I = I + 1 N = 10 I= 0 writestr "X" counter (variable) N=10 I=0 while [[ "$I" –lt "$N" ]]; do echo "X" ((I = I + 1)) done C2110 UNIX and programming Lesson 7 / Module 2 -9Exercise I 1. Write a bash script, which prints N "X" characters to the terminal. The number of characters will be entered by the user as the first argument of the script. The script prints an error message if the specified number of characters does not exceed two. C2110 UNIX and programming Lesson 7 / Module 2 -10Complex Constructions - Nesting Bash language does not have a labels and command goto, or their equivalents. Thus, more complex constructions can be implemented only by immersing loops and conditions into each other. The level of immersion is not limited. condition loop However, when designing an algorithm/script, we try to avoid unnecessary nesting (mostly for easier orientation in the script). ! condition loop exit More suitable arrangement, e.g., for testing input data from users. loop condition loop I loop II C2110 UNIX and programming Lesson 7 / Module 2 -11Nesting Loops - Example outer loop counter can affect behavior of the inner loop N=10 I=0 while [[ I –lt N ]]; do J=0 while [[ J –lt I ]]; do echo –n "X" ((J = J + 1)) done echo "" ((I = I + 1)) done For nested structures, we pay attention to indentation of text blocks which increases clarity and readability of the code. Text editors have built-in support that makes indentation easier, such as in gedit editor, the indentation of the selected text block can be achieved with the TAB or Shift + TAB key. C2110 UNIX and programming Lesson 7 / Module 2 -12Exercise II 1. Write bash scripts for the following tasks. Make user enter the dimension of the rendered shape after running the script. While working on the script, employe the algorithm created from the homework. C2110 UNIX and programming Lesson 7 / Module 2 -13Task 1 Print a square of X characters into the terminal. The length of the side of the square is entered by the user. X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X Ignore the fact that it is not visually a square. However, number of X characters on the line and the number of lines must be the same. C2110 UNIX and programming Lesson 7 / Module 2 -14Task 2 Print a right triangle with X characters into the terminal, so that one perpendicular is located at the top and the other on the left. The length of the perpendicular is entered by the user. X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X C2110 UNIX and programming Lesson 7 / Module 2 -15Task 3 Print a right triangle with X characters into the terminal, so that one perpendicular is located at the bottom and the other on the left. The length of the perpendicular is entered by the user. X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X