C2110 UNIX and programming 6th 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 6th Lesson C2110 UNIX and programming 6th lesson -2- Contents  Scripts • Scripts vs. programs • Program compilation • Running sample script and program  Variables • Setting and removing variables • Variables and processes • String types C2110 UNIX and programming 6th lesson -3- Scripts  Scripts vs. programs  Program compilation  Running sample script and program C2110 UNIX and programming 6th lesson -4Programs vs. Scripts Source code program input output Compilation script interpreter input output Program is machine instruction file processed directly by processor. It is created by procedure called compilation from source code. Compiled languages: C/C++ Fortran Script is text file containing commands and special constructions, these are processed by interpreter of scripting language. Skripting languages: bash gnuplot awk JavaScript PHP C2110 UNIX and programming 6th lesson -5Programs vs Scripts, ... No recompilation Program can generate and run self running code Poor optimization Slower processing Easy optimization Fast processing Recompilation needed Self run code not available Source code program input output Compilation script interpreter input output C2110 UNIX and programming 6th lesson -6How to write programs and scripts Scripts are text files – thus any text editor can be used, that enables saving pure text (without any format metadata). Text editors:  vi  kwrite  kate  gedit For complex programs and scripts development environments can be used – IDE (Integrated Development Enviroment). IDE contains next to editor extra tools as: project manager, debugger and more. Usually for more advanced and complex languages: JavaScript, Python, PHP, etc. IDE:  Kdevelop  qtcreator  NetBeans  Eclipse C2110 UNIX and programming 6th lesson -7Program in C #include int main(int argc,char* argv[]) { printf("This is C program! \n"); return(0); } Compilation $ gcc program.c -o program Source code Running program $ ./program C language compiler Program name file program needs permission to execute C2110 UNIX and programming 6th lesson -8Program in Fortran program Hello write(*,*) 'This is Fortran program!' end program $ gfortran program.f90 -o program $ ./program Compilation Source code Running program Fortran language compiler Program name file program needs permission to execute C2110 UNIX and programming 6th lesson -9Script in Bash #!/bin/bash echo 'This is Bash script!' Running script interpret Bash file script.bash does not need permissions to execute Script $ bash script.bash C2110 UNIX and programming 6th lesson -10Script in gnuplot #!/usr/bin/gnuplot set title "This is gnuplot script!" plot sin(x) pause -1 interpret gnuplot Script $ gnuplot skript.gnuplot Running script file script.bash does not need permissions to execute C2110 UNIX and programming 6th lesson -11- Exercise 1. Create four directories with names task01, task02, task03, task04 2. From directory /home/kulhanek/Data/programs copy program.c , program.f90, skript.bash, a skript.gnuplot to particular directories you created in 1. 3. Compile source codes of language C and Fortran. Run compiled programs. 4. What is size of compiled program in C language? Open program file in text editor, what is inside? 5. Run scripts skript.bash a skript.gnuplot. C2110 UNIX and programming 6th lesson -12Running scripts 1) Un-direct running We run interpreter and as its argument we put script name. $ bash my_bash_script_name $ gnuplot my_gnuplot_script_name Scripts does not need permission x (executable). 2) Direct running We run directly script (shell runs interpreter automatically). $ ./my_bash_script $ ./muj_gnuplotu_script Scripts must have x (executable) set and interpreter (first script line). C2110 UNIX and programming 6th lesson -13Interpreter specification #!/bin/bash echo “This is bash script!" #!/usr/bin/gnuplot set xrange[0:6] plot sin(x) pause -1 Script in bash Skript in gnuplot #!/absolute/path/to/interpreter/of/script Interpreter specification (first script line):  If no interpreter is specified, then system shell interpreter is used.  Interpreter is ignored in case of un-direct running. C2110 UNIX and programming 6th lesson -14Interpreter specification #!/usr/bin/env interpreter If absolute path may be changed over time (for example by using software modules), it may be specified dynamically: Interpreterhas to be in system path of variable PATH. #!/usr/bin/env bash echo “This is bash script!" #!/usr/bin/env gnuplot set xrange[0:6] plot sin(x) pause -1 Script in bash Script in gnuplot C2110 UNIX and programming 6th lesson -15- Exercise 1. Change access permissions to files skript.bash a skript.gnuplot (command chmod). 2. Make sure that scripts can be run directly. 3. What happens when we use interpreter bash for script skript.gnuplot? C2110 UNIX and programming 6th lesson -16- Variables  Variable setting and unsetting  Variables and processes  String types C2110 UNIX and programming 6th lesson -17- Variables In Bash language variable is named memory place, that contain value. Variable value is always of type string (test). Variable set: $ VARIABLE_NAME=value $ VARIABLE_NAME="value with spaces" Access to variable value: $ echo $VARIABLE_NAME Unsetting of variable: $ unset VARIABLE_NAME Overview of all variables: $ set No space between variable name and = C2110 UNIX and programming 6th lesson -18Variables and processes Variables Process: pid, ppid Process: pid, ppid Variables Parent process child process Each process has own space for variables and its values. Child process when started gets copy of exported variables and its values from parent process. These variables can be changed by any way or remove them and new variables can be defined too. All these changes are not visible to original variables in parent process and are deleted when child process finishes. Export proměnné: $ export VARIABLE_NAME $ export VARIABLE_NAME= "value" export export with assignment C2110 UNIX and programming 6th lesson -19- Strings In Bash llanguage there are four string types: • no quotes A=pokus B=* C=$A • with quotes A="pokus hokus" B="* $A" • single quote (apostrophe) A='pokus hokus' B='* $A' • backward single quote (backward apostrophe) A=`ls –d` B=“number : `ls | wc –l`" Expands to list of files and directories in current working directory (advanced constructions can be used) Value of variable A is inserted Value of A is inserted but no expansion is done (star is in quotes) Text is saved in exact way, no variable insertion, no expansion is done. Variable contains value with 2 words separated by space To place where are backward apostrophes, command output is inserted C2110 UNIX and programming 6th lesson -20Variables and special symbols Text expansion order: Input text (string) Variable expansion intermediate Special characters expansion (wildcards) result Expansion is not done, if text is in double apostrophes "" Expansion is not done, if text is in single apostrophes '' C2110 UNIX and programming 6th lesson -21Commands for exercise more prints text from file or standard input by pages (appropriate to view long texts) less similar to more with extended functionality (for example movement to both directions in text) xargs runs command with arguments that are from standard input. Appropriate to create long argument list. grep prints lines from files or standard input that match given search PATTERN Examples: $ set | more lists existing variables and functions by pages $ cat *.txt | less prints contents of all files with extension .txt by pages $ cat directory_list.txt | xargs mkdir creates directories with names according to contents of file directory_list.txt $ grep AHOJ file.txt prints particular lines from soubor.txt, that contain text AHOJ C2110 UNIX and programming 6th lesson -22- Exercise 1. Set variable A to value 55. 2. Print value of variable A (command echo) 3. List all variables. Is there variable A (try to use command less and more)? 4. Use command grep and print line containing variable A record. Select search pattern independent on variable value. 5. Print all variables with name beginning with A (grep ^TEXT). 6. Change variable A value to "this is long string". 7. Print value of variable A. 8. Unset variable A. 9. Make sure it is unset (use procedure as in 4). 10. Set variables A, B and C as on previous page 19. Check their values by set or echo. 11. Create file directories.txt, with words pokus1, pokus2, pokus3 on separate lines. Use command xargs to create directories of same names.