UNIX Shell Programming Featuring KornShell
Course Guide
EY–G994E-SG-0002
UNIX Shell Programming Featuring KornShell
Course Guide
EY–G994E-SG-0002
Notice
The information in this publication is subject to change without notice.
COMPAQ COMPUTER CORPORATION SHALL NOT BE LIABLE FOR TECHNICAL OR
EDITORIAL ERRORS OR OMISSIONS CONTAINED HEREIN, NOR FOR INCIDENTAL OR
CONSEQUENTIAL DAMAGES RESULTING FROM THE FURNISHING, PERFORMANCE, OR
USE OF THIS MATERIAL.
This guide contains information protected by copyright. No part of this guide may be photocopied or
reproduced in any form without prior written consent from Compaq Computer Corporation.
The software described in this guide is furnished under a license agreement or nondisclosure agreement.
The software may be used or copied only in accordance with the terms of this agreement.
Other product names mentioned herein may be trademarks and/or registered trademarks of their
respective companies.
©1999 Compaq Computer Corporation. All rights reserved. Printed in the USA.
Aero, ALPHA, ALPHA AXP, AlphaServer, AlphaStation, Armada, BackPaq, COMPAQ, Compaq
Insight Manager, CompaqCare logo, Counselor, DECterm, Deskpro, DIGITAL, DIGITAL logo,
DIGITAL Alpha Systems, Digital Equipment Corporation, DIGITAL UNIX, DirectPlus, FASTART,
Himalaya, HSZ, InfoPaq, Integrity, LAT, LicensePaq, Ministation, NetFlex, NonStop, OpenVMS,
PaqFax, Presario, ProLiant, ProLinea, ProSignia, QuickBack, QuickFind, Qvision, RDF, RemotePaq,
RomPaq, ServerNet, SERVICenter, SmartQ, SmartStart, SmartStation, SolutionPaq, SpeedPaq,
StorageWorks, Systempro/LT, Tandem, TechPaq, TruCluster, Tru64 UNIX are registered in United
States Patent and Trademark Office.
AdvantageCluster, DECevent, DECladebug, DECnsr, DECsafe, DEC VET, DECwindows, OpenVMS,
RZ, TURBOchannel, VAX Notes, are trademarks of Compaq Computer Corporation.
AIX and IBM are registered trademarks of International Business Machines Corp. BSD is a trademark
of the University of California, Berkeley, CA. Memory Channel is a trademark of Encore Computer
Corporation. Global Knowledge Network and the Global Knowledge Network logo are trademarks of
Global Nowledge Network, Inc. Microsoft and Windows NT are registered trademarks of Microsoft
Corporation. MIPS is a trademark of MIPS Computer Systems. Motif, OSF and OSF/1 are registered
trademarks of the Open Software Foundation. NetWorker is a trademark of Legato. NFS is a registered
trademark of Sun Microsystems, Inc. Oracle is a registered trademark and Oracle7 is a trademark of
Oracle Corporation. POSIX and IEEE are registered trademarks of the Institute of Electrical and
Electronics Engineers, Inc. PostScript is a registered trademark of Adobe Systems, Inc. UNIX is a
registered trademark licensed exclusively through X/Open Company Ltd. X Window System is a
trademark of the Massachusetts Institute of Technology.
UNIX Shell Programming Featuring KornShell
Course Guide
November 1999
Contents
About This Course
About This Course .................................................................................................xix
Introduction ...................................................................................................xix
Course Description ........................................................................................xix
Place in Curriculum.......................................................................................xix
Target Audience ............................................................................................xix
Prerequisites ...................................................................................................xx
Course Goals ..................................................................................................xx
Nongoals........................................................................................................xxi
Taking This Course...............................................................................................xxii
Course Organization.....................................................................................xxii
Course Map ..................................................................................................xxii
Chapter Descriptions .................................................................................. xxiii
Time Schedule.............................................................................................xxiv
Course Conventions .....................................................................................xxv
Resources......................................................................................................xxv
1 Review
About This Chapter............................................................................................... 1-3
Introduction .................................................................................................. 1-3
Objectives ..................................................................................................... 1-3
Resources...................................................................................................... 1-3
Standard Files ....................................................................................................... 1-4
Standard Input .............................................................................................. 1-4
Standard Output............................................................................................ 1-4
Standard Error (Diagnostic Output) ............................................................. 1-4
Status of the Standard Files at Login............................................................ 1-4
Redirecting Standard Files.................................................................................... 1-5
Redirecting Output to a New File................................................................. 1-5
Redirecting Output to an Existing File......................................................... 1-5
Appending Redirected Output to an Existing Output File ........................... 1-5
Redirecting Input .......................................................................................... 1-6
Redirecting Standard Error........................................................................... 1-6
Redirecting Both Standard Error and Standard Output................................ 1-6
Eliminating Any Output ............................................................................... 1-6
The /dev/null File ......................................................................................... 1-6
The Pipe Operator................................................................................................. 1-7
Definition...................................................................................................... 1-7
Using the Pipe Operator (|)........................................................................... 1-7
EY-G994E-SG-0002
iii
Using the Pipe Operator to List All Block-Structured Devices ................... 1-7
Using the Pipe Operator to List All Files in /dev on the Printer .................. 1-7
Wildcard Metacharacters ...................................................................................... 1-8
Wildcards...................................................................................................... 1-8
Using Wildcards ........................................................................................... 1-8
Command History and Command Line Editing ................................................... 1-9
The history Command .................................................................................. 1-9
Recalling a Specific Command .................................................................... 1-9
Recalling a Command ................................................................................ 1-10
Using vi Commands ................................................................................... 1-10
Filename Completion ................................................................................. 1-10
Shell Variables.................................................................................................... 1-12
Guidelines................................................................................................... 1-12
Using Variables .......................................................................................... 1-12
Shell Variables............................................................................................ 1-12
Exporting Variables.................................................................................... 1-13
Listing Exported Variables......................................................................... 1-13
Foreground and Background Processes.............................................................. 1-14
Introduction ................................................................................................ 1-14
Running a Program in the Background ...................................................... 1-14
Running Multiple Commands in the Background...................................... 1-14
Running a Pipeline in the Background....................................................... 1-15
Obtaining a Long Listing of Process Status ............................................... 1-15
Moving a Foreground Job to the Background............................................ 1-16
Moving a Background Job into the Foreground......................................... 1-16
Waiting for a Background Process to Complete ........................................ 1-16
Terminating a Background Job .................................................................. 1-16
Stopping a Background Job........................................................................ 1-17
Job References............................................................................................ 1-17
vi Refresher......................................................................................................... 1-18
Sample vi Commands................................................................................. 1-18
Summary .......................................................................................................... 1-19
Defining and Redirecting Standard Files ................................................... 1-19
Using the Pipe Operator to Connect Two Commands ............................... 1-19
Using Wildcard Metacharacters ................................................................. 1-19
Using the History Command to Recall Commands ................................... 1-19
Using Shell Variables ................................................................................. 1-19
Defining and Using Foreground and Background Processes ..................... 1-19
Reviewing Some vi Commands ................................................................. 1-19
2 Getting Started with Scripting
About This Chapter............................................................................................... 2-3
Introduction .................................................................................................. 2-3
Objectives ..................................................................................................... 2-3
Resources...................................................................................................... 2-3
............................................................................................................ 2-4
Processes
iv
EY-G994E-SG-0002
Definition...................................................................................................... 2-4
Process as Program Wrapper........................................................................ 2-4
fork() and exec() ........................................................................................... 2-4
Login Process Diagram ................................................................................ 2-4
Executing Shell Scripts......................................................................................... 2-5
Introduction .................................................................................................. 2-5
Executing a Shell Script as an Argument to ksh .......................................... 2-5
Permission Requirements for Executing a Shell Script as an Argument ..... 2-5
Executing Shell Scripts by Name................................................................. 2-5
Permission Requirements for Executing Shell Scripts by Name ................. 2-5
The #! Operator ............................................................................................ 2-5
The print Statement ...................................................................................... 2-5
print Statement Syntax ................................................................................. 2-6
Using the print Statement to Write Standard Output.................................... 2-6
Using Positional Parameters ................................................................................. 2-7
Positional Parameters ................................................................................... 2-7
Arguments .................................................................................................... 2-7
Using Positional Parameters......................................................................... 2-8
Using the shift Statement.............................................................................. 2-8
shift Statement Syntax.................................................................................. 2-8
Default Parameter Value .............................................................................. 2-8
Using Parameter Values ............................................................................... 2-9
Escape Sequences ............................................................................................... 2-10
Using Wildcards as Literals........................................................................ 2-10
How the Escape Sequences Influence Output ............................................ 2-10
Using Escape Sequences Properly.............................................................. 2-11
Passing Arguments to Shell Scripts .................................................................... 2-12
Introduction ................................................................................................ 2-12
Environmental Variables ............................................................................ 2-12
Environmental Variables and Positional Parameters ................................. 2-12
How the read Command Works ................................................................. 2-13
Using the read Command ........................................................................... 2-13
Including a Prompt ..................................................................................... 2-13
Using read with a Prompt ........................................................................... 2-14
Testing Arguments.............................................................................................. 2-15
Techniques for Testing Arguments ............................................................ 2-15
General Syntax ........................................................................................... 2-15
Using the (( )) Command............................................................................ 2-15
Using the test Command ............................................................................ 2-15
Using the [ ] Command .............................................................................. 2-15
The [[ ]] Command..................................................................................... 2-16
Using Operators .................................................................................................. 2-17
Introduction ................................................................................................ 2-17
Integer Comparison Operators ................................................................... 2-17
Using Integer Comparison Operators......................................................... 2-17
String Comparison Operators ..................................................................... 2-18
EY-G994E-SG-0002
v
Using String Comparison Operators .......................................................... 2-18
File Enquiry Operators ............................................................................... 2-19
Using File Enquiry Operators..................................................................... 2-19
Testing for Permissions .............................................................................. 2-19
Logic Operators .......................................................................................... 2-20
Using Logic Operators ............................................................................... 2-20
Review........................................................................................................ 2-20
Making Decisions and Looping.......................................................................... 2-21
Introduction ................................................................................................ 2-21
The if/then/fi Commands............................................................................ 2-21
Using the if Command to Test the Status of the [[ Command ................... 2-21
Using the if Command to Test the Status of the who | grep Command ..... 2-22
Exact Syntax............................................................................................... 2-22
Commands in then or else Blocks .............................................................. 2-22
Using the while Loop ................................................................................. 2-22
Infinite Loops ............................................................................................. 2-23
The for Loop in KornShell ......................................................................... 2-23
Using the for Loop...................................................................................... 2-23
A Special Case............................................................................................ 2-23
Using Execute Trace and Verbose Trace to Debug Shell Scripts....................... 2-24
Introduction ................................................................................................ 2-24
Execute Trace ............................................................................................. 2-24
Using Execute Trace................................................................................... 2-24
Verbose Trace............................................................................................. 2-25
Using Verbose Trace .................................................................................. 2-25
Using Execute Trace Inside a Shell Script ................................................. 2-25
Command Substitution ....................................................................................... 2-26
Introduction ................................................................................................ 2-26
Procedure.................................................................................................... 2-26
Assigning Output of a Command to a Variable ......................................... 2-26
Using the Grave Accent.............................................................................. 2-26
Shell Script Examples......................................................................................... 2-27
Example 1................................................................................................... 2-27
Example 2................................................................................................... 2-28
Summary .......................................................................................................... 2-29
Defining and Using Processes .................................................................... 2-29
Executing a Shell Script ............................................................................. 2-29
Using Positional Parameters....................................................................... 2-29
Using Escape Sequences ............................................................................ 2-29
Passing Arguments to Shell Scripts............................................................ 2-29
Testing Arguments ..................................................................................... 2-29
Using Operators.......................................................................................... 2-29
Using Conditions, Control Statements and Loops ..................................... 2-29
Debugging Scripts Using Execute Trace and Verbose Trace .................... 2-29
Using Command Substitution .................................................................... 2-30
.......................................................................................................... 2-31
Exercises
vi
EY-G994E-SG-0002