chapter 8 the bourne again shell
play

Chapter 8 The Bourne Again Shell Dr. Marjan Trutschl - PowerPoint PPT Presentation

Chapter 8 The Bourne Again Shell Dr. Marjan Trutschl marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115 Chapter 8 The Bourne Again Shell Startup Files Processes Redirecting Standard Error History Writing


  1. Chapter 8 The Bourne Again Shell Dr. Marjan Trutschl marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115

  2. Chapter 8 The Bourne Again Shell ¤ Startup Files ¤ Processes ¤ Redirecting Standard Error ¤ History ¤ Writing Simple Scripts ¤ Aliases and Functions ¤ Job Control ¤ Controlling bash ¤ Manipulating the Directory ¤ Processing the Command Stack Line ¤ Parameters and Variables Steve Bourne, the author of the ¤ Locale Bourne shell. Sorry, he’s not actually Jason Bourne.

  3. Startup Files ¤ Startup Actions ¤ It’s useful to know startup routines ¤ You might want to change them ¤ Login Shells ¤ Before bash starts, the login shell runs Hang on tight, it’s going to ¤ It executes /etc/profile to set defaults be a rough slideshow. ¤ Some versions will execute shell scripts at this point ¤ It’s possible to initialize variables here to make them available system-wide ¤ Then it looks for ~/.bash_profile ~/.bash_login and ~/.profile and runs the commands in the first file that it finds

  4. Startup Files ¤ Login Shells (cont) ¤ The default setup is ~/.bash_profile that will call ~/.bashrc which calls /etc/bashrc ¤ It’s generally recommended to make changes to ~/.bash_profile instead of ~/.bashrc, although the differences are obscure ¤ During logout, bash executes ~/.bash_logout ¤ Run ‘source ~/.bashrc’ instead of rebooting If you want to run a script that executes during login or logout or initialize a variable (such as PATH), then call the script or set the value from one of these files.

  5. Redirecting Standard Error ¤ Standard Error: Just Like Standard Output ¤ When things go wrong in your program, then you might not be watching to see the error appear on your terminal ¤ Send the error message to a place that it can be seen ¤ File Descriptors: 0=Input 1=Output 2=Error ¤ So ‘<‘ is short for ‘0<‘ and ‘>’ is short for ‘1>’ ¤ Standard error displays by default on the screen and must be redirected specifically, unlike standard output ¤ To make the standard error go to the same place as the standard output, use ‘2>&1’

  6. Redirecting Standard Error ¤ Try This: ¤ echo “This is y.” into a file named ‘y’ ¤ cat the files ‘x’ and ‘y’ to the screen ¤ The file ‘x’ shouldn’t exist and should throw an error ¤ Redirect the standard output and standard error to new files ¤ cat those files to the screen independently ¤ cat ‘x’ and ‘y’ and pipe the output to tr, using tr to switch the letters to all uppercase ¤ Use ‘2>&1’ to redirect standard error to the pipe instead of the screen

  7. Redirecting Standard Error ¤ Here’s a solution: - This creates a simple file - ‘x’ doesn’t exist, so it displays an error - The error displays by default on the screen, while the standard output moves to the pipe - Using ‘2>&1’ sends both the output and the error to the pipe, so tr does its substitution

  8. Writing Simple Scripts ¤ Writing and Executing Simple Scripts ¤ A shell script is a text file containing commands that execute on the command line ¤ They also have the ability to run control structures, like if/ then/else, case switch and for, while and until loops ¤ Make It Executable ¤ To run a new shell script, use the ‘bash’ or ‘source’ commands and give it to the shell to run ¤ Make it executable using chmod and run it with ‘./’

  9. Writing Simple Scripts ¤ Sample: This script demonstrates the simplest way to make a script, but note that it is still in the working directory and it does very little. The ‘.sh’ suffix is just a naming convention for shell scripts, but is not required by the system.

  10. Writing Simple Scripts ¤ The Shebang #! ¤ It’s also called “shabang,” “hashbang,” “pound-bang” or “hash-pling” ¤ After the ‘#!’, specify the path to the interpreter and the optional arguments ¤ If you’re using bash, then the path is the actual file location of bash, usually /bin/bash ¤ Use the ‘which’ utility to find out where your interpreter is located ¤ Many options are possible, try ‘#!bash -eu’ to create a safer script

  11. Writing Simple Scripts ¤ Here’s a Sample: When using nano, or most of the other editors, it automatically does text highlighting, but will not correct your spelling. Matt Damon is not taking this script. Maybe Nicholas Cage will.

  12. Writing Simple Scripts ¤ Control Operators: Separate and Group Commands ¤ These control operators have very specific meanings: ; Command Separator NEWLINE Command Initiator & Background Task | Pipeline ¤ ‘;’ Separates a command, but does not execute it, so that commands may be grouped together without executing them until they can all run at once ¤ The newline character separates and executes a command ¤ ‘|’ and ‘&’ also do not execute a line

  13. Writing Simple Scripts ¤ Control Operators: Separate and Group Commands || OR Operator && AND Operator () Group Commands \ Escape ¤ The ‘||’ and ‘&&’ operators are short circuiting operators, which evaluate to zero if true or one if false ¤ Parentheses group commands together, like algebra ¤ The ‘\’ doesn’t really control, but is used to escape a newline character in the middle of a long line of code ¤ Note: Jason Bourne cannot be escaped.

  14. Job Control ¤ Job Control ¤ As discussed earlier, jobs can be moved to background and foreground, using ‘bg’, ‘fg’, ‘ctrl-z’ and ‘&’ ¤ Use the ‘jobs’ command to list running jobs ¤ Use ‘fg’ to move a background job to the foreground ¤ Use ‘ctrl-z’ to suspend the job: the job will wait in the background ¤ Use ‘bg’ to run a suspended job in the background ¤ Resource: ¤ http://tldp.org/LDP/abs/html/x9644.html ¤ http://tldp.org/LDP/abs/html/index.html

  15. Manipulating the Directory Stack ¤ Using the dirs Utility ¤ The dirs utility allows quick and easy switching between directories ¤ It works as a stack, allowing directories to be pushed on and popped off using ‘pushd’ and ‘popd’ ¤ This utility works in a similar fashion to ‘cd’, but it’s a little quicker in some cases, particularly if you navigate deep in a folder tree and want to go back to your last location ¤ You can do the same thing with ‘cd -'

  16. Parameters and Variables ¤ Shell Parameters ¤ User-created, Keyword, Positional and Special variables ¤ Proper variable names are letters, numbers and underscores, but do not start with a number ¤ Shell variables are only modified from the shell, user variables are modified by the user ¤ Keyword variables are inherited by the shell, like $HOME and $PATH that identify specific directories to the system ¤ Positional parameters reflect different aspects of shell interaction, such as the last executed command

  17. Parameters and Variables ¤ User Created Variables ¤ When assigning values to variables, be careful with the spaces and the quotes ¤ Don’t use spaces around the ‘=‘ ¤ Only use ‘$’ when calling the variable, not when assigning ¤ Double quotes around the value and the variable preserves whitespace ¤ A good way to play with this is to use echo ¤ To see all the variables that are set for the shell, use the ‘set’ utility ¤ Use ‘unset’ to clear the value of a variable

  18. Parameters and Variables ¤ Using Variables: Example - Set a variable equal to another variable - Don’t forget the ‘$’ - Use the set utility - No Spaces! - Everything is a string - Use the let utility to do math with variables This is what happens to Jason Bourne when he learns math.

  19. Parameters and Variables ¤ Variable Attributes ¤ Variables can be declared with the declare utility ¤ Arrays can be implicitly declared, too

  20. Parameters and Variables ¤ Implicitly Declare an Array, Without declare ¤ Keyword Variables - Variables that store directory data ¤ HOME: Your home directory ¤ PATH: Where the shell looks for programs ¤ MAIL: It’s your mailbox Use ‘export’ to update environment variables

  21. Locale ¤ Locale ¤ The locale variables are set to compensate for regional and language differences, like time zones ¤ Display locale data with the locale utility ¤ Change these variables by altering the ~/.bash_profile or ~/.profile using the ‘export’ command ¤ Time zones can be set for the whole system, or for each user ¤ Use the date utility to check the date and time, and the tzselect utility to change the time zone

  22. Processes ¤ Creating Processes ¤ Each command executed by the Linux kernel is a process ¤ bash is a process, and executing a command from bash will fork a new process, or “spawn a subshell” ¤ Builtins are part of the shell and do not fork a new process ¤ Shell variables are local to a process, but environment variables are available to child processes ¤ The shell looks through the directories specified in PATH for every command called by a simple filename, but builds a hash table to save time after each call ¤ Call ‘hash -r’ to clear the hash table

  23. History ¤ History ¤ It tracks everything you do, whether you’re naughty or nice ¤ Flip through it using the arrow keys, or grep it by the keyword ¤ Use the fc utility to get a convenient list of everything

  24. Aliases and Functions ¤ Aliases ¤ Edit your alias file and shorten the commands you type ¤ nano ~/.bash_aliases ¤ Typing the new command executes the aliased command

Recommend


More recommend