Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
COSC 120 Computer Programming
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
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to Shell Script Programming
Linux in More Detail Shirley Moore CPS5401 August 29,
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Introduction to UNIX / Linux - 11
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Advanced Computer Architecture 0 Lecture # 1 Introduction by Husnain Sherazi.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Chapter Six Introduction to Shell Script Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and 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
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Chapter 15 Introductory Bourne Shell Programming.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapter 3 Getting Started. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To give an overview of the structure of a contemporary.
Bill Tucker Austin Community College COSC 1315
Agenda Korn Shell Functions Korn Shell Variables
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Advanced C Shell Programming
Variables, Expressions, and IO
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Chapter Four UNIX File Processing.
Linux Shell Script Programming
Chapter 5 The Bourne Shell
Chapter 5 Bourne Shells Scripts
CST8177 Scripting 2: What?.
Chapter 3 The UNIX Shells
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Chapter 16 Advanced Bourne Shell Programming

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing To describe how standard input of a command in a shell can be redirected from data within the script To explain the signal/interrupt processing capability of the Bourne shell To describe how file I/O can be performed by using file descriptors and how standard files can be redirected from within a shell script To explain the functions in the Bourne shell To discuss debugging of Bourne shell scripts To cover the commands and primitives

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing The values of Bourne shell variables are stored as character strings In order to perform arithmetic and logic operations on them, you need to convert them to integers and be sure that the result is converted back to a character string for its proper storage in a shell variable

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing expr args Purpose:Evaluate the expression arguments, ‘args’, and send the result to standard output Commonly used operators/features: \| return the first expression if it is not null or 0; else return the second expression \& Return the first expression if neither is null or 0;else return 0 =, \>, \>=, Integer comparison operators;equal, \<, \<=,!= greater than, greater than or equal to, less than,less than or equal to, not equal +, -, \*, /, % Integer arithmetic operators, add, subtract, multiply, integer divide (return quotient), remainder

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing $ var1=10 $ var1=`expr $var1 + 1` $ echo $var1 11 $ var1=`expr $var1 \* $var1` $ echo $var1 121 $ echo `expr $var1 / 10` 12 $ echo `expr $var1 % 10` 1 $

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document The here document feature of the Bourne shell allows you to redirect standard input of a command in script and attach it to data in the script

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing Three possible actions that the process receiving the signal can take: –Accept the default action as determined by the UNIX kernel –Ignore the signal –Take a programmer defined action Hardware interrupt e.g keyboard interrupt ( ) Signal received due to termination of a child process Accessing a main memory location that is not part of the process’s address space Software termination

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The exec Command and File I/O The command level version of the UNIX loader Used : –To execute a command/program instead of the current process (under which exec is executed, usually a shell) –To open and close file descriptors When the exec command is used in conjunction with redirection operators, it allows commands and shell scripts to read/write any type of files, including devices

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process exec command can be used to run a command instead of the process (usually a shell) that executes this command

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command Korn shell allows you to work with as many as 10 file descriptors at a time Three descriptors are set aside for: –Standard input(0) –Standard output(1) –Standard error(2) When executed from the command line, the exec< sample command causes each line in the sample file to be treated as a command and executed by the current shell When exec>data command is executed from command line, it causes outputs of all subsequent commands executed under the shell ( that normally go to the monitor screen) to go to the data file.

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command $ exec > data $ date $ echo Hello, World! $ uname -a $ exec > /dev/tty $ date Tue Jun 29 23:57:30 PDT 2004 $ cat data Tue Jun 29 23:56:03 PDT 2004 Hello, World! AIX pccaix $

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell Functions are normally used if a piece of code is repeated at various places in a script The mechanism of transferring the control to the function code and returning it to the calling code takes time Another alternative is to create another script file for the block of code and invoke this code by calling the script as a command-expensive operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell Before you can use a function, you have to define it Put the definitions of general purpose functions in ~/.profile file Functions can be exported using the export command The commands in a function body are not executed until the function is invoked Can invoke a function by using its name as a command

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell $ machines () > { > date > echo “These are the machines on the network:” > ruptime | cut -f1 -d’ ‘ | more > } $ machines Thu Feb 19 17:05:00 PDT 2004 These are the machines on the network: upibm0... upsun1... upsun29 $

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Debugging Shell Programs The Bourne shell programs can be debugged by using the –x and –v options of the sh command This allows viewing the commands in the user’s script after the variable substitution but before execution

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Debugging Shell Programs