C2110 UNIX and programming Lesson 3 / 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 Lesson 3 / Module 2 PS / 2020 Distance form of teaching: Rev2 C2110 UNIX and programming Lesson 3 / Module 2 -2Streams, Redirects, Pipes C2110 UNIX and programming Lesson 3 / Module 2 -3Process Communication Process Process can communicate with the environment in a number of ways: • GUI (Graphical User Interface = using the appropriate API) • signals, shared memory, MPI (Message Passing Interface), etc. • standard currents One option is to read input data from standard input current, output data into standard output or error stream. C2110 UNIX and programming Lesson 3 / Module 2 -4Standard Streams process (command, program)standard input stream (keyboard) standard output stream (terminal screen) standard error stream (terminal screen) Input-output streams serve the process for communication with its surroundings. Every process opens up three standard streams: C2110 UNIX and programming Lesson 3 / Module 2 -5- Redirection process (command, program)standard input stream (keyboard) standard output stream (terminal screen) standard error stream (terminal screen) Input-output streams can be redirected to use files instead of the keyboard or screen. C2110 UNIX and programming Lesson 3 / Module 2 -6Redirection and Ending Input Standard input redirection of program my_command from file input.txt. $ my_command < input.txt Standard input redirection of program my_command from script file. ....... ./my_command << EOF first line text second line text third line text EOF ...... mark indicating the end of the entry (selected by user) end of input, mark can not be surrounded by spaces text forming the read input This method of redirection is especially useful in scripts, but it also works in the command line. The advantage is the expansion of variables in the read text. Terminal (useful keyboard shortcuts): Ctrl + D closes the input stream of the running process C2110 UNIX and programming Lesson 3 / Module 2 -7Output Redirection Standard output redirection of program my_command to a file output.txt. (File output.txt is created. If it already exists, its original content is deleted.) $ my_command > output.txt Standard output redirection of program my_command to a file output.txt. (File output.txt is created. If it already exists, output of the program my_command is connected to its end.) $ my_command >> output.txt Similar rules apply to standard error output, in this case the following operators are used: $ my_command 2> errors.txt $ my_command 2 >> errors.txt C2110 UNIX and programming Lesson 3 / Module 2 -8Joining Output Streams Standard output and standard program error output of my_command program can be redirected at the same time to a file output.txt. $ my_command &> output.txt $ my_command &>> output.txt Alternative solutions for &>>: First, it is necessary to redirect standard output and then join standard error output with standard output. $ my_command >> output.txt 2>&1 $ my_command 2>&1 >> output.txt does not work order is important! works in new versions bash C2110 UNIX and programming Lesson 3 / Module 2 -9- Pipes Pipes serve to combine the standard output of one process with the standard input of another process. process 1 process 2 input output | input output error output error output $ command_1 | command_2 Usage: C2110 UNIX and programming Lesson 3 / Module 2 -10Pipes and Error Stream The transmission of the standard error output via the pipe can be performed after its connection with the standard output. process 1 process 2 input output | input output error output error output $ command_1 2>&1 | command_2 Use: 2>&1 C2110 UNIX and programming Lesson 3 / Module 2 -11Commands for Exercise Examples of use: $ cat file1.txt file2.txt $ paste file1.txt file2.txt $ wc file.txt $ head -15 file.txt $ tail -6 file.txt concatenates the content of the files file1.txt and file2.txt and prints the result on the screen concatenates the contents of file1.txt and file2.txt (side by side) and prints the result on the screen lists the number of lines, words, and characters in file.txt prints the first 15 lines of file.txt prints the last 6 lines of file.txt cat joins the content of several files into one (one after the other), or lists the contents of one file paste joins the content of multiple files into one (side by side) wc file information (number of lines, words and characters) head prints the inital part of a file tail prints the final part of the file C2110 UNIX and programming Lesson 3 / Module 2 -12Commands for Exercise... Command tr is used for transformation or deletion of characters from standard input. The result is sent to standard output. Examples: $ cat file.txt | tr --delete "qwe" $ cat file.txt | tr --part "[:space:]" $ echo $PATH | tr ":" "\n" from the contents of file.txt, removes the characters "q", "w" and "e" from the contents of the file file.txt removes all whitespace in the text sent by the echo command, the characters ":" will be replaced by a newline character "\n " C2110 UNIX and programming Lesson 3 / Module 2 -13Exercise 1 1. Find all files with the extension .f90 which are in the directory /home/kulhanek/Documents/C2110/Lesson03. Save list of files to a file ~/Procesy/list.txt 2. How many lines does the file list.txt contain? 3. Write the first two lines from the file list.txt, first onto the screen and then to the file two_lines.txt 4. Write only the third line of the file list.txt 5. In the directory /proc, find all files that begin with letters cpu. Remove the unauthorized access information from the listing by redirecting of the error current to /dev/null 6. List the directory names contained in the variable PATH, each on one line.