1 / 96

Chapter 8

Chapter 8. The Bourne Again Shell. Halleluiah!. Topics. Background Creating a Simple Shell Script Command Separation and Grouping Redirecting Standard Error Job Control Directory Stack Manipulation. Background.

afric
Télécharger la présentation

Chapter 8

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. Chapter 8 The Bourne Again Shell Halleluiah!

  2. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  3. Background • The original Bourne shell was developed by Steve Bourne of AT&T Bell Laboratories. • Many shell scripts have been written to help manage a UNIX system. • The bash has been written to mimic the Bourne shell • Bourne and bash use sh for invocation

  4. Background • bash is not POSIX 1003.2 compliant • Efforts are underway to make it compliant. • bash can more closely comply to POSIX with the –posix option.

  5. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  6. Shell script is not monetary • Set of command stored in a file. • Used to support operational functions by combining many command into one group • Provides flow control commands which can alter the order of command execution.

  7. Script Execution • Enter the filename on the command line • Must have execute permission • Must be in the PATH

  8. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  9. Command Separation • Newline (nl) X’0D0A’ • ends command and initiates execution • Semicolon (;) • just separates commands • Backslash (\) X’5C0D0A’ • at end of line and before you type return Allows command to be continued

  10. Command Separation (cont.) • Ampersand (&) • execute task in the background • Pipe ( | ) • pipe

  11. Command Grouping • Parenthesis used to group commands • causes Shell to create a subshell • additional processes are created as required when the subshell runs the commands within the parenthesis • (ls ; date; w) ; more • (ls ; date; w) | more

  12. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  13. Streams Revisited • Three streams • standard in < or 0< • standard out > or 1> • standard error 2>

  14. Streams standard I/O • cat x y • if x exists and y does not, contents of x and error message due to y are sent to terminal • both standard out and standard error default to the terminal

  15. Streams Continued • cat x y 2>error.log • standard error is sent to a file to separate it from the expected results of the command • cat x y 2>>newfile 1>>newfile • standard out is redirected to newfile

  16. Here boy << • The Here Document • Allows in-stream data to feed a script. • Must start with << and a data delimiter character • Data delimiter character on line by itself - terminates

  17. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  18. Job Control • Ampersand & • tells the Operating system to run the job in the background • User will still be able to interact with the shell • Pure Bourne shell has limited ability. Can not deal with a specific job it has put into background after initial creation. C shell much better.

  19. Job Control (continued) • First two jobs in background, c in foreground • a & b & c • Entire sequence put into background • a | b | c & • All three jobs executed in background • a & b & c & • jobs – builtin function displays the jobs running in the background

  20. Job Control (continued) …]$ xman& [1] 1246 …]$ date& [2] 1247 …]$ Tue Sep 11 6:17 PDT 2001 [2]+ Done date …]$ find /usr –name ace –print > out & [2] 1269 …]$ jobs [1]- Running xman & [2]+ Running find /usr –name ace …

  21. Job Control (continued) …]$ (sleep 5;cat>mytext)& [1] 1343 …]$ date Tue Sep 11 6:30 PDT 2001 [1]+ Stopped (tty input) (sleep 5;cat>mytext) …]$ fg (sleep 5;cat>mytext) Remember to let the cat out!

  22. Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation

  23. Directory Stack Manipulation • You can store a list of frequently used directories in a stack • Push-down (LIFO) • The three stack commands • dirs • pushd • popd

  24. Directory Stack Manipulation • dirs – displays all the directories in the stack • When stack is empty displays the Working Directory (~ is your home directory)

  25. Directory Stack Manipulation • pushdsomeDirectoryName– • Change working directory • “pushes” directory onto the stack • Display the directory stack

  26. Directory Stack Manipulation • pushd – • “swaps” top of stack with next element • Change working directory to top of stack • Display the directory stack

  27. Directory Stack Manipulation • pushd +2 – • “swaps” top of stack with +2 element • Change working directory to top of stack • Display the directory stack

  28. Directory Stack Manipulation • popd – • “pops” removes top entry from stack • Change working directory to top of stack

  29. Directory Stack Manipulation • popd +2 – • Removes the 3rd entry from stack • DOES NOT CHANGE Working Directory

  30. Topics • Processes • Parameters and Variables • History • Alias • Command-line Expansion

  31. Processes and Subshells • A process is the execution of a command • login to LINUX • execution of a LINUX utility • execution of a shell script creates a new process • script commands each start a new process • Process structure is hierarchical • Parent processes spawn or fork children

  32. PID’s … • Process ID’s • Sequentially Assigned by the system when a process is started • ps • Displays all processes for your userid

  33. ps–AlAll Please • Displays a long list of all processes including those not attached to a terminal. • Command preceded by – was initiated by the init process

  34. Process status • All processes have a status that can change to: • D – Sleeping Do not interrupt (Can’t) • N – Reduced priority • R – Available for execution (running) • S – Sleeping (Waiting) • T – Stopped or being traced • Z – Zombie waiting for child to terminate

  35. Process Flow • User logs in: shell process is created • User issues command, enters return • Shell creates a subshell • child process is forked or spawned • unless the command is built into the bourne shell process

  36. Process flow (cont.) • Subshell is a clone of the parent shell • Subshell tries to exec the command • If it’s a program, the program runs • If it’s a shell script, exec fails and subshell interprets commands. • If it’s neither command fails

  37. Process Flow • Parent Shell sleeps until child shell finishes • (unless job was executed in background) • Variables that are used in a parent can be sent to a child, but the reverse is not true.

  38. Process Flow • Shell Scripts need to have execute permission. You just type the file name as you would a command. • Alternative (new subshell): sh file • Alternative (current shell): • file

  39. Starting bash • When bash is called, various startup files are run to issue commands and define environmental variables • Which startup file(s) begin depends upon how bash is called • Use these startup files to make the shell work for you

  40. Login shells • Login shells are called with the --login option • We don’t usually do this – it’s done for us • Will first run /etc/profile, which contains global default settings

  41. Login shells • Next, it will attempt to run ~/.bash_profile ~/.bash_login ~./profile

  42. Login shells, con’t • Commands in those three files can override the defaults in /etc/profile • Once one of those files are executed, control is passed to the user • When the user logs out, bash runs ~/.bash_logout • Usually clears temporary information

  43. Interactive nonlogin shells • Shells that you spawn yourself by typing bash • Runs ~/.bashrc • This file is usually called by ~/.bash_profile for login shells • Often this file will also run /etc/bashrc, which again contains system defaults

  44. Noninteractive shells • These are the shells used to run scripts • These shells do not run any of the aforementioned startup files • They do however inherit the calling shell’s environmental variables marked for export

  45. Noninteractive shells • So basically anything you set for the login shell is set for the noninteractive shell

  46. Working with Startup Files • In the end, these startup files are just shell scripts • Obey the same rules and conventions that scripts must use for the particular shell you’re using • Most important files are probably .bashrc and .bash_profile

  47. Startup Files, con’t • Simplify – have .bash_profilecall .bashrc • Just edit the startup files in your favorite editor • When done, you can apply changes to your current shell using either . or source • Otherwise, logout and login again

  48. Creating a Shell Script • Use a text editor like vi • First line should start with #! Followed by the absolute pathname of the shell that is to interpret the script. (default is C shell) • #!/bin/sh • Lines which start with a # are comments • (except the special line mentioned above)

  49. Dot s de way toExecuteit • The exec command • Executes scripts or programs • Runs under the same PID • Provides access to the original environment variables • Terminates current process.

  50. Dots de way toExecit • The dot command • Executes only scripts • Runs under the same PID • Provides access to the current environment variables • Returns to next command in script

More Related