C2110 UNIX and programming 5th lesson -1C2110 UNIX and programming 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 CZ.1.07/2.2.00/15.0233 5th Lesson C2110 UNIX and programming 5th lesson -2- Contents  In-semester test I  Text editors • vi, vim, nano • Graphical text editors • kwrite, gedit, kate  Processes II • Basic commands • Running commands and applications • Killing commands and applications C2110 UNIX and programming 5th lesson -3In-semester test I C2110 UNIX and programming 5th lesson -4In-semester test I  Test is questionaire (ROPOT) in IS Student – ROPOT – e-learning – C2110 – In-semester test I Length 20 minutes. Only one set of questions. Use ‘Save temporarily’ during work. Evaluation can be done only once. It is allowed and suggested to Test commands in terminal. Search manual pages, lecture notes and lecture presentations. Call teacher if you have problems. It is forbidden to Communicate with other person except teacher C2110 UNIX and programming 5th lesson -5Text editors  vi, vim, nano  Graphical text exitors  kwrite, gedit, kate C2110 UNIX and programming 5th lesson -6vi/vim, nano Editor vi / vim is standard in operating systems of UNIX type. Only in text mode and usage is non-trivial. • It is useful to learn to open file, edit text, save changes and close editor. • Enables scripting (using variables, cycles, arrays, associative arrays). Can be used for example to automatic generation of text from data read. • Although you run command vi on WOLF cluster, program vim (Vi IMporoved) • There are control differences between vi and vim. Editor nano is default text editor on some distributions. • Not so universal and flexible as vim. • Straightforward control. C2110 UNIX and programming 5th lesson -7vi – basics Editor work modes Extended command mode Command mode Input mode EscEnter a, c, i, o, s, A, C, I, O, R, S: Changes in file i text will be places from cursor position a text will be places after cursor position Running editor Closing editor $ vi start editor $ vi filename start editor and open file filename :q close editor :q! close editor without saving changes :w save file :w filename save data to file filename :wq close editor saving changes C2110 UNIX and programming 5th lesson -8- nano Straightforward control – menu in bottom part helps with control Action is called by single keys or key combinations ^character – e.g. ^X means combination Ctrl + X M-character – e.g. M-M means combination Alt+M C2110 UNIX and programming 5th lesson -9- kwrite Extended version: kate C2110 UNIX and programming 5th lesson -10- gedit C2110 UNIX and programming 5th lesson -11- Exercise 1. Write text with ten lines in vi editor. Each line will have two or more words. Save text into file mydata.txt 2. Use command wc to make sure that mydata.txt has exactly ten lines. 3. Use pipe(s) to construct command sequence, to print only number of words in file mydata.txt 4. Create text file in graphics text editor (your choice) containing ten words, each word on separate line. Save text to file second_data.txt 5. Use command paste to create file all_data.txt , that contains data from files mydata.txt and second_data.txt next to each other. 6. Use command wc to make sure, that file all_data.txt contains ten lines. 7. Open file all_data.txt in graphical text editor and check contents. 8. Open file all_data.txt in editor nano and save to new file name in mac format, what is difference to original file? Print contents of both files by cat, open in vi or gedit. C2110 UNIX and programming 5th lesson -12Processes II  Commands  Running commands and applications  Killing commands and applications C2110 UNIX and programming 5th lesson -13- Commands top prints processes by CPU time consumption – periodic refresh (finish by key q) ps print processes running in terminal (options can print all processes and various information) (ps -u user_name) pstree process tree print kill sends signal to process (default signal is TERM), used to terminate problematic processes nohup runs process without terminal interaction sleep runs process, that waits for specified time wait wait for background processes to finish time writes process run time ssh run process on remote machine, login to remote machine jobs prints list of background processes fg switches process from background to foreground bg switches process from foreground to background disown detach process from terminal C2110 UNIX and programming 5th lesson -14Running commands & applications System commands and applications User program and scripts $ ls -l $ cp file.txt file1.txt $ ./my_script $ ~/bin/my_application Run command on background Redirect (discard) standard output to terminal $ kwrite &> /dev/null Call by command or application name Program or script name has to be with full path (absolute or relative) příkaz Command options parameters (change command behavior and are input data of command processing) Output redirection is given on the end of command line (after parameters) $ gimp & Ampersand - & on the end runs command on background (after parameters and redirections) C2110 UNIX and programming 5th lesson -15Running commands & applications II Terminal (useful key shortcuts): Ctrl+C sends signal SIGINT (Interrupt) to running process, process is usually terminated immediately Ctrl+D close input stream of running process Ctrl+Z pause process run, following process management can be done by commands bg, fg, disown Print full path to system command: type print path to system command or program Examples: $ type ls ls is /bin/ls $ type pwd pwd is a shell builtin Command is implemented as inner shell command (builtin) C2110 UNIX and programming 5th lesson -16- Examples $ ps -u kulhanek PID TTY TIME CMD ... 5440 pts/8 00:00:00 bash 5562 pts/8 00:00:00 kwrite 5566 pts/8 00:00:00 ps $ kill 5562 # terminate kwrite application $ kwrite # run kwrite application on foreground ^Z # pause application run [1]+ Stopped kwrite $ jobs # print list of applications on background [1]+ Stopped kwrite $ bg 1 # application 1(kwrite) is switched to foreground [1]+ kwrite & $ jobs [1]+ Running kwrite & C2110 UNIX and programming 5th lesson -17- Exercise 1. Measure time length of sleep 0,003 process run, how long is it, why? 2. Get name of process number 1, who is process owner? 3. Try to kill the process, why is it not possible?