1 / 71

SHELL PROGRAMMING

SHELL PROGRAMMING. These lecture notes are based on the lecture notes of Prof. Gimel’farb , Dr Riddle and Dr Nagra . Special thanks to them all…. Environment Variables. The shell is widely used by a variety of people with diverse range of tastes for a diverse number of tasks

cade
Download Presentation

SHELL PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SHELL PROGRAMMING These lecture notes are based on the lecture notes of Prof. Gimel’farb, Dr Riddle and Dr Nagra. Special thanks to them all…

  2. Environment Variables • The shell is • widely used • by a variety of people • with diverse range of tastes • for a diverse number of tasks • To be useful, a good shell must be highly configurable • Almost every configurable aspect of a shell is controlled with environment variables $ export VARIABLE=setting $ echo $VARIABLE setting COMPSCI 215

  3. PATH Variable • To launch an application, the shell needs to be able to find where all the applications are • Two alternatives: • Specify explicitly where the application is every time you run a program • Use the absolute or relative path names • The shell searches itself in “the right places” until it finds the program you want to run • PATH tells the shell where “the right places” are: • It contains a list of directories to search for applications • Directories in PATH are separated my colons (:) • Compare with %PATH% in MS-DOS and MS-Windows COMPSCI 215

  4. PATH Variable $ echo $PATH /bin:/sbin:/usr/bin:/usr/sbin: /usr/local/bin:/usr/local/teTeX/bin/powerpc-apple-darwin-current bin sbin usr bin sbin local bin teTeX bin powerpc-… COMPSCI 215

  5. Other Important Environment Variables HOME Contains the absolute path to your home directory $ echo $HOME /afs/ec.auckland.ac.nz/users/x/y/xye002/unixhome USER Contains your login name $ echo $USER xye002 PWD Contains your current working directory $ echo $PWD /afs/ec.auckland.ac.nz/users/x/y/xye002/unixhome PS1 Contains a specification of your prompt $ echo $PS1 $ COMPSCI 215

  6. Configuring Your Prompt If PS1 contains the following strings, the shell replaces it with the corresponding value: \h Short machine name \s The name of the shell \t Current time \u The username of the current user \w The current working directory \! The history number of this command $ export PS1='\h\s\t\u\w\!' georgy-bash10:14:49ggim001~/Desktop675 \h \s \t \u \w \! COMPSCI 215

  7. Configuring bash Setting the environment variables alters the behavior of the current session • On quitting, these configurations are lost • What we need is some automatic way of typing in our variable settings at the very beginning of each shell session • This is all a configuration file is! Configuration files: .bash_profile - Executed when the shell is the login shell .bashrc - Executed when the shell is interactive but not the login shell (an rc, or a run command script) .bash_logout - Executed when the user quits the shell COMPSCI 215

  8. Shell Programming • A “shell script” is a text file containing executable commands • A shell script is usually written to carry out tasks which may need to be repeated many times • Also called scripts, batch files, or commands files • Interpreted, not compiled! • Inside a shell script: • UNIX commands are basic statements • Shell variables can be used to capture state • Simple control flow statements provided COMPSCI 215

  9. Making Shell Scripts Executable • To do so, set the “executable” bit: chmod +x fileName.bash • Enter a “magic line” that denotes the correct shell: #!/bin/sh #!/bin/bash #!/bin/csh • The ‘#’ character must be the first character in the file. There cannot be a blank line above or a space to the left. • Either the directory that the executable is in must be in the PATH variable, or you must explicitly give the full path name when executing the script • The command echo prints its argument to the screen • Useful for printing out informative messages and debugging shell scripts • Comments are ignored by the shell • Comments begin with # and terminate at the end of the line COMPSCI 215

  10. f1 temp1 temp2 • Insert 2nd and 3rd line of file f1 after line 3 of file f2. The new file is stored in file f3 head line 1 in f1 line 2 in f1 line 3 in f1 line 4 in f1 line 5 in f1 line 1 in f1 line 2 in f1 line 3 in f1 tail line 2 in f1 line 3 in f1 temp3 f2 line 1 in f2 line 2 in f2 line 3 in f2 line 1 in f2 line 2 in f2 line 3 in f2 line 4 in f2 line 5 in f2 line 6 in f2 head temp4 tail line 4 in f2 line 5 in f2 line 6 in f2 9

  11. #!/bin/bash # Name: ex1.bash # Usage: ex1.bash # Extract lines 2 and 3 from f1 and insert the lines # between lines 3 and 4 in file f2 # extract lines 2 and 3 from f1 head -3 f1 > temp1 tail -2 temp1 > temp2 # separate the contents of f2 into two files # temp3 and temp4 # temp3 stores the first 3 lines of f2 # temp4 stores all the lines of f2 apart from the first three lines head -3 f2 > temp3 tail -n +4 f2 > temp4 10

  12. # create f3 as required cat temp3 temp2 temp4 > f3 # remove the temporary files rm temp[1-4] 11

  13. $ chmod +x ex1.bash $ ls -l total 6 -rwx--x--x 1 xye002 all 520 Feb 28 15:04 ex1.bash … $ ex1.bash $ cat f3 line 1 in f2 line 2 in f2 line 3 in f2 line 2 in f1 line 3 in f1 line 4 in f2 line 5 in f2 line 6 in f2 EXAMPLE COMPSCI 215

  14. Arguments and Parameters • Arguments are user-supplied data that follow the script name on the command line scriptname argOne argTwo argThree • Parameters are predefined memory variables which are used to hold the arguments entered by the users • Each parameter corresponds to one argument • In the above example: $1 is set to argOne; $2 is set to argTwo; and $3 is set to argThree $* is set to the concatenation of all arguments i.e. argOne argTwo argThree $# is set to the number of arguments, i.e. 3 in this example COMPSCI 215

  15. Example 2: Display the specified number of lines from the beginning of a file. The usage of the script is ex2.bash number_of_lines file_name $ cat ex2.bash #!/bin/bash # Name: ex2.bash # Usage: ex2.bash number_of_lines file_name head -$1 $2 $ ex2.bash 2 f1 line 1 in f1 line 2 in f1 EXAMPLE 14

  16. Command Substitution • Command substitution is a concept in shell. • A command substitution is represented as $(command) where command is a bash command. • $(command) means the output of executing the command. • $(ls) represent the contents of the current directory. $ ls music research teaching temp try $ echo $(ls) music research teaching temp try EXAMPLE 15

  17. Bourne Shell Variables • Shell variables are like environment variables except they are local in scope to the script • Variable names can have any alphanumeric characters and underscores but must begin with a letter • Variables do not need to be defined / declared • $ is used to access the contents of a variable • $ is NOT used to set the contents of a variable • Setting a variable: variable=value • Spaces on either side of = are not allowed!!!! Examples: count=10message="Press Enter to continue" COMPSCI 215

  18. Built-in Integer Arithmetic The POSIX standard shell provides a mechanism for performing integer arithmetic on shell variables called arithmetic expansion It allows to evaluate an arithmetic expression and substitute the result Some older shells do not support this feature The format for arithmetic expansion is (( EXPRESSION )) EXPRESSION is an arithmetic expression using shell variables and operators Valid shell variables contain numeric values (leading and trailing white space is allowed) The result of computing expression is substituted on the command line COMPSCI 215 17

  19. Built-in Integer Arithmetic EXPRESSION can contain constants, shell variables (which do not have to be preceded by dollar signs), and operators The operators, in order of decreasing precedence, are - unary minus (and plus) * / % multiplication, division, remainder + - addition, subtraction Parentheses may be used to override operator precedence $ x=1 $ y=2 $ (( x = x+y )) $ echo $x 3 EXAMPLE COMPSCI 215 18

  20. #!/bin/bash # Name: ex3.bash # Usage: ex3.bash insertion_point # extract lines 2 and 3 from f1 head -3 f1 > temp1 tail -2 temp1 > temp2 # separate the contents of f2 into two files temp3 and temp4 num1=$1 head -${num1} f2 > temp3 (( num2=num1+1 )) tail -n +${num2} f2 > temp4 # create f3 as required cat temp3 temp2 temp4 > f3 # remove the temporary files rm temp[1-4] Example 3: Modify example 1 so that the 2nd and the 3rd line of f1 are inserted after the specified line in f2. 19

  21. String / Integer Comparison Operators (1) -n STRINGSTRINGlengthis not zero -z STRINGSTRINGlengthis zero STRING1 = STRING2STRING1is identical toSTRING2 STRING1 != STRING2STRING1is not identical toSTRING2 INT1 == INT2INT1 is equal to INT2 INT1 >= INT2INT1is greater than or equal toINT2 INT1 > INT2INT1is greater thanINT2 INT1 <= INT2INT1is less than or equal toINT2 INT1 < INT2INT1is less than INT2 INT1 != INT2INT1is not equal toINT2 COMPSCI 215 20

  22. String / Integer Comparison Operators (2) • When comparing two integers, the comparison expression should be put in double parentheses. • (( a==5 )) • (( a>=5 )) • When comparing strings, the expression should be put in double square brackets. • [[ $a == “xyz” ]] • [[ -n $a ]] • Variables in the square brackets must be preceded with the “$” sign. 21

  23. Logical operators The operators, in order of decreasing precedence, are ! logical negation && logical AND || logical OR (( 6>5 && 1==1 )) [[ "a" = "b" || $a = c ]] (( ! 6<5 && ! 6<7 )) (( ! (( ! 6<5 && ! 6<7 )) )) COMPSCI 215 22

  24. Conditional Execution • When a command or process terminates, it returns an exit status or value 0 indicates success 1 - 255 indicates an error • you can retrieve the status of a command from $? immediately after the command is executed && and || perform logical lazy “and” and “or” operations: command1 && command2 • Run command2 only if command1 exits successfully, that is, if command1 returns a zero exit value command1 || command2 • Run command2 only if command1 fails, that is, if command1 returns a non-zero exit value COMPSCI 215

  25. $ echo 215; echo $? 215 0 $ cat kk; echo $? cat: kk: No such file or directory 1 $ echo 215 || echo $? 215 $ echo 215 && echo $? 215 0 $ cat kk || echo $? cat: kk: No such file or directory 1 $ cat kk && echo $? cat: kk: No such file or directory EXAMPLE COMPSCI 215

  26. File Operators $ ls -l f1 -rw-r--r-- 1 xye002 all 65 Feb 28 12:35 f1 $ [[ -x f1 ]] || echo no no EXAMPLE COMPSCI 215 25

  27. Reading User’s Input readVARLIST reads input from standard input and assigns each word to the corresponding variable in VARLIST The input is divided into words by white space(s) The first word is assigned to the first variable, the second to the second variable and so forth All the leftover words are assigned to the last variable In particular, if VARLIST contains a single variable, the entire input is assigned to that variable COMPSCI 215 26

  28. $ read a b auckland university $ echo $a auckland $ echo $b university $ read a b the university of auckland $ echo $a the $ echo $b university of auckland $ read line the university of auckland $ echo $line the university of auckland EXAMPLE 27

  29. if Statement if expression then command … else command … fi ifexpression then command … fi ifexpression then command … elifexpression then command … else command … fi 28

  30. Example 4: Assign grade according to score being provided. #!/bin/bash # Name: ex4.bash # Usage: ex4.bash echo Please enter a score: read score if (( score >= 90 )) then grade="A" elif (( score > 80 )) then grade="B" else grade="C" fi echo Grade is $grade $ ex4.bash Please enter a score: 91 Grade is A 29

  31. Example 5: String and file testing. $ ls -l total 4 drwxr-xr-x 2 xye002 all 2048 Feb 28 12:35 dic -rwx------ 1 xye002 all 322 Feb 28 22:16 ex5.bash -rw-r--r-- 1 xye002 all 0 Feb 28 12:35 file -rw-r--r-- 1 xye002 all 21 Feb 28 12:35 readme $ ex5.bash Please enter a string: readme This is readme file. $ ex5.bash Please enter a string: dic dic is a directory. $ ex5.bash Please enter a string: file file is a readable file. … 30

  32. #!/bin/bash # Name: ex5.bash # Usage: ex5.bash echo "Please enter a string:" read string if [[ $string == "readme" ]] then cat readme elif [[ -d $string ]] then echo $string is a directory. elif [[ -f $string && -r $string ]] then echo $string is a readable file. else echo string=$string fi 31

  33. case Statement Allows you to compare a single value against other values and execute commands when a match is found case EXPRESSION in PATTERN1) COMMANDS ;; PATTERN2) COMMANDS ;; … PATTERNn) COMMANDS ;; esac PATTERNi is any shell style wildcard pattern that EXPRESSION is matched against COMPSCI 215 32

  34. case Statement Special pattern matching characters: ?specifies any single character * specifies zero or more occurrences of any character Because the pattern * matches anything, it is frequently used at the end of the case as the “catchall” value (it is guaranteed to match if none of the previous values in the case match) […] specifies any single character enclosed between the brackets Example case "$1" in quit) exit ;; list) ls -l ;; *) echo $1 is not recognised ;; esac COMPSCI 215 33

  35. The while Command • while EXPRESSION • do COMMANDS • done • The command loops while EXPRESSION is true COMPSCI 215 34

  36. Example 6: Enter a sequence of numbers from the keyboard and calculate the sum of the numbers. Each number is separated by an “return” character. The sequence is ended with ctrl/d (end of file). #!/bin/bash # Name: ex6.bash # Usage: ex6.bash sum=0 while read num do (( sum=sum+num )) done echo sum=$sum $ ex6.bash 1 2 3 sum=6 35

  37. Example 7: Rewrite example 6, so that the numbers are read from a file “f”. In “f”, each number is stored as a row. Here, we need to use input redirection. #!/bin/bash # Name: ex7.bash # Usage: ex7.bash sum=0 while read num do (( sum=sum+num )) done < f echo sum=$sum $ cat f 1 2 3 $ ex7.bash sum=6 36

  38. The expr Command • The expr command can be used to extract a portion of the string from a given string. • The general format of the expr command is: $(expr "string" : "exp1\(exp2\)exp3") $(expr "string" : 'exp1\(exp2\)exp3') • string is the string from which the substring is to be extracted • exp2 is the regular express specifying the substring to be extracted • exp1 is the regular expression which matches the string which precedes the substring to be extracted. • exp3 is the regular expression describing the string that follows the substring that will be extracted 37

  39. exp1 exp3 exp2 double quote vs single quote $ a=123 $ echo $(expr "abc123456" : "[a-z]*\($a\).*") 123 $ echo $(expr "abc123456" : '[a-z]*\($a\).*') EXAMPLE 38

  40. Example 8: Extracting email address from web page. <tr id="g__ctl0_r" class="r1"> <td><a href="http://www.che.auckland.ac.nz/staff/staff.aspx?staffid=135">Abhyankar, Ms Kalyani</a></td> <td>09-373-7599 ext 88318</td> <td>Level 5, Room 506</td> <td><a href=“mailto:k.abhyankar@auckland.ac.nz”>k.abhyankar@auckland.ac.nz</a></td> <td>Adminstrative Assistant</td> </tr> 39

  41. A line of the html file corresponding to the above web page is as below: <tr id="g__ctl0_r" class="r1"> <td><a href="http://www.che.auckland.ac.nz/staff/staff.aspx?staffid=135">Abhyankar, Ms Kalyani</a></td> <td>09-373-7599 ext 88318</td> <td>Level 5, Room 506</td> <td><a href=“mailto:k.abhyankar@auckland.ac.nz”>k.abhyankar@auckland.ac.nz</a></td> <td>Adminstrative Assistant</td> </tr> The command below is used to retrieve the email address. $(expr "$line" : '.*>\([a-z.]*@auckland[.]ac[.]nz\).*') exp2 exp1 exp3 40

  42. #/bin/bash # Name: ex8.bash # Usage: ex8.bash while read line do email=$(expr "$line" : '.*>\([a-z.]*@auckland[.]ac[.]nz\).*') echo $email done <Chemistry.html $ ex8.bash k.abhyankar@auckland.ac.nz r.anderson@auckland.ac.nz empty lines 41

  43. In bash, the length of a string stored in a variable, say var, can be obtained by ${#var} • To avoid empty line, we can check whether email is empty before print out the value of email #!/bin/bash # Name: ex8v2.bash # Usage: ex8v2.bash while read line do email=$(expr "$line" : '.*>\([a-z.]*@auckland[.]ac[.]nz\).*') if (( ${#email} != 0 )) then echo $email fi done <Chemistry.html 42

  44. In the next version of this example, • we store the email addresses in file emails • we also want to assign a number to each of the email address found. • For example, file emails should look like below: 1 : k.abhyankar@auckland.ac.nz 2 : r.anderson@auckland.ac.nz 3 : c.arewgoda@auckland.ac.nz 43

  45. #!/bin/bash # Name: ex8v3.bash # Usage: ex8v3.bash rm emails 2>/dev/null num=1 while read line do email=$(expr "$line" : '.*>\([a-z.]*@auckland[.]ac[.]nz\).*') if (( ${#email} != 0 )) then echo ${num} : $email >> emails (( num=num+1 )) fi done <Chemistry.html 44

  46. $ ex9.bash a b c EXAMPLE Setting Positional Parameters • Normally, the positional parameters are used to store the values of the arguments. However, they can also be set with the set command. #!/bin/bash # Name: ex9.bash # Usage: ex9.bash set a b c echo $1 $2 $3 45

  47. Example 10: Write a script which counts the number of words in each line of file f1. A word consists of letters only. Words are separated by one or several spaces. $ cat f1 This is line one. second line $ ex10.bash 4 2 EXAMPLE 46

  48. #!/bin/bash # Name: ex10.bash # Usage: ex10.bash while read line do rm temp 2>/dev/null echo $line | tr -cs "a-zA-Z" "\n" >> temp set $(wc -l temp) echo $1 done < f1 rm temp 47

  49. An alternative solution is to use command substitution and two pipes #!/bin/bash # Name: ex10b.bash # Usage: ex10b.bash while read line do echo $(echo $line | tr -cs "a-zA-Z" "\n" | wc -l) done < f1 48

  50. The until Command The while command continues execution as long as the command listed after the while returns a zero exit status The until command is similar but it continues execution as long as the command that follows the until returns a nonzero exit status As soon as a zero exit status is returned, the loop is terminated The general format of the command: untilcommandt do commandLike the while, the commands between the do commandand done might never be executed if commandt …returns a zero exit status the first time it is executed done COMPSCI 215 49

More Related