1 / 20

Chapter 8 The Bourne Again Shell

Graham Glass and King Ables, UNIX for Programmers and Users , Third Edition, Pearson Prentice Hall, 2003. Original Notes by Michael Weeks. Chapter 8 The Bourne Again Shell. Bourne Again SHell (bash) ‏. Typical shell on Linux systems Written by Brian Fox of the Free Software Foundation

oralee
Download Presentation

Chapter 8 The Bourne Again Shell

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. Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Original Notes by Michael Weeks Chapter 8The Bourne Again Shell

  2. Bourne Again SHell (bash)‏ • Typical shell on Linux systems • Written by Brian Fox • of the Free Software Foundation • Backwards Compatible with Bourne Shell • Bourne Shell info applies here, too • Runs /etc/profile first, then: • At login, runs ~/.bash_profile • At startup, runs ~/.bashrc

  3. Variables • Setting and accessing variables • same as before • For example • numbers=”one two” • echo $numbers

  4. Arrays • Defining arrays • declare -a teams • teams[0]=”Hawks” • teams[1]=”Falcons” • Must use { } to access $ echo The ${teams[1]} play football. The Falcons play football.

  5. Accessing Arrays • Access all entries with [*] • Find the number of entries with {#array[*]} $ echo ${teams[*]} Hawks Falcons $ echo I know of ${#teams[*]} teams. I know of 2 teams.

  6. Building Lists • Options • Assign entries with number • Give list in parenthesis • Use \ to continue a line, as needed $ declare -a list1 $ list1[0]="Thrashers" $ list1[1]="Braves" $ list1[2]="Hawks" $ list1[3]=Falcons $ echo ${list1[*]} Thrashers Braves Hawks Falcons $ declare -a list2 $ list2=("Thrashers" "Braves" \ > "Hawks" Falcons)‏ $ echo ${list2[*]} Thrashers Braves Hawks Falcons

  7. Deleting Lists • Command unset • Delete an entry or entire list $ echo ${list2[*]} Thrashers Braves Hawks Falcons $ unset list2[1] $ echo ${list2[*]} Thrashers Hawks Falcons $ unset list2 $ echo ${list2[*]} $

  8. Aliases • Command alias defines commands • That is, put these in .bashrc alias qubit=”ssh -l myacct qubit.cs.gsu.edu” alias squbit=”sftp myacct@qubit.cs.gsu.edu” $ alias qubit="ssh -l cscmcw qubit.cs.gsu.edu" $ alias alias ls='ls --color=auto' alias qubit='ssh -l cscmcw qubit.cs.gsu.edu' $ qubit cscmcw@qubit.cs.gsu.edu's password: Last login: Tue Oct 16 17:43:26 2007 from carmaux -bash-3.00$

  9. History • Bash keeps track of the commands you type • So do other shells, e.g. Korn, Csh • $HISTSIZE defines # commands to remember • Command history shows list

  10. History Shortcuts • !! repeats last command • !34 repeats command #34 • !gre repeats the last command starting with gre • Such as grep ... • ^str1^str2 • repeats last command, substituting str1 for str2 $ mroe myfile -bash: mroe: command not found $ ^ro^or more myfile ...

  11. Bash Arithmetic • Instead of expr, use (( operation ))‏ • +, -, ++, --, *, /, % • <=, >=, <, >, ==, !=, !, &&, || • Declare an integer • declare -i intName • Integers are faster to evaluate

  12. Arithmetic Example #!/bin/bash # # Demonstrate some Bash arithmetic # declare -i x=10 while (( x > 0 ))‏ do echo x is $x (( x-- ))‏ done

  13. Another Example #!/bin/bash # declare -i x=20 # find max area x*y, declare -i y # where x+y=20 declare -i maxarea=-1 declare -i area while (( $x > 0 )); do y=20 while (( $y > 0 )); do (( area = $x * $y ))‏ if (( area > maxarea && x + y == 20 ))‏ then maxarea=$area fi (( y-- ))‏ done (( x-- ))‏ done echo "Max area is $maxarea"

  14. If statement • Allows “elif” (else if) as well as “else” #!/bin/bash # echo "enter your favorite class: " read favorite # Use " " around $favorite so it keeps multiple # words together. if [ "$favorite" == "Systems Programming" ]; then echo "good choice." elif [ "$favorite" == "3320" ]; then echo "good choice." else echo "what are you thinking?!" fi

  15. Functions • Functions may be invoked as shell command • Parameters passed to functions are accessible via the standard positional parameter mechanism • Functions must be defined before they are used • Two ways to define a function function name name() { { list of commands list of commands } }

  16. examples F() { echo parameter 1 = $1 echo parameter list = $* } # main program F 1 F cat dog goat F() { (( returnValue = $1 * $2 )) return $returnValue } # main program F 3 4 result=$? Echo return value from function was $result

  17. Local Variables • A variable created using the typeset function is limited in scope to the function in which it is created and all of the functions that the defining function calls. • If a variable of the same name already exists, its value is over-written and replaced when the function returns.

  18. example F() { typeset x (( x = $1 * $2 )) echo local x = $x return $x } # main program x=1 echo global x = $x F 3 4 result=$? echo return value from function was $result echo global x = $x

  19. Recursion • A function calls itself

  20. Review • Bash overview • Compatible with Bourne shell • Allows arrays • Has a history mechanism (as do others)‏ • Handles arithmetic well • Control structures are very similar to Bourne

More Related