C2110 UNIX and programming Lesson 8 -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 Lesson 8 PS / 2020 Distance form of teaching: Rev2 C2110 UNIX and programming Lesson 8 -2Summary L7 ➢ Bash C2110 UNIX and programming Lesson 8 -3Decision Block Yes No ? Yes No block 1 block 2 state? Yes No block 1 change initialization Conditional block execution (conditions) Cyclic block execution (loops) Typical use of decision block counter (variable) C2110 UNIX and programming Lesson 8 -4- Conditions if command1; then command2 ... else command3 ... fi if ! mkdir directory 2> /dev/null; then echo “I cannot create directory!" exit 1 fi Boolean expressions: [[ number1 operator number2 ]] [[ operator string1 ]] More complex logical expressions: || logical or && logical and ! negation It is possible to test the return value of a command or the result of a logical expression. Example: [[ (I -ge 5) && (I -le 10) ]] C2110 UNIX and programming Lesson 8 -5Loop using while 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 While loop is used mainly in situations where changing the "counter" is a complex matter or cannot be controlled by a quantifiable counter (it depends, for example, on the result of a command). while command1; do command2 ... done C2110 UNIX and programming Lesson 8 -6Next Subject ➢ Bash C2110 UNIX and programming Lesson 8 -7- Content ➢ Loop • for vs while ➢ Troubleshooting • syntactic and logical errors, syntax highlighting, static analysis, debugging ➢ Functions ➢ Lollipop Competition