Содержание
- 2. BASIC SHELL PROGRAMMING A script is a file that contains shell commands data structure: variables control
- 3. BASH SHELL PROGRAMMING Input prompting user command line arguments Decision: if-then-else case Repetition do-while, repeat-until for
- 4. USER INPUT shell allows to prompt for user input Syntax: read varname [more vars] or read
- 5. USER INPUT EXAMPLE #! /bin/sh read -p "enter your name: " first last echo "First name:
- 6. SPECIAL SHELL VARIABLES CSCI 330 - The Unix System
- 7. EXAMPLES: COMMAND LINE ARGUMENTS % set tim bill ann fred $1 $2 $3 $4 % echo
- 8. BASH CONTROL STRUCTURES if-then-else case loops for while until select CSCI 330 - The Unix System
- 9. IF STATEMENT if command then statements fi statements are executed only if command succeeds, i.e. has
- 10. TEST COMMAND Syntax: test expression [ expression ] evaluates ‘expression’ and returns true or false Example:
- 11. THE SIMPLE IF STATEMENT if [ condition ]; then statements fi executes the statements only if
- 12. THE IF-THEN-ELSE STATEMENT if [ condition ]; then statements-1 else statements-2 fi executes statements-1 if condition
- 13. THE IF…STATEMENT if [ condition ]; then statements elif [ condition ]; then statement else statements
- 14. RELATIONAL OPERATORS CSCI 330 - The Unix System
- 15. COMPOUND LOGICAL EXPRESSIONS ! not && and || or CSCI 330 - The Unix System and,
- 16. EXAMPLE: USING THE ! OPERATOR #!/bin/bash read -p "Enter years of work: " Years if [
- 17. EXAMPLE: USING THE && OPERATOR #!/bin/bash Bonus=500 read -p "Enter Status: " Status read -p "Enter
- 18. EXAMPLE: USING THE || OPERATOR #!/bin/bash read -p "Enter calls handled:" CHandle read -p "Enter calls
- 19. FILE TESTING Meaning -d file True if ‘file’ is a directory -f file True if ‘file’
- 20. EXAMPLE: FILE TESTING #!/bin/bash echo "Enter a filename: " read filename if [ ! –r "$filename"
- 21. EXAMPLE: FILE TESTING #! /bin/bash if [ $# -lt 1 ]; then echo "Usage: filetest filename"
- 22. EXAMPLE: IF… STATEMENT # The following THREE if-conditions produce the same result * DOUBLE SQUARE BRACKETS
- 23. EXAMPLE: IF..ELIF... STATEMENT #!/bin/bash read -p "Enter Income Amount: " Income read -p "Enter Expenses Amount:
- 24. THE CASE STATEMENT use the case statement for a decision that is based on multiple choices
- 25. CASE PATTERN checked against word for match may also contain: * ? [ … ] [:class:]
- 26. EXAMPLE 1: THE CASE STATEMENT #!/bin/bash echo "Enter Y to see all files including hidden files"
- 27. EXAMPLE 2: THE CASE STATEMENT #!/bin/bash ChildRate=3 AdultRate=10 SeniorRate=7 read -p "Enter your age: " age
- 28. BASH PROGRAMMING: SO FAR Data structure Variables Numeric variables Arrays User input Control structures if-then-else case
- 29. BASH PROGRAMMING: STILL TO COME Control structures Repetition do-while, repeat-until for select Functions Trapping signals CSCI
- 30. REPETITION CONSTRUCTS CSCI 330 - The Unix System
- 31. THE WHILE LOOP Purpose: To execute commands in “command-list” as long as “expression” evaluates to true
- 32. EXAMPLE: USING THE WHILE LOOP #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ] do echo The
- 33. EXAMPLE: USING THE WHILE LOOP #!/bin/bash Cont="Y" while [ $Cont = "Y" ]; do ps -A
- 34. EXAMPLE: USING THE WHILE LOOP #!/bin/bash # copies files from home- into the webserver- directory #
- 35. THE UNTIL LOOP Purpose: To execute commands in “command-list” as long as “expression” evaluates to false
- 36. EXAMPLE: USING THE UNTIL LOOP #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ] do echo $COUNTER
- 37. EXAMPLE: USING THE UNTIL LOOP #!/bin/bash Stop="N" until [ $Stop = "Y" ]; do ps -A
- 38. THE FOR LOOP Purpose: To execute commands as many times as the number of words in
- 39. EXAMPLE 1: THE FOR LOOP #!/bin/bash for i in 7 9 2 3 4 5 do
- 40. EXAMPLE 2: USING THE FOR LOOP #!/bin/bash # compute the average weekly temperature for num in
- 41. LOOPING OVER ARGUMENTS simplest form will iterate over all command line arguments: #! /bin/bash for parm
- 42. SELECT COMMAND Constructs simple menu from word list Allows user to enter a number instead of
- 43. SELECT EXAMPLE #! /bin/bash select var in alpha beta gamma do echo $var done Prints: CSCI
- 44. SELECT DETAIL PS3 is select sub-prompt $REPLY is user input (the number) #! /bin/bash PS3="select entry
- 45. SELECT EXAMPLE #!/bin/bash echo "script to make files private" echo "Select file to protect:" select FILENAME
- 46. BREAK AND CONTINUE Interrupt for, while or until loop The break statement transfer control to the
- 47. THE BREAK COMMAND while [ condition ] do cmd-1 break cmd-n done echo "done" CSCI 330
- 48. THE CONTINUE COMMAND while [ condition ] do cmd-1 continue cmd-n done echo "done" CSCI 330
- 49. EXAMPLE: for index in 1 2 3 4 5 6 7 8 9 10 do if
- 50. DONE ! BASH SHELL PROGRAMMING Sequence Decision: if-then-else case Repetition do-while, repeat-until for select Functions Traps
- 51. SHELL FUNCTIONS A shell function is similar to a shell script stores a series of commands
- 52. SHELL FUNCTIONS must be defined before they can be referenced usually placed at the beginning of
- 53. EXAMPLE: FUNCTION #!/bin/bash funky () { # This is a simple function echo "This is a
- 54. EXAMPLE: FUNCTION #!/bin/bash fun () { # A somewhat more complex function. JUST_A_SECOND=1 let i=0 REPEATS=30
- 55. FUNCTION PARAMETERS Need not be declared Arguments provided via function call are accessible inside function as
- 56. EXAMPLE: FUNCTION WITH PARAMETER #! /bin/sh testfile() { if [ $# -gt 0 ]; then if
- 57. EXAMPLE: FUNCTION WITH PARAMETERS #! /bin/bash checkfile() { for file do if [ -f "$file" ];
- 58. LOCAL VARIABLES IN FUNCTIONS Variables defined within functions are global, i.e. their values are known throughout
- 59. EXAMPLE: FUNCTION #! /bin/bash global="pretty good variable" foo () { local inside="not so good variable" echo
- 60. HANDLING SIGNALS Unix allows you to send a signal to any process -1 = hangup kill
- 61. SIGNALS ON LINUX % kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
- 62. HANDLING SIGNALS Default action for most signals is to end process term: signal handler Bash allows
- 63. EXAMPLE: TRAP HANGUP #! /bin/bash # kill -1 won’t kill this process # kill -2 will
- 64. EXAMPLE: TRAP MULTIPLE SIGNALS #! /bin/sh # plain kill or kill -9 will kill this trap
- 65. EXAMPLE: REMOVING TEMP FILES #! /bin/bash trap 'cleanup; exit' 2 cleanup () { /bin/rm -f /tmp/tempfile.$$.?
- 66. RESTORING DEFAULT HANDLERS trap without a command list will remove a signal handler Use this to
- 67. DEBUG SHELL PROGRAMS Debugging is troubleshooting errors that may occur during the execution of a program/script
- 68. DEBUGGING USING “SET” The “set” command is a shell built-in command has options to allow flow
- 70. Скачать презентацию