Workbook 6 – Part 2 The Bash Shell

Slides:



Advertisements
Similar presentations
Bash startup files Linux/Unix files stty 1.  midterms  bash startup files  stty 2.
Advertisements

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
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
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.
Using Linux Commands 2 Lab#5
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Workbook 6 The Bash Shell
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
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
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.
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
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.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Linux+ Guide to Linux Certification Chapter Eight Working with 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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
1 © 2012 John Urrutia. All rights reserved. Chapter 8 The Bourne Again Shell Halleluiah!
Customizing Linux Environment Tutorial 4 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
Writing Scripts Hadi Otrok COEN 346.
Chapter 4 LINUX Shells. Table 4.1 Shell Locations and Program Names.
Linux+ Guide to Linux Certification, Third Edition
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.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
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
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
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.
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
UNIX and Shell Programming (06CS36)
CHAPTER 8 (skip , ) CHAPTER 10
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Introduction to Bash Programming, part 3
Presentation transcript:

Workbook 6 – Part 2 The Bash Shell RH030 Linux Computing Essentials

Linux+ Guide to Linux Certification, 2e Workbook 6 Objectives This workbook is all about the shell. Last week we discussed What is a Shell What a shell is and what it does How subshells is and how it works Overviewed shell features Shell variables Should understand difference of Local verse Environmental variables Creating new shell variables Exporting variables Identifying common shell environment variables $LOGNAME, $SHELL,$HOME, $PATH Manipulating common shell environment variables ~/.bashrc Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Workbook 6 Objectives This week Shell feature = Command Line expansion Startup & Login environment files. global & local environment files. Sequence of the startup environmental files Use of each startup environmental file Manipulating common shell environment variables permanently Creating permanent aliases/variables Linux+ Guide to Linux Certification, 2e

Chapter 4 – Command line expansion Key Concepts The bash shell expands certain command line metacharacters before interpreting the command. Tilde expansion expands tokens that begin with a tilde (~) to users home directories. Brace expansion expands tokens with braces ({}) into multiple words, each of which contains a single word from the specified list. Command substitution expands text enclosed within backticks (``) or "dollar parenthesis" ($()) into the output produced by the enclosed command. Double quotes ("..." ), single quotes ('...'), and the backslash character can be used to protect characters from being expanded by the shell. Linux+ Guide to Linux Certification, 2e

Using Command Line Expansion Expansion Area Syntax Expands To History ! Refer to a previous command line Brace {} Create this specified text Tilde ~ Refer’s to a user's home directory Variable $, ${...} Shell and environment variables Arithmetic $((...)) Numeric calculation Command Substitution ` ...`, Allows command to run in subshell Pathname *, ?, [^...] Matchs to filenames in filesystem Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Brace Quick way to create items Create this specified text touch file{01,02,03,04} [student@station student]$ ls file01 file02 file03 file04 Creating a hierarchy structure [student@station student]$ mkdir chap{01,02,03,04}/{html,text} Linux+ Guide to Linux Certification, 2e

It can get quite complicated $touch soap/episode{1,2,3,4}.{draft,final}.{txt,pdf} Will produce the following directory listing. [student@station student]$ ls soap/episode1 episode2.draft.pdf episode3.draft.pdf episode4.draft.pdfepisode1.draft.txt episode2.draft.txt episode3.draft.txt episode4.draft.txt episode1.final.pdf episode2.final.pdf episode3.final.pdf episode4.final.pdf episode1.final.txt episode2.final.txt episode3.final.txt episode4.final.txt Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Pathname Expansion Character Matching * 0 or more characters ? exactly one characters [...] exactly one of the included characters [^...] exactly one of the excluded characters Linux+ Guide to Linux Certification, 2e

Different Quotes do different things Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Using quota’s Sometime the entire line needs to be enclosed in quota’s Double quotes tell the shell to: ( “ ) Display the value of this variable used. LOGIN=John echo “Welcome $LOGIN” Welcome John Single quotes tell the shell to: ( ‘ ) Display the name of this variable used echo ‘Welcome $LOGIN’ Welcome $LOGIN Back quotes ( ` ) tell the shell to run text this as a command echo “ Welcome $LOGIN today is `date` ” Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Command Substitution The "old school" syntax for command substitution is to encapsulate the command in `back ticks` The more modern syntax supported by the bash shell is similar to arithmetic expansion, but with only one set of parentheses: $(subcommand) [prince@station prince]$ mkdir reports.$(date +%d%b%Y) Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Quoting Put example of quotes from 1st ppt here Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Using quota’s Here with no quotes, bash interprets the > and < characters as requests to redirect the command's output (and input). $ echo <pre> little red $CAR </pre> $ -bash: syntax error near unexpected token `newline' Here the double quotes protected the < and > characters. The dollar sign, however, is interpreted as a marker for a variable. $ echo “<pre> little red $CAR <pre>“ $ <pre>little red corvette </pre> Here the single quotes stop the variable expansion $ echo '<pre>little red $CAR</pre>' $ <pre>little red $CAR</pre> Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Escape Characters Shell expansions occur before the command is run. Occasionally, some commands use arguments that contain characters in them that are special to the bash shell. Meaning that for bash they have some other meaning. Bash needs to be told NOT to read them as special characters. but to use them in the current syntax. \<special character> A common example is the find command. find –name “*.chap” –exec rm {} \; Linux+ Guide to Linux Certification, 2e

Use of escape character echo \<pre\>little red $CAR\</pre\> <pre>little red corvette</pre> echo \<pre\>little red \$CAR\</pre\> <pre>little red $CAR</pre> Linux+ Guide to Linux Certification, 2e

Chapter 5 – Shell Customizations Key Concepts The bash shell internally implements certain simple commands (internal Commands) Which affect the shell's behavior. These are referred to as builtin commands. Shell aliases are established and examined with the alias command. Shell aliases are removed with the unalias command. The bash shell prompt can be customized using the PS1 variable. Shell flags can can be set with the set -f command, and cleared with set +f. Shell options are examined, set, and unset using the shopt command. Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Using alias’s alias command: Used to list all the available aliases. Using aliases Short alias runs a long command-line Can create a single alias for multiple commands But are only temporarily available unless you make them permanent. Locally this is done using the .bashrc environment file. Creating temporary alias’s alias c=clear alias c=“ clear;date;ls –l ” alias ts="touch $(date +timestamp.%H:%M:%S)" alias tsc="find . -newer" Linux+ Guide to Linux Certification, 2e

~/.bashrc = local permanent aliases Making aliases permanent echo alias c=clear >> .bashrc . ./.bashrc Linux+ Guide to Linux Certification, 2e

Remember - Last week we looked at variables There are 2 types of variables: User-defined variables: Custom variables Created and defined by the local user Environment variables: Contain system information or properties that the system and programs both use in the running of the overall of the system and access regularly. Such as your unique login id. Linux+ Guide to Linux Certification, 2e

Examples of Some Frequently Used Environmental Variables Name Description LOGNAME Sets the unique id by which this user will be identified HOME Sets the directory which will be this users home directory. PATH Specifies the directories that the shell is to look through to find an executable command. These directories are searched in the order of the path. SHELL Sets your default shell. HOSTNAME Sets the name of your system MAIL Location of the users mailbox. PS1 Sets how your prompt will be displayed. Linux+ Guide to Linux Certification, 2e

Working with Variables & their values Displaying all currently available variables & their current values set Displaying the value of a specific variable: To view contents of a specified variable you must always use a $ sign infront of the variable name. echo $HOME Creating or Changing value of a variable: To create a new variable name you use the following format. <new-variable-name> equal sign (=) and new value Sheila=teacher Double quotes should surround an entire string. Sheila= “Sheila is the teacher” Linux+ Guide to Linux Certification, 2e

Modifying existing variables Customizing Your Prompt echo $PS1 “[\u@\h \W]$” Default setting PS1=“Hello World” Hello World /home/sheila $pwd /home/sheila $ /home/sheila $ PS1= “This month is `date +%h ` @” This month is May @ The back quotes (`) are used to run it as a command instead of interpreting it literally as a string of text. Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e set verse shopt These two builtin commands are used to configure the shell's behavior using shell options. In other words they turn on functions. One is the set command, which can also be used to modify the shell's behavior using (usually) single letter flags set –f set –o noclobber (c shell function) The other is shopt, which is used to configure shell options. shopt –s cdspell shopt –u cdspell Linux+ Guide to Linux Certification, 2e

Chapter 6 – Sourcing Shell Scripts and Shell Initialization Key Concepts Shell scripts are sourced with the source or . commands. Shell scripts are all executed in the same shell that sources the script. Bash shells can be either login or non-login shells. Bash shells can be either interactive or non-interactive shells. /etc/profile, files within the /etc/profile.d directory, and ~/.bash_profile are sourced on startup of every login shell. /etc/bashrc and ~/.bashrc are sourced on startup of every shell. ~/.bash_logout is sourced by every exiting login shell. Linux+ Guide to Linux Certification, 2e

source = run this script [blondie@station blondie]$ cat prompts.script PS1="whadda you want? " #PS1="\a\u@\H \$(date --iso-8601) \t [\!] \$ " #PS1="[\u@\h \W]\$ " In order to try out the first prompt, she sources the file prompts.script. [blondie@station blondie]$ source prompts.script whadda you want? Can also use . ./prompts.script Can also use bash ./prompts.script Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Environment Files Environment files are used as part of the logon process During login all users are assigned many default values. Such as a default shell, path, home directory etc All the different shells each use their own environment files. When their shell is started it reads it’s own environment files. There are 2 types of environment files. Global – used when you login and given to everyone Local – Also read when you login PLUS when you start a new subshell – used only to the local specific user. Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Login Process Environment files are just : Script configuration files Some are run everytime you login. There are 2 types: Global environment files /etc/profile /etc/bashrc local Bash environment Files ~/.bash_profile ~/.bashrc ~/.bash_logout Linux+ Guide to Linux Certification, 2e

Last week we discussed exporting variables Exporting variables = Making the entire system aware of them. It declares the existence of defined variables within the system. A variable needs to be exported or declared to the entire system so that they are available for use. Making them available to all subshells. export command = 2 uses Used on it’s own It will lists all the currently exported / declared available variables export teacher Used with an existing variable it will export/declare that variable. This then ensures that all sub-shells have access to this variable Linux+ Guide to Linux Certification, 2e

Items - you would export in Global Environment Exports the login id you used to connect to the system with. $LOGNAME for login name etc Exports PATH for Default Command Path $PATH sets default path of directories where the shell will look when a command is executed Exports MAIL for Default Mailbox location $MAIL sets default path of directories containing each specific users mailbox location. Sets TERM Variable Default Terminal Type screen and keyboard Displays Contents of /etc/motd File Holds any motd is ‘message of the day’ Sets Default File Creation Permissions sets umask values which determines the default permissions Checks for Mail Checks mail and will print a mail message upon login Linux+ Guide to Linux Certification, 2e

Order of the environment files /etc/profile is read first It contains the global variables. It exports the global variables. ~/.bash_profile is read next It contains the local variables. It exports the local variables. ~/.bashrc is read next It contains the local aliases. Code within it causes it to read the /etc/bashrc if it exists. /etc/bashrc is read last It contains the global aliases. Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Note During login different shells use different Local Environment Files Linux+ Guide to Linux Certification, 2e

Linux+ Guide to Linux Certification, 2e Summary echo $? && || set env export $ PATH $HOME $SHELL $PS1 alias set shopt source . ./script1 bash script1 /etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc ~/.bash_logout Linux+ Guide to Linux Certification, 2e