1 / 81

Shell Programming

Shell Programming. Lecture Overview. Shell variables Shell scripts Control flow and Boolean operators Shell programming tips Shell programming examples. Shell Variables. Unlike simpler operating systems (such as DOS), UNIX shells provide powerful programming languages

lyndamurphy
Télécharger la présentation

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

  2. Lecture Overview • Shell variables • Shell scripts • Control flow and Boolean operators • Shell programming tips • Shell programming examples

  3. Shell Variables • Unlike simpler operating systems (suchas DOS), UNIX shells provide powerful programming languages • Each shell has a different language • We will only learn C Shell programming: • A powerful programming language • Its syntax is similar to that of the C language

  4. Shell Variables • Like in any programming language, the most basic elements are variables • C Shell variables are always of type string • However, they can be treated as numbers, and can be used for arithmetic operations • Arrays of variables are also supported

  5. Defining Variables • Since variables have no type, there is no need for a variable declaration • To define a variable, we simply assign some value to it • Assigning values to variables is done using the set command: set [variable [= value]] ...

  6. Defining Variables • When set is used without any arguments, the values of all variables currently defined in the shell are printed • When set is used with a name but no value, the variable is created, but assigned an empty value • Such variables can be used as Booleans

  7. Using Variables • To reference a variable in any shell command, the '$' sign is used /bin/tcsh echo $shell name: Undefined variable. echo $name set name = John echo $name John

  8. Un-defining Variables • When a variable is no longer needed, it can be freed using the unset command: • This can also be used for setting the value of a Boolean variable to false unset variable set name unset name echo $name name: Undefined variable.

  9. Arrays of Variables • To define an array, a value must be assigned to each of its elements • The list of values is enclosed within parentheses – '(' and ')' • Specific values can be accessed using square braces – '[' and ']'

  10. Arrays of Variables • Unlike C and Java, arrays in the C Shell are1-based, not 0-based • If array a has 3 elements, then they can be accessed as: a[1], a[2], a[3] • To append a new element to an existing array, use the following: set a = ($a new_element)

  11. Arrays of Variables – Examples set colors = (red green blue) echo $colors red green blue echo $colors[2] green echo $colors[2-3] green blue set colors = ($colors yellow) echo $colors[4] yellow set shapes = ("" "" "" "") set shapes[4] = square echo $shapes[4] square

  12. Numeric Variables • the set command can only assign literal values to variables • To allow the right-hand-sign of an assignment to be a logical or arithmetic expression, we use the '@' command: • Note: the space after the '@' is mandatory @ [variable = expression] ...

  13. Expressions • An expression can contain most of the operators available in C (or in Java): • Arithmetic operators • +, -, *, /, % • Relational and logical operators • >, <, >=, <=, ==, !=, &&, ||, ! • The value of a logical expression is either0 (for false) or 1 (for true)

  14. Expressions • The '=' operator can be replaced with other assignment operators: • +=, -=, *=, /=, %= • The postfix increment/decrement operators ('++' and '--') can also be used • Since some operators use shell special characters, expressions containing them must be surrounded with parentheses

  15. Numeric Variables and Expressions – Examples @ count = 0 echo $count 0 @ count = 5 + 2 echo $count 7 @ result = ($count > 5) echo $result 1 @ count += 5 echo $count 12 @ count++ echo $count 13

  16. Numeric Variable Type • Numeric variables in the C Shell are always assumed to be integers – trying to assign fractional values will fail: @ c = 3.5 @: Badly formed number. echo $c c: Undefined variable.

  17. Arrays of Numeric Variables • In order to define an array of numeric values, the set command must be used • After the array is initialized, individual values can be changed using set, or using '@' and an expression • Arrays can also be mixed, containing both numeric and string values

  18. Arrays of Numeric Variables – Example set ages = (0 0 0 0) @ ages[2] = 15 @ ages[3] = ($ages[2] + 4) echo $ages[3] 19 echo $ages 0 15 19 0 set ages[1] = teen echo $ages teen 15 19 0

  19. Special Forms of Variables • Number of elements in an array: • Number of characters in a regular variable: • Determine whether a variable is defined or not (1 if defined, 0 otherwise): $#array $%variable $?variable

  20. Special Forms of Variables – Example set days = (mon tues wed thurs fri) echo $#days 5 fri echo $days[$#days] set country = "Israel" echo $%country 6 1 echo $?country unset country echo $?country 0

  21. Variable Modifiers • The following modifiers can be appended to a variable, to extract only part of it

  22. Variable Modifiers – Examples set phones_path = ~demo/text/phones.txt echo $phones_path /home/demo/text/phones.txt echo $phones_path:e txt echo $phones_path:r /home/demo/text/phones echo $phones_path:h /home/demo/text echo $phones_path:t phones.txt echo $phones_path:t:r phones

  23. Quoting Shell Variables • As we have seen, double quotes (") can be used to quote some special characters • However, this does not suppress variable substitution: set my_text = ~demo/text echo "The file is in the $my_text directory." The file is in the /home/demo/text directory.

  24. Quoting Shell Variables • To prevent variable substitution, the text should be enclosed in single quotes ('): • It is also possible to run a command, and store its output in a variable – this is called command substitution echo 'Store your name in the $user_name variable.' Store your name in the $user_name variable.

  25. Command Substitution • To use command substitution, the command, along with its arguments, should be enclosed in backquotes (`): set satoshi_phone = `grep Satoshi phones.txt` echo $satoshi_phone NAKAMURA, Satoshi 6453 set name = Satoshi echo $name\'s phone number is: \ `grep $name phones.txt | cut -d" " -f3` Satoshi's phone number is: 6453

  26. Pre-defined Shell Variables • Whenever a shell is started, several variables are already defined • The values of some of these variables are constantly updated by the shell • The user can change the values ofpre-defined variables to modify the behavior of the shell

  27. Pre-defined Shell Variables • Some pre-defined variables have values, others only act as switches (Boolean) • Shell variables that act as switches: • $noclobber– if set, does not allow the user to accidentally overwrite an existing file • $ignoreeof– when set, prevents accidental log-out using Ctrl-D. To leave a shell, exit or logout must be used

  28. Pre-defined Shell Variables • Shell variables that hold a value: • $user– contains the name of the current user • $home– contains the path to the home directory of the current user • $path– contains the command search path • $shell– contains the path to the current shell being used • Many more variables exist

  29. Lecture Overview • Shell variables • Shell scripts • Control flow and Boolean operators • Shell programming tips • Shell programming examples

  30. Shell Scripts • A shell script is a file that contains commands to be executed by the shell • Any command entered in response to a shell prompt can also be used in a script • Additionally, the shell provides control flow commands, designed specifically for use within shell scripts

  31. Executing a Shell Script • There are two approaches to running a shell script: • Running the script within the current shell • More efficient – no shell start-up required • Variable definitions remain in effect when the script ends, and can be used in the current session • Running the script in a newly-created shell • Similar to executing a binary program

  32. Executing a Shell Scriptin the Current Shell • By using the source command, a script file can be executed in the current shell: • The script is assumed to be written in the language of the current shell • If the script was written in the language of a different shell – an error may occur source script_file

  33. Executing a Shell Script in a New Shell • Normally, when a script is run, a new shell is created for running it • This can be done explicitly: • This is not very convenient, and still requires the user to know which shell should be used to interpret the script /bin/tcsh script_file

  34. Executing a Shell Script in a New Shell • The name of the shell that should be used can be embedded in the script itself • Set the first line of the script to: • If this approach is used, the script file must be made executable: #!/bin/tcsh chmod +x script_file

  35. Automatically Executed Shell Scripts • Several scripts are automatically executed by the C Shell at different times: • .login– runs at the beginning of a session • .logout– runs at the end of a session • .tcshrc or .cshrc– runs every time a new shell is created • All of these files must be located in the user's home directory

  36. The .tcshrc File • The .tcshrc (or .cshrc) file is run once when the user logs in, and again every time a new shell is created (for example when a shell script file is executed) • It is normally used for defining local variables and common aliases • Any C Shell command can be used in it

  37. A Sample .tcshrc File #!/bin/tcsh # Define aliases. alias l ls -F --color alias ll l -l alias la ll -a alias hgrep 'h | grep' alias + more set noclobber set ignoreeof set nobeep umask 077

  38. Command Line Arguments –$argv • The '$argv' variable contains the command line arguments: • '$argv[0]' – the name of the current script • '$argv[1]', '$argv[2]', …– specific command line arguments • '$argv[*]' – all command line arguments • '$#argv' – the number of command line arguments

  39. Special Variables for Use WithinShell Scripts • The following shortcuts may be used: • '$*' instead of '$argv[*]' • '$1' instead of '$argv[1]','$2' instead of '$argv[2]', etc. • '$#' instead of '$#argv' • '$<' is used for getting input from the user: echo –n "Please enter your name: " set user_name = $<

  40. Using Temporary Files • The '$$' variable contains the number ofthe current process, and can be used for generating unique file names • Somewhere in the script: • Before the script ends: ls *.c > .tmp_file.$$ /bin/rm .tmp_file.$$

  41. Debugging Shell Scripts • Shell scripts are run using an interpreter, so all errors are found during run-time • In order to debug a shell script, the -x option should be given (either in the first line of the script, or in the command line) • With this option set, any command is printed out just before it is executed

  42. Debugging Shell Scripts • Consider a script called debug_script: • First, we run it without the -x option: or #!/bin/tcsh set a = 5 @ a = 7 + 12 echo $a /bin/tcsh debug_script debug_script 19

  43. Debugging Shell Scripts • Now, we run it with the -x option: • Alternatively, the -x option can be inserted directly into the first line of the script /bin/tcsh –x debug_script set a = 5 @ a = 7 + 12 echo 19 19 #!/bin/tcsh –x ...

  44. Lecture Overview • Shellvariables • Shell scripts • Control flow and Boolean operators • Shell programming tips • Shell programming examples

  45. Control Flow Commands • The C Shell supports the common control structures if, while and switch • It does not have a for command • Instead, arrays and lists can be traversed using the foreach command • Unlike C and Java, a ';' is not required at the end of a line, and blocks are not surrounded by '{' and '}'

  46. The if Control Structure • The C Shell if structure has two forms: • A simple form, for executing a single command • An if-then-else structure, for executing complex blocks of command • The format of the simple if structure: • The command must be on the same line if (expression) simple-command

  47. The if-then-elseControl Structure • The format of the if-then-else structure: if (expression) then commands else if (expression) then commands ... else commands endif

  48. if– Example • Read command line arguments: #!/bin/tcsh if ($# == 0 || $# > 2) then echo "Usage: $0:t source [target]" exit 1 endif set source = $1 if ($# == 1) then set target = "~/backup/default" else # There are exactly two arguments. set target = $2 endif

  49. File Inquiry Operators • In addition to arithmetic and logical expressions, you can check the status of a file using expressions of the form:where n is a file inquiry operator • For example, is trueif filename exists -nfilename -e filename

  50. File Inquiry Operators

More Related