C2110 UNIX and programming Lesson 9 / Module 2 PS / 2020 Distance form of teaching: Rev3 Petr Kulhánek kulhanek@chemi.muni.cz National Center for Biomolecular Research, Faculty of Science Masaryk University, Kamenice 5, CZ-62500 Brno 10 UNIX and programming Lesson 9 / Module 2 for... in Loop 10 UNIX and programming Lesson 9 / Module 2 for... in Loop Commands in the block do/done (commandl,...) are performed for each element in the list LIST. In then given run of the cycle, variable VAR contains the current element from the list LIST. for VAR in LIST do commandl $VAR • • • done Compact notation: for VAR in LIST; do commandl $VAR • • • done 10 UNIX and programming Lesson 9 / Module 2 -3- for... in Loop, Lists for A in a b c; do echo $A done Loop cycles three times, during which it prints one by one characters a, b, c. Lists of items can be created programmatically (using the commands given in inverted apostrophes). for A in xls *.epsx; do ./process_file $A done Command process_fill is executed for each file with an extension .eps, which is located in the current directory. for A in x seq 1 0.25 10x; do printf "%8 .3f\n " $A done Lists real numbers in the range from 1 to 10 in 0.25 increments. The numbers will be given to three decimal places, right-aligned and in an 8-character field. Documentation: man seq 10 UNIX and programming Lesson 9 / Module 2 Exercise 1 1. Write a script that prints real numbers in the range from -10 to 10 in increments of 0.5. The numbers will be given including the sign, right-aligned in a field of 10 characters and with precision of one decimal place. 2. Write a script that prints the file name and the number of lines for each *.cpp file in directory/home/kulhanek/Documents/C2110/Lesson09/datal. Use for... in loop and use the Is command to list files to solve the task. 3. Modify the previous script so that for each *.f90 file in the /home/kulhanek/Documents/C2110/Lesson09/data2 directory the file name and the number of lines it contains is printed. Use for... in loop and use the find command to list files. 10 UNIX and programming Lesson 9 / Module 2 -5- Loops and Redirections 10 UNIX and programming Lesson 9 / Module 2 Redirection and pipes - input It is possible to transfer input data to the cycle using a pipe or redirection. Typical use is loading file/input stream line by line: cat file.txt while read A; do command2 \ • • • done \ pipe while read A; do command2 • • • done < file.txt redirection T4 10 UNIX and programming Lesson 9 / Module 2 Redirection and pipes - input Input can be "consumed"commands in the block of the loop. This can be prevented by redirecting standard input from /dev/null to a block of commands in {}. cat file.txt I while read A; do { command2 } < /dev/null done while read A; do { command2 } < /dev/null done < file.txt 10 UNIX and programming Lesson 9 / Module 2 Redirects and pipes - voutput Redirecting output to a file: for((1=1;I <= 10;I++)); do echo $1 done > file.txt The output of all commands in the cycle is redirected to a file file.txt. Pipe: for((1=1;I <= 10;I++)); do echo $1 done I tee file.txt The output of all commands in the cycle is connected to the standard input of command tee. It prints it to standard output and saves it to a file file.txt at the same time. 10 UNIX and programming Lesson 9 / Module 2 -9- Redirects and pipes - examples for((1=1;I <= 10;I++)); do echo $1 done > file.txt for((1=1;I <= 10;I++)); do echo $1 printf "N=%10d\n" $1 done > file.txt same functionality rm -f file . txt for((1=1;I <= 10;I++)); do echo $1 » file.txt done different functionality rm -f file.txt for((1=1;I <= 10;I++)); do echo $1 » file.txt printf "N=%10d\n" $1 done 10 UNIX and programming Lesson 9 / Module 2 Exercise 2 1. Write a script that prints file name and the number of lines it contains for each *.f90 file in the directory /home/kulhanek/Documents/C2110/Lesson09 /data2. In the solution, use a pipe with while read and use the find command to list files. 2. Modify the shape rendering scripts (L08.M01.C01) so that the dimension of the shape is read from standard input and the resulting shape is printed to a file whose name is entered again by the user from standard input. 10 UNIX and programming Lesson 9 / Module 2 -11- Homework 10 UNIX and programming Lesson 9 / Module 2 Homework I Explain the different behavior of the following scripts. File data.txt contains five lines. #!/bin/bash 1 = 0 cat data.txt | while read A; do I = $((I + D ) done echo $1 prints number 0 #!/bin/bash 1 = 0 while read A; do I = $((I + D ) done < data.txt echo $1----' prints number 5 10 UNIX and programming Lesson 9 / Module 2 Homework II File rst.out (home/kulhanek/Documents/C2110/Lesson09/rst.out) contains results from molecular dynamics. The task is to extract the dependence of the temperature of the simulated system on time from the file and save this data in the temp file temp.out, which will contain exactly two columns. The first column will be the time and the second column will be the temperature. time temperature NSTEP = Etot BOND 1-4 NB = EELEC = 500 TIME (PS) = 942.6248 EKtot 51.3204 ANGLE 17.7099 1-4 EEL -494.7423 EGB 0.500 TEMP(K) 151.0990 292.3619 981. 4071 -164.7991 288.02 EPtot DIHED VDWAALS RESTRAINT PRESS 0.0 791.5258 176.5980 -68.3301 0.1822 EAMBER (non-restraint) 791.3436 ATTENTION: The script must not contain grep commands, awk nor their variants. Use command read and while loop. 10 UNIX and programming Lesson 9 / Module 2