Shell Programming—an Introduction Copyright Conditions: Open Publication License (see http://www.opencontent.org/openpub/) Nick Urbanik nicku@vtc.edu.hk Department of Information and Communications Technology Intro Aim . . . . . . . . . . . . . . . . . . . . Why Shell Scripting? . . . . . . . . Where to get more information . The Shell is an Interpreter . . . . The Shebang . . . . . . . . . . . . . . Making Executable . . . . . . . . . Quoting and Funny Chars Special Characters . . . . . . . . . . Special Chars—2 . . . . . . . . . . . Special Chars—3 . . . . . . . . . . . Quoting . . . . . . . . . . . . . . . . . Quoting—2 . . . . . . . . . . . . . . . Quoting—When to use it? . . . . Variables True and False . . . . . . . . . . . . Variables—1 . . . . . . . . . . . . . . Variables—Assignments . . . . . . Variables—Local to Script . . . . Variables—Unsetting Them . . . Command-line Parameters . . . . Special Built-in Variables . . . . . Variables: use Braces {...} . . After 9 . . . . . . . . . . . . . . . . . More about Quoting . . . . . . . . Command Substitution Command Substitution . . . . . . Example of Cmd Subst . . . . . . Conditions Comparing Strings. . . . . . . . . . Comparing Integers . . . . . . . . . File Tests & NOT . . . . . . . . . . AND OR Conditions . . . . . . . . Arithmetic Arithmetic Assignments . . . . . . ((...)) . . . . . . . . . . . . . . . . ((...)) . . . . . . . . . . . . . . . . .       . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . slide slide slide slide slide slide 2 3 4 5 6 7 . slide 8 . slide 9 slide 10 slide 11 slide 12 slide 13 slide slide slide slide slide slide slide slide slide slide 14 15 16 17 18 19 20 21 22 23 . . . . . . . . . . . . . . . . . . . . slide 24 . . . . . . . . . . . . . . . . . . . . slide 25 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . slide slide slide slide 26 27 28 29 . . . . . . . . . . . . . . . . . . . . slide 30 . . . . . . . . . . . . . . . . . . . . slide 31 . . . . . . . . . . . . . . . . . . . . slide 32 Statements if Statement. . . . . . . . . . . . . while Statement . . . . . . . . . . for Statement. . . . . . . . . . . . for Loops: Another Example . for (( ; ; )) . . . . . . . . . . . break and continue . . . . . . . Blocks: {...} . . . . . . . . . . . . Flow Control: || && . . . . . . . . Input & Output Output: echo and printf . . . Input: the read Command . . . Split with set . . . . . . . . . . . . More about set, and IFS . . . . Example: Changing IFS . . . . . Command-line Parameters case Statement . . . . . . . . . . . case Statement: Example . . . shift Up . . . . . . . . . . . . . . . shift: Many Places. . . . . . . . Command-Line Options—1 . . Command-Line Options—2 . . Command-Line Options—3 . . getopts—4. . . . . . . . . . . . . . getopts—5. . . . . . . . . . . . . . Temporary Files, Signals Temporary Files: mktemp . . . . Signals that Kill . . . . . . . . . . Signals: trap . . . . . . . . . . . . Signals: trap Example . . . . . . Functions Functions . . . . . . . . . . . . . . . Parameters in Functions. . . . . Example, Calling a Function. . Debugging Debugging Shell Scripts—1. . . Debugging Shell Scripts—2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 Writing Shell Scripts . . . . . . Useful External Programs—1 Useful External Programs—2 Regular Expressions Regular Expressions. . . . . . . What is In a RegEx? . . . . . . Literal characters. . . . . . . . . Character Classes: [...] . . . [^...] . . . . . . . . . . . . . . . . Match Any Character . . . . . Match Start or End . . . . . . . Repetitions . . . . . . . . . . . . . Matching Alternatives: “|” . . Examples . . . . . . . . . . . . . . awk and sed Basic awk . . . . . . . . . . . . . . What Does awk Do?. . . . . . . awk Examples . . . . . . . . . . . sed—the Stream Editor . . . . sed—Backreferencees . . . . . . Backrefs Example . . . . . . . . find find Examples . . . . . . . . . . Finding SUID Programs . . . . Long find Example . . . . . . . rpm Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . slide 64 . . . . . . . . . . . . . . . . . . . . . . slide 65 . . . . . . . . . . . . . . . . . . . . . . slide 66 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide slide 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 . . . . . . . . . . . . . . . . . . . . . slide 59 . . . . . . . . . . . . . . . . . . . . . slide 60 . . . . . . . . . . . . . . . . . . . . . slide 61 . . . . . . . . . . . . . . . . . . . . . slide 62 . . . . . . . . . . . . . . . . . . . . . slide 63 Aim After successfully working through this exercise, You will: write simple shell scripts using for, if, while, case, getopts statements; write shell script functions, and be able to handle parameters; understand basic regular expressions, and be able to create your own regular expressions; understand how to execute and debug these scripts; understand some simple shell scripts written by others. Shell Programming — slide 2 ¡ Where to get more information the Libarary has two copies of the book, Learning the Bash Shell, second edition, by Cameron Newham & Bill Rosenblatt, O’Reilly, 1998. There is a free on-line book about shell programming at: http://tldp.org/LDP/abs/html/index.html and http://tldp.org/LDP/abs/abs-guide.pdf. It has hundreds of pages, and is packed with examples. The handy reference to shell programming is: $ or $ ¢ ©¨ © ¥     £   ¥¤ §¦ ©¨  £  ¢ ¢ ¢ OSSI — ver. 1.12 ¡ ¡ ¡ ¡ IMPORTANT: $ bash provides simple on-line help for all built-in commands, e.g., OSSI — ver. 1.12 Shell Programming — slide 4 Why Shell Scripting? Basic startup, shutdown of Linux, Unix systems uses large number of shell scripts ◦ understanding shell scripting important to understand and perhaps modify behaviour of system Very high level: powerful script can be very short Can build, test script incrementally Useful on the command line: “one liners” Shell Programming — slide 3 ¢ ¢ The Shell is an Interpreter Some languages are compiled: C, C++, Java,. . . Some languages are interpreted: Java bytecode, Shell Shell is an interpreter: kernel does not run shell program directly: ◦ kernel runs the shell program /bin/sh with script file name as a parameter ◦ the kernel cannot execute the shell script directly, as it can a binary executable file that results from compiling a C program OSSI — ver. 1.12 Shell Programming — slide 5 ¢ ¢ ¢ OSSI — ver. 1.12 ¢ ¢ The Shebang You ask the Linux kernel to execute the shell script kernel reads first two characters of the executable file ◦ If first 2 chars are “#!” then ◦ kernel executes the name that follows, with the file name of the script as a parameter Example: a file called find.sh has this as the first line: #! /bin/sh then kernel executes this: /bin/sh find.sh What will happen in each case if an executable file begins with: ◦ #! /bin/rm ◦ #! /bin/ls OSSI — ver. 1.12 Shell Programming — slide 6 ¢ ¢ ¢ ¢ Making the script executable To easily execute a script, it should: be on the PATH have execute permission. ¢ ¢ ¢ ¢ How to do each of these? Red Hat Linux by default, includes the directory ∼/bin on the PATH, so create this directory, and put your scripts there: $ ¢ ∼ ¤ If your script is called script, then this command will make it executable: $   ¤ § !"   £  OSSI — ver. 1.12 ¨ ¥¤    Shell Programming — slide 7 Special Characters Many characters are special to the shell, and have a particular meaning to the shell. Meaning Home directory Command substitution. Better: (...) Comment Variable expression Background Job File name matching wildcard Pipe $ # # # ¢ Character ∼ ‘ # $ See slide 7 24 & * | 15 2.10 on page 43 2.18 on page 51 2.9 on page 42 OSSI — ver. 1.12 Shell Programming — slide 8 Special Characters—continued: 2 Character ( ) [ ] { ; \ ’ " Meaning Start subshell End subshell Start character set file name matching End character set file name matching Start command block Command separator Quote next character Strong quote Weak quote # # Quoting See slide 45, 17, 39 45, 17, 39 2.9 on page 42 2.9 on page 42 39 40 23 23 23 ¢ ¢ ¢ Sometimes you want to use a special character literally; i.e., without its special meaning. Called quoting Suppose you want to print the string: 2 * 3 > 5 is a valid inequality? If you did this: $ ¤ ¤ ¥¤ § & ( 0  )  4 23  '  © 1©  © ¤ OSSI — ver. 1.12 Shell Programming — slide 9 # # # # # ¢ the new file ‘5’ is created, containing the character ‘2’, then the names of all the files in the current directory, then the string “3 is a valid inequality”. OSSI — ver. 1.12 Shell Programming — slide 11 Quoting—2 To make it work, you need to protect the special characters ‘*’ and ‘>’ from the shell by quoting them. There are three methods of quoting: ◦ Using double quotes (“weak quotes”) Character < > / ? ! space or tab % ¢ Special Characters—continued: 3 Meaning Input redirect Output redirect Pathname directory separator Single-character match in filenames Pipline logical NOT shell normally splits at white space See slide 2.7 on page 40 2.6 on page 39 2.18 on page 51 28 44 ¢ ◦ Using single quotes (“strong quotes”) ◦ Using a backslash in front of each special character you want to quote This example shows all three: $ $ $ 5 ¤ ¤ ¥¤ ¤ § & ( 0  )  ¤ ¤ ¥¤ § 6 & ( 0   ¤ ) 7 7 ¤ ¤ ¥¤ § & ( 0  )  4 23 ©  '  © ©1  5 # # Note that references to pages in the tables above refer to the modules in the workshop notes OSSI — ver. 1.12 Shell Programming — slide 10 OSSI — ver. 1.12 Shell Programming — slide 12 4 23 ©  '  © 1©  ¤ 4 23 ©  '  © ©1  6 Quoting—When to use it? Use quoting when you want to pass special characters to another program. Examples of programs that often use special characters: ◦ find, locate, grep, expr, sed and echo Here are examples where quoting is required for the program to work properly: $ $ $ $ ¦ ¥¤  7 9¥ ©  ' 8 8 £A @ ¢ ¢ Variables—1 Variables not declared; they just appear when assigned to Assignment: ◦ no dollar sign ◦ no space around equals sign ◦ examples: $ $ ¢ ¢ ¢ ¢ # correct # wrong: try to execute program called ‘‘x’’ " C FG C   FG 3  ¨ ¥¤ § 6   6 Read value of variable: ◦ put a ‘ ’ in front of variable name ©  ¥¤ 6 ' 6 B A   £ © 8 B D 7 C¤ ¤ " OSSI — ver. 1.12 £ ' 0 E 8 ' ' Shell Programming — slide 13 ◦ example: $ "D H5 ¤ §  3  1©  § " ¦ " OSSI — ver. 1.12  Shell Programming — slide 15 True and False Shell programs depend on executing external programs When any external program execution is successful, the exit status is zero, 0 An error results in a non-zero error code To match this, in shell programming: ◦ The value 0 is true ◦ any non-zero value is false This is opposite from other programming languages Shell Programming — slide 14 OSSI — ver. 1.12 ¢ ¢ Variables—Assignments You can put multiple assignments on one line: i=0 j=10 k=100 You can set a variable temporarily while executing a program: $ emacsclient $ $ emacsclient § DI PQ  I PQ H RS C A  § DI PQ H RS ¢ ¢ ¢ ¢ ¢  ¤  ©  § ¥ ¨ H OSSI — ver. 1.12  RS 9 Shell Programming — slide 16 5 Variables—Local to Script Variables disappear after a script finishes Variables created in a sub shell disappear ◦ parent shell cannot read variables in a sub shell ◦ example: $ #! /bin/sh echo $HOME HOME=happy echo $HOME $ /home/nicku happy $ /home/nicku ©¤ ©  1©   ©¤ 1©  ¨ 8 § DT  RU  ¨  ¢ Command-line Parameters Command-line parameters are called 0, 1, 2, . . .     ¢ Example: when call a shell script called “shell-script” like this: $    ¤ F & (    £  £©  ©  £©  ©  £©  ©  £©  © 9  ` ¢ ¢ variable 0 1 2 3 4 #     value shell-script param1 param2 param3 param4 number of parameters to the program, e.g., 4   I         ◦ Note: these variables are read-only. Shell Programming — slide 17 OSSI — ver. 1.12 Shell Programming — slide 19 OSSI — ver. 1.12 Variables—Unsetting Them You can make a variable hold the null string by assigning it to nothing, but it does not disappear totally: $ $ VAR= VW  ¢ C S ¢ X 6Y VW S  A   You can make it disappear totally using $ $ 3 ¥   VW S £ 6 : 3 ¥ X 6Y VW S   A   OSSI — ver. 1.12 £ 6 Shell Programming — slide 18     Special Built-in Variables Both @ and * are a list of all the parameters.     ¢ Braces and Parameters after 9 Need braces to access parameters after 9: $ #! /bin/sh echo $10 echo ${10} $ a0 j  © £©  ©  £©  ©  8 ¢   ¥   ¢ The only difference between them is when they are quoted in quotes—see manual page for bash ? is exit status of last command is the process ID of the current shell   ¢ ¢     ¤ ¨  ¦ ¥ ©  A    h Example shell script: #! /bin/sh echo $0 is the full name of this shell script echo first parameter is $1 echo first parameter is $2 echo first parameter is $3 echo total number of parameters is $# echo process ID is $$ ¢ Notice that 10 is the same as {1}0, i.e., the first parameter “a” then the literal character zero “0”   OSSI — ver. 1.12  ¢ OSSI — ver. 1.12 Shell Programming — slide 20 More about Quoting Double quotes: "..." stop the special behaviour of all special characters, except for: ◦ variable interpretation ( ) ◦ backticks (‘) — see slide 24 ◦ the backslash (\) Single quotes ’...’: ◦ stop the special behaviour of all special characters ¢   ¢ Variables: use Braces { a ¢ } b It’s good to put braces round a variable name when getting its value Then no problem to join its value with other text: $ $ 123  F  C  & ( ¢ b b { } ©¤ D ¤ ¦ ¥¤  ¨ ` 0f      3 ¥    g Backslash: ◦ preserves literal behaviour of character, except for newline; see slides 29, 31, 35 i i i § D   c §d A § §  $ § D e     ` 1© 0f   ¢ $ 123456 §  { } ◦ Putting “\” at the end of the line lets you continue a long line on more than one physical line, but the shell will treat it as if it were all on one line. Shell Programming — slide 21 OSSI — ver. 1.12 Shell Programming — slide 23 D  OSSI — ver. 1.12   ` 0f @ Shell Programming — slide 22 p Command Substitution — ¢   q or Conditions—String Comparisons All programming languages depend on conditions for if statements and for while loops Shell programming uses a built-in command which is either test or [...] Examples of string comparisons: [ [ [ [ [ "$USER" = root ] # true if the value of $USER is "root" "$USER" != root ] # true if the value of $USER is not "root" -z "$USER" ] # true if the string "$USER" has zero length string1 \< string2 ] # true if string1 sorts less than string2 string1 \> string2 ] # true if string1 sorts greater than string2 ¢ ¢ ¢ ¢ ¢ ¢ a r b b b b Enclose command in (...) or backticks:‘...‘ Means, “Execute the command in the (...) and put the output back here.” Here is an example using $ 5 $ $ $ (  " £ ! &   ¢ :  " ¢ # error: try execute command ‘3’ # correct # also correct Shell Programming — slide 24 C¤ (  " B D £ ! & C¤ ( " s £ ! C¤ ( " OSSI — ver. 1.12 £ ! & s & E £ b b r Note that we need to quote the ‘>’ and the ‘<’ to avoid interpreting them as file redirection. Note: the spaces after the “[“ and before the “]” are essential. Also spaces are essential around operators Shell Programming — slide 26 Command Substitution—Example We want to put the output of the command hostname into a variable: $ nickpc.tyict.vtc.edu.hk $ $ hostname § ¥  © C § ¥  ©   §   D ¢ OSSI — ver. 1.12 Conditions—Integer Comparisons Examples of numeric integer comparisons: [ [ [ [ [ [ ¢ ¢ Oh dear, we only stored the name of the command, not the output of the command! Command substitution solves the problem: D B ¢ $ $ nickpc.tyict.vtc.edu.hk C § ¥  © §   D  "$x" "$x" "$x" "$x" "$x" "$x" -eq -ne -lt -gt -le -ge 5 5 5 5 5 5 ] ] ] ] ] ] # # # # # # true true true true true true if if if if if if the value of $x is 5 integer $x is not 5 integer $x is < 5 integer $x is > 5 integer $x is ≤ 5 integer $x is ≥ 5 ¢ E We put (...) around the command. You can then assign the output of the command.   Note again that the spaces after the “[“ and before the “]” are essential. Also spaces are essential around operators Shell Programming — slide 27 ¢ OSSI — ver. 1.12 Shell Programming — slide 25 OSSI — ver. 1.12 ¢ Conditions—File Tests, NOT Operator The shell provides many tests of information about files. Do man test to see the complete list. Some examples: € ¢ Arithmetic Assignments Can do with the external program % y …’ ◦ . . . but expr is not so easy to use, although it is very standard and portable: see man expr ◦ Easier is to use the built in – see help let command x y  $ $ $ $ $ $ $ $ $ t ¢ ¢ # # # # # # # # true true true true true true true true ‘ € if file is an ordinary file if file is NOT an ordinary file if file is a directory if file has SUID permission if file has SGID permission if file exists and is executable if file exists and is readable if file exists and is writeable # true if file1 is newer than file2 xw v v u y ◦ Examples: t xw  v v t u xw ‚ v u y € y € Note again: the spaces after the “[“ and before the “]” are essential. Also spaces are essential around operators Shell Programming — slide 28 $ $ $ $ $ $ $ $ ”“ • t ƒ xw v u y € –— d ˜™ # Now x is 6 ˜ ™ d ™ d e e ”“ ™ • ™ t „ xw v u y € ”“ • t … xw v u y € ”“ t € • † xw v u y – e –— — e ˜ – j # # # # † now now now now x x x x is is is is 25 17 22 27; NOTE NO $ f ”“ i t € xw v • ™ ‡ t xw u y  xw v ˆ v f –— k h i • ™ f – — • – f — ™ l f • – — – ™ l f k l f u ◦ Notice that you do not need to quote the special characters with let. ◦ Quote if you want to use white space. ◦ Do not put a dollar in front of variable, even on right side of assignment; see last example. OSSI — ver. 1.12 Shell Programming — slide 30 OSSI — ver. 1.12 Conditions—Combining Comparisons Examples of combining comparisons with AND: -a and OR: -o, and grouping with \(...\) # true if the value of $x is 5 AND $USER is not equal to root: [ "$x" -eq 5 -a "$USER" != root ] # true if the value of $x is 5 OR $USER is not equal to root: [ "$x" -eq 5 -o "$USER" != root ] # true if ( the value of $x is 5 OR $USER is not equal to root ) AND # ( $y > 7 OR $HOME has the value happy ) [ \( "$x" -eq 5 -o "$USER" != root \) -a \ \( "$y" -gt 7 -o "$HOME" = happy \) ] ¢ ¢ ”“ ¢ ”“ ”“ y ‰ y l f gh p p Arithmetic Expressions with ¢ a b The shell interprets anything inside ((...)) as an arithmetic expression You could calculate the number of days left in the year like this: $ § H5     ©   7   ¢ D B B B B D m E E  E E b o  @ (f 0 n  © ! Note again that the spaces after the “[“ and before the “]” are essential. Do man test to see the information about all the operators. Shell Programming — slide 29 ¢ ¢ ¤ 9  ¦   P ¨     No dollar sign in front of variables in these arithmetic expressions. Shell Programming — slide 31 OSSI — ver. 1.12 ¢ OSSI — ver. 1.12  ( F 5   7 b q q Arithmetic Conditions with ¢ Statement Syntax: if test-commands then statements-if-test-commands-1-true elif test-commands-2 then statements-if-test-commands-2-true else statements-if-all-test-commands-false fi ¢ ¢ p p q q b A (less portable) alternative to the arithmetic conditions in slide 27 is putting the expression in ((...)) So you can do (( (3>2) && (4<=1) )) instead of [ \( 3 -gt 2 \) -a \( 4 -le 1 \) ] Operators that work with let, ((...)) and ((...)) include:   ¢ ¢ b b qp Example: if grep nick /etc/passwd > /dev/null 2>&1 then echo Nick has a local account here else echo Nick has no local account here fi ++ -- ** + - * / % << >> & | ~ ! ^ < > <= >= == != ?: which have exactly the same effect as in the C programming language ◦ except exponentiation operator **, i.e., echo prints the value of 220 , i.e., 1048576 ◦ For details, see $    OSSI — ver. 1.12 Shell Programming — slide 33 ((2**20)) OSSI — ver. 1.12 £  Shell Programming — slide 32   Statement Loops: Another Example Here the shell turns *.txt into a list of file names ending in “.txt”: for i in *.txt do echo $i grep ’lost treasure’ $i done You can leave the in words out; in that case, name is set to each parameter in turn: i=0 for parameter do let ’i = i + 1’ echo "parameter $i is $parameter" done ¡ ¡ tp s r u q Syntax: while test-commands do loop-body-statements done ¢ Example: i=0 while [ "$i" -lt 10 ] do echo -n "$i " # -n suppresses newline. let "i = i + 1" # i=$(expr $i + 1) also works done OSSI — ver. 1.12 ¢ Shell Programming — slide 34 OSSI — ver. 1.12 vw Shell Programming — slide 36 Statement Syntax: for name in words do loop-body-statements done Example: for planet in Mercury Venus Earth Mars \ Jupiter Saturn Uranus Neptune Pluto do echo $planet done ◦ The backslash “\” quotes the newline. It’s just a way of folding a long line in a shell script over two or more lines. ¢ ¢ q vw Loops: second, C-like syntax There is a second (less frequently used, and less portable) C-like loop syntax:  ¢ §¦ q vw for (( expr1 ; expr2 ; expr3 do loop-body-statements done ¢ )) Rules: same as for arithmetic conditions—see slide 32 Example: for (( i = 0; i < 10; ++i )) do echo $i done OSSI — ver. 1.12 Shell Programming — slide 35 OSSI — ver. 1.12 ¢ Shell Programming — slide 37 and Error Handling: ||, ¢ and } |p wx z   uy { v| ~ u Use inside a loop Work like they do in C terminates the innermost loop; execution goes on after the ¨ Suppose we want the user to provide exactly two parameters, and exit otherwise A common method of handling this is something like: [ $# -eq 2 ] || { echo "Need two parameters"; exit 1; } ¢ ¢ ¢ loop will skip the rest of the body of the loop, and resume execution on the next itteration of the loop.  ¥¤ ¥ 3  ¢ § ¢  ©  ¢ Read this as “the number of parameters is two OR exit” Works because this logical OR uses short-circuit Boolean evaluation; the second statement is executed only if the first fails (is false) Logical AND “&&” can be used in the same way; the second statement will be executed only if the first is successful (true) A note about blocks: must have semicolon “;” or newline at end of last statement before closing brace Shell Programming — slide 40 OSSI — ver. 1.12 Shell Programming — slide 38 OSSI — ver. 1.12 Output: Blocks: { ¢ ¢ ¢ ¢ and |p s u { v } ¢ ¢ To perform output, use echo, or for more formatting, Use echo -n to print no newline at end. Just echo by itself prints a newline w }q u€ } p . ¥¤ £ b A subshell is one way of grouping commands together, but it starts a new process, and any variable changes are localised An alternative is to group commands into a block, enclosing a set of commands in braces: {...} Useful for grouping commands for file input or output ◦ . . . though variables are not localised See next slide for another application. Shell Programming — slide 39 b b ¢ printf works the same as in the C programming language, except no parentheses or commas: m 7 ¥ ƒ7 D ¢ ¢ ¢ $ ¢ ¥¤ 5 5 ¥¤ ¦ Ff  D m‚ „¥ Do man printf (or look it up in the bash manual page) to read all about it. Shell Programming — slide 41 OSSI — ver. 1.12 ¢ OSSI — ver. 1.12 „ ¨    £  4  A 4 3    ¦ Input: the ¢ Command   ©  : Splitting a Multi-Word Variable Sometimes may want to split a multi-word variable into single-word variables read won’t work like this: MY_FILE_INFO=$(ls -lR | grep $file) # ... echo $MY_FILE_INFO | read perms links \ user group size month day time filename Use the builtin command set instead: MY_FILE_INFO=$(ls -lR | grep $file) # ... set $MY_FILE_INFO perms=$1 links=$2 user=$3 group=$4 size=$5 month=$6 day=$7 time=$8 filename=$9 … w uy For input, use the built-in shell command † u read reads standard input and puts the result into one or more variables If use one variable, variable holds the whole line Syntax: read var1 ... Often used with a while loop like this: while read var1 var2 do # do something with $var1 and $var2 done Loop terminates when reach end of file To prompt and read a value from a user, you could do: while [ -z "$value" ]; do echo -n "Enter a value: " read value done # Now do something with $value ¢ ¢ ¢ ¢ OSSI — ver. 1.12 ¢ ¢ ¢ } Shell Programming — slide 43 ¢ ¢ More about ¢ , and ‡ˆ   † set splits its arguments into pieces (usually) at whitespace It sets the first value as 1, the second as 2, and so on. Note that you can change how set and the shell splits things up by changing the value of a special variable called IFS IFS stands for Internal Field Separator Normally the value of IFS is the string “ space tab newline ” Next slide shows how changing IFS to a colon let us easily split the PATH into separate directories: next slide Shell Programming — slide 44   OSSI — ver. 1.12 Shell Programming — slide 42 ¢ OSSI — ver. 1.12 ¢ ¢ ¢ ¢ u } ‰ Example: Changing ¢ Statement: Example ‡ˆ ‰ {y Notice that here, I make the change to IFS in a subshell. I have simply typed the loop at the prompt. As I said in slide 17, changes in a subshell are local to the subshell: Œ‹ Ž  yŠ ‘ † ¢ ¢ $ /usr/bin:/bin:/usr/X11R6/bin:/home/nicku/bin $ > > > > > /usr/bin /bin /usr/X11R6/bin /home/nicku/bin OSSI — ver. 1.12 Shell Programming — slide 45 ’“ ”• w –— Œv ‚ w Ž  † † Œ‚  ‰ Œ‹ yŠ ‚ Œ‚ ˜ ‰ y † w ‘ case $filename in *.tif) tifftopnm $filename > $ppmfile ;; *.jpg) tjpeg $filename > $ppmfile ;; *) echo -n "Sorry, cannot handle this " echo "graphics format" ;; esac OSSI — ver. 1.12 Shell Programming — slide 47 Statement {y † ¢ ¢ ¢ OSSI — ver. 1.12 u Similar to the switch statement in C, but more useful and more general Uses pattern matching against a string to decide on an action to take Syntax: case expression in pattern1 ) statements ;; pattern2 ) statements ;; ... esac Shell Programming — slide 46 u This example code runs the appropriate program on a graphics file, depending on the file extension, to convert the file to another format: : Move all Parameters Up statement is made for this : Many Places ◦ If before, we have four parameters: parameter value one two parameter 3 4     qp } qp s † ¢ s † Sometimes we want to process command-line parameters in a loop The    ¦ ¤ You can give a number argument to shift: ¢ ¢ Say that we have four parameters: parameter 1 2     ¢ } value three four value one two parameter 3 4     value three four 1 2 ◦ After executing the statement: $  ¦   ¤ & Then after executing the shift statement, the values are now: parameter 1 2     ¢ value two three parameter 3 4     value four no longer exists Shell Programming — slide 48     we have two parameters left: parameter   value three four parameter     value no longer exists no longer exists OSSI — ver. 1.12 1 2 3 4 OSSI — ver. 1.12   Shell Programming — slide 49 Command-Line Options—1 Sometimes we want to modify the behaviour of a shell script ◦ For example, want an option to show more information on request ◦ could use an option “-v” (for “verbose”) to tell the shell script that we want it to tell us more information about what it is doing ◦ If script is called showme, then we could use our -v option like this: $ §  o  9 1 ¢ ◦ the script then shows more information. OSSI — ver. 1.12 Shell Programming — slide 50 Command-Line Options—2 For example, We might provide an option to give a starting point for a script to search for suid programs Could make the option -d directory If script is called findsuid, could call it like this: 3 ¢ : Command-Line Options—4 } ™ u v ¢  Problems with above solution: inflexibility: ◦ Does not allow options to be “bundled” together like -abc instead of -a -b -c ◦ Requires a space between option and its argument, i.e., doesn’t let you do -d/etc as well as -d /etc ◦ Better method: use the built-in command : § A  £ ¢ ¢ † } $ ¦ ¥¤  ¤ 9 3  to tell the script to start searching in the directory /usr instead of the current directory OSSI — ver. 1.12 Shell Programming — slide 51 while getopts ":vd:" opt do case opt in v) VERBOSE=1 ;; d) DIRECTORY=$OPTARG ;; *) echo "usage: $0 [-v] [-d dir]" exit 1 ;; esac done shift $((OPTIND - 1)) OSSI — ver. 1.12 Shell Programming — slide 53 Command-Line Options—3 We could do this using shift, a while loop, and a case statement, like this: while [ -n "$(echo $1 | grep ’-’)" ] do case $1 in -v) VERBOSE=1 ;; -d) shift DIRECTORY=$1 ;; *) echo "usage: $0 [-v] [-d dir]" exit 1 ;; esac shift done OSSI — ver. 1.12 Shell Programming — slide 52 ¢   : Command-Line Options—5 Signals that may Terminate your Script Many key strokes will send a signal to a process Examples: ◦ Control-C © sends a SIGINT signal to the current process running in the foreground ◦ Control-\ © sends a SIGQUIT signal When you log out, all your processes are sent a SIGHUP (hangup) signal If your script is connected to another process that terminates unexpectedly, it will receive a SIGPIPE signal If anyone terminates the program with the kill program, the default signal is SIGTERM Shell Programming — slide 56 ¢ ¢ ¢ } ™ u v ¢  getopts takes two arguments: ◦ first comes the string that can contain letters and colons. – Each letter represents one option – A colon comes after a letter to indicate that option takes an arguement, like -d directory – A colon at the beginning makes getopts less noisy, so you can provide your own error message, as shown in the example. ◦ The second is a variable that will hold the option (without the hyphen “-”) Shift out all processed options using the variable OPTIND, leaving any other arguments accessible Search for getopts in the bash man page Shell Programming — slide 54 † }   ¨ ¨ ¢ OSSI — ver. 1.12 OSSI — ver. 1.12 Temporary Files: ¢ ¢ š } z u Sometimes it is convenient to store temporary data in a temporary file The program is designed for this     £ ¢ We use it something like this: TMPFILE=$(mktemp /tmp/temp.XXXXXX) || exit 1 mktemp will create a new file, replacing the “XXXXXX” with a random string Do man mktemp for the complete manual. Shell Programming — slide 55 ¢ š  Signals: ¢ ¢ ¢ w} y Sometimes you want your script to clean up after itself nicely, and remove temporary files Do this using  © £ ¢ ¢ OSSI — ver. 1.12 OSSI — ver. 1.12 ¢  Shell Programming — slide 57 Signals: ¢ Example Functions The shell supports functions and function calls A function works like an external command, except that it does not start another process Syntax: function functname { shell commands } Or: functname () { shell commands } OSSI — ver. 1.12 Shell Programming — slide 59 ¢ ¢ ¢ ¢ w} y Supose your script creates some temporary files, and you want to remove them if your script recieves any of these signals You can “catch” the signal, and remove the files when the signals are received before the program terminates Suppose the temporary files have names stored in the variables TEMP1 and TEMP2 Then you would trap these signals like this: trap "rm $TEMP1 $TEMP2" HUP INT QUIT PIPE TERM Conveniently, (but not very portably), bash provides a “pretend” signal called EXIT; can add this to the list of signals you trap, so that the temporary files will be removed when the program exits normally. Shell Programming — slide 58 OSSI — ver. 1.12 ¢ ¢ ¢ ¢  Parameters in Functions Work the same as parameters to entire shell script First parameter is 1, second is 2,. . . , the tenth parameter is {10}, and so on.       ¢ ¢ ¢ ¢ # is the number of parameters passed to the function OSSI — ver. 1.12   As with command line parameters, they are read-only Assign to meaningful names to make your program more understandable Shell Programming — slide 60 Example, Calling a Function This is a simple example program: #! /bin/sh function cube { echo $(($1 * $1 * $1)) } j=$(cube 5) echo $j # Output is 125 Note the use of command substitution to get a return value The function prints result to standard output. Shell Programming — slide 61 ¢ ¢ Debugging Shell Scripts—2 Use echo to display the value of variables as the program executes You can turn the -x shell option on in any part of your script with the line: set -x and turn it off with: set +x ◦ No, that’s not a typo: +x turns it off, -x turns it on. The book Learning the bash Shell includes a bash shell debugger if you get desperate Shell Programming — slide 63 ¢ ¢ ¢ ¢ OSSI — ver. 1.12 ¢ OSSI — ver. 1.12 Writing Shell Scripts Build your shell script incrementally: ◦ Open the editor in one window (and leave it open), have a terminal window open in which to run your program as you write it ◦ Test as you implement: this makes shell script development easy ◦ Do not write a very complex script, and then begin testing it! Use the standard software engineering practice you know: ◦ Use meaningful variable names, function names ◦ Make your program self-documenting ◦ Add comment blocks to explain obscure or difficult parts of your program OSSI — ver. 1.12 Shell Programming — slide 64 ¢ Debugging Shell Scripts—1 If you run the script with: $ script   9 1   9" ¢ ¢ then each statement will be printed as it is executed If you run the script with: $ script then an execution trace will show the value of all variables as the script executes. OSSI — ver. 1.12 Shell Programming — slide 62 Useful External Programs—1 Each of these has a manual page, and many have info manuals. Read their online documentation for more information. awk — powerful tool for processing columns of data basename — remove directory and (optionally) extension from file name cat — copy to standard output cut — process columns of data du — show disk space used by directories and files egrep, grep — find lines containing patterns in files find — find files using many criteria Shell Programming — slide 65 ¢ Regular Expressions Many programs and programming languages use regular expressions, including Java 1.4 and later, Perl, VB.NET, C# (and any language using the .NET Framework), PHP, Python, Ruby, Tcl and MySQL (plus many others; even MS Word uses regular expressions under Edit → Find → More → Use wildcards) These programs use regular expressions: ◦ ¢ ¢ ¢ ¢ ¢ , , ,  A   £  A   £  © ¢ All programmer’s editors support regular expressions (Emacs, vi, . . . ) Regular expressions provide a powerful language for manipulating data and extracting important information from masses of data Shell Programming — slide 67 ¢ ¢ OSSI — ver. 1.12 OSSI — ver. 1.12 Useful External Programs—2 last — show the last time a user was logged in lastb — show last bad log in attempt by a user rpm — rpm package manager: manage software package database sed — stream editor: edit files automatically sort — sort lines of files by many different criteria tr — translate one set of characters to another set uniq — replace repeated lines with just one line, optionally with a count of the number of repeated lines Shell Programming — slide 66 ¢ ¢ ¢ What is In a Regular Expression? There are two types of character in a regular expression: ◦ Metacharacters ¢ ¢ ¢ ¢ ¢ – These include: – *\. +? ^()[{| ◦ Ordinary, literal characters: – i.e., all the other characters that are not metacharacters OSSI — ver. 1.12 Shell Programming — slide 68 OSSI — ver. 1.12 ¢ ¢ o  Literal characters Find all lines containing ”chan” in the password file:  ¢ Negated Character Classes: ›« ¢ b • »¼¶ ³ º² ­ ¹ »¶ ³ º² ¶¹ · ¸ Examples of negated character classes: # # $ # # $ $ # # $ Find all words in the dictionary that contain a character that is not a vowel: µ ¶ ·¶ f ³² f ¯° ¸ ¬­ ” ® ±” ´ ´· ­ $ ¢ ©  o A   £ ¥  £©   The regular expression is ”chan” It is made entirely of literal characters It matches only lines that contain the exact string It will match lines containing the words chan, changed, merchant, mechanism,. . . Shell Programming — slide 69 ¢ Two ways of finding all lines that contain a character that is not a digit: µ ¶ ·¶ f À¿ ½ ˜h i d l¾ Á f ¯° ¬­ ” ® ´· ­ ±­ ” • ­ µ ¶ ·¶  ¼½ »¶ f f º² ¯° Á ¸ ¶¹ ¬­ ” ® ´· ­ ±­ ” • ³ ­ · ¹ · ¹ · ¹ ­ ¢ ¢ Find all lines that contain a character that is not a digit, or a letter µ ¶ ·¶  ¼½ à ÂÄ • »¼¶ ³ f f º² ” ¶¹ ¯° Á Å ¬­ ” ® ´· ­ ±­ ±Â ¸ OSSI — ver. 1.12 ¢ Remember: each set of square brackets represents exactly one character. Shell Programming — slide 71 OSSI — ver. 1.12 › Character Classes: ¢ b A character class represents one character Examples: words in the dictionary that contain a vowel: €   ¡    Œw  ‚ Šw ž‹   ¡‚ ƒ ƒ¡ † † y  ‡Œ †  ž Ÿt # Find all $ # Find all $ # Find all $ # Find all $ OSSI — ver. 1.12 „ † y’  t¢ „ † y’  t¢ „ † y’ t  „ † y’ ¢ y b b œ lines that contain a digit: €   ¡    ¨§  Šw ‘£ ¤¥¦ © ž‹ ‚   ¡‚ ƒ¡ † † y  ‡Œ † Match Any Character The dot “.” matches any single character, except a newline. The pattern ‘.....’ matches all lines that contain at least five characters Shell Programming — slide 72 ¢ ˆ lines that contain a digit: €   ¡     ‚ Šw © ž‹   u lines that contain a capital letter: €   ¡     ‚ Šw ª ž‹   ¡‚ ƒ¡ † † y  ‡Œ † ¡‚ ƒ¡ † † y  ‡Œ † u  Shell Programming — slide 70 OSSI — ver. 1.12 ¢ ±­ ” ¶¹ b b œ Matching the Beginning or End of Line To match a line that contains exactly five characters: D 3  ¢ Matching Alternatives: “|” the vertical bar represents alternatives: The regular expresssion ‘nick|albert|alex’ will match either the string “nick” or the string “albert” or the string “alex” Note that the vertical bar has very low precedence: the pattern ‘^fred|nurk’ matches “fred” only if it occurs at the start of the line, while it will match “nurk” at any position in the line Shell Programming — slide 75 ¢ ¢ ¢ ¢ $ ¢ 6Y 6 © ¤   A   £      o§  8 8 8   The hat, ^ represents the position right at the start of the line The dollar represents the position right at the end of the line. represents a character     ¢ Neither ^ nor ¢ 8 8 They represent a position Sometimes called anchors, since they anchor the other characters to a specific part of the string Shell Programming — slide 73 OSSI — ver. 1.12 OSSI — ver. 1.12 ¢ ¢ Match Repetitions: ¢ , , , { }, { Ç È | } Æ | To match zero or more: a* represents zero or more of the lower case letter a, so the pattern will match "" (the empty string), “a”, “aa”, “aaaaaaaaaaaaaaa”, “qwewtrryu” or the “nothing” in front of any string! To match one or more: ‘a+’ matches one or more “a”s ‘a?’ matches zero or one “a” ‘a{10}’ matches exactly 10 “a”s ‘a{5,10}’ matches between 5 and 10 (inclusive) “a”s Shell Programming — slide 74 OSSI — ver. 1.12 ¢ ¢ ¢ ¢ ¢ ¢ É š Putting it All Together: Examples Find all words that contain at least three ‘a’s: 3  ¢ Basic © ¢ y $ Mostly used for one-line solutions to problems of extracting columns of data from text, and processing it A complete book is available on awk; you can buy it here: http://www.oreilly.com/catalog/awkprog3/ or read it on your computer, as it is the official manual for gawk (gnu awk); do $ ¥¤ §¦ A © o  ©6 6 © ¤    A   £ '© '©      §o   8 ◦ Why is this different from 3  8  $ ¢ ©6 6 © ¤    A   £ © ©      o§    Find all words that begin in ‘a’ and finish in ‘z’, ignoring case: D 3  $ ¤ 6Y 6 © ¤    A   £ © 'Ê      o§   9 ◦ How is this different from: 3 8  ¢ ¢ ¢ $ ¢ © or read it in Emacs.   ¤ 6Y 6 ¤    A   £ © 'Ê      9 8 §o   Find all words that contain at least two vowels: ¯ ´ Ë̵ ¯ µ ¶ ·¶ OSSI — ver. 1.12 o  ◦ A printable postscript file of the book (353 pages) is on my computer at /usr/share/doc/gawk-3.1.3/gawk.ps Shell Programming — slide 77 ­ · ¹ $ ¢ Find all words that contain exactly two vowels: $ 6Y • » ¼¶ ³² ³² º² e e ¸ ¬­ ” ® ±” k ±” ´ ´· ­ ±­ ” ¶¹ ³ What Does ' ©Í YÍ Î Î ©Í YÍ Î Î D §¤ §¤ §¤ §¤ '  3 ©  3  3 ©  3 ' 6 7 ¤    o§    ¢ r is a complete programming language z Do?     y  A   YÍ £ §¤ ©  3 Î 7 3 © awk reads file(s) or standard input one line at a time, and automatically splits the line into fields, and calls them 1, 2,. . . , NF NF is equal to the number of fields the line was split into 0 contains the whole line          Find all lines that are empty, or contain only spaces: $ D 6Y 6 ¦  A   £ ' ¤ ¢ OSSI — ver. 1.12 ¢ Shell Programming — slide 76 ¢ ¢ awk has an option -F that allows you to select another pattern as the field separator ◦ Normally awk splits columns by white space To execute code after all lines are processed, create an END block. Shell Programming — slide 78 OSSI — ver. 1.12 ¢ ¢ r z Examples Print the sizes of all files in current directory: { } X ¥¤    6 0 D  © o 9 £  6 —the Stream Editor sed provides many facilities for editing files The substitute command, s///, is the most important The syntax (using sed as an editor of standard input), is: $ original replacement    6   6  z y r ¢ † u Add the sizes of all files in current directory: { } { } X D ¥¤    6 0 I d P  © o 3  !C £  3 9  6 ¢ ¢ ¢ ¢ ¢ … Print only the permissions, user, group and file names of files in current directory: { } X D D D ¥¤    6 F ( ` dÏ D  © o 9 £  e e e 6 Example: replace the first instance of Windows with Linux on each line of the input: ¥¤  ¥¤  6   § o  3 " 6 A " 6  Ð Ñ OSSI — ver. 1.12 ¢ Shell Programming — slide 79 Example: replace all instances of Windows with Linux on each line of the input: ¥¤  ¥¤ o  Ñ   § ©  !   3  6 Ð ¢ ◦ Note: by default, sed uses “basic regular expressions”, which require a backslash ‘\’ in front of the metacharacters ’{’, ‘(’, ‘)’, ‘|’, ‘+’ and ‘?’. ◦ To use “extended regular expressions” (which we covered here), call sed with the option -r, as in this example: $   9 OSSI — ver. 1.12 Shell Programming — slide 80 —Backreferencees You can match part of the original in a sed -r substitute command, and put that part back into the replacement part. You enclose the part you want to refer to later in (...) You can get the first value in the replacement part by \1, the second opening parenthesis of (...) by \2, and so on. Shell Programming — slide 81 † u OSSI — ver. 1.12 ¢ ¢ ¢ … —Backreferencees: Example If you do  X Finding SUID Programs Finding suid or sgid files:  ¢ † u ¢ … $ $ 7 ) ¦ ¥¤  ¤ ¦ ¥¤  ¦  ¨   4 " ©  A  3 § 9 B 9 £ ¦ 7 7 G& G G `G £   9§ £  9 9  E you will get a lot of output like this: symbolic symbolic symbolic symbolic symbolic ¢ ¦  ¤   3 link link link link link to to to to to bg5ps.conf.zh_TW.Big5 rc.d/rc.local rc.d/rc rc.d/rc.sysinit ../../X11/xdm/Xservers Let’s compare with a list of suid and sgid files to see if there are any changes, since suid and sgid programs can be a security risk:  ¢ 8   $ 7 X ¦ ¥¤   4 3 § B 9 £ ¦ 7 9 G G 7 G& G G `G £   9§ £  9 9  ¤  ¦ ¦ ¦  ¤   3 9 E If you want to edit each line to remove everything after “symbolic link”, then you could pipe the data through sed like this:  X OSSI — ver. 1.12 8   9 G G Shell Programming — slide 84 $ X ¦ ¥¤  ¦  ¤  " ©  A   B E 9 ¨  7 7 ¤  ¥¤  6 §¨   F  9  4  See slide 83 for an application Shell Programming — slide 82 OSSI — ver. 1.12 ¢ Examples |p q ¢ … Count the number of unique manual pages on the computer: 3   8 ' $ ¢ ¦ ¥¤  © ¦ X 6  4     © ¥ £ 9 o 9  Print a table of types of file under the /etc directory, with the most common file type down at the bottom:  X A Example with Many Options |p q $ Set all directories to have the access mode 771, set all backup files (*.BAK) to mode 600, all shell scripts (*.sh) to mode 755, and all text files (*.txt) to mode 644: {} {} {} {} Shell Programming — slide 85 × ŸÔ Ô ˜ ×Ô Ô ˜ uŒ uŒ uŒ ×Ô Ô ˜ ×Ô Ô ˜ Ô Ô Ô Ô ’ ‚ ֋ ‚ § Ր ’ y už y … yŠ Š Ô ’ u Ù Ú  ֋ Œ ‚¦ Ò Ø ‰ž Ö y už y … yŠ Š Œ ¢ ¢ ˆ Ô ’ u  ֋ Ò Ø ‰ž Ö y ¡ už y … yŠ Š Ô ’ u  ֋ Œ ‚¦  Ò Ø ‰ž Ö y …  už y … yŠ Š u Œ ¤ ¥§ ‹ ‚ ¤ ¥ § ¦ ¥¤  ¦  ¤  " ©  A  X  B 9 ¨ E 7  7  ¤  ¥¤  6 §¨   F  9  4  8 ' 6 7 $ 7 v X X § 3 ¥ 2 ¤   X §   OSSI — ver. 1.12 9¥ 9 7 Shell Programming — slide 83 OSSI — ver. 1.12 ‰ Òӂ w ¢ … 7 7 Database Query Commands The rpm software package management system includes a database with very detailed information about every file of every software package that is installed on the computer. You can query this database using the rpm command. The manual page does not give the complete picture, but there is a book called Maximum RPM that comes on the Red Hat documentation cd This package is installed on ictlab You can see the appropriate section at this url: w  ¢ ¢ ¢ ¢ ¢ š http://ictlab.tyict.vtc.edu.hk/doc/maximum-rpm-1.0/html/s1-rpm-query-parts.html OSSI — ver. 1.12 Shell Programming — slide 86