Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.

Slides:



Advertisements
Similar presentations
Chapter 9: The TC Shell Everything you thought you knew is now wrong.
Advertisements

A Guide to Unix Using Linux Fourth Edition
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 3.
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Chapter 8 The Bourne Again Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
Introduction to Shell Script Programming
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 2061 Shells Also known as: Unix Command Interpreter.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
Workbook 6 – Part 1 The Bash Shell
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
CS465 - UNIX The Bourne Shell.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
1 © 2012 John Urrutia. All rights reserved. Chapter 8 The Bourne Again Shell Halleluiah!
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Linux+ Guide to Linux Certification, Second Edition
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Shells.
Implementation of a simple shell, xssh
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Agenda Korn Shell Functions Korn Shell Variables
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
Chapter 8 The Bourne Again Shell Halleluiah!.
UNIX and Shell Programming (06CS36)
CHAPTER 8 (skip , ) CHAPTER 10
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini

In this chapter … Background Startup files Basic Scripting Variables Processes History Aliases

Some history Very early UNIX shell was written by Steve Bourne – called the Bourne Shell (sh) Another popular shell from UNIX days was the C shell (csh) – a C programmer’s best friend Linux sought to emulate Bourne and make it better – Bourne Again Shell (bash) UNIX stole from bash, David Korn released the Korn shell (ksh) POSIX standards …

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 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 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 Simplify – have.bash_profile call.bashrc

Startup Files, con’t 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

Symbol Commands in bash ( ) – run contents in a separate subshell –Creates a separate process with unique ID $( ) – command substitution –Runs contents and replaces with output (( )) – evaluate contents as arithmetic expression or equation $(( )) – same as above, just expressions (no equal signs)

Redirection Revisited Standard Input < (or 0<) Standard Output > (or 1>) Standard Error 2> Recall that we can redirect standard out and standard error to different locations When you use the pipe ( | ), remember it only passes standard output to standard input; standard error still goes to it’s normal location

Redirection con’t Ex: file x exists, file y does not cat x y | tr “[a-z]” “[A-Z]” THIS IS FILE X cat: y: No such file or directory Notice that only standard out is piped to tr So how would we pipe standard error as well?

Redirection con’t Duplicating streams [n]>&[m] –Redirects stream n to stream m So to send std error to std out, 2>&1 So for our previous example cat x y 2>&1 | tr “[a-z]” “[A-Z]” THIS IS FILE X CAT: Y: NO SUCH FILE OR DIRECTORY

Writing simple scripts A shell script is, in its simplest form, just a list of shell commands in a file File must have read and execute permissions to call implicitly If script is in PATH, just call it from command line If not in PATH, include pathname (such as./myscript)

Shell Scripts To specify what shell (or program) to use to execute commands, make first line: #![absolute path to program/shell] Without this line, whatever shell you are in when calling the script is used To make comments in the script, begin the line with #

Shell Scripts con’t When a command is issued, fork creates a new process (subshell) If the command is a binary executable, exec makes a system call to run the binary If the command is a shell script, the subshell itself runs the script When done, subshell closes and returns control to calling shell

Separating and Grouping Commands You can have multiple commands on a single command line, they just need to be separated –;–; –NEWLINE –&–& To continue a long command on several lines, end a line with \ and press return

Separating and Grouping con’t To group commands to control execution, use parenthesis Ex: (a; b; c) | (d; e) Commands a, b, and c are executed first, and once c finishes standard output is piped to the result of commands d and e Each set of parenthesis runs in its own subshell

Shell Variables Variables are stored in memory and contain data Variables can be pre-defined by operating system, shell, or can be user created Variable names can consist of letters, digits and underscores –Cannot start with a number

Shell Variables con’t In bash, to assign a value to a variable: VARIABLE=value Notice there are no spaces around the equal sign If VARIABLE does not exist, it is created and set If it does exist, the current value is overwritten

Keyword Variables Certain variables are set already by the shell Have very standard names and are essential Ex: –HOME – contains user’s home directory path –PATH – contains user’s path Change these carefully – you can confuse the shell and lock yourself out of places

Shell Variables To reference a variable’s contents, proceed the variable name with a $ Ex: echo HOME HOME echo $HOME /home/jhowell $variable is actually short for ${variable}

Shell Variables con’t To remove the value of a variable, just set it equal to a null string –Ex. myvar= To remove a variable altogether, use unset –Ex. unset myvar

Shell Variable Attributes You can use declare or typeset to define attributes when you create a variable -a: create an array -f: create a function -i: create an integer -r: make variable readonly -x: make variable global (export)

Attributes con’t Most of these we’ll come back to when we start doing shell scripting Making a variable readonly can also be accomplished by the readonly command Marking a variable for export (ie, making it available to subshells) can also be accomplished using export

Keyword Variables HOME – your home directory PATH – your path MAIL – location of mail storage PS1 – your primary prompt PS2 – secondary prompt IFS – input field separator (BEWARE!!!) Plus many more … just type set to see current shell variables (keyword variables usually all caps)

Processes Processes have a hierarchy similar to the file system init process (PID 1), is the root process It then forks other processes (its children) Those processes fork more processes Etc … so we get a tree-like structure To see this structure use ps --forest

Processes con’t When a process (such as the shell) calls another process (like a command or script), it calls fork to create a new process, then goes to sleep If the process is sent to the background, fork is called but sleep is not If a builtin is called, neither fork nor sleep is called

Processes con’t fork creates a new process, assigns it a unique PID, and associates it with its parent via the PPID When a process forks a child process, the current shell variables *are not* passed on, unless marked for export

History bash contains command history mechanism inherited from the C Shell Use the builtin history to view your current history The history is, by default, stored in your home directory in a file called.bash_history History entries are ordered, the last entry being the most recently executed

History con’t There are lots of ways to take advantage of the history Use the up and down arrows to cycle it Use !event_num to re-execute a previous command There’s also a lot more with fc and the Readline Library

fc fix command fc –l lists recent commands fc –l [string] lists most recent commands, starting with one that contains string fc [string] will open an editor to edit command FCEDIT variable lets you set the editor

Readline Completion The Readline library brings us the wonders of tab completion Command completion Pathname completion Start typing, then hit tab to auto-complete If there is more than one match, hit tab again to see a list

Aliases An alias is just another name for another command (or commands) Ideally, the alias is shorter and/or easier to remember Syntax (bash): alias alias=‘commands_to_alias’ Ex: alias ll=‘ls -l --colors’ Single vs. double quotes – variable substitution

functions Like a shell script stored in memory Set like a variable (and use unset to delete) [function] funct_name() {.. } Or use typeset or declare with –f Like a function? Wanna keep it? .bashrc

Command Line Processing History Expansion Alias Substitution Command Line Expansion –Brace Expansion –Tilde Expansion –Parameter/variable Expansion –Arithmetic Expansion –Command Substituion –Word Splitting –Pathname Expansion –Process Substitution

Brace Expansion Used to specify multiple filenames when pathname expansion doesn’t really apply Ex: mkdir ver_{A,B,C,Final} Expands to mkdir ver_A ver_B ver_C ver_Final

Tilde Expansion ~ becomes the path to your home directory ~username becomes to the path to username’s home directory ~+ becomes the current working directory ~- becomes the previous working directory

Arithmetic Expansion $((expression)) is evaluated Any variable names inside are substituted for their values You can omit the $’s in front of the variable names within the expression

Command Substitution $(command) is replaced with the output of command Could also be written as `command` Command is run in a separate subshell, usual rules apply

Process Substitution Substitutes a process where a filename is required <(command) places the output of the command as a filename argument >(command) places the input of the command as a filename argument Ex: uniq <(cat list | sort)