Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Chapter 6: Shell Programming Shell Scripts
  2. 2. Using the UNIX Shell as a Programming Objectives: <ul><li>After studying this lesson, you should be able to: </li></ul><ul><ul><li>Learn about shell variables, operators </li></ul></ul><ul><ul><li>Write simple shell scripts to illustrate programming logic </li></ul></ul>
  3. 3. My first Shell Script <ul><li>vi </li></ul><ul><li> #! /bin/csh </li></ul><ul><ul><li>set directory=`pwd` </li></ul></ul><ul><ul><li>echo The date today is `date` </li></ul></ul><ul><ul><li>echo The current directory is $directory </li></ul></ul><ul><li>chmod u+x </li></ul><ul><li> </li></ul>
  4. 4. Using UNIX Shell Scripts <ul><li>UNIX shell scripts are text files that contain sequences of UNIX commands </li></ul><ul><li>Like high-level source files, a programmer creates shell scripts with a text editor </li></ul><ul><li>Shell scripts do not have to be converted into machine language by a compiler </li></ul><ul><li>This is because the UNIX shell acts as an interpreter when reading script files </li></ul>
  5. 5. Using UNIX Shell Scripts Continued <ul><li>As an interpreter reads the statements in a program file, it immediately translates them into executable instructions, and causes them to run </li></ul><ul><li>After you create a shell script, you simply tell the operating system that the file is a program that can be executed </li></ul><ul><li>This is accomplished by using the chmod command to change the files’ mode </li></ul>
  6. 6. Using UNIX Shell Scripts Continued <ul><li>Further, the chmod command tells the computer who is allowed to use the file: the owner (u), the group (g), or all other users (o) </li></ul><ul><li>Shell programs run less quickly than do compiled programs, because the shell must interpret each UNIX command inside the executable script file before it is executed </li></ul>
  7. 7. The Programming Shell <ul><li>Commonly used shell in most variant of UNIX are: </li></ul><ul><li>Bourne Shell ( sh ), first shell developed for UNIX </li></ul><ul><li>Bourne Again Shell ( bash ), written by programmers of Free Software Foundation, open source shell from GNU </li></ul><ul><li>Korn Shell ( ksh ), written by David Korn, superset of Bourne shell, not widely distributed. </li></ul><ul><li>C Shell ( csh ), written by Bill Joy, the author of vi, shared much of the C language structure. </li></ul><ul><li>Terminal Based C Shell ( tcsh ), enhanced version of the Berkeley UNIX C shell csh </li></ul>
  8. 8. The Programming Shell <ul><li>All Linux versions use the Bash shell (Bourne Again Shell) as the default shell </li></ul><ul><li>All UNIX system include C shell and its predecessor Bourne shell. </li></ul>
  9. 9. Shell Programming <ul><li>programming features of the UNIX shell: </li></ul><ul><ul><li>Shell variables </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Logic structures </li></ul></ul>
  10. 10. Shell Programming <ul><li>programming features of the UNIX shell: </li></ul><ul><ul><li>Shell variables : Your scripts often need to keep values in memory for later use. Shell variables are symbolic names that can access values stored in memory </li></ul></ul><ul><ul><li>Operators : Shell scripts support many operators, including those for performing mathematical operations </li></ul></ul><ul><ul><li>Logic structures : Shell scripts support sequential logic (for performing a series of commands), decision logic (for branching from one point in a script to another), looping logic (for repeating a command several times), and case logic (for choosing an action from several possible alternatives) </li></ul></ul>
  11. 11. Shell Programming <ul><li>programming features of the UNIX shell: </li></ul><ul><ul><li>Shell variables </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Logic structures </li></ul></ul>
  12. 12. Variables <ul><li>Variables are symbolic names that represent values stored in memory </li></ul><ul><li>The three types of variables discussed in this section are configuration variables , environment variables , and shell variables </li></ul><ul><li>Use configuration variables to store information about the setup of the operating system, and do not change them </li></ul><ul><li>You can set up environment variables with initial values that you can change as needed </li></ul>
  13. 13. Variables Continued <ul><li>These variables, which UNIX reads when you log in, determine many characteristics of your session </li></ul><ul><li>Shell variables are those you create at the command line or in a shell script </li></ul><ul><li>Environment and configuration variables bear standard names, such as HOME, PATH, SHELL, USERNAME, and PWD </li></ul><ul><li>(Configuration and environment variables are capitalized to distinguish them from user variables) </li></ul>
  14. 14. Variables Continued <ul><li>To see a list of your environment variables : </li></ul><ul><li>$ printenv </li></ul><ul><li>or: </li></ul><ul><ul><li>$ printenv | more </li></ul></ul>
  15. 15. Variables Continued <ul><li>1. Typical Environment Variables </li></ul><ul><ul><li>HOME : pathname of your home directory </li></ul></ul><ul><ul><li>PATH : directories where shell is to look for commands </li></ul></ul><ul><ul><li>USER : your user name </li></ul></ul><ul><ul><li>PWD : your current working directory </li></ul></ul><ul><ul><li>MAIL : pathname of your system mailbox </li></ul></ul><ul><ul><li>SHELL : pathname of your shell </li></ul></ul><ul><li>2. Variable contents are accessed using ‘ $ ’: </li></ul><ul><ul><li>e.g. $ echo $HOME </li></ul></ul>
  16. 16. Variables Continued <ul><li>A shell variable take on the generalized form variable=value (except in the C shell). </li></ul><ul><li>$ x=37; echo $x </li></ul><ul><li>$ 37 </li></ul><ul><li>$ unset x; echo $x </li></ul><ul><li>The C shell uses the set statement set variables. </li></ul><ul><li>$ set x = 37 </li></ul><ul><li>You can set a pathname or a command to a variable or substitute to set the variable. </li></ul><ul><li>$ set mydir=`pwd`; echo $mydir </li></ul>
  17. 17. Variables Continued <ul><li>To create lists: </li></ul><ul><li>$ set Y = (UNL 123 CS251) </li></ul><ul><li>To set a list element: </li></ul><ul><li>$ set Y[2] = HUSKER </li></ul><ul><li>To view a list element: </li></ul><ul><li>$ echo $Y[2] </li></ul>
  18. 18. Variables Continued <ul><li>vi </li></ul><ul><li>#! /bin/csh </li></ul><ul><li>echo Total number of inputs: $#argv </li></ul><ul><li>echo First input: $argv[1] </li></ul><ul><li>echo Second input: $argv[2] </li></ul><ul><li>chmod u+x </li></ul><ul><li> HUSKER UNL CSE </li></ul>
  19. 19. Shell Programming <ul><li>programming features of the UNIX shell: </li></ul><ul><ul><li>Shell variables </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Logic structures </li></ul></ul>
  20. 20. Shell Operators <ul><li>The Bash shell operators are divided into three groups: defining and evaluating operators, arithmetic operators, and redirecting and piping operators </li></ul>
  21. 21. Arithmetic Operators <ul><li>expr supports the following operators: </li></ul><ul><ul><li>arithmetic operators: +,-,*,/,% </li></ul></ul><ul><ul><li>comparison operators: <, <=, ==, !=, >=, > </li></ul></ul><ul><ul><li>boolean/logical operators: &, | </li></ul></ul><ul><ul><li>parentheses: (, ) </li></ul></ul><ul><ul><li>precedence is the same as C, Java </li></ul></ul>
  22. 22. Arithmetic Operators (example) <ul><li>vi </li></ul><ul><ul><li>#!/bin/csh </li></ul></ul><ul><ul><li>set count=5 </li></ul></ul><ul><li> set count=`expr $count + 1` </li></ul><ul><li> echo $count </li></ul><ul><li>chmod u+x </li></ul><ul><li> </li></ul>
  23. 23. Shell Programming <ul><li>programming features of the UNIX shell: </li></ul><ul><ul><li>Shell variables </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Logic structures </li></ul></ul>
  24. 24. Shell Logic Structures <ul><li>The four basic logic structures needed for program development are: </li></ul><ul><ul><li>Sequential logic </li></ul></ul><ul><ul><li>Decision logic </li></ul></ul><ul><ul><li>Looping logic </li></ul></ul><ul><ul><li>Case logic </li></ul></ul>
  25. 25. Sequential Logic <ul><li>Sequential logic states that commands will be executed in the order in which they appear in the program </li></ul><ul><li>The only break in this sequence comes when a branch instruction changes the flow of execution </li></ul>
  26. 26. Decision Logic <ul><li>Decision logic enables your program to execute a statement or series of statements only if a certain condition exists </li></ul><ul><li>The if statement is the primary decision-making control structure in this type of logic </li></ul>
  27. 27. Decision Logic (continued) <ul><li>if-then </li></ul><ul><li>if ( expr ) simple-command </li></ul><ul><li>if-then-else </li></ul><ul><li>if ( expr ) then </li></ul><ul><li>command-set-1 </li></ul><ul><li>[else </li></ul><ul><li>command-set-2] </li></ul><ul><li>endif </li></ul>
  28. 28. Decision Logic (continued) <ul><li>A simple example </li></ul><ul><ul><li>#!/bin/csh </li></ul></ul><ul><ul><li>if ($#argv != 2) then </li></ul></ul><ul><ul><li>echo $0 needs two parameters! </li></ul></ul><ul><ul><li>echo You are inputting $#argv parameters. </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>set par1 = $argv[1] </li></ul></ul><ul><ul><li>set par2 = $argv[2] </li></ul></ul><ul><ul><li>endif </li></ul></ul>
  29. 29. Decision Logic (continued) <ul><li>Another example: </li></ul><ul><li>#! /bin/csh </li></ul><ul><li># number is positive, zero or negative </li></ul><ul><li>echo &quot;enter a number:&quot; </li></ul><ul><li>set number = $< </li></ul><ul><li>if ( $number < 0 ) then </li></ul><ul><li> echo &quot;negative&quot; </li></ul><ul><li>else if ( $number == 0 ) then </li></ul><ul><li> echo zero </li></ul><ul><li>else </li></ul><ul><li> echo positive </li></ul><ul><li>endif </li></ul>
  30. 30. Decision Logic (continued) <ul><li>Another example: </li></ul><ul><li>#!/bin/csh </li></ul><ul><ul><ul><li>if {( grep UNIX $argv[1] > /dev/null )} then </li></ul></ul></ul><ul><ul><ul><li>echo UNIX occurs in $argv[1] </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>echo No! </li></ul></ul></ul><ul><ul><ul><li>echo UNIX does not occur in $argv[1] </li></ul></ul></ul><ul><ul><ul><li>endif </li></ul></ul></ul><ul><li>Redirect intermediate results into >/dev/null, instead of showing on the screen. </li></ul>
  31. 31. Looping Logic <ul><li>In looping logic , a control structure (or loop) repeats until some condition exists or some action occurs </li></ul><ul><li>You will learn two looping mechanisms in this section: the for loop and the while loop </li></ul><ul><li>You use the for command for looping through a range of values. </li></ul><ul><li>It causes a variable to take on each value in a specified set, one at a time, and perform some action while the variable contains each individual value </li></ul>
  32. 32. The while Loop <ul><li>A different pattern for looping is created using the while statement </li></ul><ul><li>The while statement best illustrates how to set up a loop to test repeatedly for a matching condition </li></ul><ul><li>The while loop tests an expression in a manner similar to the if statement </li></ul><ul><li>As long as the statement inside the brackets is true, the statements inside the do and done statements repeat </li></ul>
  33. 33. Looping Logic <ul><ul><li>while ( expr ) </li></ul></ul><ul><ul><li>command_set </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>foreach var ( worddlist ) </li></ul></ul><ul><ul><li>command_set </li></ul></ul><ul><ul><li>end </li></ul></ul>
  34. 34. Looping Logic <ul><li>Program: </li></ul><ul><ul><li>#!/bin/csh </li></ul></ul><ul><ul><li>foreach person (Bob Susan Joe Gerry) </li></ul></ul><ul><ul><li>echo Hello $person </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><li>Output: </li></ul><ul><li>Hello Bob </li></ul><ul><li>Hello Susan </li></ul><ul><li>Hello Joe </li></ul><ul><li>Hello Gerry </li></ul>
  35. 35. Looping Logic <ul><li>Adding integers from 1 to 10 </li></ul><ul><ul><li>#!/bin/csh </li></ul></ul><ul><ul><li>set i=1 </li></ul></ul><ul><ul><li>set sum=0 </li></ul></ul><ul><ul><li>while ($i <= 10) </li></ul></ul><ul><ul><li> echo Adding $i into the sum. </li></ul></ul><ul><ul><li> set sum=`expr $sum + $i` </li></ul></ul><ul><ul><li>set i=`expr $i + 1` </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>echo The sum is $sum. </li></ul></ul>
  36. 36. Switch Logic <ul><li>The switch logic structure simplifies the selection of a match when you have a list of choices </li></ul><ul><li>It allows your program to perform one of many actions, depending upon the value of a variable </li></ul>
  37. 37. Switch Logic <ul><li>switch ( var ) </li></ul><ul><li>case string1: </li></ul><ul><li>command_set_1 </li></ul><ul><li>breaksw </li></ul><ul><li>case string2: </li></ul><ul><li>command_set_2 </li></ul><ul><li>breaksw </li></ul><ul><li>default </li></ul><ul><li>command_set_3 </li></ul><ul><li>endsw </li></ul>
  38. 38. Switch Logic <ul><li>#!/bin/csh </li></ul><ul><li>if ($#argv == 0 ) then </li></ul><ul><li>echo &quot;No arguments supplied...exiting&quot; </li></ul><ul><li>else </li></ul><ul><li>switch ($argv[1]) </li></ul><ul><li>case [yY]: </li></ul><ul><li>echo Argument one is yes. </li></ul><ul><li>breaksw </li></ul><ul><li>case [nN]: </li></ul><ul><li>echo Argument one is no. </li></ul><ul><li>breaksw </li></ul><ul><li>default: </li></ul><ul><li>echo Argument one is neither yes nor no. </li></ul><ul><li>breaksw </li></ul><ul><li>endsw </li></ul><ul><li>endif </li></ul>
  39. 39. Chapter Summary <ul><li>A high-level language must be converted into a low-level (machine) language before the computer can execute it </li></ul><ul><li>The shell interprets UNIX shell scripts </li></ul><ul><li>UNIX shell scripts, created with the vi or other editor, contain instructions that do not need to be written from scratch, but rather can be selectively chosen from the operating system’s inventory of executable commands </li></ul>
  40. 40. Chapter Summary Section A Continued <ul><li>Linux shells are derived from the UNIX Bourne, Korn, and C shells </li></ul><ul><li>UNIX keeps three types of variables: </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>Environment </li></ul></ul><ul><ul><li>Shell </li></ul></ul><ul><li>The shell supports numerous operators, including many for performing arithmetic operations </li></ul><ul><li>The logic structures supported by the shell are sequential , decision , looping , and case </li></ul>
  41. 41. Homework 1 <ul><li>Please refer to handout. </li></ul>