Linux+ Guide to Linux Certification, Second Edition

Slides:



Advertisements
Similar presentations
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 4: More Command Line Interface (CLI) Chapter 7: The Linux Shell By Fred R. McClurg Linux.
Advertisements

A Guide to Unix Using Linux Fourth Edition
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Workbook 6 – Part 1 The Bash Shell
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Linux+ Guide to Linux Certification, Third Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Lesson 5-Exploring Utilities
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSE 303 Concepts and Tools for Software Development
Agenda Control Flow Statements Purpose test statement
Chapter Four UNIX File Processing.
Linux Shell Script Programming
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
LPI Linux Certification
Presentation transcript:

Linux+ Guide to Linux Certification, Second Edition Chapter 8 Working with the BASH Shell

Linux+ Guide to Linux Certification, 2e Objectives Redirect the input and output of a command Identify and manipulate common shell environment variables Create and export new shell variables Edit environment files to create variables upon shell startup Linux+ Guide to Linux Certification, 2e

Objectives (continued) Describe the purpose and nature of shell scripts Create and execute basic shell scripts Effectively use common decision constructs in shell scripts Linux+ Guide to Linux Certification, 2e

Command Input and Output BASH shell responsible for: Providing user interface Interpreting commands Manipulating command input and output Provided user specifies certain shell metacharacters with command File descriptors: Numeric labels that define command input and command output Linux+ Guide to Linux Certification, 2e

Command Input and Output (continued) Standard Input (stdin): File descriptor representing command input Standard Output (stdout): File descriptor representing command output Standard Error (stderror): File descriptor representing command error messages Linux+ Guide to Linux Certification, 2e

Command Input and Output (continued) Figure 8-1: The three common file descriptors Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Redirection Redirect stdout and stderr from terminal screen to a file Use “>” shell metacharacter Can redirect stdout and stderr to separate files Use separate filenames for stdout and stderr Linux+ Guide to Linux Certification, 2e

Redirection (continued) Redirecting stdin to a file: Use “<“ shell metacharacter tr command: Replace characters in a file sent via stdin Linux+ Guide to Linux Certification, 2e

Redirection (continued) Table 8-1: Common redirection examples Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Pipes Send stdout of one command to another command as stdin Pipe: String of commands connected by “|” metacharacters stdout on left, stdin on right Commonly used to reduce amount of information displayed on terminal screen Linux+ Guide to Linux Certification, 2e

Pipes (continued) Figure 8-2: Piping information from one command to another Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Pipes (continued) Can use multiple pipes on command line Pass information from one command to another over a series of commands filter commands: Commands that can take from stdin and give to stdout Can be on either side of a pipe tee commands: Filter commands that also send information to a file Linux+ Guide to Linux Certification, 2e

Pipes (continued) Figure 8-3: Piping several commands Linux+ Guide to Linux Certification, 2e

Pipes (continued) Table 8-2: Common filter commands Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Pipes (continued) Can combine redirection and piping Input redirection must occur at beginning of pipe Output redirection must occur at end of pipe sed filter command: Search for and replace text strings awk filter command: Search for text and perform specified action on it Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Shell Variables Variable: A reserved portion of memory containing accessible information BASH shell has several variables in memory Environment variables: Contain information that system and programs access regularly User-defined variables: Custom variables define by users Special variables Useful when executing commands and creating new files and directories Linux+ Guide to Linux Certification, 2e

Environment Variables set command: Lists environment variables and current values echo command: View contents a specified variable Use $ shell metacharacter Changing value of a variable: Specify variable name followed by equal sign (=) and new value Linux+ Guide to Linux Certification, 2e

Environment Variables (continued) Table 8-3: Common BASH environment variables Linux+ Guide to Linux Certification, 2e

Environment Variables (continued) Table 8-3 (continued): Common BASH environment variables Linux+ Guide to Linux Certification, 2e

Environment Variables (continued) Table 8-3 (continued): Common BASH environment variables Linux+ Guide to Linux Certification, 2e

User-Defined Variables Variable identifier: Name of a variable Creating new variables: Specify variable identifier followed by equal sign and the new contents Features of variable identifiers: Can contain alphanumeric characters, dash characters, or underscore characters Must not start with a number Typically capitalized to follow convention Linux+ Guide to Linux Certification, 2e

User-Defined Variables (continued) Subshell: Shell created by current shell Most shell commands run in a subshell Variables created in current shell are not available to subshells export command: Exports user-defined variables to subshells Ensures that programs started by current shell have access to variables env command: Lists all exported environment and user-defined variables in a shell Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Other Variables Not displayed by set or env commands Perform specialized functions in the shell e.g., UMASK variable alias command: Creates shortcuts to commands Use unique alias names Aliases stored in special variables Can create single alias to multiple commands Use ; metacharacter Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Environment Files When exiting BASH shell, all stored variables are destroyed Environment files: Store variables and values Executed each time BASH shell is started Ensures variables are always accessible Linux+ Guide to Linux Certification, 2e

Environment Files (continued) Common BASH shell environment files (in order they are executed): /etc/profile ~/.bash_profile ~/.bash_login ~/.profile Hidden environment files allow users to set customized variables Linux+ Guide to Linux Certification, 2e

Environment Files (continued) To add a variable, add a line to environment file Use command line syntax Any command can be placed inside any environment file e.g., alias creation .bashrc (BASH run-time configuration): First hidden environment file executed at login Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Shell Scripts Shell script: Text file containing a list of commands or constructs for shell to execute May contain any command that can be entered on command line Hashpling: First line in a shell script Defines which shell is used to interpret shell script commands Linux+ Guide to Linux Certification, 2e

Shell Scripts (continued) Executing shell scripts with read permission: Start another BASH shell, specify the shell script as an argument Executing shell scripts with read/write permission: Executed like any executable program Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Escape Sequences Character sequences having special meaning in the echo command Prefixed by \ character Must use –e option in echo command Linux+ Guide to Linux Certification, 2e

Escape Sequences (continued) Table 8-4: Common echo escape sequences Linux+ Guide to Linux Certification, 2e

Reading Standard Input Shell scripts may need input from user Input may be stored in a variable for later use read command: Takes user input from stdin Places in a variable specified by an argument to read command Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Decision Constructs Most common type of construct used in shell scripts Alter flow of a program: Based on whether a command completed successfully Based on user input Linux+ Guide to Linux Certification, 2e

Decision Constructs (continued) Figure 8-4: A sample decision construct Linux+ Guide to Linux Certification, 2e

Decision Constructs (continued) Figure 8-5: A sample decision construct Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e The if Construct Control flow of program based on true/false decisions Syntax: Linux+ Guide to Linux Certification, 2e

The if Construct (continued) Common rules governing if constructs: elif (else if) and else statements optional Unlimited number of elif statements do these commands section may consist of multiple commands One per line do these commands section typically indented for readability End of statement must be “if” this is true may be a command or test statement Linux+ Guide to Linux Certification, 2e

The if Construct (continued) test statement: Used to test a condition Generates a true/false value Inside of square brackets ( [ … ] ) Must have spaces after “[” and before “]” Special comparison operators: –o (OR) –a (AND) ! (NOT) Linux+ Guide to Linux Certification, 2e

The if Construct (continued) Table 8-5: Common test statements Linux+ Guide to Linux Certification, 2e

The if Construct (continued) Table 8-6: Special operators in test statements Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e The case Construct Compares value of a variable with several different patterns of text or numbers Syntax: Linux+ Guide to Linux Certification, 2e

The case Construct (continued) If a match is found, commands to right of pattern are executed Must end with esac Linux+ Guide to Linux Certification, 2e

The && and || Constructs Time-saving shortcut constructs When only one decision needs to be made during execution Syntax: command && command command || command Linux+ Guide to Linux Certification, 2e

The && and || Constructs (continued) &&: Second command executed only if first completes successfully ||: Second command executed only if first fails Linux+ Guide to Linux Certification, 2e