Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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.
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
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.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification Chapter Four Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Third Edition
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
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.
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Workbook 6 – Part 2 The Bash Shell
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
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.
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.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
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.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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 Chapter Eight 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 Describe the purpose and nature of shell scripts Create and execute basic shell scripts Effectively use common decision constructs in shell scripts

Command Input and Output The BASH shell is responsible for providing a user interface and interpreting commands entered on the command line The BASH shell can also manipulate command input and output, provided the user specifies certain shell metacharacters on the command line alongside the command File descriptors –Numeric labels used to define command input and command output

Command Input and Output There are three file descriptors available to each command that can be manipulated by the BASH shell: –Standard Input (stdin) –Standard Output (stdout) –Standard Error (stderror)

Command Input and Output Standard Input (stdin) –Represent information inputed to a command during execution Standard Output (stdout) –Represents the desired output from a command Standard Error (stderror) –Represents any error message generated by a command

Command Input and Output Figure 8-1: The three common file descriptors

Redirection The BASH shell can redirect Standard Output and Standard Error from the terminal screen to a file on the filesystem using the “>” shell metacharacter followed by absolute or relative pathname of the file You can redirect both Standard Output and Standard Error to separate files at the same time It is important to use separate filenames to hold the contents of Standard Output and Standard Error

Redirection Table 8-1: Common redirection examples

Redirection Table 8-1 (continued): Common redirection examples

Pipes Redirection only occurs from a command to a file and vice versa You may also send the Standard Output of one command to another command as Standard Input Pipe –A string of commands connected by “|” metacharacters

Pipes Figure 8-2: Piping information from one command to another

Pipes Figure 8-3: Piping several commands

Pipes Table 8-2: Common filter commands

Shell Variables A BASH shell has several variables in memory at any one time –A variable is simply a reserved portion of memory containing information that may be accessed Most variables in the shell are referred to as environment variables since they are typically set by the system and contain information that the system and programs access regularly

Shell Variables You may create your own custom variables –These variables are called user-defined variables In addition to environment and user-defined variables, there are special variables that are useful when executing commands and creating new files and directories

Environment Variables There are many environment variables Environment variables are set by default in the BASH shell To see a list of these variables and their current values, you may use the set command

Environment Variables Table 8-3: Common BASH environment variables

Environment Variables Table 8-3 (continued): Common BASH environment variables

Environment Variables Table 8-3 (continued): Common BASH environment variables

User-Defined Variables Variable identifier –The name of a variable When creating new variables, it is important to note the following features of variable identifiers: –They can contain alphanumeric characters, the dash character, or the underscore character –They must not start with a number –They are typically capitalized to follow convention

User-Defined Variables Most commands that are run by the shell are run in a separate subshell –The subshell is created by the current shell –Variables created in the current shell are not available to those subshells and the commands running within them

Other Variables There are other variables that are not displayed by the set or env commands –These variables perform specialized functions in the shell The UMASK variable used earlier in this textbook us an example of a special variable that performs a special function in the BASH shell and must be set by the umask command

Environment Files When you exit your BASH shell, all variables stored in memory are destroyed along with the shell itself Thus to ensure that variables are accessible to a shell at all times, you must place variables in a file that is executed each time you log in and start a BASH shell –These files are called environment files

Environment Files Some common BASH shell environment files and the order they are executed in are listed below: –/etc/profile –~/.bash_profile –~/.bash_login –~/.profile

Shell Scripts Shell script –Text files that contain a list of commands or constructs for the shell to execute in order Hashpling –The first line in a shell script, which defines the shell that will be used to interpret the commands in the script file

Escape Sequences Escape sequences –Character sequences that have special meaning inside the echo command Table 8-4: Common echo sequences

Escape Sequences Table 8-4: Common echo sequences

Reading Standard Input There may be times where a shell script may need input from the user executing the program –This input may then be stored in a variable for later use The read command takes user input from Standard Input and places it in a variable specified by an argument to the read command

Decision Constructs Figure 8-4: A sample decision construct

Decision Constructs Figure 8-5: A sample decision construct

The if Construct Common rules that govern if constructs: –elif (else if) and else statements are optional –You may have an unlimited number of elif statements –The do these commands section may consist of multiple commands, one per line

The if Construct Common rules that govern if constructs (continued): –The do these commands section is typically indented from the left hand side of the text file for readability, but does not need to be –The end of the statement must be a backwards “if” fi –The this is true of the if syntax seen earlier may be a command or a test statement

The if Construct Table 8-5: Common test statements

The if Construct Table 8-5 (continued): Common test statements

The if Construct Table 8-6: Special operators in test statements

The case Construct The case statement compares the value of a variable with several different patterns of text or numbers If there is a match, then the commands to the right of the pattern will be executed As with the if construct, the case construct must be ended by a backwards “case” (esac)

The && and || Constructs Although the if and case constructs are versatile, there are some shortcut constructs that take less time when only one decision needs to be made during the execution of a program –These constructs are && and || –The syntax of these constructs is listed below: command && command command || command

Chapter Summary There are three components available to commands Standard Input is typically user input taken from the keyboard Standard Output and Standard Error are sent to the terminal screen by default You may redirect the Standard Output and Standard Error of a command to a file using redirection symbols

Chapter Summary To redirect the Standard Output from one command to the Standard Input of another, you must use the pipe symbol Most variables available to the BASH shell are environment variables, which are loaded into memory after login from environment files You may create your own variables in the BASH shell and export them such that they are available to programs started by the shell

Chapter Summary 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 may be used within shell scripts to execute certain Linux commands based on user input or the results of a certain command