Working with the bash shell l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 51

Working with the BASH shell PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on
  • Presentation posted in: General

Working with the BASH shell. Unit objectives Redirect the input and output of a command Identify, manipulate, create and export shell variables and edit environment files to create variables

Download Presentation

Working with the BASH shell

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Working with the bash shell l.jpg

Working with the BASH shell

Unit objectives

  • Redirect the input and output of a command

  • Identify, manipulate, create and export shell variables and edit environment files to create variables

  • Describe the purpose of shell scripts, create and execute them, and use common decision constructs in shell scripts

  • Use and customize features of the BASH shell


Topic a command input and output l.jpg

Topic A: Command input and output


The role of a shell l.jpg

The role of a shell

  • Provides a user interface

  • Interprets commands

  • Can manipulate command input and output using shell metacharacters


File descriptors l.jpg

File descriptors

  • Standard Input (stdin)

    • Represent information inputted 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


Three common file descriptors l.jpg

Three common file descriptors


Redirection l.jpg

Redirection

  • Redirect Standard Output and Standard Error to a file using the “>”

  • You can redirect both Standard Output and Standard Error to separate files at the same time

  • You can redirect both Standard Output and Standard Error to the same file using the “&” character


Redirection7 l.jpg

Redirection

  • Append data

    • date >> dateoutput

      • Adds output of date command to dateoutput file

  • Redirect to St. Input

    • tr 1 L </etc/hosts

    • tr 1 L </etc/hosts >newhosts


Types of redirection l.jpg

Types of redirection


Pipes l.jpg

Pipes

  • Sends Standard Output of one command as Standard Input to another command

  • Pipe

    • A string of commands connected by “|” metacharacter

  • Example

    • Ls –l /etc| less


Piping information l.jpg

Piping information


Using multiple pipes l.jpg

Using multiple pipes


Common filter commands l.jpg

Common filter commands


Using filters l.jpg

Using Filters

  • What does this command do?

    cat prologue | tr a A | sort | pr –d |less


The sed and awk commands l.jpg

The sed and awk commands

  • sed

    • Can search for text string

    • Can replace text with another string

  • awk

    • Treats each line as a database record

    • Treats each word as a database field


The sed and awk commands15 l.jpg

The sed and awk commands

  • sed

    • cat prologue |sed s/the/THE/


Any questions l.jpg

Any Questions?


Slide17 l.jpg

A user wants the script 'name1' to process the contents of the file 'name2', then redirect the output to the program 'name3'. Which of the following shell commands will do this?

A. name1 | name2 > name3

B. name1 < name2 | name3

C. name1 > name2 | name3

D. name1 | name2 < name3


Slide18 l.jpg

A user wants the script 'name1' to process the contents of the file 'name2', then redirect the output to the program 'name3'. Which of the following shell commands will do this?

A. name1 | name2 > name3

B. name1 < name2 | name3

C. name1 > name2 | name3

D. name1 | name2 < name3

Answer: A


Slide19 l.jpg

How could a user substitute all instances of PC with Computer in a file named instructions and display the results in the terminal window?

A. sed s/PC/computer/g instructions

B. sed ­s PC ­r computer instructions

C. cat instructions | awk C omputer

D. cat instructions | awk '/PC/ print omputer'


Slide20 l.jpg

How could a user substitute all instances of PC with Computer in a file named instructions and display the results in the terminal window?

A. sed s/PC/computer/g instructions

B. sed ­s PC ­r computer instructions

C. cat instructions | awk C omputer

D. cat instructions | awk '/PC/ print omputer'

Answer: A


Slide21 l.jpg

A user wants the script name1 to process the contents of the file "name2", then redirect the output to the program name3. Which if the following shell commands will do this?

A. name1 | name2 > name3

B. name1 < name2 | name3

C. name1 > name2 | name 3

D. name1 | name2 < name3


Slide22 l.jpg

A user wants the script name1 to process the contents of the file "name2", then redirect the output to the program name3. Which if the following shell commands will do this?

A. name1 | name2 > name3

B. name1 < name2 | name3

C. name1 > name2 | name 3

D. name1 | name2 < name3

Answer: B.


Slide23 l.jpg

An administrator needs to append the list of files in the /tmp directory to the existing file "DoNotCreateBackup". What command would accomplish this goal?

A. ls /tmp || DoNotCreateBackup

B. ls /tmp && DoNotCreateBackup

C. ls /tmp >> DoNotCreateBackup

D. ls /tmp :: DoNotCreateBackup


Slide24 l.jpg

An administrator needs to append the list of files in the /tmp directory to the existing file "DoNotCreateBackup". What command would accomplish this goal?

A. ls /tmp || DoNotCreateBackup

B. ls /tmp && DoNotCreateBackup

C. ls /tmp >> DoNotCreateBackup

D. ls /tmp :: DoNotCreateBackup

Answer: C.


Slide25 l.jpg

If the command below is executed in a user's home directory, which of the following would be accomplished? echo hello >> allusers

A. Sends the message 'hello' to all users on the system.

B. Creates a new file called 'hello' and sends it to all users.

C. Sends the message 'hello' to all currently logged in users.

D. Appends 'hello' to the end of the file llusers? if it exists.


Slide26 l.jpg

If the command below is executed in a user's home directory, which of the following would be accomplished? echo hello >> allusers

A. Sends the message 'hello' to all users on the system.

B. Creates a new file called 'hello' and sends it to all users.

C. Sends the message 'hello' to all currently logged in users.

D. Appends 'hello' to the end of the file llusers? if it exists.

Answer: D


Topic b shell variables l.jpg

Topic B: Shell variables


Shell variables l.jpg

Shell variables

  • Variable

    • A reserved portion of memory containing information that may be accessed

  • Environment variables

    • Typically set by the system

    • Contain information that the system and programs access regularly

  • User-defined variables

    • Your own custom variables


Environment variables l.jpg

Environment variables

  • Set by default in the BASH shell

  • The set command

    • Displays a list of environment variables and their current values


The path variable l.jpg

The PATH variable

  • One of the most important variables

  • Helps shell find location of commands

  • Saves you having to enter full path to commands

  • Most commands located in /bin or /sbin


Bash environment variables l.jpg

BASH environment variables


User defined variables l.jpg

User-defined variables

  • Variable identifier

    • The name of a variable

  • Features of variable identifiers

    • Can contain alphanumeric characters, the dash character, or the underscore character

    • Must not start with a number

    • Typically capitalized


Subshells l.jpg

Subshells

  • Most shell scripts 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 subshells or the commands running within them


Built in commands l.jpg

Built-in commands

  • Other variables not displayed by the set or env commands

    • Perform specialized functions in the shell

  • The UMASK variable

    • A special variable is set by the umask command


Environment files l.jpg

Environment files

  • Store defined variables

  • Can define different variables for different users

  • Load variables at log in


Environment file execution l.jpg

Environment file execution

Order

  • /etc/profile

  • ~/.bash_profile

  • ~/.bash_login

  • ~/.profile


Topic c shell scripts l.jpg

Topic C: Shell scripts


Shell scripts l.jpg

Shell scripts

  • Text files that contain a list of commands or constructs for the shell to execute in order

  • Can enter commands just as a user would


Escape sequences l.jpg

Escape sequences

  • Character sequences that have special meaning inside the echo command


Reading standard input l.jpg

Reading standard input

  • Shell script may need input from the user

    • Input may be stored in a variable for later use

  • The read command

    • Takes input from Standard Input and places it in a variable specified by an argument to the read command


Decision constructs l.jpg

Decision constructs

continued


Decision constructs continued l.jpg

Decision constructs, continued


If construct rules l.jpg

if construct rules

  • elif (else if) and else statements are optional

  • Can have unlimited number of elif statements

  • do these commands section can consist of multiple commands

  • do these commands section is typically indented from the left

  • End of statement is backwards if, “fi”

  • this is true part of syntax may be command or test statement


Common test statements l.jpg

Common test statements


Special operators l.jpg

Special operators


The case construct l.jpg

The case construct

  • The case statement

    • Compares variable value with several patterns of text or numbers

    • If there is a match, commands to the right are executed

  • Ended by a backwards case, “esac”


The and constructs l.jpg

The && and || constructs

  • Shortcut constructs that take less time form making only one decision

  • Syntax

    • command && command

    • command || command


Topic d bash command history l.jpg

Topic D: BASH command history


The bash history feature l.jpg

The bash history feature

  • Recalls previous commands

  • Reduces the number of number of keystrokes


Customizing bash history l.jpg

Customizing BASH history

  • Update the bash_history file

  • Specify number of commands to remember

  • Set HISTSIZE environment variable


Unit summary l.jpg

Unit summary

  • Redirected the input and output of commands

  • Identified, manipulated, created, and exported shell variables; and edited environment files to create variables

  • Described the purpose of shell scripts, executed scripts, and used common decision constructs in shell scripts

  • Used and learned how to customize the BASH shell


  • Login