960 likes | 1.1k Views
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.
E N D
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 • 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
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.
Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation
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.
Script Execution • Enter the filename on the command line • Must have execute permission • Must be in the PATH
Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation
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
Command Separation (cont.) • Ampersand (&) • execute task in the background • Pipe ( | ) • pipe
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
Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation
Streams Revisited • Three streams • standard in < or 0< • standard out > or 1> • standard error 2>
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
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
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
Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation
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.
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
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 …
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!
Topics • Background • Creating a Simple Shell Script • Command Separation and Grouping • Redirecting Standard Error • Job Control • Directory Stack Manipulation
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
Directory Stack Manipulation • dirs – displays all the directories in the stack • When stack is empty displays the Working Directory (~ is your home directory)
Directory Stack Manipulation • pushdsomeDirectoryName– • Change working directory • “pushes” directory onto the stack • Display the directory stack
Directory Stack Manipulation • pushd – • “swaps” top of stack with next element • Change working directory to top of stack • Display the directory stack
Directory Stack Manipulation • pushd +2 – • “swaps” top of stack with +2 element • Change working directory to top of stack • Display the directory stack
Directory Stack Manipulation • popd – • “pops” removes top entry from stack • Change working directory to top of stack
Directory Stack Manipulation • popd +2 – • Removes the 3rd entry from stack • DOES NOT CHANGE Working Directory
Topics • Processes • Parameters and Variables • History • Alias • Command-line Expansion
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
PID’s … • Process ID’s • Sequentially Assigned by the system when a process is started • ps • Displays all processes for your userid
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
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
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
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
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.
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
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
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
Login shells • Next, it will attempt to run ~/.bash_profile ~/.bash_login ~./profile
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
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
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
Noninteractive shells • So basically anything you set for the login shell is set for the noninteractive shell
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
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
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)
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.
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