C2110 UNIX and programming 12th Lesson Petr Kulhánek, Jakub Štěpán kulhanek@chemi.muni.cz National Centre for Biomolecular Research, Faculty of Science Masaryk University, Kotlářská 2, CZ-61137 Brno Hf european H9 ^MT| £_J M ^5^^ 1 SOCial fund in the I HflflS | M|N|STRY 0F EDUCATION, OP Education *j£^JF<:, m^^0 ■ CZeCh repUbllC EUROPEAN UNION YOUTH AND SPORTS for Competltlvness '^.VAÍ>V INVESTMENTS IN EDUCATION DEVELOPMENT CZ.1.07/2.2.00/15.0233 JIXand programming 12th lesson Contents > AWK • Text files analysis • Regular expressions • Arrays > BASH user input check • BASH error states • Input values check NIX and programming 12th lesson Script execution €) BEGIN { } { } /VZOR/ { } END { } Block BEGIN (1) is executed (if present) before file analysis. • Record from file is read. By default one record is whole line from input file or stream. Record is split to fields. By default words of line are fields. • Block (2) is executed for any record. • Block (3) is executed for any record matching PATTERN. • .... Possible other blocks are executed .... Block END (4) is executed (if present) after analyzing whole file content. Each block is in curly brackets {}. Some program blocks are optional - see description. Default record separator is new line - one line = one record. IIX and programming 12th lesson Regular expressions /PATTERN/ { If record matches PATTERN, then block is executed. } PATTERN may be regular expression. Regular expression is string construction, that describes structure of set of text strings. It may be used to search text strings or substitutions of substrings. Simple regular expressions samples: TEXT - matches if record contain text TEXT (TEXT may occur anywhere in record) ATEXT - matches if record contain text TEXT on record beginning TEXT$ - matches if record contain text TEXT on record end JIX and programming 12th lesson Exercise 1. Extract temperature dependency on time from file rst.out. Display graph of dependency by gnuplot. NSTEP = 1000 TIME(PS) = 1.000 TEMP(K) = 305.69 PRESS = 0.0 Etot = 907.8481 EKtot = 160.3711 EPtot = 747.4770 BOND = 40.6154 ANGLE = 273.9238 DIHED = 164.5827 1-4 NB = 14.6900 1-4 EEL = 973.2602 VDWAALS = -67.6091 EELEC = -488.9232 EGB = -163.0629 RESTRAINT = 0.3793 EAMBER (non-restraint) = 747.0977 2. Extract time dependency of energies from file rst.out. Extract total energy (Etot), kinetic energy (EKtot) and potential energy (EPtot) time dependency. Display graphs of all energies in gnuplot. Make sure, that sum of potential and kinetic energy is equal to total energy. JlXand programming 12th lesson -5- AWK provides associative arrays. An array has name, all items are accessed by key. Key may have arbitrary type and value. Key may be variable value. Value assignment: my array[key] = value; Obtain value: variable = my array[key]; Examples: i = 5; my array[i] = 15; print my array[i]; a = "word"; my_array[a] = "value"; print my array["word"], my array[5]; JlXand programming 12th lesson -6- Cycle does one iteration for each key value used in array. Actual key value is in variable. sllX and programming 12th lesson Exercise 1. Extract temperature time dependency from file rst.out. Remove last 2 values (these are average and fluctuation). Display graph in gnuplot. 2. Extract temperature values from file rst.out and calculate its average value. Compare calculated value with value printed in file rst.out. Why both values differ? JlXand programming 12th lesson -8- BASH user input check > BASH error states > Input values check IIX and programming 12th lesson BASH error states Example from lession 8. Script does not behave correctly if started with no argument or with non-numerical argument. exit 1 eater then zero! $ ./my_script my script: line 2 "Number greater then zero. 11 exit 0 :est: -le: unary operator expected $ echo $? en zero ay sc: Number greater then zero." IX and programming 12th lesson Input values check It is neccessary to check values obtained from user. Check • Number and type of arguments • Validity of numerical values (zero devision, negative counter values) • Zero string length • Existence of files for processing Exercise 1. To script from home work 1 in lesson 8 (rectangle drawing) add check that user submitted exactly two arguments. 2. Adjust previous script to check that user submitted size in natural numbers. 3. Adjust previous script in such a way, that third argument will be character or string to print rectangle with (instead of character "X"). Check if argument is non-empty string. 4. Adjust previous script in such a way, that user will insert values interactively on request after script start. 5. Adjust script from home work II lesson 9 in such a way, that script will accept name of analyzed file as a argument and file path existence will be checked. JlXand programming 12th lesson -12-