BashPrograming.pdf

(1306 KB) Pobierz
Bash Programming
Student Workbook
Bash Programming
Bash Programming
Published by ITCourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO 80112
Contributing Authors:
Julie Johnson, Rob Roselius
Editor:
Jeff Howell
Special thanks to:
Many Shell instructors whose ideas and careful review have contributed to the quality
of this workbook, including Brandon Caldwell, Roger Jones, Jim McNally, and Bill Parette, and the many
students who have offered comments, suggestions, criticisms, and insights.
Copyright © 2002 by ITCourseware, LLC. All rights reserved. No part of this book may be reproduced
or utilized in any form or by any means, electronic or mechanical, including photocopying, recording, or by
an information storage retrieval system, without permission in writing from the publisher. Inquiries should be
addressed to ITCourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO 80112. (303)
302-5280.
All brand names, product names, trademarks, and registered trademarks are the property of their respective
owners.
Page ii
Rev 1.1.3
© 2002 ITCourseware, LLC
Bash Programming
Contents
Chapter 1 - Course Introduction ............................................................................................................... 7
Course Objectives .............................................................................................................................. 8
Course Overview .............................................................................................................................. 10
Using the Workbook ......................................................................................................................... 11
Suggested References and Reading ................................................................................................... 12
Chapter 2 - UNIX Processes .................................................................................................................. 15
What is a Process? ............................................................................................................................ 16
Process Structure .............................................................................................................................. 18
The ps Utility ..................................................................................................................................... 20
Options to the ps Utility ..................................................................................................................... 22
Background Commands (&) ............................................................................................................. 24
Killing Background Processes ........................................................................................................... 26
Redirecting the Standard Error .......................................................................................................... 28
Labs ................................................................................................................................................. 30
Chapter 3 - Getting Started ..................................................................................................................... 33
What is a Shell? ................................................................................................................................ 34
Running Scripts ................................................................................................................................. 36
Specifying the Script's Interpreter ...................................................................................................... 38
The PATH Environment Variable ....................................................................................................... 40
Sub-shells ......................................................................................................................................... 42
Labs ................................................................................................................................................. 44
Chapter 4 - Variables .............................................................................................................................. 47
Shell Variables ................................................................................................................................... 48
The read Command .......................................................................................................................... 50
The export Command ....................................................................................................................... 52
The Shell Environment ....................................................................................................................... 54
Parameter Expansion......................................................................................................................... 56
Command Substitution ...................................................................................................................... 58
Labs ................................................................................................................................................. 60
© 2002 ITCourseware, LLC
Rev 1.1.3
Page iii
Bash Programming
Chapter 5 - The Login Process ............................................................................................................... 63
The Login Process ............................................................................................................................ 64
The System Profile Script .................................................................................................................. 66
Your .bash_profile Script ................................................................................................................... 68
The . Command ................................................................................................................................ 70
Labs ................................................................................................................................................. 72
Chapter 6 - Conditional Statements ......................................................................................................... 75
The Exit Status of Commands ........................................................................................................... 76
Command Line Examples .................................................................................................................. 78
The test Command ............................................................................................................................ 80
The if-then-else Construct ................................................................................................................. 82
The elif Construct .............................................................................................................................. 84
case Statements ................................................................................................................................ 86
Labs ................................................................................................................................................. 88
Chapter 7 - Loops .................................................................................................................................. 91
The for Loop .................................................................................................................................... 92
The while Loop ................................................................................................................................. 94
break and continue ............................................................................................................................ 96
Reading Lines From Files .................................................................................................................. 98
Using Arrays with Loops ................................................................................................................. 100
Labs ............................................................................................................................................... 102
Chapter 8 - Special Variables ................................................................................................................ 105
$$ - PID of Shell ............................................................................................................................. 106
Command-Line Arguments .............................................................................................................. 108
$# - Number of Arguments ............................................................................................................. 110
$* - All Arguments .......................................................................................................................... 112
The shift Command ......................................................................................................................... 114
The set Command ........................................................................................................................... 116
Getting Options ............................................................................................................................... 118
Labs ............................................................................................................................................... 120
Page iv
Rev 1.1.3
© 2002 ITCourseware, LLC
Bash Programming
Chapter 9 - Quoting Mechanisms .......................................................................................................... 123
Single vs. Double Quotes ................................................................................................................ 124
What is a Here Document? ............................................................................................................. 126
Using a Here Document .................................................................................................................. 128
Here Document Quoting ................................................................................................................. 130
Ignoring Leading Tabs ..................................................................................................................... 132
Labs ............................................................................................................................................... 134
Chapter 10 - Functions ......................................................................................................................... 137
Shell Functions ................................................................................................................................ 138
Passing Arguments to Functions ...................................................................................................... 140
Returning Values from Functions ...................................................................................................... 142
Function Declarations ...................................................................................................................... 144
Labs ............................................................................................................................................... 146
Chapter 11 - Advanced Programming ................................................................................................... 149
Shell Arithmetic ............................................................................................................................... 150
The select Statement ....................................................................................................................... 152
Terminal Independence in Scripts .................................................................................................... 154
The eval Command ......................................................................................................................... 156
Labs ............................................................................................................................................... 158
Chapter 12 - Debugging Techniques ...................................................................................................... 161
Using echo ...................................................................................................................................... 162
Using Standard Error ...................................................................................................................... 164
Script Tracing .................................................................................................................................. 166
Options for Debugging .................................................................................................................... 168
Conditional Debugging .................................................................................................................... 170
Labs ............................................................................................................................................... 172
Solutions - Bash Programming ............................................................................................................... 175
Index..................................................................................................................................................... 193
© 2002 ITCourseware, LLC
Rev 1.1.3
Page v
Zgłoś jeśli naruszono regulamin