14.06.2014 Views

Unix Shells By Example 4th Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UNIX <strong>Shells</strong> by <strong>Example</strong> Fourth <strong>Edition</strong><br />

Table of Contents<br />

Copyright<br />

Preface<br />

ACKNOWLEDGMENTS<br />

Chapter 1. Introduction to UNIX/Linux <strong>Shells</strong><br />

1.1. What Is UNIX? What Is Linux? A Little History<br />

1.2. Definition and Function of a Shell<br />

1.3. History of the Shell<br />

1.4. System Startup and the Login Shell<br />

1.5. Processes and the Shell<br />

1.6. The Environment and Inheritance<br />

1.7. Executing Commands from Scripts<br />

Chapter 2. Shell Programming QuickStart<br />

2.1. Taking a Peek at Shell Scripts<br />

2.2. Sample Scripts: Comparing the Major <strong>Shells</strong><br />

2.3. The C and TC Shell Syntax and Constructs<br />

2.4. The Bourne Shell Syntax and Constructs<br />

2.5. The Korn Shell Constructs<br />

2.6. The Bash Shell Constructs<br />

Chapter 3. Regular Expressions and Pattern Matching<br />

3.1. Regular Expressions<br />

3.2. Combining Regular Expression Metacharacters<br />

Chapter 4. The grep Family<br />

4.1. The grep Command<br />

4.2. grep <strong>Example</strong>s with Regular Expressions<br />

4.3. grep with Options<br />

4.4. grep with Pipes<br />

4.5. egrep (Extended grep)<br />

4.6. fgrep (Fixed grep or Fast grep)


4.7. Linux and GNU grep<br />

4.8. GNU Basic grep (grep -G) with Regular Expressions<br />

4.9. grep -E or egrep (GNU Extended grep)<br />

4.10. Fixed grep (grep -F and fgrep)<br />

4.11. Recursive grep (rgrep, grep -R)<br />

4.12. GNU grep with Options<br />

4.13. grep with Options (UNIX and GNU)<br />

LAB 1: grep EXERCISE<br />

Chapter 5. sed, the Streamlined Editor<br />

5.1. What Is sed ?<br />

5.2. Versions of sed<br />

5.3. How Does sed Work?<br />

5.4. Regular Expressions<br />

5.5. Addressing<br />

5.6. Commands and Options<br />

5.7. Error Messages and Exit Status<br />

5.8. Metacharacters<br />

5.9. sed <strong>Example</strong>s<br />

5.10. sed Scripting<br />

LAB 2: sed EXERCISE<br />

Chapter 6. The awk Utility<br />

6.1. What's awk? What's nawk? What's gawk?<br />

6.2. awk's Format<br />

6.3. How awk Works<br />

6.4. Formatting Output<br />

6.5. awk Commands from Within a File<br />

6.6. Records and Fields<br />

6.7. Patterns and Actions<br />

6.8. Regular Expressions<br />

6.9. awk Commands in a Script File<br />

6.10. Review


LAB 3: awk EXERCISE<br />

6.11. Comparison Expressions<br />

6.12. Review<br />

LAB 4: awk EXERCISE<br />

6.13. Variables<br />

6.14. Redirection and Pipes<br />

6.15. Pipes<br />

6.16. Review<br />

LAB 5: nawk EXERCISE<br />

6.17. Conditional Statements<br />

6.18. Loops<br />

6.19. Program Control Statements<br />

6.20. Arrays<br />

6.21. awk Built-In Functions<br />

6.22. Built-In Arithmetic Functions<br />

6.23. User-Defined Functions (nawk)<br />

6.24. Review<br />

LAB 6: nawk EXERCISE<br />

6.25. Odds and Ends<br />

LAB 7: nawk EXERCISE<br />

6.26. awk Built-In Functions<br />

Chapter 7. The Interactiven Bourne Shell<br />

7.1. Introduction<br />

7.2. The Environment<br />

7.3. The Command Line<br />

7.4. Shell Metacharacters (Wildcards)<br />

7.5. Filename Substitution<br />

7.6. Variables<br />

7.7. Quoting<br />

7.8. Command Substitution<br />

7.9. An Introduction to Functions


7.10. Standard I/O and Redirection<br />

7.11. Pipes<br />

7.12. The here document and Input<br />

Chapter 8. Programming the Bourne Shell<br />

8.1. Introduction<br />

8.2. Reading User Input<br />

8.3. Arithmetic<br />

8.4. Positional Parameters and Command-Line Arguments<br />

8.5. Conditional Constructs and Flow Control<br />

8.6. Looping Commands<br />

8.7. Functions<br />

8.8. Trapping Signals<br />

8.9. The Command Line<br />

8.10. Shell Invocation Options<br />

LAB 8: BOURNE SHELL-GETTING STARTED<br />

LAB 9: METACHARACTERS<br />

LAB 10: REDIRECTION<br />

LAB 11: FIRST SCRIPT<br />

LAB 12: COMMAND-LINE ARGUMENTS<br />

LAB 13: GETTING USER INPUT<br />

LAB 14: CONDITIONAL STATEMENTS<br />

LAB 15: CONDITIONALS AND FILE TESTING<br />

LAB 16: THE case STATEMENT<br />

LAB 17: LOOPS<br />

LAB 18: FUNCTIONS<br />

Chapter 9. The Interactive C and TC <strong>Shells</strong><br />

9.1. Introduction<br />

9.2. The Environment<br />

9.3. The C/TC Shell Command Line<br />

9.4. Aliases<br />

9.5. Manipulating the Directory Stack


9.6. Job Control<br />

9.7. Shell Metacharacters<br />

9.8. Filename Substitution<br />

9.9. Redirection and Pipes<br />

9.10. Variables<br />

9.11. Command Substitution<br />

9.12. Quoting<br />

9.13. New Features of the Interactive TC Shell<br />

9.14. The TC Shell Command Line<br />

9.15. TC Shell Command, Filename, and Variable Completion<br />

9.16. TC Shell Spelling Correction<br />

9.17. TC Shell Aliases<br />

9.18. TC Shell Job Control<br />

9.19. Printing the Values of Variables in the TC Shell<br />

9.20. TC Shell Built-In Commands<br />

LAB 19: THE TC SHELL-GETTING STARTED<br />

LAB 20: HISTORY<br />

LAB 21: SHELL METACHARACTERS<br />

LAB 22: REDIRECTION<br />

LAB 23: VARIABLES AND ARRAYS<br />

Chapter 10. Programming the C and TC <strong>Shells</strong><br />

10.1. Introduction<br />

10.2. Reading User Input<br />

10.3. Arithmetic<br />

10.4. Debugging Scripts<br />

10.5. Command-Line Arguments<br />

10.6. Conditional Constructs and Flow Control<br />

10.7. Looping Commands<br />

10.8. Interrupt Handling<br />

10.9. setuid Scripts<br />

10.10. Storing Scripts


10.11. Built-In Commands<br />

LAB 24: C/TC SHELLS-GETTING STARTED<br />

LAB 25: SHELL METACHARACTERS<br />

LAB 26: REDIRECTION<br />

LAB 27: FIRST SCRIPT<br />

LAB 28: GETTING USER INPUT<br />

LAB 29: COMMAND-LINE ARGUMENTS<br />

LAB 30: CONDITIONALS AND FILE TESTING<br />

LAB 31: THE switch STATEMENT<br />

LAB 32: LOOPS<br />

Chapter 11. The Interactive Korn Shell<br />

11.1. Introduction<br />

11.2. The Environment<br />

11.3. The Command Line<br />

11.4. Commenting and Filename Expansion<br />

11.5. Aliases<br />

11.6. Job Control<br />

11.7. Metacharacters<br />

11.8. Filename Substitution (Wildcards)<br />

11.9. Variables<br />

11.10. Quoting<br />

11.11. Command Substitution<br />

11.12. Functions<br />

11.13. Standard I/O and Redirection<br />

11.14. Pipes<br />

11.15. Timing Commands<br />

Chapter 12. Programming the Korn Shell<br />

12.1. Introduction<br />

12.2. Reading User Input<br />

12.3. Arithmetic<br />

12.4. Positional Parameters and Command-Line Arguments


12.5. Conditional Constructs and Flow Control<br />

12.6. Looping Commands<br />

12.7. Arrays<br />

12.8. Functions<br />

12.9. Trapping Signals<br />

12.10. Coprocesses<br />

12.11. Debugging<br />

12.12. The Command Line<br />

12.13. Security<br />

12.14. Built-In Commands<br />

12.15. Korn Shell Invocation Arguments<br />

LAB 33: KORN SHELL-GETTING STARTED<br />

LAB 34: HISTORY<br />

LAB 35: ALIASES AND FUNCTIONS<br />

LAB 36: SHELL METACHARACTERS<br />

LAB 37: TILDE EXPANSION, QUOTES, AND COMMAND<br />

SUBSTITUTION<br />

LAB 38: REDIRECTION<br />

LAB 39: JOB CONTROL<br />

LAB 40: WRITING THE info SHELL SCRIPT<br />

LAB 41: VARIABLE EXPANSION OF SUBSTRINGS<br />

LAB 42: THE lookup SCRIPT<br />

LAB 43: USING typeset<br />

LAB 44: THE if/else CONSTRUCT AND THE let COMMAND<br />

LAB 45: THE case STATEMENT<br />

LAB 46: THE select LOOP<br />

LAB 47: AUTOLOADING FUNCTIONS<br />

Chapter 13. The Interactive Bash Shell<br />

13.1. Introduction<br />

13.2. The Environment<br />

13.3. The Command Line


13.4. Job Control<br />

13.5. Command-Line Shortcuts<br />

13.6. Aliases<br />

13.7. Manipulating the Directory Stack<br />

13.8. Metacharacters (Wildcards)<br />

13.9. Filename Substitution (Globbing)<br />

13.10. Variables<br />

13.11. Quoting<br />

13.12. Command Substitution<br />

13.13. Arithmetic Expansion<br />

13.14. Order of Expansion<br />

13.15. Arrays<br />

13.16. Functions<br />

13.17. Standard I/O and Redirection<br />

13.18. Pipes<br />

13.19. Shell Invocation Options<br />

13.20. Shell Built-In Commands<br />

LAB 48: bash SHELL-GETTING STARTED<br />

LAB 49: JOB CONTROL<br />

LAB 50: COMMAND COMPLETION, HISTORY, AND ALIASES<br />

LAB 51: SHELL METACHARACTERS<br />

LAB 52: REDIRECTION<br />

LAB 53: VARIABLES<br />

Chapter 14. Programming the Bash Shell<br />

14.1. Introduction<br />

14.2. Reading User Input<br />

14.3. Arithmetic<br />

14.4. Positional Parameters and Command-Line Arguments<br />

14.5. Conditional Constructs and Flow Control<br />

14.6. Looping Commands<br />

14.7. Functions


14.8. Trapping Signals<br />

14.9. Debugging<br />

14.10. The Command Line<br />

14.11. bash Options<br />

14.12. Shell Built-In Commands<br />

LAB 54: bash SHELL-FIRST SCRIPT<br />

LAB 55: COMMAND-LINE ARGUMENTS<br />

LAB 56: GETTING USER INPUT<br />

LAB 57: CONDITIONAL STATEMENTS<br />

LAB 58: CONDITIONALS AND FILE TESTING<br />

LAB 59: THE case STATEMENT<br />

LAB 60: LOOPS<br />

LAB 61: FUNCTIONS<br />

Chapter 15. Debugging Shell Scripts<br />

15.1. Introduction<br />

15.2. Style Issues<br />

15.3. Types of Errors<br />

15.4. Probable Causes for Syntax Errors<br />

15.5. Tracing with Shell Options and the set Command<br />

15.6. Summary<br />

Chapter 16. The System Administrator and the Shell<br />

16.1. Introduction<br />

16.2. The Superuser<br />

16.3. Becoming a Superuser with the su Command<br />

16.4. Boot Scripts<br />

16.5. Summary<br />

Appendix A. Useful UNIX/Linux Utilities for Shell Programmers<br />

apropos-searches the whatis database for strings<br />

arch-prints the machine architecture (see uname -m)<br />

at, batch-executes commands at a later time<br />

awk-pattern scanning and processing language


anner-makes posters<br />

basename-with a directory name delivers portions of the pathname<br />

bash-GNU Bourne Again Shell<br />

bc-processes precision arithmetic<br />

bdiff-compares two big files<br />

cal-displays a calendar<br />

cat-concatenates and displays files<br />

chfn-changes the finger information<br />

chmod-changes the permissions mode of a file<br />

chown-changes owner of file<br />

chsh-changes your login shell<br />

clear-clears the terminal screen<br />

cmp-compares two files<br />

compress-compress, uncompress, zcat compress, uncompress files, or<br />

display expanded files<br />

cp-copies files<br />

cpio-copies file archives in and out<br />

cron-the clock daemon<br />

crypt-encodes or decodes a file<br />

cut-removes selected fields or characters from each line of a file<br />

date-displays the date and time or sets the date<br />

dd-converts a file while copying it<br />

diff-compares two files for differences<br />

dos, xdos, dosexec, dosdebug-a Linux DOS emulator that runs MS-<br />

DOS and MS-DOS programs under Linux<br />

df-summarizes free disk space<br />

du-summarizes disk usage<br />

echo-echoes arguments<br />

egrep-searches a file for a pattern using full regular expressions<br />

expr-evaluates arguments as an expression<br />

fgrep-searches a file for a character string


file-determines the type of a file by looking at its contents<br />

find-finds files<br />

finger-displays information about local and remote users<br />

fmt-simple text formatters<br />

fold-folds long lines<br />

ftp-file transfer program<br />

free-displays amount of free and used memory in the system<br />

fuser-identifies processes using files or sockets<br />

gawk-pattern scanning and processing language<br />

gcc, g++-GNU project C and C++ Compiler (v2.7)<br />

getopt(s)-parses command-line options<br />

grep-searches a file for a pattern<br />

groups-prints group membership of user<br />

gzip, gunzip, zcat-compresses or expands files<br />

head-outputs the first ten lines of a file(s)<br />

host-prints information about specified hosts or zones in DNS<br />

id-prints the username, user ID, group name, and group ID<br />

jsh-the standard, job control shell<br />

kill-sends a signal to terminate one or more processes<br />

killall-kills processes by name<br />

less-opposite of more<br />

line-reads one line<br />

ln-creates hard links to files<br />

logname-gets the name of the user running the process<br />

look-displays lines beginning with a given string<br />

lp-sends output to a printer (AT&T)<br />

lpr-sends output to a printer (UCB)<br />

lpstat-prints information about the status of the LP print service (AT&T)<br />

lpq-prints information about the status of the printer (UCB)<br />

ls-lists contents of directory<br />

mail, rmail-reads mail or sends mail to users


mailx-interactive message processing system<br />

make-maintains, updates, and regenerates groups of related programs<br />

and files<br />

man-formats and displays the online manual pages<br />

manpath-determines user's search path for man pages<br />

mesg-permits or denies messages resulting from the write command<br />

mkdir-creates a directory<br />

more-browses or pages through a text file<br />

mtools-utilities to access DOS disks in UNIX<br />

mv-moves or renames files<br />

nawk-pattern scanning and processing language<br />

newgrp-logs into a new group<br />

news-prints news items<br />

nice-runs a command at low priority<br />

nohup-makes commands immune to hangups and quits<br />

od-octal dump<br />

pack, pcat, unpack-compresses and expands files<br />

passwd-changes the login password and password attributes<br />

paste-merges same lines of several files or subsequent lines of one file<br />

pcat-(see pack)<br />

pine-a Program for Internet News and E-mail<br />

pg-displays files one page at a time<br />

pr-prints files<br />

ping-reports if a remote system is reachable and alive<br />

ps-reports process status<br />

pstree-displays a tree of processes<br />

pwd-displays the present working directory name<br />

quota-displays users' disk usage and limits<br />

rcp-remote file copy<br />

rdate-gets the date and time via the network<br />

rgrep-a recursive, highlighting grep program


login-remote login<br />

rm-removes files from directories<br />

rmdir-removes a directory<br />

rsh-starts a remote shell<br />

ruptime-shows the host status of local machines<br />

rwho-who is logged in on local machines<br />

script-creates a typescript of a terminal session<br />

sed-streamlined editor<br />

size-prints section sizes in bytes of object files<br />

sleep-suspends execution for some number of seconds<br />

sort-sorts and/or merges files<br />

spell-finds spelling errors<br />

split-splits a file into pieces<br />

strings-finds any printable strings in an object or binary file<br />

stty-sets the options for a terminal<br />

su-become superuser or another user<br />

sum-calculates a checksum for a file<br />

sync-updates the superblock and sends changed blocks to disk<br />

tabs-sets tab stops on a terminal<br />

tail-displays the tail end of a file<br />

talk-allows you to talk to another user<br />

tar-stores and retrieves files from an archive file, normally a tape device<br />

tee-replicates the standard output<br />

telnet-communicates with a remote host<br />

test-evaluates an expression<br />

time-displays a summary of time used by this shell and its children<br />

timex-times a command; reports process data and system activity<br />

top-displays top CPU processes<br />

touch-updates access time and/or modification time of a file<br />

tput-initializes a terminal or queries the terminfo database<br />

tr-translates characters


true-provides successful exit status<br />

tsort-topological sort<br />

tty-gets the name of the terminal<br />

umask-sets file-creation mode mask for permissions<br />

uname-prints name of current machine<br />

uncompress-restores files to their original state after they have been<br />

compressed using the compress command<br />

uniq-reports on duplicate lines in a file<br />

units-converts quantities expressed in standard scales to other scales<br />

unpack-expands files created by pack<br />

uucp-copies files to another system, UNIX-to-UNIX system copy<br />

uuencode, uudecode-encodes a binary file into ASCII text in order to<br />

send it through e-mail, or converts it back into its original form<br />

wc-counts lines, words, and characters<br />

what-extracts SCCS version information from a file by printing<br />

information found after the @(#) pattern<br />

which-locates a command and displays its pathname or alias (UCB)<br />

whereis-locates the binary, source, and manual page files for a command<br />

(UCB)<br />

who-displays who is logged on the system<br />

write-writes a message to another user<br />

xargs-constructs an argument list(s) and executes a command<br />

zcat-uncompresses a compressed file to standard output; Same as<br />

uncompress c<br />

zipinfo-lists detailed information about a ZIP archive<br />

zmore-file perusal filter for crt viewing of compressed text<br />

Appendix B. Comparison of the <strong>Shells</strong><br />

B.1. The <strong>Shells</strong> Compared<br />

B.2. tcsh versus csh<br />

B.3. bash versus sh<br />

CD-ROM Warranty

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!