C2110 UNIX and programming Lesson 7 -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 7 PS / 2020 Distance form of teaching: Rev3 C2110 UNIX and programming Lesson 7 -2Summary L6 ➢ Bash C2110 UNIX and programming Lesson 7 -3- Variables In Bash, a variable means a named location in the memory that contains a value. The value of a Bash variable is always of string (text) type. Variable settings: $ VARIABLE_NAME=value $ VARIABLE_NAME="value with spaces" Access to the value of variable: $ echo $VARIABLE_NAME To delete a variable: $ unset VARIABLE_NAME Overview of all defined variables: $ set cannot be a space between the variable name and = "TEXT${VARIABLE}TEXT" if the value is to be part of a text, the variable name is enclosed in braces C2110 UNIX and programming Lesson 7 -4Arithmetic Operations Possible entries: (( I = I + 1 )) (( I++ )) Arithmetic operation with obtaining the result: echo "Value I is increased by one : $(( I + 1 ))" More details: man bash the value of the result is written to the position of $ sign • Arithmetic operations with integers can be performed in ((...)) block. • Characters in the block are interpreted as variable names. Therefore, it is not necessary to use $ operator to obtain their value. • The values ​​of the variables are interpreted as integers. If the conversion fails, a value of zero is used. C2110 UNIX and programming Lesson 7 -5- Input/Output Input • Script arguments • Available in special variables (0, 1, 2, …, 9, #) • Command read serves for reading text from standard input and storing it in variables. Output • Command echo serves for unformatted printing to standard output current. • Command printf serves for printing formatted texts and numbers into the standard output stream. printf [format] [value1] [value2] ... echo [options] [string1] [string2] ... read A # the whole line is stored in variable A read A B # the first word is stored in variable A # the rest of the line in variable B C2110 UNIX and programming Lesson 7 -6New Subject ➢ Bash C2110 UNIX and programming Lesson 7 -7Decision 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 7 -8- Content ➢ Decision block • conditions, cycles ➢ Decision making ➢ The return value of the process • exit command ➢ Test command • comparison operators, logical operators • simplified notation ➢ Conditions ➢ Loops: while/until