Linux+ Guide to Linux Certification, Third Edition

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

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.
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.
Linux+ Guide to Linux Certification, Second Edition
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 C Programming
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
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.
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
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.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
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.
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 11: Shell.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
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.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
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.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Lesson 5-Exploring Utilities
SUSE Linux Enterprise Desktop Administration
Shell Features CSCI N321 – System and Network Administration
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Agenda Control Flow Statements Purpose test statement
CHAPTER 8 (skip , ) CHAPTER 10
Chapter Four UNIX File Processing.
Linux Shell Script Programming
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Presentation transcript:

Linux+ Guide to Linux Certification, Third Edition Chapter 7 Working with the BASH Shell

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, 3e

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, 3e

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

Command Input and Output (continued) Standard input (stdin): file descriptor referring to information processed by the command during execution Often user input typed on keyboard Standard output (stdout): file descriptor referring to normal output of the command Displayed on terminal Standard error (stderror): file descriptor referring to error messages generated by the command Linux+ Guide to Linux Certification, 3e

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

Redirection Redirect stdout and stderr from terminal screen to a file Use “>” shell metacharacter Can redirect stdout and stderr to separate files using file descriptor numbers Use separate filenames for stdout and stderr The file is automatically created if it did not exist prior to redirection, and is overwritten if it did exist Use “>>” to append additional output to an existing file Linux+ Guide to Linux Certification, 3e

Redirection (continued) Redirecting stdin to a file: Use “<“ shell metacharacter tr command: Replace characters in a file sent via stdin Can redirect stdin and stdout for the same command using “<” and “>“ Use different file names Linux+ Guide to Linux Certification, 3e

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

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, 3e

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

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 command: Filter command that receives information from stdin and sends that information to a file, as well as to stdout Linux+ Guide to Linux Certification, 3e

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

Pipes (continued) Can combine redirection and piping Input redirection must occur at beginning of pipe Output redirection must occur at end of pipe sed command: filter command used to search for and replace text strings Can be used to replace some or all appearances of the search term and to delete specific input lines awk command: filter command used to search for text and perform specified action on it Use -F option to set delimiters Linux+ Guide to Linux Certification, 3e

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

Shell Variables Variable: 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, 3e

Environment Variables set command: lists environment variables and current values echo command: view contents of a specified variable Prefix variable name with the $ shell metacharacter Changing value of a variable: Specify variable name followed by equal sign (=) and new value HOME and PWD variables should not be changed Linux+ Guide to Linux Certification, 3e

Table 7-3: Common BASH environment variables Linux+ Guide to Linux Certification, 3e

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

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, 3e

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, 3e

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 which will be executed in sequence Use ; metacharacter Linux+ Guide to Linux Certification, 3e

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, 3e

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

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, 3e

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 Specifies which shell is used to interpret shell script commands Linux+ Guide to Linux Certification, 3e

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/execute permission: Executed like any executable program Use echo command to add descriptions and spaces to script output Linux+ Guide to Linux Certification, 3e

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, 3e

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

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 and places it in a variable Variable name specified by an argument to read command Linux+ Guide to Linux Certification, 3e

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, 3e

Decision Constructs (continued) Figure 7-4: A two-question decision construct Linux+ Guide to Linux Certification, 3e

Decision Constructs (continued) Figure 7-5: A command-based decision construct Linux+ Guide to Linux Certification, 3e

The if Construct Control flow of program based on true/false decisions Most common type of decision construct Syntax: Linux+ Guide to Linux Certification, 3e

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

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

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

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

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

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, 3e

The && and || Constructs Time-saving shortcut constructs When only one decision needs to be made during execution Syntax: command && command command || command &&: Second command executed only if first completes successfully ||: Second command executed only if first fails Linux+ Guide to Linux Certification, 3e

Loop Constructs Loop constructs: execute commands repetitively Alter the flow of a program based on the results of a particular statement Repeat entire program until reach desired result Linux+ Guide to Linux Certification, 3e

The for Construct Can be used to process a list of objects Syntax: for var_name in string1 string2 … … do these commands done During execution sets var_name to a string name, and executes the commands between do and done for that string Repeats for all the strings Linux+ Guide to Linux Certification, 3e

The while Construct Begins with test statement Syntax: Commands within the loop construct are executed as long as the test statement returns true Contains a counter variable Value changes with each iteration of the loop Syntax: while this returns true do these commands done Linux+ Guide to Linux Certification, 3e

Summary Three components are available to commands: standard input, standard output, and standard error Stdin is typically user input taken from the keyboard; Stdout and Stderr are sent to the terminal screen You can redirect the stdout and stderr of a command to a file using redirection symbols Linux+ Guide to Linux Certification, 3e

Summary (continued) Use the pipe (|) symbol to redirect stdout from one command to the stdin of another Most variables available to the BASH shell are environment variables that are loaded into memory after login from environment files You can create your own variables in the BASH shell and export them so that they are available to programs started by the shell Linux+ Guide to Linux Certification, 3e

Summary (continued) The UMASK variable and command aliases are special variables that must be set using a certain command Shell scripts can be used to execute several Linux commands Decision constructs can be used in shell scripts to execute certain Linux commands based on user input or the results of a certain command Loop constructs can be used within shell scripts to execute a series of commands repetitively Linux+ Guide to Linux Certification, 3e