C2110 UNIX and programming Lesson 6 -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 6 PS / 2020 Distance form of teaching: Rev2 C2110 UNIX and programming Lesson 6 -2Summary L5 ➢ Bash C2110 UNIX and programming Lesson 6 -3Interactive Mode [kulhanek@wolf ~]$ username computer name current directory (~ means home directory /home/username) Prompt - type of user / prompt ($ regular user, # super user, other possible %, >) place for command The command is executed by pressing Enter key. History: Use the up and down arrow keys to scroll through the list of commands you have already entered. The history command can be reused or modified and the modified one can be used. The history is also accessible by command history. Autocomplete: By pressing Tab key, the command line interpreter tries to complete the spelled word. Command names, paths and file names are added (if one press does not cause anything, there are more options to add, repeated press will display them). Shell interprets (expands) wild characters and other special characters, before the actual execution of the command. In interactive mode it is possible to run control structures of bash language. Interactive mode is terminated by a command exit. C2110 UNIX and programming Lesson 6 -4Bash Script #!/bin/bash # this is a comment echo 'This is a script in Bash interpreter!' echo "Content of directory `pwd` is:" ls # prints contents of directory A=6 # sets value of variable A echo "Value of variable A is $A" echo "first command"; echo "second command" ./mujprikaz first_argument second_argument \ third_argument orderofcommandexecution • blank lines are ignored • text preceded by # character is ignored (used to comment script functionality) • multiple commands can be specified per line, commands are separated by a semicolon ; • one command can be written on multiple lines using a backslash \ immediately follows new line C2110 UNIX and programming Lesson 6 -5Non-interactive Mode - Scripts 1) Indirect start We run the language interpreter and give the name of the script as an argument. $ bash my_bash_script Scripts do not have to have the x flag set (executable). 2) Direct start We run the script directly (shell automatically starts the interpreter). $ chmod u+x my_bash_script $./my_bash_script Scripts must have x flag (executable) and interpreter set (part of the script). #!/bin/bash echo 'This is a script in Bash interpreter!' C2110 UNIX and programming Lesson 6 -6Next Subject ➢ Bash C2110 UNIX and programming Lesson 6 -7- Content Bash ➢ Variables • setting and obtaining values, interpretation of strings • integer operations • variables and processes ➢ Input and output • read, echo, printf • script arguments