Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.

Similar presentations


Presentation on theme: "Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad."— Presentation transcript:

1 Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad

2 The Shell Definitions, Reserved words, Shell Grammar Creating simple shell scripts Variables definition Shell expansion Built-in shell variables Flow control

3 Definitions blank A space or tab. word A sequence of characters considered as a single unit by the shell. Also known as a token. name A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier. metacharacterA character that, when unquoted, separates words. One of the following: | & ; ( ) space tab control operator A token that performs a control function. It is one of the following symbols: || & && ; ;; ( ) |

4 Reserved Words Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command: ! case do done elif else esac fi for function if in select then until while { }

5 Shell Grammar Simple commands Pipelines Lists Compound commands Comments Quoting Parameters Shell variables Expansion Redirection Functions Aliases Job control ||

6 Creating a Simple Shell Script A shell script is a file that contains commands to be executed by the shell. Making a File Executable – use chmod $ chmod +x scriptname Example: $ cat > whoson date echo Users Currently Logged In who Ctrl+D $ whoson bash: whoson: command not found $ chmod u+x whoson Start whoson now!$ whoson Does not work! $./whoson WHY?

7 Ways to run a shell script

8 Command Separation and Grouping Command separator - ; character $ a; b; c (a,b,c - commands) The \ character – when entering very long command line – quotes the New-line character The | and & characters $ a | b | c $ a & b & c a and b in the background Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed. | - pipeline symbol If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

9 Example 1.Create files a, b and c which echo its names $ cat > a echo “aaaaaaaaaaaaaaaaaaaaaaaa” Ctrl+D 4.Execute now: $./a&./b&./c& 2.Now we will create files b and c using tr command: $ tr 'a' 'b' b $ tr 'a' 'c' c 3.………………??? 5.Explain (comment) the output

10 Processes Process structure – parents, child and root Example: 1.$ ps –ef |more 2.$ ps -ef | grep your_user_name Discussion ….. Parent process forkes (spawns) a child, child may forks another child, etc.

11 Variables User-Created variables ( = ) $ person=alex $ echo $person Removing variables – unset or $ person= export command NO spaces around = sign!

12 The read Command $ cat read2 echo –n Enter command: read command $command echo Thanks $ cat read1 echo –n Go ahead: read firstline echo “You entered: $firstline” $ cat read3 echo -n Enter something: read word1 word2 word3 echo "Word 1 is: $word1" echo "Word 2 is: $word2" echo "Word 3 is: $word3" To accept the user input and store the input in variable – examples: Create script files read1, read2 and read3 and execute the scripts. Explain (comment) the output!

13 Expansion Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion tilde expansion parameter and variable expansion command substitution arithmetic expansion word splitting, and pathname expansion. The order of expansions is: brace expansion, tilde expansion, parameter, variable, command, and arithmetic substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

14 Tilde expansion If a word begins with a tilde character (`~'), all of the characters preceding the first slash (or all characters, if there is no slash) are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. If a `+' follows the tilde, the value of PWD replaces the tilde and `+'. If a `-' follows, the value of OLDPWD is substituted. If the value following the tilde is a valid login name, the tilde and login name are replaced with the home directory associated with that name. If the name is invalid, or the tilde expansion fails, the word is unchanged.

15 Command substitution Command substitution allows the output of a command to replace the command name. There are two forms: $(command) or `command` Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Command substitutions may be nested. To nest when using the old form, escape the inner backquotes with back slashes. If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.

16 Command substitution Example $ cat dir dir=`pwd` echo You are using the $dir directory. ls –l $dir Create script file named dir and execute it. What does the script do? Explain!

17

18

19

20

21

22

23

24

25

26

27

28 Example If you want a for loop that stores several different shell variables and makes one pass through the loop for each set of variables (instead of one pass for each item, as a regular for loop does). This loop does the job: for bunch in "ellie file16" "donna file23" "steve file34" do # PUT FIRST WORD (USER) IN $1, SECOND (FILE) IN $2... set $bunch echo ”mail $1 < $2” done

29

30 Example1: $ cat sysmgr #!/bin/sh until who | grep "vkaragiozov " do sleep 60 done echo The Instructor just logged on. $ sh sysmgr & [1] 2345...time passes... AUBGIN\vkaragiozov pts/2 2008-02-25 09:52 The Instructor just logged on.

31

32

33

34 Example: echo “Enter A, B, or C:” read letter case “$letter” in a|A) echo You entered A ;; b|B) echo You entered B ;; c|C) echo You entered C ;; esac

35


Download ppt "Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad."

Similar presentations


Ads by Google