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

chapter 8 the bourne again shell
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Chapter 8 The Bourne Again Shell

  • Dr. Marjan Trutschl

marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115

slide-2
SLIDE 2

Chapter 8 The Bourne Again Shell

¤ Startup Files ¤ Redirecting Standard Error ¤ Writing Simple Scripts ¤ Job Control ¤ Manipulating the Directory Stack ¤ Parameters and Variables ¤ Locale ¤ Processes ¤ History ¤ Aliases and Functions ¤ Controlling bash ¤ Processing the Command Line

Steve Bourne, the author of the Bourne shell. Sorry, he’s not actually Jason Bourne.

slide-3
SLIDE 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 ¤ It executes /etc/profile to set defaults ¤ 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

Hang on tight, it’s going to be a rough slideshow.

slide-4
SLIDE 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.

slide-5
SLIDE 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’

slide-6
SLIDE 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

slide-7
SLIDE 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

  • utput moves to the

pipe

  • Using ‘2>&1’ sends

both the output and the error to the pipe, so tr does its substitution

slide-8
SLIDE 8

Writing Simple Scripts

¤ Writing and Executing Simple Scripts

¤ A shell script is a text file containing commands that execute

  • n 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 ‘./’

slide-9
SLIDE 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.

slide-10
SLIDE 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

  • ptional 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

slide-11
SLIDE 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.

slide-12
SLIDE 12

Writing Simple Scripts

¤ Control Operators: Separate and Group Commands

¤ These control operators have very specific meanings: ¤ ‘;’ 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 ; NEWLINE & | Command Separator Command Initiator Background Task Pipeline

slide-13
SLIDE 13

Writing Simple Scripts

¤ Control Operators: Separate and Group Commands

¤ 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. || && () \ OR Operator AND Operator Group Commands Escape

slide-14
SLIDE 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

slide-15
SLIDE 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 -'

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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.

slide-19
SLIDE 19

Parameters and Variables

¤ Variable Attributes

¤ Variables can be declared with the declare utility ¤ Arrays can be implicitly declared, too

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

Aliases and Functions

¤ Functions

¤ Functions are like shell scripts that are saved to RAM instead

  • f storage, so they run quickly without forking a new shell

¤ Declare them in ~/.bash_profile or directly from the command line, but they’ll disappear when you logoff ¤ Within functions, variables can be declared as ‘local’ to prevent them from interfering with other shell variables ¤ Use ‘export –f’ in the function call to make it available to child processes

slide-26
SLIDE 26

Controlling bash

¤ Controlling bash

¤ Most utilities, including bash, have command line options ¤ Use bash –help to see a list ¤ Try ‘set -o’ and ‘set +o’ to turn features on and off ¤ Or try the shopt utility ¤ Check the text for an enormous list of options, or use the man page

slide-27
SLIDE 27

Processing the Command Line

¤ Processing the Command Line

¤ The command line is read as a high level programming language, because it is a high level programming language ¤ History expansion refers to the process of running commands from the saved history ¤ Alias substitution uses a short command to execute a task ¤ Parsing and Scanning is done by the shell before executing any task to break down each command into tokens ¤ Once these items are checked, the shell expands the command line before calling programs

slide-28
SLIDE 28

Processing the Command Line

¤ Command Line Expansion

¤ Expansion is similar to algebraic order of operations ¤ Eight different expansions are performed before quote removal

slide-29
SLIDE 29

Processing the Command Line

¤ Order of Expansion

¤ Brace Expansion ¤ Braces must be in this format: {stuff1,stuff2,…stuffn} and they will be evaluated left to right ¤ Braces can be nested and whitespace must be quoted ¤ Try using it to count with ‘..’ to complete a sequence

slide-30
SLIDE 30

Processing the Command Line

¤ Tilde Expansion ¤ Tilde is a special character that can represent the HOME variable ¤ When bash encounters it, it will check for a valid username immediately after it ¤ If it finds a valid username, it substitutes the HOME variable value for the tilde ¤ Otherwise, it takes no action

slide-31
SLIDE 31

Processing the Command Line

¤ Parameter and Variable Expansion ¤ The shell expands variables and parameters declared by a ‘$’, unless followed by ‘()’ ¤ They are not expanded if enclosed in single quotes, but are still expanded in double quotes

slide-32
SLIDE 32

Processing the Command Line

¤ Arithmetic Expansion ¤ The shell evaluates $((anything in here)) as arithmetic ¤ Similar to ‘let’, the ‘$’ is not required in front of variables, but is recommended for consistency ¤ Arithmetic in bash uses the same operators as C ¤ bash is not the best tool for heavy math but works well for integers, use python or perl for floating point

slide-33
SLIDE 33

Processing the Command Line

¤ Command Substitution ¤ Command substitution uses the output of one command as the input of another, similar to the value of a variable ¤ The syntax treats it as a variable, using ‘$(command)’ ¤ This allows commands to be nested inside arithmetic expressions or braces with predictable results ¤ Be careful when scripting expressions to keep it readable This expression evaluates the math first, then uses the command to count the words in a text file using wc, clips the filename off using cut, then adds it in to the other values.

slide-34
SLIDE 34

Processing the Command Line

¤ Arithmetic is denoted by ‘$((‘ and Command Substitution is ‘$(‘, which must be separated by whitespace ¤ Word Splitting can control the output from the command line when set, and using command substitution ¤ Try and install the args utility, but be careful when making changes to the Internal Field Separator (IFS)

slide-35
SLIDE 35

Processing the Command Line

¤ Pathname Expansion ¤ Uses ambiguous file references and creates specific filenames ¤ Useful for programmatically creating unique filenames ¤ Pathname expansion is suppressed by all quotes This example uses brace expansion to iterate through the numbers one through four, then inserts that into the filename that is created using touch, which is called using command substitution, so the pathname expansion will have the data available for it when echo executes.

slide-36
SLIDE 36

Processing the Command Line

¤ Process Substitution ¤ Use the syntax ‘<(command)’ to cause the command to execute and the output to write to a named pipe ¤ After this, it removes single quotes, double quotes and backslashes…and it’s done! This example creates two lists and merges them together into a single list, if the lists are properly sorted.

slide-37
SLIDE 37

Summary

¤ Startup Files ¤ Redirecting Standard Error ¤ Writing Simple Scripts ¤ Job Control ¤ Manipulating the Directory Stack ¤ Parameters and Variables ¤ Locale ¤ Processes ¤ History ¤ Aliases and Functions ¤ Controlling bash ¤ Processing the Command Line

How fast can I get out of here?

slide-38
SLIDE 38

Exercise

¤ Use an editor to write a script, starting with a shebang ¤ Be sure you have some files with some text hanging about to work with ¤ Use ‘wc’ to count lines and words in a couple of files ¤ Save the output to different array positions ¤ Add a line that tries a file that doesn’t exist ¤ Redirect the standard error output to append to a text file ¤ Multiply the first value in your array by the second, store it as the last and display them all (try using ‘bc’) ¤ Use the ‘chmod’ command to make it executable ¤ Execute the script using ‘./’ from the working directory

slide-39
SLIDE 39

Work Cited

¤ Sobell, Mark G. ” The Bourne Again Shell." A Practical Guide to Linux Commands, Editors, and Shell

  • Programming. Upper Saddle River, NJ: Prentice Hall/

Pearson, 2013. N. pag. Print. ¤ Wikipedia. Wikimedia Foundation, n.d. Web. 21 Dec. 2015.