Chapter 8 The Bourne Again Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Original Notes.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Some simple examples A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (week13)
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Chapter 15 Introductory Bash Programming
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.
Chapter 6: Shell Programming
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
1. 2 What is a Shell Script? A Text File With Instructions Executable  Why shell script? Simply and quickly initiate a complex series of tasks or a.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
INFO 320 Server Technology I Week 5 Shell environments and scripting 1INFO 320 week 5.
UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.
Writing Scripts Hadi Otrok COEN 346.
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.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
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:
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
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
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.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
Bash Jerome Lewis Kelly Benson Andrew Kimble. Overview  Shell Language (UNIX based)  Paradigms – Command, Scripting  Has ability to read straight from.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Department of Computer Engineering
SUSE Linux Enterprise Desktop Administration
C-Shell with Functions
Shell Features CSCI N321 – System and Network Administration
Agenda Korn Shell Functions Korn Shell Variables
CAP652 Lecture - Shell Programming
CHAPTER 8 (skip , ) CHAPTER 10
The Linux Command Line Chapter 11
Shell Programming.
Linux Shell Script Programming
Chapter 8 The Bourne Again Shell
Unix Shell Environments
Chapter 5 The Bourne Shell
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
The Linux Command Line Chapter 11
Presentation transcript:

Chapter 8 The Bourne Again Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes by Michael Weeks

Bourne Again SHell (bash)‏ Typical shell on Linux systems Written by Brian Fox of the Free Software Foundation Backwards Compatible with Bourne Shell Bourne Shell info applies here, too Runs /etc/profile first, then: At login, runs ~/.bash_profile At startup, runs ~/.bashrc

Variables Setting and accessing variables  same as before  For example numbers=”one two” echo $numbers

Arrays Defining arrays  declare -a teams  teams[0]=”Hawks”  teams[1]=”Falcons” Must use { } to access $ echo The ${teams[1]} play football. The Falcons play football.

Accessing Arrays Access all entries with [*] Find the number of entries with {#array[*]} $ echo ${teams[*]} Hawks Falcons $ echo I know of ${#teams[*]} teams. I know of 2 teams.

Building Lists Options  Assign entries with number  Give list in parenthesis Use \ to continue a line, as needed $ declare -a list1 $ list1[0]="Thrashers" $ list1[1]="Braves" $ list1[2]="Hawks" $ list1[3]=Falcons $ echo ${list1[*]} Thrashers Braves Hawks Falcons $ declare -a list2 $ list2=("Thrashers" "Braves" \ > "Hawks" Falcons)‏ $ echo ${list2[*]} Thrashers Braves Hawks Falcons

Deleting Lists Command unset Delete an entry or entire list $ echo ${list2[*]} Thrashers Braves Hawks Falcons $ unset list2[1] $ echo ${list2[*]} Thrashers Hawks Falcons $ unset list2 $ echo ${list2[*]} $

Aliases Command alias defines commands That is, put these in.bashrc alias qubit=”ssh -l myacct qubit.cs.gsu.edu” alias squbit=”sftp $ alias qubit="ssh -l cscmcw qubit.cs.gsu.edu" $ alias alias ls='ls --color=auto' alias qubit='ssh -l cscmcw qubit.cs.gsu.edu' $ qubit password: Last login: Tue Oct 16 17:43: from carmaux -bash-3.00$

History Bash keeps track of the commands you type  So do other shells, e.g. Korn, Csh $HISTSIZE defines # commands to remember Command history shows list

History Shortcuts !! repeats last command !34 repeats command #34 !gre repeats the last command starting with gre  Such as grep... ^str1^str2  repeats last command, substituting str1 for str2 $ mroe myfile -bash: mroe: command not found $ ^ro^or more myfile...

Bash Arithmetic Instead of expr, use (( operation ))‏  +, -, ++, --, *, /, %  =,, ==, !=, !, &&, || Declare an integer  declare -i intName  Integers are faster to evaluate

Arithmetic Example #!/bin/bash # # Demonstrate some Bash arithmetic # declare -i x=10 while (( x > 0 ))‏ do echo x is $x (( x-- ))‏ done

Another Example #!/bin/bash # declare -i x=20 # find max area x*y, declare -i y # where x+y=20 declare -i maxarea=-1 declare -i area while (( $x > 0 )); do y=20 while (( $y > 0 )); do (( area = $x * $y ))‏ if (( area > maxarea && x + y == 20 ))‏ then maxarea=$area fi (( y-- ))‏ done (( x-- ))‏ done echo "Max area is $maxarea"

If statement Allows “elif” (else if) as well as “else” #!/bin/bash # echo "enter your favorite class: " read favorite # Use " " around $favorite so it keeps multiple # words together. if [ "$favorite" == "Systems Programming" ]; then echo "good choice." elif [ "$favorite" == "3320" ]; then echo "good choice." else echo "what are you thinking?!" fi

Functions Functions may be invoked as shell command Parameters passed to functions are accessible via the standard positional parameter mechanism Functions must be defined before they are used Two ways to define a function function name name() { list of commands }

examples F() { echo parameter 1 = $1 echo parameter list = $* } # main program F 1 F cat dog goat F() { (( returnValue = $1 * $2 )) return $returnValue } # main program F 3 4 result=$? Echo return value from function was $result

Local Variables A variable created using the typeset function is limited in scope to the function in which it is created and all of the functions that the defining function calls. If a variable of the same name already exists, its value is over-written and replaced when the function returns.

example F() { typeset x (( x = $1 * $2 )) echo local x = $x return $x } # main program x=1 echo global x = $x F 3 4 result=$? echo return value from function was $result echo global x = $x

Recursion A function calls itself

Review Bash overview Compatible with Bourne shell Allows arrays Has a history mechanism (as do others)‏ Handles arithmetic well Control structures are very similar to Bourne