You are on page 1of 433

ÒëÕßÐò

ºÁÎÞÒÉÎÊ£¬UNIX/Linux×îÖØÒªµÄÈí¼þÖ®Ò»¾ÍÊÇshell£¬Ä¿Ç°×îÁ÷ÐеÄshell±»³ÆΪBash(Bourne Again Shell


shellÊÇÈç´ËµØÖØÒª£¬µ«ÁîÈ˾ªÆæµÄÊÇ£¬½éÉÜshellµÄÊéûÓÐÕæÕýÁîÈËÂúÒâµÄ¡£ËùÐÒµÄÊÇ£¬ÎÒ¿´µ½ÁËÕâ±¾±»È
ÔÚ¿´µ½±¾ÊéµÄÓ¢ÎÄ°æºó£¬ÎÒ¾ö¶¨°ÑËü·­Òë³öÀ´£¬ÔÚLinuxsirÂÛ̳ÉϽáʶÁËÒëÕßÖ®Ò»Ñî´ºÃô¹²Í¬·­ÒëÕâ±¾Êé£
¹ØÓÚ°æȨµÄÎÊÌ⣬ӢÎÄ°æµÄ×÷ÕßMendel Cooper¶ÔÓ¢ÎÄ°æµÄ°æȨ×öÁËÏêϸµÄÔ¼¶¨£¬Çë²Î¿¼£ºAppendix Q. Co
±¾ÊéµÃÒԳɸ壬ÎÒ£¨»ÆÒ㣩Ҫ¶àлÎÒµÄÅ®ÅóÓÑ£¬±¾¸Ã¸øÓèËýµÄʱ¼äÎÒÓÃÀ´ÁË·­Ò룬¶àлÄãµÄÀí½â£¬ÄãÊÇÒ»¸
ÒëÕß Ñî´ºÃô »ÆÒã
2006.5.15
Advanced Bash-Scripting Guide
<<¸ß¼¶Bash½Å±¾±à³ÌÖ¸ÄÏ>>
Ò»±¾ÉîÈëѧϰshell½Å±¾ÒÕÊõµÄÊé¼®
Version 3.7.2
2005/11/16
×÷Õß:Mendel Cooper
mail:thegrendel@theriver.com
Õâ±¾Êé¼Ù¶¨ÄãûÓÐÈκνű¾»òÒ»°ã³ÌÐòµÄ±à³Ì֪ʶ,µ«ÊÇÈç¹ûÄãÓÐÏà¹ØµÄ֪ʶ,ÄÇôÄ㽫ºÜÈÝÒ×
´ïµ½Öи߼¶µÄˮƽ...all the while sneaking in little snippets of UNIX? wisdom and
lore(Õâ¾ä²»ÖªµÀÔõôÒë).Äã¿ÉÒ԰ѱ¾Êé×÷Ϊ½Ì²Ä,×ÔѧÊÖ²á,»òÕßÄã»ñµÃshell½Å±¾¼¼ÊõµÄÎĵµ.
ÊéÖеÄÁ·Ï°ºÍÀý×ӽű¾ÖеÄ×¢Êͽ«»áÓë¶ÁÕßÓиüºÃµÄ»¥¶¯,µ«ÊÇ×î¹Ø¼üµÄÇ°ÌáÊÇ:
ÏëÕæÕýѧϰ½Å±¾±à³ÌµÄΨһ;¾¶¾ÍÊDZàд½Å±¾.
Õâ±¾ÊéÒ²¿É×÷Ϊ½Ì²ÄÀ´½²½âÒ»°ãµÄ±à³Ì¸ÅÄî.
ÏÂÔر¾Êé×îа汾,http://personal.riverusers.com/~thegrendel/abs-guide-3.7.tar.bz2,
ÕâÊÇÒ»¸öÒÔtarºÍbzip2½øÐдò°üµÄ,²¢ÇÒÊÇÒÔHTMLÀ´·¢ÐеÄ.µ±È»,ÄãÒ²¿ÉÒÔ»ñµÃ±¾ÊéµÄpdf°æ±¾
ÔÚhttp://www.tldp.org/LDP/abs/abs-guide.pdf.¿ÉÒÔÔÚ
http://personal.riverusers.com/~thegrendel/Change.logÖв鿴ÐÞ¶©ÀúÊ·.
ÒëÕß:Ñî´ºÃô£¬»ÆÒã
mail:chunmin.yang@gmail.com
Ò»Ö±ÏëºÃºÃѧϰһÏÂbash,¿ÉϧÍøÉϵÄ×ÊÁ϶¼ÔÓÂÒ²»¿°,ÎÒ»¹ÊÇϲ»¶Í¨¹ýÒ»±¾ÊéϵͳµÄѧϰ.Õâ±¾
ÊéÀ´µÃÕýÊÇʱºò.±¾ÊéµÄ×÷ÕßÕæÊǷdz£µÄÑϽ÷,´ÓÀý×ÓÀïµÄ¸Ä½øÈËÃûµ¥¾ÍÄÜ¿´³öÀ´.¿ÉϧÎÒˮƽÕæ
µÄÊǷdz£ÓÐÏÞ,ºÃ¶àµØ·½¹À¼ÆÒëµÃ¶¼ÓÐÎÊÌâ.Ï£ÍûÔĶÁµÄÅóÓÑÃǶà¶àÌáЩÐ޸Ľ¨Òé.ÎһᾡÎÒµÄ×î
´óŬÁ¦È¥ÐÞÕýËü.
Ŀ¼
++++
µÚÒ»²¿·Ö. ÈÈÉí
1. ΪʲôʹÓÃshell±à³Ì
2. ´ø×ÅÒ»¸öSha-Bang³ö·¢(Sha-BangÖ¸µÄÊÇ#!)
2.1. µ÷ÓÃÒ»¸ö½Å±¾
2.2. ³õ²½µÄÁ·Ï°
µÚ¶þ²¿·Ö. »ù±¾
3. ÌØÊâ×Ö·û
4. ±äÁ¿ºÍ²ÎÊýµÄ½éÉÜ
4.1. ±äÁ¿Ìæ»»
4.2. ±äÁ¿¸³Öµ
4.3. Bash±äÁ¿ÊDz»·ÖÀàÐ͵Ä
4.4. ÌØÊâµÄ±äÁ¿ÀàÐÍ
5. ÒýÓÃ(·­ÒëµÄ¿ÉÄÜÓÐÎÊÌâ,ÌØÖ¸ÒýºÅ)
5.1. ÒýÓñäÁ¿
5.2. תÒå(\)
6. Í˳öºÍÍ˳ö״̬
7. Tests
7.1. Test½á¹¹
7.2. Îļþ²âÊÔ²Ù×÷
7.3. ÆäËû±È½Ï²Ù×÷
7.4. ǶÌ×µÄif/thenÌõ¼þtest
7.5. ¼ì²éÄãµÄtest֪ʶ
8. ²Ù×÷·ûºÍÏà¹ØµÄÖ÷Ìâ
8.1. ²Ù×÷·û
8.2. Êý×Ö³£Á¿
µÚÈý²¿·Ö. ³¬Ô½»ù±¾
9. ±äÁ¿ÖØÓÎ
9.1. ÄÚ²¿±äÁ¿
9.2. ²Ù×÷×Ö·û´®
9.3. ²ÎÊýÌæ»»
9.4. Ö¸¶¨ÀàÐ͵ıäÁ¿:declare»òÕßtypeset
9.5. ±äÁ¿µÄ¼ä½ÓÒýÓÃ
9.6. $RANDOM: ²úÉúËæ»úÕûÊý
9.7. Ë«Ô²À¨ºÅ½á¹¹
10. Ñ­»·ºÍ·ÖÖ§
10.1. Ñ­»·
10.2. ǶÌ×Ñ­»·
10.3. Ñ­»·¿ØÖÆ
10.4. ²âÊÔÓë·ÖÖ§(caseºÍselect½á¹¹)
11. ÄÚ²¿ÃüÁîÓëÄÚ½¨
11.1. ×÷Òµ¿ØÖÆÃüÁî
12. Íⲿ¹ýÂËÆ÷,³ÌÐòºÍÃüÁî
12.1. »ù±¾ÃüÁî
12.2. ¸´ÔÓÃüÁî
12.3. ʱ¼ä/ÈÕÆÚ ÃüÁî
12.4. Îı¾´¦ÀíÃüÁî
12.5. ÎļþÓë¹éµµÃüÁî
12.6. ͨѶÃüÁî
12.7. Öն˿ØÖÆÃüÁî
12.8. Êýѧ¼ÆËãÃüÁî
12.9. »ìÔÓÃüÁî
13. ϵͳÓë¹ÜÀíÃüÁî
13.1. ·ÖÎöÒ»¸öϵͳ½Å±¾
14. ÃüÁîÌæ»»
15. ËãÊõÀ©Õ¹
16. I/O Öض¨Ïò
16.1. ʹÓÃexec
16.2. ´úÂë¿éµÄÖض¨Ïò
16.3. Ó¦ÓÃ
17. Here Documents
17.1. Here Strings
18. ÐÝϢʱ¼ä
Part 4. ¸ß¼¶
19. ÕýÔò±í´ïʽ
19.1. Ò»¸ö¼òÒªµÄÕýÔò±í´ïʽ½éÉÜ
19.2. ͨÅä
20. ×Óshell(Subshells)
21. ÊÜÏÞshell(Restricted Shells)
22. ½ø³ÌÌæ»»
23. º¯Êý
23.1. ¸´ÔÓº¯ÊýºÍº¯Êý¸´ÔÓÐÔ
23.2. ¾Ö²¿±äÁ¿
23.3. ²»Ê¹Óþֲ¿±äÁ¿µÄµÝ¹é
24. ±ðÃû(Aliases)
25. Áбí½á¹¹
26. Êý×é
27. /dev ºÍ /proc
27.1. /dev
27.2. /proc
28. ¹ØÓÚZerosºÍNulls
29. µ÷ÊÔ
30. Ñ¡Ïî
31. Gotchas
32. ½Å±¾±à³Ì·ç¸ñ
32.1. ·Ç¹Ù·½µÄShell½Å±¾·ç¸ñ
33. ÔÓÏî
33.1. ½»»¥Ê½ºÍ·Ç½»»¥Ê½µÄshellsºÍ½Å±¾
33.2. Shell °ü×°
33.3. ²âÊԺͱȽÏ: ÁíÒ»ÖÖ·½·¨
33.4. µÝ¹é
33.5. ²ÊÉ«½Å±¾
33.6. ÓÅ»¯
33.7. ¸÷ÖÖС¼¼ÇÉ
33.8. °²È«»°Ìâ
33.8.1. ±»¸ÐȾµÄ½Å±¾
33.8.2. Òþ²ØShell½Å±¾Ô´Âë
33.9. ÒÆÖ²»°Ìâ
33.10. ÔÚWindowsϽøÐÐShell±à³Ì
34. Bash, °æ±¾ 2 ºÍ 3
34.1. Bash, °æ±¾2
34.2. Bash, °æ±¾3
35. ºó¼Ç
35.1. ×÷Õߺó¼Ç
35.2. ¹ØÓÚ×÷Õß
35.3. ÄÄÀï¿ÉÒÔÈ¡µÃ°ïÖú?
35.4. ÖÆ×÷Õâ±¾ÊéµÄ¹¤¾ß
35.4.1. Ó²¼þ
35.4.2. Èí¼þºÍÅÅ°æÈí¼þ
35.5. Credits
Bibliography
A. Contributed Scripts
B. Reference Cards
C. A Sed and Awk Micro-Primer
C.1. Sed
C.2. Awk
D. Exit Codes With Special Meanings
E. A Detailed Introduction to I/O and I/O Redirection
F. Standard Command-Line Options
G. Important Files
H. Important System Directories
I. Localization
J. History Commands
K. A Sample .bashrc File
L. Converting DOS Batch Files to Shell Scripts
M. Exercises
M.1. Analyzing Scripts
M.2. Writing Scripts
N. Revision History
O. Mirror Sites
P. To Do List
Q. Copyright
±í¸ñÇåµ¥:
11-1. ×÷Òµ±êʶ·û
30-1. Bash Ñ¡Ïî
33-1. תÒåÐòÁÐÖÐÊýÖµºÍ²ÊÉ«µÄ¶ÔÓ¦
B-1. Special Shell Variables
B-2. TEST Operators: Binary Comparison
B-3. TEST Operators: Files
B-4. Parameter Substitution and Expansion
B-5. String Operations
B-6. Miscellaneous Constructs
C-1. Basic sed operators
C-2. Examples of sed operators
D-1. "Reserved" Exit Codes
L-1. Batch file keywords / variables / operators, and their shell equivalents
L-2. DOS commands and their UNIX equivalents
N-1. Revision History
Àý×ÓÇåµ¥:
2-1. Çå³ý:Çå³ý/var/logϵÄlogÎļþ
2-2. Çå³ý:Ò»¸ö¸ÄÁ¼µÄÇå³ý½Å±¾
2-3. cleanup:Ò»¸öÔöÇ¿µÄºÍ¹ãÒåµÄɾ³ýlogfileµÄ½Å±¾
3-1. ´úÂë¿éºÍI/OÖض¨Ïò
3-2. ½«Ò»¸ö´úÂë¿éµÄ½á¹û±£´æµ½Îļþ
3-3. ÔÚºǫ́ÔËÐÐÒ»¸öÑ­»·
3-4. ±¸·Ý×îºóÒ»ÌìËùÓÐÐ޸ĵÄÎļþ.
4-1. ±äÁ¿¸³ÖµºÍÌæ»»
4-2. Ò»°ãµÄ±äÁ¿¸³Öµ
4-3. ±äÁ¿¸³Öµ,Ò»°ãµÄºÍ±È½ÏÌØÊâµÄ
4-4. ÕûÐÍ»¹ÊÇstring?
4-5. λÖòÎÊý
4-6. wh,whois½ÚµãÃû×Ö²éѯ
4-7. ʹÓÃshift
5-1. echoһЩ¹îÒìµÄ±äÁ¿
5-2. תÒå·û
6-1. exit/exit״̬
6-2. ·ñ¶¨Ò»¸öÌõ¼þʹÓÃ!
7-1. ʲôÇé¿öÏÂΪÕæ?
7-2. ¼¸¸öµÈЧÃüÁîtest,/usr/bin/test,[],ºÍ/usr/bin/[
7-3. ËãÊý²âÊÔʹÓÃ(( ))
7-4. testËÀµÄÁ´½ÓÎļþ
7-5. Êý×ÖºÍ×Ö·û´®±È½Ï
7-6. ²âÊÔ×Ö·û´®ÊÇ·ñΪnull
7-7. zmore
8-1. ×î´ó¹«Ô¼Êý
8-2. ʹÓÃËãÊõ²Ù×÷·û
8-3. ʹÓÃ&&ºÍ||½øÐлìºÏ״̬µÄtest
8-4. Êý×Ö³£Á¿µÄ´¦Àí
9-1. $IFSºÍ¿Õ°×
9-2. ʱ¼äÊäÈë
9-3. ÔÙÀ´Ò»¸öʱ¼äÊäÈë
9-4. Timed read
9-5. ÎÒÊÇroot?
9-6. arglist:ͨ¹ý$*ºÍ$@ÁгöËùÓеIJÎÊý
9-7. ²»Ò»ÖµÄ$*ºÍ$@ÐÐΪ
9-8. µ±$IFSΪ¿ÕʱµÄ$*ºÍ$@
9-9. Ï»®Ïß±äÁ¿
9-10. ÔÚÒ»¸öÎı¾ÎļþµÄ¶Î¼ä²åÈë¿ÕÐÐ
9-11. ÀûÓÃÐÞ¸ÄÎļþÃû,À´×ª»»Í¼Æ¬¸ñʽ
9-12. Ä£·ÂgetoptÃüÁî
9-13. ÌáÈ¡×Ö·û´®µÄÒ»ÖÖ¿ÉÑ¡µÄ·½·¨
9-14. ʹÓòÎÊýÌæ»»ºÍerror messages
9-15. ²ÎÊýÌæ»»ºÍ"usage"messages
9-16. ±äÁ¿³¤¶È
9-17. ²ÎÊýÌæ»»ÖеÄģʽƥÅä
9-18. ÖØÃüÃûÎļþÀ©Õ¹Ãû
9-19. ʹÓÃģʽƥÅäÀ´·ÖÎö±È½ÏÌØÊâµÄ×Ö·û´®
9-20. ¶Ô×Ö·û´®µÄǰ׺»òºó׺ʹÓÃÆ¥Åäģʽ
9-21. ʹÓÃdeclareÀ´Ö¸¶¨±äÁ¿µÄÀàÐÍ
9-22. ¼ä½ÓÒýÓÃ
9-23. ´«µÝÒ»¸ö¼ä½ÓÒýÓøøawk
9-24. ²úÉúËæ»úÊý
9-25. ´ÓÒ»¸±ÆË¿ËÅÆÖÐÈ¡³öÒ»ÕÅËæ»úµÄÅÆ
9-26. Á½¸öÖ¸¶¨ÖµÖ®¼äµÄËæ»úÊý
9-27. ʹÓÃËæ»úÊýÀ´Ò¡Ò»¸ö÷»×Ó
9-28. ÖØзÖÅäËæ»úÊýÖÖ×Ó
9-29. ʹÓÃawk²úÉúαËæ»úÊý
9-30. C·ç¸ñµÄ±äÁ¿´¦Àí
10-1. Ñ­»·µÄÒ»¸ö¼òµ¥Àý×Ó
10-2. ÿ¸ö[list]ÔªËØ´øÁ½¸ö²ÎÊýµÄforÑ­»·
10-3. ÎļþÐÅÏ¢:¶Ô°üº¬ÔÚ±äÁ¿ÖеÄÎļþÁбí½øÐвÙ×÷
10-4. ÔÚforÑ­»·ÖвÙ×÷Îļþ
10-5. ÔÚforÑ­»·ÖÐÊ¡ÂÔ[list]
10-6. ʹÓÃÃüÁîÌæ»»À´²úÉúforÑ­»·µÄ[list]
10-7. ¶ÔÓÚ¶þ½øÖÆÎļþµÄÒ»¸ögrepÌæ»»
10-8. ÁгöϵͳÉϵÄËùÓÐÓû§
10-9. ÔÚĿ¼µÄËùÓÐÎļþÖвéÕÒÔ´×Ö´®
10-10. ÁгöĿ¼ÖÐËùÓеķûºÅÁ¬½ÓÎļþ
10-11. ½«Ä¿Â¼ÖеķûºÅÁ¬½ÓÎļþÃû±£´æµ½Ò»¸öÎļþÖÐ
10-12. Ò»¸öC·ç¸ñµÄforÑ­»·
10-13. ÔÚbatch modeÖÐʹÓÃefax
10-14. ¼òµ¥µÄwhileÑ­»·
10-15. ÁíÒ»¸öwhileÑ­»·
10-16. ¶àÌõ¼þµÄwhileÑ­»·
10-17. C·ç¸ñµÄwhileÑ­»·
10-18. untilÑ­»·
10-19. ǶÌ×Ñ­»·
10-20. breakºÍcontinueÃüÁîÔÚÑ­»·ÖеÄЧ¹û
10-21. ¶à²ãÑ­»·µÄÍ˳ö
10-22. ¶à²ãÑ­»·µÄcontinue
10-23. ÔÚʵ¼ÊµÄÈÎÎñÖÐʹÓÃ"continue N"
10-24. ʹÓÃcase
10-25. ʹÓÃcaseÀ´´´½¨²Ëµ¥
10-26. ʹÓÃÃüÁîÌæ»»À´²úÉúcase±äÁ¿
10-27. ¼òµ¥×Ö·û´®Æ¥Åä
10-28. ¼ì²éÊÇ·ñÊÇ×ÖĸÊäÈë
10-29. ÓÃselectÀ´´´½¨²Ëµ¥
10-30. Óú¯ÊýÖÐselect½á¹¹À´´´½¨²Ëµ¥
11-1. Ò»¸öfork³ö¶à¸ö×Ô¼ºÊµÀýµÄ½Å±¾
11-2. printf
11-3. ʹÓÃread,±äÁ¿·ÖÅä
11-4. µ±Ê¹ÓÃÒ»¸ö²»´ø±äÁ¿²ÎÊýµÄreadÃüÁîʱ,½«»á·¢Éúʲô?
11-5. readÃüÁîµÄ¶àÐÐÊäÈë
11-6. ¼ì²â·½Ïò¼ü
11-7. ͨ¹ýÎļþÖض¨ÏòÀ´Ê¹ÓÃread
11-8. ¹ÜµÀÊä³öµ½readÖеÄÎÊÌâ
11-9. Ð޸ĵ±Ç°µÄ¹¤×÷Ŀ¼
11-10. ÓÃ"let"ÃüÁîÀ´×÷ËãÊõ²Ù×÷.
11-11. ÏÔʾevalÃüÁîµÄЧ¹û
11-12. Ç¿ÖƵdzö(log-off)
11-13. ÁíÒ»¸ö"rot13"µÄ°æ±¾
11-14. ÔÚPerl½Å±¾ÖÐʹÓÃevalÃüÁîÀ´Ç¿ÖƱäÁ¿Ìæ»»
11-15. ʹÓÃsetÀ´¸Ä±ä½Å±¾µÄλÖòÎÊý
11-16. ÖØзÖÅäλÖòÎÊý
11-17. UnsetÒ»¸ö±äÁ¿
11-18. ʹÓÃexportÃüÁî´«µÝÒ»¸ö±äÁ¿µ½Ò»¸öÄÚǶawkµÄ½Å±¾ÖÐ
11-19. ʹÓÃgetoptsÃüÁîÀ´¶ÁÈ¡´«µÝ¸ø½Å±¾µÄÑ¡Ïî/²ÎÊý.
11-20. "Including"Ò»¸öÊý¾ÝÎļþ
11-21. Ò»¸öûʲôÓõÄ,source×ÔÉíµÄ½Å±¾
11-22. execµÄЧ¹û
11-23. Ò»¸öexec×ÔÉíµÄ½Å±¾
11-24. ÔÚ¼ÌÐø´¦Àí֮ǰ,µÈ´ýÒ»¸ö½ø³ÌµÄ½áÊø
11-25. Ò»¸ö½áÊø×ÔÉíµÄ½Å±¾.
12-1. ʹÓÃlsÃüÁîÀ´´´½¨Ò»¸öÉÕ¼CDRµÄÄÚÈÝÁбí
12-2. Hello or Good-bye
12-3. ɾ³ýµ±Ç°Ä¿Â¼ÏÂÎļþÃûÖаüº¬Ò»Ð©ÌØÊâ×Ö·û(°üÀ¨¿Õ°×)µÄÎļþ..
12-4. ͨ¹ýÎļþµÄ inode ºÅÀ´É¾³ýÎļþ
12-5. Logfile: ʹÓà xargs À´¼à¿Øϵͳ log
12-6. °Ñµ±Ç°Ä¿Â¼ÏµÄÎļþ¿½±´µ½ÁíÒ»¸öÎļþÖÐ
12-7. ͨ¹ýÃû×ÖKill½ø³Ì
12-8. ʹÓÃxargs·ÖÎöµ¥´Ê³öÏÖµÄƵÂÊ
12-9. ʹÓà expr
12-10. ʹÓà date ÃüÁî
12-11. ·ÖÎöµ¥´Ê³öÏÖµÄƵÂÊ
12-12. ÄǸöÎļþÊǽű¾?
12-13. ²úÉú10½øÖÆËæ»úÊý
12-14. ʹÓà tail ÃüÁîÀ´¼à¿Øϵͳlog
12-15. ÔÚÒ»¸ö½Å±¾ÖÐÄ£·Â "grep" µÄÐÐΪ
12-16. ÔÚ1913ÄêµÄΤÊϴʵäÖвéÕÒ¶¨Òå
12-17. ¼ì²éÁбíÖе¥´ÊµÄÕýÈ·ÐÔ
12-18. ת»»´óд: °ÑÒ»¸öÎļþµÄÄÚÈÝÈ«²¿×ª»»Îª´óд.
12-19. ת»»Ð¡Ð´: ½«µ±Ç°Ä¿Â¼ÏµÄËùÓÐÎÄÈ«²¿×ª»»ÎªÐ¡Ð´.
12-20. Du: DOS µ½ UNIX Îı¾ÎļþµÄת»».
12-21. rot13: rot13, ÈõÖǼÓÃÜ.
12-22. Generating "Crypto-Quote" Puzzles
12-23. ¸ñʽ»¯ÎļþÁбí.
12-24. ʹÓà column À´¸ñʽ»¯Ä¿Â¼Áбí
12-25. nl: Ò»¸ö×Ô¼º¼ÆËãÐкŵĽű¾.
12-26. manview: ²é¿´¸ñʽ»¯µÄmanÒ³
12-27. ʹÓà cpio À´¿½±´Ò»¸öĿ¼Ê÷
12-28. ½â°üÒ»¸ö rpm ¹éµµÎļþ
12-29. ´Ó C ÎļþÖÐÈ¥µô×¢ÊÍ
12-30. Exploring /usr/X11R6/bin
12-31. Ò»¸ö"¸Ä½ø¹ý"µÄ strings ÃüÁî
12-32. ÔÚÒ»¸ö½Å±¾ÖÐʹÓà cmp À´±È½Ï2¸öÎļþ.
12-33. basename ºÍ dirname
12-34. ¼ì²éÎļþÍêÕûÐÔ
12-35. Uudecod ±àÂëºóµÄÎļþ
12-36. ²éÕÒÀÄÓõÄÁ¬½ÓÀ´±¨¸æÀ¬»øÓʼþ·¢ËÍÕß
12-37. ·ÖÎöÒ»¸öÀ¬»øÓʼþÓò
12-38. »ñµÃÒ»·Ý¹ÉƱ±¨¼Û
12-39. ¸üРFedora Core 4
12-40. ʹÓà ssh
12-41. Ò»¸ö¿ÉÒÔmail×Ô¼ºµÄ½Å±¾
12-42. °´Ô³¥»¹´û¿î
12-43. ÊýÖÆת»»
12-44. ʹÓà "here document" À´µ÷Óà bc
12-45. ¼ÆËãÔ²ÖÜÂÊ
12-46. ½«10½øÖÆÊý×Öת»»Îª16½øÖÆÊý×Ö
12-47. Òò×Ó·Ö½â
12-48. ¼ÆËãÖ±½ÇÈý½ÇÐεÄб±ß
12-49. ʹÓà seq À´²úÉúÑ­»·²ÎÊý
12-50. ×Öĸͳ¼Æ
12-51. ʹÓÃgetoptÀ´·ÖÎöÃüÁîÐÐÑ¡Ïî
12-52. Ò»¸ö¿½±´×ÔÉíµÄ½Å±¾
12-53. Á·Ï°dd
12-54. ¼Ç¼°´¼ü
12-55. °²È«µÄɾ³ýÒ»¸öÎļþ
12-56. ÎļþÃû²úÉúÆ÷
12-57. ½«Ã×ת»»ÎªÓ¢Àï
12-58. ʹÓà m4
13-1. ÉèÖÃÒ»¸öÐÂÃÜÂë
13-2. ÉèÖÃÒ»¸ö²Á³ý×Ö·û
13-3. ¹ØµôÖն˶ÔÓÚÃÜÂëµÄecho
13-4. °´¼ü¼ì²â
13-5. Checking a remote server for identd<rojy bug>
13-6. pidof °ïÖúɱµôÒ»¸ö½ø³Ì
13-7. ¼ì²éÒ»¸öCD¾µÏñ
13-8. ÔÚÒ»¸öÎļþÖд´½¨Îļþϵͳ
13-9. Ìí¼ÓÒ»¸öеÄÓ²ÅÌÇý¶¯Æ÷
13-10. ʹÓÃumaskÀ´½«Êä³öÎļþÒþ²ØÆðÀ´
13-11. killall, À´×ÔÓÚ /etc/rc.d/init.d
14-1. ÓÞ´ÀµÄ½Å±¾²ßÂÔ
14-2. ´ÓÑ­»·µÄÊä³öÖвúÉúÒ»¸ö±äÁ¿
14-3. ÕÒanagram(»ØÎĹ¹´Ê·¨, ¿ÉÒÔ½«Ò»¸öÓÐÒâÒåµÄµ¥´Ê, ±ä»»Îª1¸ö»ò¶à¸öÓÐÒâÒåµÄµ¥´Ê, µ«ÊÇ»¹ÊÇÔ­À´
16-1. ʹÓÃexecÖض¨Ïò±ê×¼ÊäÈë
16-2. ʹÓÃexecÀ´Öض¨Ïòstdout
16-3. ʹÓÃexecÔÚͬһ½Å±¾ÖÐÖض¨ÏòstdinºÍstdout
16-4. ±ÜÃâ×Óshell
16-5. whileÑ­»·µÄÖض¨Ïò
16-6. ÁíÒ»ÖÖwhileÑ­»·µÄÖض¨Ïò
16-7. untilÑ­»·Öض¨Ïò
16-8. forÑ­»·Öض¨Ïò
16-9. forÑ­»·Öض¨Ïò loop (½«±ê×¼ÊäÈëºÍ±ê×¼Êä³ö¶¼Öض¨ÏòÁË)
16-10. Öض¨Ïòif/then²âÊԽṹ
16-11. ÓÃÓÚÉÏÃæÀý×ÓµÄ"names.data"Êý¾ÝÎļþ
16-12. ¼Ç¼ÈÕ־ʼþ
17-1. ¹ã²¥: ·¢ËÍÏûÏ¢¸øÿ¸öµÇ¼ÉϵÄÓû§
17-2. ·ÂÔìÎļþ: ´´½¨Ò»¸öÁ½ÐеķÂÔìÎļþ
17-3. ʹÓÃcatµÄ¶àÐÐÏûÏ¢
17-4. ´øÓÐÒÖÖÆtab¹¦ÄܵĶàÐÐÏûÏ¢
17-5. ʹÓòÎÊýÌæ»»µÄhere document
17-6. ÉÏ´«Ò»¸öÎļþ¶Ôµ½"Sunsite"µÄincomingĿ¼
17-7. ¹Ø±Õ²ÎÊýÌæ»»
17-8. Ò»¸ö²úÉúÁíÍâÒ»¸ö½Å±¾µÄ½Å±¾
17-9. Here documentsÓ뺯Êý
17-10. "ÄäÃû" here Document
17-11. ×¢Ê͵ôÒ»¶Î´úÂë¿é
17-12. Ò»¸ö×ÔÎĵµ»¯(self-documenting)µÄ½Å±¾
17-13. ÔÚÒ»¸öÎļþµÄ¿ªÍ·Ìí¼ÓÎı¾
20-1. ×ÓshellÖеıäÁ¿×÷ÓÃÓò
20-2. ÁгöÓû§µÄÅäÖÃÎļþ
20-3. ÔÚ×ÓshellÀï½øÐд®Ðд¦Àí
21-1. ÔÚÊÜÏÞµÄÇé¿öÏÂÔËÐнű¾
23-1. ¼òµ¥º¯Êý
23-2. ´ø×ŲÎÊýµÄº¯Êý
23-3. º¯ÊýºÍ±»´«¸ø½Å±¾µÄÃüÁîÐвÎÊý
23-4. ´«µÝ¼ä½ÓÒýÓøøº¯Êý
23-5. ½â³ý´«µÝ¸øº¯ÊýµÄ²ÎÊýÒýÓÃ
23-6. Ôٴγ¢ÊÔ½â³ý´«µÝ¸øº¯ÊýµÄ²ÎÊýÒýÓÃ
23-7. Á½¸öÊýÖеÄ×î´óÕß
23-8. °ÑÊý×Öת»¯³ÉÂÞÂíÊý×Ö
23-9. ²âÊÔº¯Êý×î´óµÄ·µ»ØÖµ
23-10. ±È½ÏÁ½¸ö´óÕûÊý
23-11. Óû§ÃûµÄÕæʵÃû
23-12. ¾Ö²¿±äÁ¿µÄ¿É¼û·¶Î§
23-13. Óþֲ¿±äÁ¿À´µÝ¹é
23-14. ººÅµËþ
24-1. ½Å±¾ÖеıðÃû
24-2. unalias: ÉèÖúÍɾ³ý±ðÃû
25-1. ʹÓÃ"ÓëÁбí(and list)"À´²âÊÔÃüÁîÐвÎÊý
25-2. ÓÃ"ÓëÁбí"µÄÁíÒ»¸öÃüÁîÐвÎÊý²âÊÔ
25-3. "»òÁбí"ºÍ"ÓëÁбí"µÄ½áºÏʹÓÃ
26-1. ¼òµ¥µÄÊý×éÓ÷¨
26-2. ¸ñʽ»¯Ò»Ê×Ê«
26-3. ¶àÖÖÊý×é²Ù×÷
26-4. ÓÃÓÚÊý×éµÄ×Ö·û´®²Ù×÷·û
26-5. ½«½Å±¾µÄÄÚÈÝ´«¸øÊý×é
26-6. һЩÊý×éרÓõŤ¾ß
26-7. ¹ØÓÚ¿ÕÊý×éºÍ¿ÕÊý×éÔªËØ
26-8. ³õʼ»¯Êý×é
26-9. ¸´ÖƺÍÁ¬½ÓÊý×é
26-10. ¹ØÓÚÁ¬½ÓÊý×éµÄ¸ü¶àÐÅÏ¢
26-11. һλÀÏÅóÓÑ: ðÅÝÅÅÐò
26-12. ÄÚǶÊý×éºÍ¼ä½ÓÒýÓÃ
26-13. ¸´ÔÓÊý×éÓ¦ÓÃ: °£À­ÍÐÉ«ÄáËØÊýɸ×Ó
26-14. Ä£ÄâÏÂÍƵĶÑÕ»
26-15. ¸´ÔÓµÄÊý×éÓ¦ÓÃ: ÁгöÒ»ÖÖ¹ÖÒìµÄÊýѧÐòÁÐ
26-16. Ä£Äâ¶þάÊý×é,²¢Ê¹ËüÇãб
27-1. ÀûÓÃ/dev/tcp À´¼ìÐÞ¹ÊÕÏ
27-2. ËÑË÷ÓëÒ»¸öPIDÏà¹ØµÄ½ø³Ì
27-3. ÍøÂçÁ¬½Ó״̬
28-1. Òþ²Øcookie¶ø²»ÔÙʹÓÃ
28-2. ÓÃ/dev/zero´´½¨Ò»¸ö½»»»ÁÙʱÎļþ
28-3. ´´½¨ramdisk
29-1. Ò»¸ö´íÎóµÄ½Å±¾
29-2. ¶ªÊ§¹Ø¼ü×Ö(keyword)
29-3. ÁíÒ»¸ö´íÎó½Å±¾
29-4. ÓÃ"assert"²âÊÔÌõ¼þ
29-5. ²¶×½ exit
29-6. ÔÚControl-CºóÇå³ýÀ¬»ø
29-7. ¸ú×Ù±äÁ¿
29-8. ÔËÐжà½ø³Ì (ÔڶദÀíÆ÷µÄ»úÆ÷Àï)
31-1. Êý×ÖºÍ×Ö·û´®±È½ÏÊDz»ÏàµÈͬµÄ
31-2. ×ÓSHELLȱÏÝ
31-3. °ÑechoµÄÊä³öÓùܵÀÊäË͸øreadÃüÁî
33-1. shell °ü×°
33-2. ÉÔ΢¸´ÔÓһЩµÄshell°ü×°
33-3. дµ½ÈÕÖ¾ÎļþµÄshell°ü×°
33-4. °ü×°awkµÄ½Å±¾
33-5. ÁíÒ»¸ö°ü×°awkµÄ½Å±¾
33-6. °ÑPerlǶÈëBash½Å±¾
33-7. Bash ºÍ Perl ½Å±¾ÁªºÏʹÓÃ
33-8. µÝ¹éµ÷ÓÃ×Ô¼º±¾ÉíµÄ(ÎÞÓÃ)½Å±¾
33-9. µÝ¹éµ÷ÓÃ×Ô¼º±¾ÉíµÄ(ÓÐÓÃ)½Å±¾
33-10. ÁíÒ»¸öµÝ¹éµ÷ÓÃ×Ô¼º±¾ÉíµÄ(ÓÐÓÃ)½Å±¾
33-11. Ò»¸ö "²ÊÉ«µÄ" µØÖ·×ÊÁÏ¿â
33-12. »­ºÐ×Ó
33-13. ÏÔʾ²ÊÉ«Îı¾
33-14. "ÈüÂí" ÓÎÏ·
33-15. ·µ»ØÖµ¼¼ÇÉ
33-16. ÕûÐÍ»¹ÊÇstring?
33-17. ´«µÝºÍ·µ»ØÊý×é
33-18. anagramsÓÎÏ·
33-19. ÔÚshell½Å±¾Öе÷ÓõĴ°¿Ú²¿¼þ
34-1. ×Ö·û´®À©Õ¹
34-2. ¼ä½Ó±äÁ¿ÒýÓà - з½·¨
34-3. ʹÓüä½Ó±äÁ¿ÒýÓõļòµ¥Êý¾Ý¿âÓ¦ÓÃ
34-4. ÓÃÊý×éºÍÆäËûµÄС¼¼ÇÉÀ´´¦ÀíËÄÈËËæ»ú´òÅÆ
A-1. mailformat: Formatting an e-mail message
A-2. rn: A simple-minded file rename utility
A-3. blank-rename: renames filenames containing blanks
A-4. encryptedpw: Uploading to an ftp site, using a locally encrypted password
A-5. copy-cd: Copying a data CD
A-6. Collatz series
A-7. days-between: Calculate number of days between two dates
A-8. Make a "dictionary"
A-9. Soundex conversion
A-10. "Game of Life"
A-11. Data file for "Game of Life"
A-12. behead: Removing mail and news message headers
A-13. ftpget: Downloading files via ftp
A-14. password: Generating random 8-character passwords
A-15. fifo: Making daily backups, using named pipes
A-16. Generating prime numbers using the modulo operator
A-17. tree: Displaying a directory tree
A-18. string functions: C-like string functions
A-19. Directory information
A-20. Object-oriented database
A-21. Library of hash functions
A-22. Colorizing text using hash functions
A-23. Mounting USB keychain storage devices
A-24. Preserving weblogs
A-25. Protecting literal strings
A-26. Unprotecting literal strings
A-27. Spammer Identification
A-28. Spammer Hunt
A-29. Making wget easier to use
A-30. A "podcasting" script
A-31. Basics Reviewed
A-32. An expanded cd command
C-1. Counting Letter Occurrences
K-1. Sample .bashrc file
L-1. VIEWDATA.BAT: DOS Batch File
L-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT
P-1. Print the server environment
µÚÒ»²¿·Ö ÈÈÉí
++++++++++++++++
shellÊÇÒ»¸öÃüÁî½âÊÍÆ÷.ÊǽéÓÚ²Ù×÷ϵͳkernelÓëÓû§Ö®¼äµÄÒ»¸ö¾øÔµ²ã.׼ȷµØ˵,ËüÒ²ÊÇÒ»
Ò»ÖÖÇ¿Á¦µÄ¼ÆËã»úÓïÑÔ.Ò»¸öshell³ÌÐò,±»³ÆΪһ¸ö½Å±¾,ÊÇÒ»ÖÖºÜÈÝÒ×ʹÓõŤ¾ß,Ëü¿ÉÒÔͨ¹ý
½«ÏµÍ³µ÷ÓÃ,¹«¹²³ÌÐò,¹¤¾ß,ºÍ±àÒë¹ýµÄ¶þ½øÖƳÌÐòÕ³ºÏÔÚÒ»ÆðÀ´½¨Á¢Ó¦ÓÃ.ÊÂʵÉÏ,ËùÓеÄUNIX
ÃüÁîºÍ¹¤¾ßÔÙ¼ÓÉϹ«¹²³ÌÐò,¶ÔÓÚshell½Å±¾À´Ëµ,¶¼Êǿɵ÷ÓõÄ.Èç¹ûÕâЩÄ㻹¾õµÃ²»¹»,ÄÇô
shellÄÚ½¨ÃüÁî,±ÈÈçtestÓëÑ­»·½á¹¹,Ò²»á¸ø½Å±¾Ìí¼ÓÇ¿Á¦µÄÖ§³ÖºÍÔö¼ÓÁé»îÐÔ.Shell½Å±¾¶ÔÓÚ
¹ÜÀíϵͳÈÎÎñºÍÆäËüµÄÖظ´¹¤×÷µÄÀý³ÌÀ´Ëµ,±íÏֵķdz£ºÃ,¸ù±¾²»ÐèÒªÄÇЩ»ª¶ø²»ÊµµÄ³ÉÊì
½ô´ÕµÄ³ÌÐòÓïÑÔ.
µÚ1Õ ΪʲôʹÓÃshell±à³Ì
===========================
ûÓгÌÐòÓïÑÔÊÇÍêÃÀµÄ.ÉõÖÁûÓÐÒ»¸öΨһ×îºÃµÄÓïÑÔ,Ö»ÓжÔÓÚÌض¨Ä¿µÄ,±È½ÏÊʺϺͲ»ÊʺÏ
µÄ³ÌÐòÓïÑÔ.
Herbert Mayer
¶ÔÓÚÈκÎÏëÊʵ±¾«Í¨Ò»Ð©ÏµÍ³¹ÜÀí֪ʶµÄÈËÀ´Ëµ,ÕÆÎÕshell½Å±¾ÖªÊ¶¶¼ÊÇ×î»ù±¾µÄ,¼´Ê¹ÕâЩ
ÈË¿ÉÄܲ¢²»´òËãÕæÕýµÄ±àдһЩ½Å±¾.ÏëÒ»ÏÂLinux»úÆ÷µÄÆô¶¯¹ý³Ì,ÔÚÕâ¸ö¹ý³ÌÖÐ,±Ø½«ÔËÐÐ
/etc/rc.dĿ¼ÏµĽű¾À´´æ´¢ÏµÍ³ÅäÖúͽ¨Á¢·þÎñ.ÏêϸµÄÀí½âÕâЩÆô¶¯½Å±¾¶ÔÓÚ·ÖÎöϵͳµÄ
ÐÐΪÊǷdz£ÖØÒªµÄ,²¢ÇÒÓÐʱºò¿ÉÄܱØÐëÐÞ¸ÄËü.
ѧϰÈçºÎ±àдshell½Å±¾²¢²»ÊÇÒ»¼þºÜÀ§ÄѵÄÊÂ,ÒòΪ½Å±¾¿ÉÒÔ·ÖΪºÜСµÄ¿é,²¢ÇÒÏà¶ÔÓÚshell
ÌØÐԵIJÙ×÷ºÍÑ¡Ïî[1]²¿·Ö,Ö»ÐèҪѧϰºÜСµÄÒ»²¿·Ö¾Í¿ÉÒÔÁË.Óï·¨ÊǼòµ¥²¢ÇÒÖ±¹ÛµÄ,±àд½Å
±¾ºÜÏñÊÇÔÚÃüÁîÐÐÉÏ°ÑһЩÏà¹ØÃüÁîºÍ¹¤¾ßÁ¬½ÓÆðÀ´,²¢ÇÒÖ»ÓкÜÉÙµÄÒ»²¿·Ö¹æÔòÐèҪѧϰ.
¾ø´ó²¿·Ö½Å±¾µÚÒ»´Î¾Í¿ÉÒÔÕý³£µÄ¹¤×÷,¶øÇÒ¼´Ê¹µ÷ÊÔÒ»¸ö³¤Ò»Ð©µÄ½Å±¾Ò²ÊǺÜÖ±¹ÛµÄ.
Ò»¸öshell½Å±¾ÊÇÒ»¸öÀàËÆÓÚС³ÔµêµÄ(quick and dirty)·½·¨,ÔÚÄãʹÓÃÔ­ÐÍÉè¼ÆÒ»¸ö¸´ÔÓµÄ
Ó¦ÓõÄʱºò.ÔÚ¹¤³Ì¿ª·¢µÄµÚÒ»½×¶Î,¼´Ê¹´Ó¹¦ÄÜÖÐÈ¡µÃºÜÓÐÏÞµÄÒ»¸ö×Ó¼¯·Åµ½shell½Å±¾ÖÐÀ´
Íê³ÉÍùÍù¶¼ÊǷdz£ÓÐÓõÄ.ʹÓÃÕâÖÖ·½·¨,³ÌÐòµÄ½á¹û¿ÉÒÔ±»²âÊԺͳ¢ÊÔÔËÐÐ,²¢ÇÒÔÚ´¦ÀíʹÓÃ
ÖîÈçC/C++,Java»òÕßPerlÓïÑÔ±àдµÄ×îÖÕ´úÂëÇ°,Ö÷ÒªµÄȱÏݺÍÏÝÚåÍùÍù¾Í±»·¢ÏÖÁË.
Shell½Å±¾×ñÑ­µäÐ͵ÄUNIXÕÜѧ,¾ÍÊÇ°Ñ´óµÄ¸´ÔӵŤ³Ì·Ö³ÉС¹æÄ£µÄ×ÓÈÎÎñ,²¢ÇÒ°ÑÕâЩ²¿¼þ
ºÍ¹¤¾ß×éºÏÆðÀ´.Ðí¶àÈËÈÏΪÕâÖÖ°ì·¨¸üºÃһЩ,ÖÁÉÙÕâÖÖ°ì·¨±ÈʹÓÃÄÇÖÖ¸ß\´ó\È«µÄÓïÑÔ¸ü
ÃÀ,¸üÓäÔÃ,¸üÊʺϽâ¾öÎÊÌâ.±ÈÈçPerl¾ÍÊÇÕâÖÖÄܸÉÈκÎÊÂÄÜÊʺÏÈκÎÈ˵ÄÓïÑÔ,µ«ÊÇ´ú¼Û¾ÍÊÇ
ÄãÐèҪǿÆÈ×Ô¼ºÊ¹ÓÃÕâÖÖÓïÑÔÀ´Ë¼¿¼½â¾öÎÊÌâµÄ°ì·¨.
ʲôʱºò²»Ê¹ÓÃShell½Å±¾
×ÊÔ´Ãܼ¯Ð͵ÄÈÎÎñ,ÓÈÆäÔÚÐèÒª¿¼ÂÇЧÂÊʱ(±ÈÈç,ÅÅÐò,hashµÈµÈ)
ÐèÒª´¦Àí´óÈÎÎñµÄÊýѧ²Ù×÷,ÓÈÆäÊǸ¡µãÔËËã,¾«È·ÔËËã,»òÕ߸´ÔÓµÄËãÊõÔËËã
(ÕâÖÖÇé¿öÒ»°ãʹÓÃC++»òFORTRANÀ´´¦Àí)
Óпçƽ̨ÒÆÖ²ÐèÇó(Ò»°ãʹÓÃC»òJava)
¸´ÔÓµÄÓ¦ÓÃ,ÔÚ±ØÐëʹÓýṹ»¯±à³ÌµÄʱºò(ÐèÒª±äÁ¿µÄÀàÐͼì²é,º¯ÊýÔ­ÐÍ,µÈµÈ)
¶ÔÓÚÓ°Ïìϵͳȫ¾ÖÐԵĹؼüÈÎÎñÓ¦Óá£
¶ÔÓÚ°²È«ÓкܸßÒªÇóµÄÈÎÎñ,±ÈÈçÄãÐèÒªÒ»¸ö½¡×³µÄϵͳÀ´·ÀÖ¹ÈëÇÖ,Æƽâ,¶ñÒâÆÆ»µµÈµÈ.
ÏîÄ¿ÓÉÁ¬´®µÄÒÀÀµµÄ¸÷¸ö²¿·Ö×é³É¡£
ÐèÒª´ó¹æÄ£µÄÎļþ²Ù×÷
ÐèÒª¶àάÊý×éµÄÖ§³Ö
ÐèÒªÊý¾Ý½á¹¹µÄÖ§³Ö,±ÈÈçÁ´±í»òÊýµÈÊý¾Ý½á¹¹
ÐèÒª²úÉú»ò²Ù×÷ͼÐλ¯½çÃæGUI
ÐèÒªÖ±½Ó²Ù×÷ϵͳӲ¼þ
ÐèÒªI/O»òsocket½Ó¿Ú
ÐèҪʹÓÿâ»òÕßÒÅÁôÏÂÀ´µÄÀÏ´úÂëµÄ½Ó¿Ú
˽È˵Ä,±ÕÔ´µÄÓ¦ÓÃ(shell½Å±¾°Ñ´úÂë¾Í·ÅÔÚÎı¾ÎļþÖÐ,È«ÊÀ½ç¶¼ÄÜ¿´µ½)
Èç¹ûÄãµÄÓ¦Ó÷ûºÏÉϱߵÄÈÎÒâÒ»Ìõ,ÄÇô¾Í¿¼ÂÇһϸüÇ¿´óµÄÓïÑÔ°É--»òÐíÊÇPerl,Tcl,Python,
Ruby -- »òÕßÊǸü¸ß²ã´ÎµÄ±àÒëÓïÑÔ±ÈÈçC/C++,»òÕßÊÇJava.¼´Ê¹Èç´Ë,Äã»á·¢ÏÖ,ʹÓÃshell
À´Ô­ÐÍ¿ª·¢ÄãµÄÓ¦ÓÃ,ÔÚ¿ª·¢²½ÖèÖÐÒ²ÊǷdz£ÓÐÓõÄ.
ÎÒÃǽ«¿ªÊ¼Ê¹ÓÃBash,BashÊÇ"Bourne-Again shell"Ê××ÖĸµÄËõд,Ò²ÊÇStephen BourneµÄ¾­µä
µÄBourne shellµÄÒ»¸öË«¹ØÓï,(ÒëÕß:˵ʵ»°,ÎÒÒ»Ö±¸ã²»ÇåÕâ¸öË«¹ØÓïÊÇʲôÒâ˼,Ϊʲô½Ð
"Bourn-Again shell",ÕâÆäÖÐÓ¦¸ÃÓиöʲôµä¹Ê°É,ÄÄλºÃÐÄ,¸æËßÎÒÒ»ÏÂ^^).BashÒѾ­³ÉΪÁË
ËùÓÐUNIXÖÐshell½Å±¾µÄÊÂʵÉϵıê×¼ÁË.ͬʱÕâ±¾ÊéÒ²¸²¸ÇÁ˾ø´ó²¿·ÖµÄÆäËûһЩshellµÄÔ­
Ôò,±ÈÈçKorn Shell,Bash´ÓkshÖм̳ÐÁËÒ»²¿·ÖÌØÐÔ,[2]C ShellºÍËüµÄ±äÖÖ.(×¢Òâ:C Shell
±à³ÌÊDz»±»ÍƼöµÄ,ÒòΪһЩÌض¨µÄÄÚÔÚÎÊÌâ,Tom ChristiansenÔÚ1993Äê10ÔÂÖ¸³öÁËÕâ¸öÎÊÌâ
ÇëÔÚhttp://www.etext.org/Quartz/computer/unix/csh.harmful.gzÖв鿴¾ßÌåÄÚÈÝ.)
½ÓÏÂÀ´Êǽű¾µÄһЩ˵Ã÷.ÔÚչʾshell²»Í¬µÄÌØÕ÷֮ǰ,Ëü¿ÉÒÔ¼õÇáһЩÔĶÁÊéÖÐÀý×Ó
µÄ¸ºµ£.±¾ÊéÖеÄÀý×ӽű¾,¶¼ÔÚ¾¡¿ÉÄܵķ¶Î§ÄÚ½øÐÐÁ˲âÊÔ,²¢ÇÒÆäÖеÄһЩ½«Ê¹ÓÃÔÚÕæ
ʵµÄÉú»îÖÐ.¶ÁÕß¿ÉÒÔÔËÐÐÕâЩÀý×ӽű¾(ʹÓÃscriptname.sh»òÕßscriptname.bashµÄÐÎʽ),
[3]²¢¸øÕâЩ½Å±¾Ö´ÐÐȨÏÞ(chmod u+rx scriptname),È»ºóÖ´ÐÐËüÃÇ,¿´¿´·¢ÉúÁËʲô.Èç¹û´æ
µµµÄ½Å±¾²»¿ÉÓÃ,ÄÇô¾Í´Ó±¾ÊéµÄHTML,pdf»òÕßtextµÄ·¢Ðа汾ÖаÑËüÃÇ¿½±´Õ³Ìù³öÀ´.¿¼Âǵ½
ÕâЩ½Å±¾ÖеÄÄÚÈÝÔÚÎÒÃÇ»¹Ã»½âÊÍËü֮ǰ¾Í±»ÁÐÔÚÕâÀï,¿ÉÄÜ»áÓ°Ïì¶ÁÕßµÄÀí½â,Õâ¾ÍÐèÒª¶ÁÕß
ÔÝʱºöÂÔÕâЩÄÚÈÝ.
³ý·ÇÌرð×¢Ã÷,±¾Êé×÷Õß±àдÁ˱¾ÊéÖеľø´ó²¿·ÖÀý×ӽű¾.
×¢ÒâÊÂÏî:
[1] ÕâЩÔÚbuiltinsÕ½ڱ»ÒýÓÃ,ÕâЩÊÇshellµÄÄÚ²¿ÌØÕ÷.
[2] ksh88µÄÐí¶àÌØÐÔ,ÉõÖÁÊÇһЩksh93µÄÌØÐÔ¶¼±»ºÏ²¢µ½BashÖÐÁË.
[3] ¸ù¾Ý¹ßÀý,Óû§±àдµÄBourne shell½Å±¾Ó¦¸ÃÔڽű¾µÄÃû×Öºó±ß¼ÓÉÏ.shÀ©Õ¹Ãû.
һЩϵͳ½Å±¾,±ÈÈçÄÇЩÔÚ/etc/rc.dÖеĽű¾,Ôò²»×ñÑ­ÕâÖÖÃüÃûÏ°¹ß.

µÚ2Õ ´ø×ÅÒ»¸öSha-Bang³ö·¢(Sha-BangÖ¸µÄÊÇ#!)
==============================================
ÔÚÒ»¸ö×î¼òµ¥µÄÀý×ÓÖÐ,Ò»¸öshell½Å±¾Æäʵ¾ÍÊǽ«Ò»¶ÑϵͳÃüÁîÁÐÔÚÒ»¸öÎļþÖÐ.ËüµÄ×î»ù±¾µÄ
Óô¦¾ÍÊÇ,ÔÚÄãÿ´ÎÊäÈëÕâЩÌض¨Ë³ÐòµÄÃüÁîʱ¿ÉÒÔÉÙÇÃһЩ×Ö.
Example 2-1 Çå³ý:Çå³ý/var/logϵÄlogÎļþ
################################Start Script####################################
###
1 # Cleanup
2 # µ±È»ÒªÊ¹ÓÃrootÉí·ÝÀ´ÔËÐÐÕâ¸ö½Å±¾
3
4 cd /var/log
5 cat /dev/null > messages
6 cat /dev/null > wtmp
7 echo "Logs cleaned up."
################################End Script######################################
###
Õâ¸ù±¾¾ÍûʲôϡÆæµÄ, Ö»²»¹ýÊÇÃüÁîµÄ¶Ñ»ý, À´ÈôÓconsole»òÕßxtermÖÐÒ»¸öÒ»¸öµÄÊäÈëÃü
Áî¸ü·½±ãһЩ.ºÃ´¦¾ÍÊÇ°ÑËùÓÐÃüÁ·ÅÔÚÒ»¸ö½Å±¾ÖÐ,²»ÓÃÿ´Î¶¼ÇÃËüÃÇ.ÕâÑùµÄ»°,¶ÔÓÚÌض¨
µÄÓ¦ÓÃÀ´Ëµ,Õâ¸ö½Å±¾¾ÍºÜÈÝÒ×±»Ð޸Ļò¶¨ÖÆ.
Example 2-2 Çå³ý:Ò»¸ö¸ÄÁ¼µÄÇå³ý½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # Ò»¸öBash½Å±¾µÄÕýÈ·µÄ¿ªÍ·²¿·Ö.
3
4 # Cleanup, °æ±¾ 2
5
6 # µ±È»ÒªÊ¹ÓÃrootÉí·ÝÀ´ÔËÐÐ.
7 # ÔÚ´Ë´¦²åÈë´úÂë,À´´òÓ¡´íÎóÏûÏ¢,²¢ÇÒÔÚ²»ÊÇrootÉí·ÝµÄʱºòÍ˳ö.
8
9 LOG_DIR=/var/log
10 # Èç¹ûʹÓñäÁ¿,µ±È»±È°Ñ´úÂëдËÀµÄºÃ.
11 cd $LOG_DIR
12
13 cat /dev/null > messages
14 cat /dev/null > wtmp
15
16
17 echo "Logs cleaned up."
18
19 exit # Õâ¸öÃüÁîÊÇÒ»ÖÖÕýÈ·²¢ÇÒºÏÊʵÄÍ˳ö½Å±¾µÄ·½·¨.
################################End Script######################################
###
ÏÖÔÚ,ÈÃÎÒÃÇ¿´Ò»ÏÂÒ»¸öÕæÕýÒâÒåµÄ½Å±¾.¶øÇÒÎÒÃÇ¿ÉÒÔ×ߵøüÔ¶...
Example 2-3. cleanup:Ò»¸öÔöÇ¿µÄºÍ¹ãÒåµÄɾ³ýlogfileµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # Çå³ý, °æ±¾ 3
3
4 # Warning:
5 # -------
6 # Õâ¸ö½Å±¾ÓкöàÌØÕ÷,ÕâЩÌØÕ÷ÊÇÔÚºó±ßÕ½ڽøÐнâÊ͵Ä,´ó¸ÅÊǽøÐе½±¾ÊéµÄÒ»°ëµÄ
7 # ʱºò,
8 # Äã¾Í»á¾õµÃËüûÓÐʲôÉñÃصÄÁË.
9 #
10
11
12
13 LOG_DIR=/var/log
14 ROOT_UID=0 # $UIDΪ0µÄʱºò,Óû§²Å¾ßÓиùÓû§µÄȨÏÞ
15 LINES=50 # ĬÈϵı£´æÐÐÊý
16 E_XCD=66 # ²»ÄÜÐÞ¸ÄĿ¼?
17 E_NOTROOT=67 # ·Ç¸ùÓû§½«ÒÔerrorÍ˳ö
18
19
20 # µ±È»ÒªÊ¹ÓøùÓû§À´ÔËÐÐ
21 if [ "$UID" -ne "$ROOT_UID" ]
22 then
23 echo "Must be root to run this script."
24 exit $E_NOTROOT
25 fi
26
27 if [ -n "$1" ]
28 # ²âÊÔÊÇ·ñÓÐÃüÁîÐвÎÊý(·Ç¿Õ).
29 then
30 lines=$1
31 else
32 lines=$LINES # ĬÈÏ,Èç¹û²»ÔÚÃüÁîÐÐÖÐÖ¸¶¨
33 fi
34
35
36 # Stephane Chazelas ½¨ÒéʹÓÃϱß
37 #+ µÄ¸üºÃ·½·¨À´¼ì²âÃüÁîÐвÎÊý.
38 #+ µ«¶ÔÓÚÕâÕÂÀ´Ëµ»¹ÊÇÓе㳬ǰ.
39 #
40 # E_WRONGARGS=65 # ·ÇÊýÖµ²ÎÊý(´íÎóµÄ²ÎÊý¸ñʽ)
41 #
42 # case "$1" in
43 # "" ) lines=50;;
44 # *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS
;;
45 # * ) lines=$1;;
46 # esac
47 #
48 #* Ö±µ½"Loops"µÄÕ½ڲŻá¶ÔÉϱߵÄÄÚÈݽøÐÐÏêϸµÄÃèÊö.
49
50
51 cd $LOG_DIR
52
53 if [ `pwd` != "$LOG_DIR" ] # »òÕß if[ "$PWD" != "$LOG_DIR" ]
54 # ²»ÔÚ /var/logÖÐ?
55 then
56 echo "Can't change to $LOG_DIR."
57 exit $E_XCD
58 fi # ÔÚ´¦Àílog file֮ǰ,ÔÙÈ·ÈÏÒ»±éµ±Ç°Ä¿Â¼ÊÇ·ñÕýÈ·.
59
60 # ¸üÓÐЧÂʵÄ×ö·¨ÊÇ
61 #
62 # cd /var/log || {
63 # echo "Cannot change to necessary directory." >&2
64 # exit $E_XCD;
65 # }
66
67
68
69
70 tail -$lines messages > mesg.temp # ±£´ælog fileÏûÏ¢µÄ×îºó²¿·Ö.
71 mv mesg.temp messages # ±äΪеÄlogĿ¼.
72
73
74 # cat /dev/null > messages
75 #* ²»ÔÙÐèÒªÁË,ʹÓÃÉϱߵķ½·¨¸ü°²È«.
76
77 cat /dev/null > wtmp # ': > wtmp' ºÍ '> wtmp'¾ßÓÐÏàͬµÄ×÷ÓÃ
78 echo "Logs cleaned up."
79
80 exit 0
81 # Í˳ö֮ǰ·µ»Ø0,·µ»Ø0±íʾ³É¹¦.
82 #
################################End Script######################################
###
ÒòΪÄã¿ÉÄÜÏ£Íû½«ÏµÍ³logÈ«²¿ÏûÃð,Õâ¸ö°æ±¾ÁôÏÂÁËlogÏûÏ¢×îºóµÄ²¿·Ö.Ä㽫²»¶ÏµØÕÒµ½ÐÂ
µÄ·½·¨À´ÍêÉÆÕâ¸ö½Å±¾,²¢Ìá¸ßЧÂÊ.
ҪעÒâ,ÔÚÿ¸ö½Å±¾µÄ¿ªÍ·¶¼Ê¹ÓÃ"#!",ÕâÒâζןæËßÄãµÄϵͳÕâ¸öÎļþµÄÖ´ÐÐÐèÒªÖ¸¶¨Ò»¸ö½â
ÊÍÆ÷.#!ʵ¼ÊÉÏÊÇÒ»¸ö2×Ö½Ú[1]µÄħ·¨Êý×Ö,ÕâÊÇÖ¸¶¨Ò»¸öÎļþÀàÐ͵ÄÌØÊâ±ê¼Ç, »»¾ä»°Ëµ, ÔÚ
ÕâÖÖÇé¿öÏÂ,Ö¸µÄ¾ÍÊÇÒ»¸ö¿ÉÖ´ÐеĽű¾(¼üÈëman magicÀ´»ñµÃ¹ØÓÚÕâ¸öÃÔÈË»°ÌâµÄ¸ü¶àÏêϸ
ÐÅÏ¢).ÔÚ#!Ö®ºó½Ó×ÅÊÇÒ»¸ö·¾¶Ãû.Õâ¸ö·¾¶ÃûÖ¸¶¨ÁËÒ»¸ö½âÊͽű¾ÖÐÃüÁîµÄ³ÌÐò,Õâ¸ö³ÌÐò¿É
ÒÔÊÇshell,³ÌÐòÓïÑÔ»òÕßÊÇÈÎÒâÒ»¸öͨÓóÌÐò.Õâ¸öÖ¸¶¨µÄ³ÌÐò´ÓÍ·¿ªÊ¼½âÊͲ¢ÇÒÖ´Ðнű¾ÖÐ
µÄÃüÁî(´Ó#!ÐÐϱߵÄÒ»ÐпªÊ¼),ºöÂÔ×¢ÊÍ.[2]
Èç:
1 #!/bin/sh
2 #!/bin/bash
3 #!/usr/bin/perl
4 #!/usr/bin/tcl
5 #!/bin/sed -f
6 #!/usr/awk -f
Éϱßÿһ¸ö½Å±¾Í·µÄÐж¼Ö¸¶¨ÁËÒ»¸ö²»Í¬µÄÃüÁî½âÊÍÆ÷,Èç¹ûÊÇ/bin/sh,ÄÇô¾ÍÊÇĬÈÏshell
(ÔÚLinuxϵͳÖÐĬÈÏÊÇBash).[3]ʹÓÃ#!/bin/sh,ÔÚ´ó¶àÊýÉÌÒµ·¢ÐеÄUNIXÉÏ,ĬÈÏÊÇBourne
shell,Õ⽫ÈÃÄãµÄ½Å±¾¿ÉÒÔÕý³£µÄÔËÐÐÔÚ·ÇLinux»úÆ÷ÉÏ,ËäÈ»Õ⽫»áÎþÉüBashһЩ¶ÀÌصÄÌØÕ÷.
½Å±¾½«ÓëPOSIX[4] µÄsh±ê×¼ÏàÒ»ÖÂ.
×¢Òâ: #! ºó±ß¸ø³öµÄ·¾¶Ãû±ØÐëÊÇÕýÈ·µÄ,·ñÔò½«»á³öÏÖÒ»¸ö´íÎóÏûÏ¢,ͨ³£ÊÇ
"Command not found",Õ⽫ÊÇÄãÔËÐÐÕâ¸ö½Å±¾Ê±ËùµÃµ½µÄΨһ½á¹û.
µ±È»"#!"Ò²¿ÉÒÔ±»ºöÂÔ,²»¹ýÕâÑùÄãµÄ½Å±¾Îļþ¾ÍÖ»ÄÜÊÇһЩÃüÁîµÄ¼¯ºÏ,²»Äܹ»Ê¹ÓÃshellÄÚ½¨
µÄÖ¸ÁîÁË,Èç¹û²»ÄÜʹÓñäÁ¿µÄ»°,µ±È»ÕâÒ²¾ÍʧȥÁ˽ű¾±à³ÌµÄÒâÒåÁË.
×¢Òâ:Õâ¸öÀý×Ó¹ÄÀøÄãʹÓÃÄ£¿é»¯µÄ·½Ê½À´±àд½Å±¾,ƽʱҲҪעÒâÊÕ¼¯Ò»Ð©ÁãËéµÄ´úÂë,
ÕâЩÁãËéµÄ´úÂë¿ÉÄÜÓÃÔÚÄ㽫À´±àдµÄ½Å±¾ÖÐ.ÕâÑùÄã¾Í¿ÉÒÔͨ¹ýÕâЩ´úÂëƬ¶ÎÀ´¹¹
ÔìÒ»¸ö½Ï´óµÄ¹¤³ÌÓÃÀý. ÒÔϱ߽ű¾×÷ΪÐò,À´²âÊԽű¾±»µ÷ÓõIJÎÊýÊÇ·ñÕýÈ·.
################################Start Script####################################
###
1 E_WRONG_ARGS=65
2 script_parameters="-a -h -m -z"
3 # -a = all, -h = help, µÈµÈ.
4
5 if [ $# -ne $Number_of_expected_args ]
6 then
7 echo "Usage: `basename $0` $script_parameters"
8 # `basename $0`ÊÇÕâ¸ö½Å±¾µÄÎļþÃû
9 exit $E_WRONG_ARGS
10 fi
################################End Script######################################
###
´ó¶àÊýÇé¿öÏÂ,ÄãÐèÒª±àдһ¸ö½Å±¾À´Ö´ÐÐÒ»¸öÌض¨µÄÈÎÎñ,ÔÚ±¾ÕÂÖеÚÒ»¸ö½Å±¾¾ÍÊÇÒ»¸öÕâÑù
µÄÀý×Ó, È»ºóÄã»áÐÞ¸ÄËüÀ´Íê³ÉÒ»¸ö²»Í¬µÄ,µ«±È½ÏÏàËƵÄÈÎÎñ.ÓñäÁ¿À´´úÌæдËÀµÄ³£Á¿,¾ÍÊÇ
Ò»¸öºÃ·½·¨,½«Öظ´µÄ´úÂë·Åµ½Ò»¸öº¯ÊýÖÐ,Ò²ÊÇÒ»ÖÖºÃÏ°¹ß.

2.1 µ÷ÓÃÒ»¸ö½Å±¾
----------------
±àдÍê½Å±¾Ö®ºó,Äã¿ÉÒÔʹÓÃsh scriptname,[5]»òÕßbash scriptnameÀ´µ÷ÓÃËü.
(²»ÍƼöʹÓÃsh <scriptname,ÒòΪÕâ½ûÓÃÁ˽ű¾´ÓstdinÖжÁÊý¾ÝµÄ¹¦ÄÜ.)
¸ü·½±ãµÄ·½·¨ÊÇÈýű¾±¾Éí¾Í¾ßÓпÉÖ´ÐÐȨÏÞ,ͨ¹ýchmodÃüÁî¿ÉÒÔÐÞ¸Ä.
±ÈÈç:
chmod 555 scriptname (ÔÊÐíÈκÎÈ˶¼¾ßÓÐ ¿É¶ÁºÍÖ´ÐÐȨÏÞ) [6]
»ò:
chmod +rx scriptname (ÔÊÐíÈκÎÈ˶¼¾ßÓÐ ¿É¶ÁºÍÖ´ÐÐȨÏÞ)
chmod u+rx scriptname (Ö»¸ø½Å±¾µÄËùÓÐÕß ¿É¶ÁºÍÖ´ÐÐȨÏÞ)
¼ÈÈ»½Å±¾ÒѾ­¾ßÓÐÁË¿ÉÖ´ÐÐȨÏÞ,ÏÖÔÚÄã¿ÉÒÔʹÓÃ./scriptname.[7]À´²âÊÔËüÁË.Èç¹ûÕâ¸ö½Å±¾
ÒÔÒ»¸ö"#!"ÐпªÍ·,ÄÇô½Å±¾½«»áµ÷ÓúÏÊʵÄÃüÁî½âÊÍÆ÷À´ÔËÐÐ.
×îºóÒ»²½,Ôڽű¾±»²âÊÔºÍdebugÖ®ºó,Äã¿ÉÄÜÏë°ÑËüÒƶ¯µ½/usr/local/bin(µ±È»ÊÇÒÔrootÉí·Ý)
,À´ÈÃÄãµÄ½Å±¾¶ÔËùÓÐÓû§¶¼ÓÐÓÃ.ÕâÑùÓû§¾Í¿ÉÒÔÖ±½ÓÇýű¾Ãû×ÖÀ´ÔËÐÐÁË.
×¢ÒâÊÂÏî:
[1] ÄÇЩ¾ßÓÐUNIXζµÀµÄ½Å±¾(»ùÓÚ4.2BSD)ÐèÒªÒ»¸ö4×Ö½ÚµÄħ·¨Êý×Ö,ÔÚ#!ºó±ßÐèÒªÒ»¸ö
¿Õ¸ñ#! /bin/sh.
[2] ½Å±¾ÖеÄ#!ÐеÄ×îÖØÒªµÄÈÎÎñ¾ÍÊÇÃüÁî½âÊÍÆ÷(sh»òÕßbash).ÒòΪÕâÐÐÊÇÒÔ#¿ªÊ¼µÄ,
µ±ÃüÁî½âÊÍÆ÷Ö´ÐÐÕâ¸ö½Å±¾µÄʱºò,»á°ÑËü×÷Ϊһ¸ö×¢ÊÍÐÐ.µ±È»,ÔÚÕâ֮ǰ,ÕâÐÐÓï¾ä
ÒѾ­Íê³ÉÁËËüµÄÈÎÎñ,¾ÍÊǵ÷ÓÃÃüÁî½âÊÍÆ÷.
Èç¹ûÔڽű¾µÄÀï±ß»¹ÓÐÒ»¸ö#!ÐÐ,ÄÇôbash½«°ÑËüÈÏΪÊÇÒ»¸öÒ»°ãµÄ×¢ÊÍÐÐ.
1 #!/bin/bash
2
3 echo "Part 1 of script."
4 a=1
5
6 #!/bin/bash
7 # Õ⽫²»»á¿ªÊ¼Ò»¸öнű¾.
8
9 echo "Part 2 of script."
10 echo $a # Value of $a stays at 1.
[3] ÕâÀï¿ÉÒÔÍæһЩС¼¼ÇÉ.
1 #!/bin/rm
2 # ×Ôɾ³ý½Å±¾.
3
4 # µ±ÄãÔËÐÐÕâ¸ö½Å±¾Ê±,»ù±¾ÉÏʲô¶¼²»»á·¢Éú...³ý·ÇÕâ¸öÎļþÏûʧ²»¼û.
5
6 WHATEVER=65
7
8 echo "This line will never print (betcha!)."
9
10 exit $WHATEVER # û¹Øϵ,½Å±¾ÊDz»»áÔÚÕâÍ˳öµÄ.
µ±È»,Ä㻹¿ÉÒÔÊÔÊÔÔÚÒ»¸öREADMEÎļþµÄ¿ªÍ·¼ÓÉÏ#!/bin/more,²¢ÈÃËü¾ßÓÐÖ´ÐÐȨÏÞ.
½á¹û½«ÊÇÎĵµ×Ô¶¯Áгö×Ô¼ºµÄÄÚÈÝ.(Ò»¸öʹÓÃcatÃüÁîµÄhere document¿ÉÄÜÊÇÒ»¸ö
¸üºÃµÄÑ¡Ôò,--¼ûExample 17-3).
[4] ¿ÉÒÆÖ²µÄ²Ù×÷ϵͳ½Ó¿Ú,±ê×¼»¯ÀàUNIX²Ù×÷ϵͳµÄÒ»ÖÖ³¢ÊÔ.POSIX¹æ·¶¿ÉÒÔÔÚ
http://www.opengroup.org/onlinepubs/007904975/toc.htmÖвéÔÄ.
[5] СÐÄ:ʹÓÃsh scriptnameÀ´µ÷Óýű¾µÄʱºò½«»á¹Ø±ÕһЩBashÌض¨µÄÀ©Õ¹,½Å±¾¿ÉÄÜ
Òò´Ë¶øµ÷ÓÃʧ°Ü.
[6] ½Å±¾ÐèÒª¶ÁºÍÖ´ÐÐȨÏÞ,ÒòΪshellÐèÒª¶ÁÕâ¸ö½Å±¾.
[7] Ϊʲô²»Ö±½ÓʹÓÃscriptnameÀ´µ÷Óýű¾?Èç¹ûÄ㵱ǰµÄĿ¼ÏÂ($PWD)ÕýºÃÓÐÄãÏëÒª
Ö´ÐеĽű¾,ΪʲôËüÔËÐв»ÁËÄØ?ʧ°ÜµÄÔ­ÒòÊÇ,³öÓÚ°²È«¿¼ÂÇ,µ±Ç°Ä¿Â¼²¢Ã»Óб»
¼ÓÔÚÓû§µÄ$PATH±äÁ¿ÖÐ.Òò´Ë,ÔÚµ±Ç°Ä¿Â¼Ïµ÷Óýű¾±ØÐëʹÓÃ./scriptnameÕâÖÖ
ÐÎʽ.

2.2 ³õ²½µÄÁ·Ï°
--------------
1. ϵͳ¹ÜÀíÔ±¾­³£»áΪÁË×Ô¶¯»¯Ò»Ð©³£ÓõÄÈÎÎñ¶ø±àд½Å±¾.¾Ù³ö¼¸¸öÕâÖÖÓÐÓõĽű¾µÄʵÀý.
2. ±àдһ¸ö½Å±¾,ÏÔʾʱ¼äºÍÈÕÆÚ,ÁгöËùÓеĵǼÓû§,ÏÔʾϵͳµÄ¸üÐÂʱ¼ä.È»ºóÕâ¸ö½Å±¾
½«»á°ÑÕâЩÄÚÈݱ£´æµ½Ò»¸ölog fileÖÐ.

µÚ¶þ²¿·Ö »ù±¾
++++++++++++++++
µÚ3ÕÂ ÌØÊâ×Ö·û
================
# ×¢ÊÍ,ÐÐÊ×ÒÔ#¿ªÍ·Îª×¢ÊÍ(#!ÊǸöÀýÍâ).
1 # This line is a comment.
×¢ÊÍÒ²¿ÉÒÔ´æÔÚÓÚ±¾ÐÐÃüÁîµÄºó±ß.
1 echo "A comment will follow." # ×¢ÊÍÔÚÕâÀï
2 # ^ ×¢Òâ#Ç°±ßµÄ¿Õ°×
×¢ÊÍÒ²¿ÉÒÔÔÚ±¾Ðпհ׵ĺó±ß.
1 # A tab precedes this comment.
×¢Òâ:ÃüÁîÊDz»ÄܸúÔÚͬһÐÐÉÏ×¢Ê͵ĺó±ßµÄ,ûÓа취,ÔÚͬһÐÐÉÏ,×¢Ê͵ĺó±ßÏë
ÒªÔÙʹÓÃÃüÁî,Ö»ÄÜÁíÆðÒ»ÐÐ.
µ±È»,ÔÚechoÃüÁîÖб»×ªÒåµÄ#ÊDz»ÄÜ×÷ΪעÊ͵Ä.
ͬÑùµÄ,#Ò²¿ÉÒÔ³öÏÖÔÚÌض¨µÄ²ÎÊýÌæ»»½á¹¹ÖлòÕßÊÇÊý×Ö³£Á¿±í´ïʽÖÐ.
1 echo "The # here does not begin a comment."
2 echo 'The # here does not begin a comment.'
3 echo The \# here does not begin a comment.
4 echo The # ÕâÀ↑ʼһ¸ö×¢ÊÍ
5
6 echo ${PATH#*:} # ²ÎÊýÌæ»»,²»ÊÇÒ»¸ö×¢ÊÍ
7 echo $(( 2#101011 )) # ÊýÖÆת»»,²»ÊÇÒ»¸ö×¢ÊÍ
8
9 # Thanks, S.C.
±ê×¼µÄÒýÓúÍתÒå×Ö·û("'\)¿ÉÒÔÓÃÀ´×ªÒå#
; ÃüÁî·Ö¸ô·û,¿ÉÒÔÓÃÀ´ÔÚÒ»ÐÐÖÐÀ´Ð´¶à¸öÃüÁî.
1 echo hello; echo there
2
3
4 if [ -x "$filename" ]; then # ×¢Òâ:"if"ºÍ"then"ÐèÒª·Ö¸ô
5 # Ϊɶ?
6 echo "File $filename exists."; cp $filename $filename.bak
7 else
8 echo "File $filename not found."; touch $filename
9 fi; echo "File test complete."
ÓÐʱºòÐèҪתÒå
;; ÖÕÖ¹"case"Ñ¡Ïî.
1 case "$variable" in
2 abc) echo "\$variable = abc" ;;
3 xyz) echo "\$variable = xyz" ;;
4 esac
. .ÃüÁîµÈ¼ÛÓÚsourceÃüÁî(¼ûExample 11-20).ÕâÊÇÒ»¸öbashµÄÄÚ½¨ÃüÁî.
. .×÷ΪÎļþÃûµÄÒ»²¿·Ö.Èç¹û×÷ΪÎļþÃûµÄǰ׺µÄ»°,ÄÇôÕâ¸öÎļþ½«³ÉΪÒþ²ØÎļþ.
½«²»±»lsÃüÁîÁгö.
bash$ touch .hidden-file
bash$ ls -l
total 10
-rw-r--r-- 1 bozo 4034 Jul 18 22:04 data1.addressbook
-rw-r--r-- 1 bozo 4602 May 25 13:58 data1.addressbook.ba
k
-rw-r--r-- 1 bozo 877 Dec 17 2000 employment.addressbo
ok

bash$ ls -al
total 14
drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./
drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressb
ook
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressb
ook.bak
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.add
ressbook
-rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file
.ÃüÁîÈç¹û×÷ΪĿ¼ÃûµÄÒ»²¿·ÖµÄ»°,ÄÇô.±í´ïµÄÊǵ±Ç°Ä¿Â¼.".."±íʾÉÏÒ»¼¶Ä¿Â¼.
bash$ pwd
/home/bozo/projects
bash$ cd .
bash$ pwd
/home/bozo/projects
bash$ cd ..
bash$ pwd
/home/bozo/
.ÃüÁî¾­³£×÷Ϊһ¸öÎļþÒƶ¯ÃüÁîµÄÄ¿µÄµØ.
bash$ cp /home/bozo/current_work/junk/* .
. .×Ö·ûÆ¥Åä,ÕâÊÇ×÷ΪÕýÔò±í´ïÊǵÄÒ»²¿·Ö,ÓÃÀ´Æ¥ÅäÈκεĵ¥¸ö×Ö·û.
" ²¿·ÖÒýÓÃ."STRING"×èÖ¹ÁËÒ»²¿·ÖÌØÊâ×Ö·û,¾ßÌå¼ûµÚ5ÕÂ.
' È«ÒýÓÃ. 'STRING' ×èÖ¹ÁËÈ«²¿ÌØÊâ×Ö·û,¾ßÌå¼ûµÚ5ÕÂ.
, ¶ººÅÁ´½ÓÁËһϵÁеÄËãÊõ²Ù×÷,ËäÈ»Àï±ßËùÓеÄÄÚÈݶ¼±»ÔËÐÐÁË,µ«Ö»ÓÐ×îºóÒ»Ïî±»
·µ»Ø.
Èç:
1 let "t2 = ((a = 9, 15 / 3))" # Set "a = 9" and "t2 = 15 / 3"
\ תÒå×Ö·û,Èç\XµÈ¼ÛÓÚ"X"»ò'X',¾ßÌå¼ûµÚ5ÕÂ.
/ ÎļþÃû·¾¶·Ö¸ô·û.»òÓÃÀ´×ö³ý·¨²Ù×÷.
` ºóÖÃÒýÓÃ,ÃüÁîÌæ»»,¾ßÌå¼ûµÚ14ÕÂ
: ¿ÕÃüÁî,µÈ¼ÛÓÚ"NOP"(no op,Ò»¸öʲôҲ²»¸ÉµÄÃüÁî).Ò²¿ÉÒÔ±»ÈÏΪÓëshellµÄÄÚ½¨ÃüÁî(
¸öbashµÄÄÚ½¨ÃüÁî,ËüµÄ·µ»ØֵΪ0,¾ÍÊÇshell·µ»ØµÄtrue.
Èç:
1 :
2 echo $? # 0
ËÀÑ­»·,Èç:
1 while :
2 do
3 operation-1
4 operation-2
5 ...
6 operation-n
7 done
8
9 # ÓëϱßÏàͬ:
10 # while true
11 # do
12 # ...
13 # done
ÔÚif/thenÖеÄռλ·û,Èç:
1 if condition
2 then : # ʲô¶¼²»×ö,Òý³ö·ÖÖ§.
3 else
4 take-some-action
5 fi
ÔÚÒ»¸ö2ÔªÃüÁîÖÐÌṩһ¸öռλ·û,¾ßÌå¼ûExample 8-2,ºÍ"ĬÈϲÎÊý".Èç:
1 : ${username=`whoami`}
2 # ${username=`whoami`} Èç¹ûûÓÐ":"µÄ»°,½«¸ø³öÒ»¸ö´íÎó,³ý·Ç"username"ÊÇ
3 # ¸öÃüÁî
ÔÚhere documentÖÐÌṩһ¸öռλ·û,¼ûExample 17-10.
ʹÓÃ"²ÎÊýÌæ»»"À´ÆÀ¹À×Ö·û´®±äÁ¿(¼ûExample 9-14).Èç:
1 : ${HOSTNAME?} ${USER?} ${MAIL?}
2 # Èç¹ûÒ»¸ö»ò¶à¸ö±ØÒªµÄ»·¾³±äÁ¿Ã»±»ÉèÖõĻ°,
3 #+ ¾Í´òÓ¡´íÎóÐÅÏ¢.
"±äÁ¿À©Õ¹/×Ó´®Ìæ»»"
ÔÚºÍ > (Öض¨Ïò²Ù×÷·û)½áºÏʹÓÃʱ,°ÑÒ»¸öÎļþ½Ø¶Ïµ½0³¤¶È,ûÓÐÐÞ¸ÄËüµÄȨÏÞ.
Èç¹ûÎļþÔÚ֮ǰ²¢²»´æÔÚ,ÄÇô¾Í´´½¨Ëü.Èç:
1 : > data.xxx #Îļþ"data.xxx"ÏÖÔÚ±»Çå¿ÕÁË.
2
3 #Óë cat /dev/null >data.xxx µÄ×÷ÓÃÏàͬ
4 #È»¶ø,Õâ²»»á²úÉúÒ»¸öеĽø³Ì,ÒòΪ":"ÊÇÒ»¸öÄÚ½¨ÃüÁî.
¾ßÌå²Î¼ûExample 12-14.
ÔÚºÍ>>Öض¨Ïò²Ù×÷·û½áºÏʹÓÃʱ,½«²»»á¶ÔÏëÒª¸½¼ÓµÄÎļþ²úÉúÈκÎÓ°Ïì.
Èç¹ûÎļþ²»´æÔÚ,½«´´½¨.
×¢Òâ: ÕâÖ»ÊÊÓÃÓÚÕý¹æÎļþ,¶ø²»ÊǹܵÀ,·ûºÅÁ¬½Ó,ºÍijЩÌØÊâÎļþ.
Ò²¿ÉÄÜÓÃÀ´×÷ΪעÊÍÐÐ,ËäÈ»ÎÒÃDz»ÍƼöÕâô×ö.ʹÓÃ#À´×¢Ê͵Ļ°,½«¹Ø±ÕÊ£ÓàÐеÄ
´íÎó¼ì²é,ËùÒÔ¿ÉÒÔÔÚ×¢ÊÍÐÐÖÐдÈκζ«Î÷.È»¶ø,ʹÓÃ:µÄ»°½«²»»áÕâÑù.Èç:
1 : This is a comment thar generates an error,(if [ $x -eq 3] ).
":"»¹ÓÃÀ´ÔÚ/etc/passwdºÍ$PATH±äÁ¿ÖÐÓÃÀ´×ö·Ö¸ô·û.
bash$ echo $PATH
/usr/local/bin:/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games
! È¡·´²Ù×÷·û,½«·´×ª"Í˳ö״̬"½á¹û,(¼ûExample 6-2).Ò²»á·´×ªtest²Ù×÷·ûµÄÒâÒå.±È
ÈçÐÞ¸Ä=Ϊ!=.!²Ù×÷ÊÇBashµÄÒ»¸ö¹Ø¼ü×Ö.
ÔÚÒ»¸ö²»Í¬µÄÉÏÏÂÎÄÖÐ,!Ò²»á³öÏÖÔÚ"¼ä½Ó±äÁ¿ÒýÓÃ"¼ûExample 9-22.
ÔÚÁíÒ»ÖÖÉÏÏÂÎÄÖÐ,!»¹ÄÜ·´×ªbashµÄ"history mechanism"(¼û¸½Â¼J ÀúÊ·ÃüÁî)
ÐèҪעÒâµÄÊÇ,ÔÚÒ»¸ö½Å±¾ÖÐ,"history mechanism"ÊDZ»½ûÓõÄ.
* ÍòÄÜÆ¥Åä×Ö·û,ÓÃÓÚÎļþÃûÆ¥Åä(Õâ¸ö¶«Î÷ÓиöרÓÐÃû´Ê½Ðfile globbing),»òÕßÊÇÕýÔò
±í´ïʽÖÐ.×¢Òâ:ÔÚÕýÔò±í´ïʽƥÅäÖеÄ×÷ÓúÍÔÚÎļþÃûÆ¥ÅäÖеÄ×÷ÓÃÊDz»Í¬µÄ.
bash$ echo *
abs-book.sgml add-drive.sh agram.sh alias.sh
* Êýѧ³Ë·¨.
**ÊÇÃÝÔËËã.
? ²âÊÔ²Ù×÷.ÔÚÒ»¸öÈ·¶¨µÄ±í´ïʽÖÐ,ÓÃ?À´²âÊÔ½á¹û.
(())½á¹¹¿ÉÒÔÓÃÀ´×öÊýѧ¼ÆËã»òÕßÊÇдc´úÂë,ÄÇ?¾ÍÊÇcÓïÑÔµÄ3Ôª²Ù×÷·ûµÄ
Ò»¸ö.
ÔÚ"²ÎÊýÌæ»»"ÖÐ,?²âÊÔÒ»¸ö±äÁ¿ÊÇ·ñ±»setÁË.
? ÔÚfile globbingÖкÍÔÚÕýÔò±í´ïʽÖÐÒ»ÑùÆ¥ÅäÈÎÒâµÄµ¥¸ö×Ö·û.
$ ±äÁ¿Ìæ»»
1 var1=5
2 var2=23skidoo
3
4 echo $var1 # 5
5 echo $var2 # 23skidoo
$ ÔÚÕýÔò±í´ïʽÖÐ×÷ΪÐнáÊø·û.
${} ²ÎÊýÌæ»»,¼û9.3½Ú.
$*,$@ λÖòÎÊý
$? Í˳ö״̬±äÁ¿.$?±£´æÒ»¸öÃüÁî/Ò»¸öº¯Êý»òÕ߽ű¾±¾ÉíµÄÍ˳ö״̬.
$$ ½ø³ÌID±äÁ¿.Õâ¸ö$$±äÁ¿±£´æÔËÐнű¾½ø³ÌID
() ÃüÁî×é.Èç:
1 (a=hello;echo $a)
×¢Òâ:ÔÚ()ÖеÄÃüÁîÁбí,½«×÷Ϊһ¸ö×ÓshellÀ´ÔËÐÐ.
ÔÚ()ÖеıäÁ¿,ÓÉÓÚÊÇÔÚ×ÓshellÖÐ,ËùÒÔ¶ÔÓڽű¾Ê£ÏµIJ¿·ÖÊDz»¿ÉÓõÄ.
Èç:
1 a=123
2 ( a=321; )
3
4 echo "a = $a" # a = 123
5 # ÔÚÔ²À¨ºÅÖÐa±äÁ¿,¸üÏñÊÇÒ»¸ö¾Ö²¿±äÁ¿.
ÓÃÔÚÊý×é³õʼ»¯,Èç:
1 Array=(element1,element2,element3)
{xxx,yyy,zzz...}
´óÀ¨ºÅÀ©Õ¹,Èç:
1 cat {file1,file2,file3} > combined_file
2 # °Ñfile1,file2,file3Á¬½ÓÔÚÒ»Æð,²¢ÇÒÖض¨Ïòµ½combined_fileÖÐ.
3
4
5 cp file22.{txt,backup}
6 # ¿½±´"file22.txt" µ½"file22.backup"ÖÐ
Ò»¸öÃüÁî¿ÉÄÜ»á¶Ô´óÀ¨ºÅÖеÄÒÔ¶ººÅ·Ö¸îµÄÎļþÁбíÆð×÷ÓÃ[1]. file globbing½«¶Ô
´óÀ¨ºÅÖеÄÎļþÃû×÷À©Õ¹.
×¢Òâ: ÔÚ´óÀ¨ºÅÖÐ,²»ÔÊÐíÓпհ×,³ý·ÇÕâ¸ö¿Õ°×ÊÇÓÐÒâÒåµÄ.
echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C
{} ´úÂë¿é.ÓÖ±»³ÆΪÄÚ²¿×é.ÊÂʵÉÏ,Õâ¸ö½á¹¹´´½¨ÁËÒ»¸öÄäÃûµÄº¯Êý.µ«ÊÇÓ뺯Êý²»Í¬µÄ
ÊÇ,ÔÚÆäÖÐÉùÃ÷µÄ±äÁ¿,¶ÔÓڽű¾ÆäËû²¿·ÖµÄ´úÂëÀ´Ëµ»¹ÊǿɼûµÄ.Èç:
bash$
{
local a;
a= 123;
}
bashÖеÄlocalÉêÇëµÄ±äÁ¿Ö»Äܹ»ÓÃÔÚº¯ÊýÖÐ.
1 a=123
2 { a=321; }
3 echo "a = $a" # a = 321 (˵Ã÷ÔÚ´úÂë¿éÖжԱäÁ¿aËù×÷µÄÐÞ¸Ä,Ó°ÏìÁËÍâ±ßµÄ±ä
4
5 # Thanks, S.C.
ϱߵĴúÂëչʾÁËÔÚ{}½á¹¹ÖдúÂëµÄI/OÖض¨Ïò.
Example 3-1. ´úÂë¿éºÍI/OÖض¨Ïò
################################Start Script####################################
###
1 #!/bin/bash
2 # ´Ó /etc/fstabÖжÁÐÐ
3
4 File=/etc/fstab
5
6 {
7 read line1
8 read line2
9 } < $File
10
11 echo "First line in $File is:"
12 echo "$line1"
13 echo
14 echo "Second line in $File is:"
15 echo "$line2"
16
17 exit 0
18
19 # ÏÖÔÚ,ÄãÔõô·ÖÎöÿÐеķָîÓò
20 # °µÊ¾: ʹÓà awk.
################################End Script######################################
###
Example 3-2. ½«Ò»¸ö´úÂë¿éµÄ½á¹û±£´æµ½Îļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # rpm-check.sh
3
4 # Õâ¸ö½Å±¾µÄÄ¿µÄÊÇΪÁËÃèÊö,Áбí,ºÍÈ·¶¨ÊÇ·ñ¿ÉÒÔ°²×°Ò»¸örpm°ü.
5 # ÔÚÒ»¸öÎļþÖб£´æÊä³ö.
6 #
7 # Õâ¸ö½Å±¾Ê¹ÓÃÒ»¸ö´úÂë¿éÀ´Õ¹Ê¾
8
9 SUCCESS=0
10 E_NOARGS=65
11
12 if [ -z "$1" ]
13 then
14 echo "Usage: `basename $0` rpm-file"
15 exit $E_NOARGS
16 fi
17
18 {
19 echo
20 echo "Archive Description:"
21 rpm -qpi $1 # ²éѯ˵Ã÷
22 echo
23 echo "Archive Listing:"
24 rpm -qpl $1 # ²éѯÁбí
25 echo
26 rpm -i --test $1 # ²éѯrpm°üÊÇ·ñ¿ÉÒÔ±»°²×°
27 if [ "$?" -eq $SUCCESS ]
28 then
29 echo "$1 can be installed."
30 else
31 echo "$1 cannot be installed."
32 fi
33 echo
34 } > "$1.test" # °Ñ´úÂë¿éÖеÄËùÓÐÊä³ö¶¼Öض¨Ïòµ½ÎļþÖÐ
35
36 echo "Results of rpm test in file $1.test"
37
38 # ²é¿´rpmµÄmanÒ³À´²é¿´rpmµÄÑ¡Ïî
39
40 exit 0
################################End Script######################################
###
×¢Òâ: Óë()ÖеÄÃüÁͬµÄÊÇ,{}ÖеĴúÂë¿é½«²»ÄÜÕý³£µØ¿ªÆôÒ»¸öÐÂshell.[2]
{} \; ·¾¶Ãû.Ò»°ã¶¼ÔÚfindÃüÁîÖÐʹÓÃ.Õâ²»ÊÇÒ»¸öshellÄÚ½¨ÃüÁî.
×¢Òâ: ";"ÓÃÀ´½áÊøfindÃüÁîÐòÁеÄ-execÑ¡Ïî.
[] test.
testµÄ±í´ïʽ½«ÔÚ[]ÖÐ.
ÖµµÃ×¢ÒâµÄÊÇ[ÊÇshellÄÚ½¨testÃüÁîµÄÒ»²¿·Ö,²¢²»ÊÇ/usr/bin/testÖеÄÀ©Õ¹ÃüÁî
µÄÒ»¸öÁ¬½Ó.
[[]] test.
test±í´ïʽ·ÅÔÚ[[]]ÖÐ.(shell¹Ø¼ü×Ö)
¾ßÌå²é¿´[[]]½á¹¹µÄÌÖÂÛ.
[] Êý×éÔªËØ
Array[1]=slot_1
echo ${Array[1]}
[] ×Ö·û·¶Î§
ÔÚÕýÔò±í´ïʽÖÐʹÓÃ,×÷Ϊ×Ö·ûÆ¥ÅäµÄÒ»¸ö·¶Î§
(()) Êýѧ¼ÆËãµÄÀ©Õ¹
ÔÚ(())½á¹¹ÖпÉÒÔʹÓÃһЩÊý×Ö¼ÆËã.
¾ßÌå²ÎÔÄ((...))½á¹¹.
>&>>&>><
Öض¨Ïò.
scriptname >filename Öض¨Ïò½Å±¾µÄÊä³öµ½ÎļþÖÐ.¸²¸ÇÎļþÔ­ÓÐÄÚÈÝ.
command &>filename Öض¨ÏòstdoutºÍstderrµ½ÎļþÖÐ
command >&2 Öض¨ÏòcommandµÄstdoutµ½stderr
scriptname >>filename Öض¨Ïò½Å±¾µÄÊä³öµ½ÎļþÖÐ.Ìí¼Óµ½Îļþβ¶Ë,Èç¹ûûÓÐÎļþ,
Ôò´´½¨Õâ¸öÎļþ.
½ø³ÌÌæ»»,¾ßÌå¼û"½ø³ÌÌæ»»²¿·Ö",¸úÃüÁîÌæ»»¼«ÆäÀàËÆ.
(command)>
<(command)
<ºÍ> ¿ÉÓÃÀ´×ö×Ö·û´®±È½Ï
<ºÍ> ¿ÉÓÃÔÚÊýѧ¼ÆËã±È½Ï
<< Öض¨Ïò,ÓÃÔÚ"here document"
<<< Öض¨Ïò,ÓÃÔÚ"here string"
<,> ASCII±È½Ï
1 veg1=carrots
2 veg2=tomatoes
3
4 if [[ "$veg1" < "$veg2" ]]
5 then
6 echo "Although $veg1 precede $veg2 in the dictionary,"
7 echo "this implies nothing about my culinary preferences."
8 else
9 echo "What kind of dictionary are you using, anyhow?"
10 fi
\<,\> ÕýÔò±í´ïʽÖеĵ¥´Ê±ß½ç.Èç:
bash$grep '\<the\>' textfile
| ¹ÜµÀ.·ÖÎöÇ°±ßÃüÁîµÄÊä³ö,²¢½«Êä³ö×÷Ϊºó±ßÃüÁîµÄÊäÈë.ÕâÊÇÒ»ÖÖ²úÉúÃüÁîÁ´µÄ
ºÃ·½·¨.
1 echo ls -l | sh
2 # ´«µÝ"echo ls -l"µÄÊä³öµ½shellÖÐ,
3 #+ ÓëÒ»¸ö¼òµ¥µÄ"ls -l"½á¹ûÏàͬ.
4
5
6 cat *.lst | sort | uniq
7 # ºÏ²¢ºÍÅÅÐòËùÓеÄ".lst"Îļþ,È»ºóɾ³ýËùÓÐÖظ´µÄÐÐ.
¹ÜµÀÊǽø³Ì¼äͨѶµÄÒ»¸öµäÐÍ°ì·¨,½«Ò»¸ö½ø³ÌµÄstdout·Åµ½ÁíÒ»¸ö½ø³ÌµÄstdinÖÐ.
±ê×¼µÄ·½·¨Êǽ«Ò»¸öÒ»°ãÃüÁîµÄÊä³ö,±ÈÈçcat»òecho,´«µÝµ½Ò»¸ö¹ýÂËÃüÁîÖÐ(ÔÚÕâ¸ö
¹ýÂËÃüÁîÖн«´¦ÀíÊäÈë),µÃµ½½á¹û,Èç:
cat $filename1 | $filename2 | grep $search_word
µ±È»Êä³öµÄÃüÁîÒ²¿ÉÒÔ´«µÝµ½½Å±¾ÖÐ.Èç:
################################Start Script####################################
###
1 #!/bin/bash
2 # uppercase.sh : ÐÞ¸ÄÊä³ö,È«²¿×ª»»Îª´óд
3
4 tr 'a-z' 'A-Z'
5 # ×Ö·û·¶Î§±ØÐë±»""ÒýÓÃÆðÀ´
6 #+ À´×èÖ¹²úÉúµ¥×Ö·ûµÄÎļþÃû.
7
8 exit 0
################################End Script######################################
###
ÏÖÔÚÈÃÎÒÃÇÊäËÍls -lµÄÊä³öµ½Ò»¸ö½Å±¾ÖÐ.
bash$ ls -l | ./uppercase.sh
-RW-RW-R-- 1 BOZO BOZO 109 APR 7 19:49 1.TXT
-RW-RW-R-- 1 BOZO BOZO 109 APR 14 16:48 2.TXT
-RW-R--R-- 1 BOZO BOZO 725 APR 20 20:56 DATA-FILE
×¢Òâ:¹ÜµÀÖеÄÒ»¸ö½ø³ÌµÄstdout±ØÐë±»ÏÂÒ»¸ö½ø³Ì×÷Ϊstdin¶ÁÈë.·ñÔò,Êý¾ÝÁ÷»á×è
Èû,²¢ÇҹܵÀ½«²úÉú·ÇÔ¤ÆÚµÄÐÐΪ.
Èç:
1 cat file1 file2 | ls -l | sort
2 #´Ó"cat file1 file2"ÖеÄÊä³ö²¢Ã»³öÏÖ
×÷Ϊ×Ó½ø³ÌµÄÔËÐеĹܵÀ,²»Äܹ»¸Ä±ä½Å±¾µÄ±äÁ¿.
1 variable="initial_value"
2 echo "new_value" | read variable
3 echo "variable = $variable" #variable = init
ial_value
Èç¹û¹ÜµÀÖеÄij¸öÃüÁî²úÉúÁËÒ»¸öÒì³£,²¢ÖÐ;ʧ°Ü,ÄÇôÕâ¸ö¹ÜµÀ½«¹ýÔçµÄÖÕÖ¹.
ÕâÖÖÐÐΪ±»½Ð×öa broken pipe,²¢ÇÒÕâÖÖ״̬Ͻ«·¢ËÍÒ»¸öSIGPIPEÐźÅ.
>| Ç¿ÖÆÖض¨Ïò(¼´Ê¹ÉèÖÃÁËnoclobberÑ¡Ïî--¾ÍÊÇ-CÑ¡Ïî).Õ⽫ǿÖƵĸ²¸ÇÒ»¸öÏÖ´æÎļþ.
|| »ò-Âß¼­²Ù×÷.
& ºǫ́ÔËÐÐÃüÁî.Ò»¸öÃüÁîºó±ß¸úÒ»¸ö&,½«±íʾÔÚºǫ́ÔËÐÐ.
bash$sleep 10 &
[1] 850
[1]+ Done sleep 10
ÔÚÒ»¸ö½Å±¾ÖÐ,ÃüÁîºÍÑ­»·¶¼¿ÉÄÜÔËÐÐÔÚºǫ́.
Example 3-3. ÔÚºǫ́ÔËÐÐÒ»¸öÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2 #background-loop.sh
3
4 for i in 1 2 3 4 5 6 7 8 9 10 #µÚÒ»¸öÑ­»·
5 do
6 echo -n "$i"
7 done& #ÔÚºǫ́ÔËÐÐÕâ¸öÑ­»·
8 #ÔÚµÚ2¸öÑ­»·Ö®ºó,½«ÔÚijЩʱºò
9
10 echo #Õâ¸ö'echo'ijЩʱºò½«²»»áÏÔʾ.
11
12 for i in 11 12 13 14 15 16 17 18 19 20 #µÚ¶þ¸öÑ­»·
13 do
14 echo -n "$i"
15 done
16
17 echo #Õâ¸ö'echo'ijЩʱºò½«²»»áÏÔʾ.
18
19 #--------------------------------------------------------
20
21 #ÆÚÍûµÄÊä³öÓ¦¸ÃÊÇ
22 #1 2 3 4 5 6 7 8 9 10
23 #11 12 13 14 15 16 17 18 19 20
24
25 #È»¶øʵ¼ÊµÄ½á¹ûÓпÉÄÜÊÇ
26 #11 12 13 14 15 16 17 18 19 20
27 #1 2 3 4 5 6 7 8 9 10 bozo $
28 #(µÚ2¸ö'echo'ûִÐÐ,Ϊʲô?)
29
30 #Ò²¿ÉÄÜÊÇ
31 #1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
32 #(µÚ1¸ö'echo'ûִÐÐ,Ϊʲô?)
33
34 #·Ç³£ÉÙ¼ûµÄÖ´Ðнá¹û,Ò²ÓпÉÄÜÊÇ:
35 #11 12 13 1 2 3 4 5 6 7 8 9 10 14 15 16 17 18 19 20
36 #ǰ̨µÄÑ­»·ÏÈÓÚºǫ́µÄÖ´ÐÐ
37
38 exit 0
39
40 # Nasimuddin Ansari ½¨Òé¼ÓÒ»¾ä sleep 1
41 #+ ÔÚ 6ÐкÍ14ÐÐµÄ echo -n "$i"Ö®ºó¼Ó
42 #+ ½«¿´µ½Ò»Ð©ÀÖȤ
################################End Script######################################
###
×¢Òâ:ÔÚÒ»¸ö½Å±¾ÄÚºǫ́ÔËÐÐÒ»¸öÃüÁî,ÓпÉÄÜÔì³ÉÕâ¸ö½Å±¾µÄ¹ÒÆð,µÈ´ýÒ»¸ö°´¼ü
ÏìÓ¦.ÐÒÔ˵ÄÊÇ,ÎÒÃÇ¿ÉÒÔÔÚExample 11-24¸½½ü,¿´µ½Õâ¸öÎÊÌâµÄ½â¾ö°ì·¨.
&& Óë-Âß¼­²Ù×÷.
- Ñ¡Ïî,ǰ׺.ÔÚËùÓеÄÃüÁîÄÚÈç¹ûÏëʹÓÃÑ¡Ïî²ÎÊýµÄ»°,Ç°±ß¶¼Òª¼ÓÉÏ"-".
COMMAND -[Option1][Option2][...]
ls -al
sort -dfu $filename
set -- $variable
1 if [ $file1 -ot $file2 ]
2 then
3 echo "File $file1 is older than $file2."
4 fi
5
6 if [ "$a" -eq "$b" ]
7 then
8 echo "$a is equal to $b."
9 fi
10
11 if [ "$c" -eq 24 -a "$d" -eq 47 ]
12 then
13 echo "$c equals 24 and $d equals 47."
14 fi
- ÓÃÓÚÖض¨Ïò stdin »ò stdout.
################################Start Script####################################
###
1 (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
2 # ´ÓÒ»¸öĿ¼Òƶ¯Õû¸öĿ¼Ê÷µ½ÁíÒ»¸öĿ¼
3 # [courtesy Alan Cox <a.cox@swansea.ac.uk>, with a minor change]
4
5 # 1) cd /source/directory ԴĿ¼
6 # 2) && Óë²Ù×÷,Èç¹ûcdÃüÁî³É¹¦ÁË,ÄÇô¾ÍÖ´ÐÐϱߵÄÃüÁî
7 # 3) tar cf - . 'c'´´½¨Ò»¸öÐÂÎĵµ,'f'ºó±ß¸ú'-'Ö¸¶¨Ä¿±êÎļþ×÷Ϊstdout
8 # '-'ºó±ßµÄ'f'(file)Ñ¡Ïî,Ö¸Ã÷×÷ΪstdoutµÄÄ¿±êÎļþ.
9 # ²¢ÇÒÔÚµ±Ç°Ä¿Â¼('.')Ö´ÐÐ.
10 # 4) | ¹ÜµÀ...
11 # 5) ( ... ) Ò»¸ö×Óshell
12 # 6) cd /dest/directory ¸Ä±äµ±Ç°Ä¿Â¼µ½Ä¿±êĿ¼.
13 # 7) && Óë²Ù×÷,ͬÉÏ.
14 # 8) tar xpvf - 'x'½âµµ,'p'±£Ö¤ËùÓÐȨºÍÎļþÊôÐÔ,
15 # 'v'·¢ÍêÕûÏûÏ¢µ½stdout
16 # 'f'ºó±ß¸ú'-',´Óstdin¶ÁÈ¡Êý¾Ý
17 #
18 # ×¢Òâ:'x' ÊÇÒ»¸öÃüÁî, 'p', 'v', 'f' ÊÇÑ¡Ïî.
19 # Whew!
20
21
22
23 # ¸üÓÅÑŵÄд·¨Ó¦¸ÃÊÇ
24 # cd source/directory
25 # tar cf - . | (cd ../dest/directory; tar xpvf -)
26 #
27 # µ±È»Ò²¿ÉÒÔÕâôд:
28 # cp -a /source/directory/* /dest/directory
29 # »òÕß:
30 # cp -a /source/directory/* /source/directory/.[^.]* /dest/directory
31 # Èç¹ûÔÚ/source/directoryÖÐÓÐÒþ²ØÎļþµÄ»°.
################################End Script######################################
###
################################Start Script####################################
###
1 bunzip2 linux-2.6.13.tar.bz2 | tar xvf -
2 # --δ½âѹµÄtarÎļþ-- | --È»ºó°ÑËü´«µÝµ½"tar"ÖÐ--
3 # Èç¹û "tar" ûÄܹ»Õý³£µÄ´¦Àí"bunzip2",
4 # Õâ¾ÍÐèҪʹÓùܵÀÀ´Ö´ÐÐ2¸öµ¥¶ÀµÄ²½ÖèÀ´Íê³ÉËü.
5 # Õâ¸öÁ·Ï°µÄÄ¿µÄÊǽ⵵"bzipped"µÄkernelÔ´Îļþ.
################################End Script######################################
###
×¢Òâ:ÔÚÉϱßÕâ¸öÀý×ÓÖÐ'-'²»Ì«ÏóÊÇbashµÄ²Ù×÷·û,¶ø¸üÏñÊÇtarµÄ²ÎÊý.
bash$echo "whatever" | cat -
whatever
ÔÚÐèÒªÒ»¸öÎļþÃûµÄµØ·½,-Öض¨ÏòÊä³öµ½stdout(ÈçÔÚtarºÍcfÃüÁîÖÐ),»òÕß´Ó
stdinÖнÓÊÜÊäÈë,¶ø²»ÊÇ´ÓÒ»¸öÎļþÖнÓÊÜÊäÈë.ÕâÊÇÔڹܵÀÖÐ×÷Ϊһ¸ö¹ýÂË
Æ÷,À´Ê¹ÓÃÎļþ¶¨Î»¹¤¾ßµÄÒ»ÖÖ°ì·¨.
bash$file
Ó÷¨: file [-bciknvzl] [-f namefile] [-m magicfiles] file...
ÉϱßÕâ¸öÀý×Ófile½«»á³ö´í,ÌáʾÄãÈçºÎʹÓÃfileÃüÁî.
Ìí¼ÓÒ»¸ö"-"½«µÃµ½Ò»¸ö¸üÓÐÓõĽá¹û.Õ⽫ʹµÃshellµÈ´ýÓû§ÊäÈë.
bash$file -
abc
standard input: ASCII text
bash$file -
#!/bin/bash
standard input: Bourn-Again shel
l script tesxt executable
ÏÖÔÚÃüÁî´ÓstdinÖнÓÊÜÁËÊäÈë,²¢·ÖÎöËü.
"-"³£ÓÃÓڹܵÀºó±ßµÄÃüÁî,¾ßÌå²Î¿´33.7½Ú,À´¿´Ê¹Óü¼ÇÉ.
ʹÓÃdiffÃüÁîÀ´ºÍÁíÒ»¸öÎļþµÄÒ»²¿·Ö½øÐбȽÏ.
grep Linux file1 | diff file2 -
×îºó,Ò»¸öÕæʵÊÀ½çµÄʹÓÃtarÃüÁîµÄÀý×Ó.
Example 3-4. ±¸·Ý×îºóÒ»ÌìËùÓÐÐ޸ĵÄÎļþ.
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ÔÚÒ»¸ö"tarball"ÖÐ(¾­¹ýtarºÍgzip´¦Àí¹ýµÄÎļþ)
4 #+ ±¸·Ý×îºó24Сʱµ±Ç°Ä¿Â¼ÏÂdËùÓÐÐ޸ĵÄÎļþ.
5
6 BACKUPFILE=backup-$(date +%m-%d-%Y)
7 # ÔÚ±¸·ÝÎļþÖÐǶÈëʱ¼ä.
8 # Thanks, Joshua Tschida, for the idea.
9 archive=${1:-$BACKUPFILE}
10 # Èç¹ûÔÚÃüÁîÐÐÖÐûÓÐÖ¸¶¨±¸·ÝÎļþµÄÎļþÃû,
11 #+ ÄÇô½«Ä¬ÈÏʹÓÃ"backup-MM-DD-YYYY.tar.gz".
12
13 tar cvf - `find . -mtime -1 -type f -print` > $archive.tar
14 gzip $archive.tar
15 echo "Directory $PWD backed up in archive file \"$archive.tar.gz\"."
16
17
18 # Stephane ChazelasÖ¸³öÉϱߴúÂë,
19 #+ Èç¹ûÔÚ·¢ÏÖÌ«¶àµÄÎļþµÄʱºò,»òÕßÊÇÈç¹ûÎļþ
20 #+ Ãû°üÀ¨¿Õ¸ñµÄʱºò,½«Ö´ÐÐʧ°Ü.
21
22 # Stephane Chazelas½¨ÒéʹÓÃϱߵÄÁ½ÖÖ´úÂëÖ®Ò»
23 # -------------------------------------------------------------------
24 # find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar"
25 # ʹÓÃgnu°æ±¾µÄfind.
26
27
28 # find . -mtime -1 -type f -exec tar rvf "$archive.tar" '{}' \;
29 # ¶ÔÓÚÆäËû·ç¸ñµÄUNIX±ãÓÚÒÆÖ²,µ«ÊDZȽÏÂý.
30 # -------------------------------------------------------------------
31
32
33 exit 0
################################End Script######################################
###
×¢Òâ:ÒÔ"-"¿ªÍ·µÄÎļþÃûÔÚʹÓÃ"-"×÷ΪÖض¨Ïò²Ù×÷·ûµÄʱºò,¿ÉÄÜ»á²úÉúÎÊÌâ.
Ó¦¸Ãдһ¸ö½Å±¾À´¼ì²éÕâ¸öÎÊÌâ,²¢¸øÕâ¸öÎļþ¼ÓÉϺÏÊʵÄǰ׺.Èç:
./-FILENAME, $PWD/-FILENAME,»ò$PATHNAME/-FILENAME.
Èç¹û±äÁ¿µÄÖµÒÔ"-"¿ªÍ·,¿ÉÄÜÒ²»áÒýÆðÎÊÌâ.
1 var="-n"
2 echo $var
3 #¾ßÓÐ"echo -n"µÄЧ¹ûÁË,ÕâÑùʲô¶¼²»»áÊä³öµÄ.
- ֮ǰ¹¤×÷µÄĿ¼."cd -"½«»Øµ½Ö®Ç°µÄ¹¤×÷Ŀ¼,¾ßÌåÇë²Î¿¼"$OLDPWD"»·¾³±äÁ¿.
×¢Òâ:Ò»¶¨ÒªºÍ֮ǰÌÖÂÛµÄÖض¨Ïò¹¦ÄÜ·Ö¿ª,µ«ÊÇÖ»ÄÜÒÀÀµÉÏÏÂÎÄÇø·Ö.
- ËãÊõ¼õºÅ.
= ËãÊõµÈºÅ,ÓÐʱҲÓÃÀ´±È½Ï×Ö·û´®.
1 a=28
2 echo $a # 28
+ ËãÊõ¼ÓºÅ,Ò²ÓÃÔÚÕýÔò±í´ïʽÖÐ.
+ Ñ¡Ïî,¶ÔÓÚÌض¨µÄÃüÁîÀ´ËµÊ¹ÓÃ"+"À´´ò¿ªÌض¨µÄÑ¡Ïî,ÓÃ"-"À´¹Ø±ÕÌض¨µÄÑ¡Ïî.
% ËãÊõÈ¡Ä£ÔËËã.Ò²ÓÃÔÚÕýÔò±í´ïʽÖÐ.
~ homeĿ¼.Ï൱ÓÚ$HOME±äÁ¿.~bozoÊÇbozoµÄhomeĿ¼,²¢ÇÒls ~bozo½«ÁгöÆäÖеÄ
ÄÚÈÝ. ~/¾ÍÊǵ±Ç°Óû§µÄhomeĿ¼,²¢ÇÒls ~/½«ÁгöÆäÖеÄÄÚÈÝ,Èç:
bash$ echo ~bozo
/home/bozo
bash$ echo ~
/home/bozo
bash$ echo ~/
/home/bozo/
bash$ echo ~:
/home/bozo:
bash$ echo ~nonexistent-user
~nonexistent-user
~+ µ±Ç°¹¤×÷Ŀ¼,Ï൱ÓÚ$PWD±äÁ¿.
~- ֮ǰµÄ¹¤×÷Ŀ¼,Ï൱ÓÚ$OLDPWDÄÚ²¿±äÁ¿.
=~ ÓÃÓÚÕýÔò±í´ïʽ,Õâ¸ö²Ù×÷½«ÔÚÕýÔò±í´ïʽƥÅ䲿·Ö½²½â,Ö»ÓÐversion3²ÅÖ§³Ö.
^ ÐÐÊ×,ÕýÔò±í´ïʽÖбíʾÐÐÊ×."^"¶¨Î»µ½ÐÐÊ×.

¿ØÖÆ×Ö·û
ÐÞ¸ÄÖն˻òÎı¾ÏÔʾµÄÐÐΪ.¿ØÖÆ×Ö·ûÒÔCONTROL + key×éºÏ.
¿ØÖÆ×Ö·ûÔڽű¾Öв»ÄÜÕý³£Ê¹ÓÃ.
Ctl-B ¹â±êºóÍË,ÕâÓ¦¸ÃÒÀÀµÓÚbashÊäÈëµÄ·ç¸ñ,ĬÈÏÊÇemacs·ç¸ñµÄ.
Ctl-C Break,ÖÕֹǰ̨¹¤×÷.
Ctl-D ´Óµ±Ç°shellµÇ³ö(ºÍexitºÜÏñ)
"EOF"(Îļþ½áÊø·û).ÕâÒ²ÄÜ´ÓstdinÖÐÖÕÖ¹ÊäÈë.
ÔÚconsole»òÕßÔÚxterm windowÖÐÊäÈëµÄʱºò,Ctl-D½«É¾³ý¹â
µ±Ã»ÓÐ×Ö·ûʱ,Ctrl-D½«Í˳öµ±Ç°»á»°.ÔÚxterm windowÒ²Óй
µÄЧ¹û.
Ctl-G beep.ÔÚһЩÀϵÄÖÕ¶Ë,½«ÏìÁå.
Ctl-H backspace,ɾ³ý¹â±êÇ°±ßµÄ×Ö·û.Èç:
1 #!/bin/bash
2 # ÔÚÒ»¸ö±äÁ¿ÖвåÈëCtl-H
3
4 a="^H^H" # Á½¸ö Ctl-H (backs
paces).
5 echo "abcdef" # abcdef
6 echo -n "abcdef$a " # abcd f
7 # ×¢Òâ½áβµÄ¿Õ¸ñ ^ ^ Á½¸ö twice.
8 echo -n "abcdef$a" # abcdef
9 # ½áβûÓпոñ ûÓÐ backspace µÄЧ¹ûÁ
10 # ½á¹û²¢²»ÏñÆÚÍûµÄÄÇÑù
11 echo; echo
Ctl-I ¾ÍÊÇtab¼ü.
Ctl-J ÐÂÐÐ.
Ctl-K ´¹Ö±tab.(´¹Ö±tab?ÐÂÓ±,ûÌý¹ý)
×÷ÓþÍÊÇɾ³ý¹â±êµ½ÐÐβµÄ×Ö·û.
Ctl-L clear,ÇåÆÁ.
Ctl-M »Ø³µ
################################Start Script####################################
###
1 #!/bin/bash
2 # Thank you, Lee Maschmeyer, for this example.
3
4 read -n 1 -s -p $'Control-M leaves cursor at beginning of this line. Press En
ter. \x0d'
5 #µ±È»,'0d'¾ÍÊǶþ½øÖƵĻسµ.
6 echo >&2 # '-s'²ÎÊýʹµÃÈκÎÊäÈ붼²»½«»ØÏÔ³öÀ´
7 #+ ËùÒÔ,Ã÷È·µÄÖØÆðÒ»ÐÐÊDZØÒªµÄ.
8
9 read -n 1 -s -p $'Control-J leaves cursor on next line. \x0a'
10 echo >&2 # Control-J ÊÇ»»ÐÐ.
11
12 ###
13
14 read -n 1 -s -p $'And Control-K\x0bgoes straight down.'
15 echo >&2 # Control-K ÊÇ´¹Ö±ÖƱí·û.
16
17 # ¹ØÓÚ´¹Ö±ÖƱí·ûЧ¹ûµÄÒ»¸ö¸üºÃµÄÀý×Ó¼ûϱß:
18
19 var=$'\x0aThis is the bottom line\x0bThis is the top line\x0a'
20 echo "$var"
21 # Õâ¾äÓëÉϱߵÄÀý×ÓʹÓõÄÊÇͬÑùµÄ°ì·¨,È»¶ø:
22 echo "$var" | col
23 # Õ⽫Ôì³É´¹Ö±ÖƱí·ûÓұߵIJ¿·ÖÔÚ×ó±ß²¿·ÖµÄÉϱß.
24 # ÕâÒ²½âÊÍÁËΪʲôÎÒÃÇÒªÔÚÐÐÊ׺ÍÐÐβ¼ÓÉÏÒ»¸ö»»Ðзû--
25 #+ À´±ÜÃâÒ»¸ö»ìÂÒµÄÆÁÄ»Êä³ö.
26
27 # Lee MaschmeyerµÄ½âÊÍ:
28 # ---------------------
29 # In the [first vertical tab example] . . . the vertical tab
29 # ÔÚÕâÀï[µÚÒ»¸ö´¹Ö±ÖƱí·ûµÄÀý×ÓÖÐ] . . . Õâ¸ö´¹Ö±ÖƱí·û
30 #+ makes the printing go straight down without a carriage return.
31 # This is true only on devices, such as the Linux console,
32 #+ that can't go "backward."
33 # The real purpose of VT is to go straight UP, not down.
34 # It can be used to print superscripts on a printer.
34 # Ëü¿ÉÒÔÓÃÀ´ÔÚÒ»¸ö´òÓ¡»úÉÏ´òÓ¡Éϱê.
35 # colµÄ×÷ÓÃ,¿ÉÒÔÓÃÀ´Ä£·ÂVTµÄºÏÊʵÄÐÐΪ.
36
37 exit 0
################################End Script######################################
###
Ctl-Q ¼ÌÐø(µÈ¼ÛÓÚXON×Ö·û),Õâ¸ö¼ÌÐøµÄ±ê×¼ÊäÈëÔÚÒ»¸öÖÕ¶ËÀï
Ctl-S ¹ÒÆð(µÈ¼ÛÓÚXOFF×Ö·û),Õâ¸ö±»¹ÒÆðµÄstdinÔÚÒ»¸öÖÕ¶ËÀï,ÓÃCtl-Q»Ö¸
Ctl-U ɾ³ý¹â±êµ½ÐÐÊ×µÄËùÓÐ×Ö·û,ÔÚijЩÉèÖÃÏÂ,ɾ³ýÈ«ÐÐ.
Ctl-V µ±ÊäÈë×Ö·ûʱ,Ctl-VÔÊÐí²åÈë¿ØÖÆ×Ö·û.±ÈÈç,ϱß2¸öÀý×ÓÊǵȼ۵Ä
echo -e '\x0a'
echo <Ctl-V><Ctl-J>
Ctl-VÔÚÎı¾±à¼­Æ÷ÖÐÊ®·ÖÓÐÓÃ,ÔÚvimÖÐÒ»Ñù.
Ctl-W ɾ³ýµ±Ç°¹â±êµ½Ç°±ßµÄ×î½üÒ»¸ö¿Õ¸ñÖ®¼äµÄ×Ö·û.
ÔÚijЩÉèÖÃÏÂ,ɾ³ýµ½µÚÒ»¸ö·Ç×Öĸ»òÊý×ÖµÄ×Ö·û.
Ctl-Z ÖÕֹǰ̨¹¤×÷.
¿Õ°×²¿·Ö
·Ö¸îÃüÁî»òÕßÊDZäÁ¿.°üÀ¨¿Õ¸ñ,tab,¿ÕÐÐ,»òÈκÎËüÃǵÄ×éºÏ.
ÔÚһЩÌØÊâÇé¿öÏÂ,¿Õ°×ÊDz»ÔÊÐíµÄ,Èç±äÁ¿¸³ÖµÊ±,»áÒýÆðÓï·¨´íÎó.
¿Õ°×ÐÐÔڽű¾ÖÐûÓÐЧ¹û.
"$IFS",¶ÔÓÚijЩÃüÁîÊäÈëµÄÌØÊâ±äÁ¿·Ö¸îÓò,ĬÈÏʹÓõÄÊÇ¿Õ°×.
Èç¹ûÏë±£Áô¿Õ°×,ʹÓÃÒýÓÃ.
×¢ÒâÊÂÏî:
[1] shell×ö´óÀ¨ºÅµÄÃüÁîÀ©Õ¹.µ«ÊÇÃüÁî±¾ÉíÐèÒª¶ÔÀ©Õ¹µÄ½á¹û×÷´¦Àí.
[2] ÀýÍâ:ÔÚpipeÖеÄÒ»¸ö´óÀ¨ºÅÖеĴúÂë¶Î¿ÉÄÜÔËÐÐÔÚÒ»¸ö×ÓshellÖÐ.
1 ls | { read firstline; read secondline; }
2 # ´íÎó,ÔÚ´òÀ¨ºÅÖеĴúÂë¶Î,½«ÔËÐе½×ÓshellÖÐ.
3 #+ ËùÒÔlsµÄÊä³ö½«²»ÄÜ´«µÝµ½´úÂë¿éÖÐ.
4 echo "First line is $firstline; second line is $secondline" #
²»Äܹ¤×÷
5
6 # Thanks, S.C.
[3] »»ÐзûÒ²±»ÈÏΪÊÇ¿Õ°×.ÕâÒ²½âÊÍÁËΪʲôһ¸ö¿ÕÐÐÒ²»á±»ÈÏΪÊÇ¿Õ°×.

µÚ4Õ ±äÁ¿ºÍ²ÎÊýµÄ½éÉÜ
======================
4.1 ±äÁ¿Ìæ»»
------------
$ ±äÁ¿Ìæ»»²Ù×÷·û
Ö»ÓÐÔÚ±äÁ¿±»ÉùÃ÷,¸³Öµ,unset»òexported»òÕßÊÇÔÚ±äÁ¿´ú±íÒ»¸ösignalµÄʱºò,
±äÁ¿²Å»áÊÇÒÔ±¾À´µÄÃæÄ¿³öÏÖÔڽű¾Àï.±äÁ¿ÔÚ±»¸³ÖµµÄʱºò,¿ÉÄÜÐèҪʹÓÃ"=",
read״̬»òÕßÊÇÔÚÑ­»·µÄÍ·²¿.
ÔÚ""Öл¹ÊǻᷢÉú±äÁ¿Ìæ»»,Õâ±»½Ð×ö²¿·ÖÒýÓÃ,»ò½ÐÈõÒýÓÃ.¶øÔÚ''ÖоͲ»»á·¢Éú±ä
Á¿Ìæ»»,Õâ½Ð×öÈ«ÒýÓÃ,Ò²½ÐÇ¿ÒýÓÃ.¾ßÌå¼ûµÚ5ÕµÄÌÖÂÛ.
×¢Òâ:$varÓë${var}µÄÇø±ð,²»¼Ó{},ÔÚijЩÉÏÏÂÎĽ«ÒýÆð´íÎó,ΪÁË°²È«,ʹÓÃ2.
¾ßÌå¼û9.3½Ú ²ÎÊýÌæ»».
Example 4-1. ±äÁ¿¸³ÖµºÍÌæ»»
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ±äÁ¿¸³ÖµºÍÌæ»»
4
5 a=375
6 hello=$a
7
8 #-------------------------------------------------------------------------
9 # Ç¿ÁÒ×¢Òâ,ÔÚ¸³ÖµµÄÇ°ºóÒ»¶¨²»ÒªÓпոñ.
10 # Èç¹ûÓпոñ»á·¢Éúʲô?
11
12 # Èç¹û"VARIABLE =value",
13 # ^
14 #+ ½Å±¾½«³¢ÊÔÔËÐÐÒ»¸ö"VARIABLE"µÄÃüÁî,´ø×ÅÒ»¸ö"=value"²ÎÊý.
15
16 # Èç¹û"VARIABLE= value",
17 # ^
18 #+ script tries to run "value" command with
18 #+ ½Å±¾½«³¢ÊÔÔËÐÐÒ»¸ö"value"µÄÃüÁî,´ø×Å
19 #+ the environmental variable "VARIABLE" set to "".
19 #+ Ò»¸ö±»¸³³É""ÖµµÄ»·¾³±äÁ¿"VARIABLE".
20 #-------------------------------------------------------------------------
21
22
23 echo hello # ûÓбäÁ¿ÒýÓÃ,²»¹ýÊǸöhello×Ö·û´®
24
25 echo $hello
26 echo ${hello} # ͬÉÏ
27
28 echo "$hello"
29 echo "${hello}"
30
31 echo
32
33 hello="A B C D"
34 echo $hello # A B C D
35 echo "$hello" # A B C D
36 # ¾ÍÏóÄã¿´µ½µÄecho $hello ºÍ echo "$hello" ½«¸ø³ö²»Í¬µÄ½á¹û.
37 # ^ ^
38 # Quoting a variable preserves whitespace.
38 # ÒýÓÃÒ»¸ö±äÁ¿½«±£ÁôÆäÖеĿհ×,µ±È»,Èç¹ûÊDZäÁ¿Ìæ»»¾Í²»»á±£ÁôÁË.
39
40 echo
41
42 echo '$hello' # $hello
43 # ^ ^
44 # È«ÒýÓõÄ×÷ÓÃ
45 #+ ½«µ¼ÖÂ"$"±ä³ÉÒ»¸öµ¥¶ÀµÄ×Ö·û.
46
47 # ×¢ÒâÁ½ÖÖÒýÓò»Í¬µÄЧ¹û
48
49
50 hello= # ÉèÖÃΪ¿ÕÖµ
51 echo "\$hello (null value) = $hello"
52 # ×¢ÒâÉèÖÃÒ»¸ö±äÁ¿Îª¿Õ,ÓëunsetËü,²»ÊÇÒ»»ØÊÂ,ËäÈ»¿´ÆðÀ´Ò»Ñù
53 #
54
55 # --------------------------------------------------------------
56
57 # ¿ÉÒÔÔÚͬһÐÐÉÏÉèÖöà¸ö±äÁ¿.
58 #+ ÒªÒÔ¿Õ°×·Ö¸ô
59 # СÐÄ,Õâ»á½µµÍ¿É¶ÁÐÔ,ºÍ¿ÉÒÆÖ²ÐÔ.
60
61 var1=21 var2=22 var3=$V3
62 echo
63 echo "var1=$var1 var2=$var2 var3=$var3"
64
65 # ÔÚÀÏ°æ±¾µÄ"sh"ÉÏ,¿ÉÄÜ»áÓÐÎÊÌâ.
66
67 # --------------------------------------------------------------
68
69 echo; echo
70
71 numbers="one two three"
72 # ^ ^
73 other_numbers="1 2 3"
74 # ^ ^
75 # Èç¹û±äÁ¿ÖÐÓпհ×,ÄÇôÒýÓþͱØÒªÁË.
76 #
77 echo "numbers = $numbers"
78 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
79 echo
80
81 echo "uninitialized_variable = $uninitialized_variable"
82 # Uninitialized±äÁ¿Îª¿ÕÖµ(¸ù±¾¾Íû¸³Öµ).
83 uninitialized_variable= # ÉùÃ÷,µ«ÊÇû±»³õʼ»¯
84 #+ ÆäʵºÍÇ°±ßÉèÖÃΪ¿ÕÖµµÃ×÷ÓÃÊÇÒ»ÑùµÄ.
85 echo "uninitialized_variable = $uninitialized_variable"
86 # »¹ÊÇÒ»¸ö¿ÕÖµ
87
88 uninitialized_variable=23 # ¸³Öµ
89 unset uninitialized_variable # Unset it.
90 echo "uninitialized_variable = $uninitialized_variable"
91 # »¹ÊÇ¿ÕÖµ
92 echo
93
94 exit 0
################################End Script######################################
###
×¢Òâ: Ò»¸ö¿ÕÖµ±äÁ¿,»òÕßÊǸù±¾¾ÍûÉùÃ÷µÄ±äÁ¿,ÔÚ¸³ÖµÖ®Ç°Ê¹ÓÃËü¿ÉÄÜ»áÒýÆðÎÊÌâ.
µ«ÊÇ»¹ÊÇ¿ÉÒÔÓÃÀ´×öËãÊõÔËËã
################################Start Script####################################
###
1 echo "$uninitialized" # (blank line)
2 let "uninitialized += 5" # Add 5 to it.
3 echo "$uninitialized" # 5
4
5 # ½áÂÛ:
6 # ¶ÔÓÚÒ»¸ö¿ÕÖµ±äÁ¿ÔÚ×öËãÊõ²Ù×÷µÄʱºò,¾ÍºÃÏñËüµÄֵΪ0Ò»Ñù.
8 # This is undocumented (and probably non-portable) behavior.
7 # ÕⲢû±»Îĵµ»¯(¿ÉÄÜÊDz»¿ÉÒÆÖ²)µÄÐÐΪ.
################################End Script######################################
###
¾ßÌå²Î¿¼ Example 11-21

4.2 ±äÁ¿¸³Öµ
------------
= ¸³Öµ²Ù×÷·û(Ç°ºó¶¼²»ÄÜÓпհ×)
²»ÒªÓë-eq»ìÏý,ÄǸöÊÇtest,²¢²»ÊǸ³Öµ.
×¢Òâ,=Ò²¿É±»ÓÃÀ´×ötest²Ù×÷,ÕâÒÀÀµÓÚÉÏÏÂÎÄ.
Example 4-2. Ò»°ãµÄ±äÁ¿¸³Öµ
################################Start Script####################################
###
1 #!/bin/bash
2 # "ÂãÌå"±äÁ¿
3
4 echo
5
6 # ±äÁ¿Ê²Ã´Ê±ºòÊÇ"ÂãÌå"µÄ,±ÈÈçÇ°±ßÉÙÁË$µÄʱºò.
7 # µ±Ëü±»¸³ÖµµÄʱºò,¶ø²»ÊDZ»ÒýÓõÄʱºò.
8
9 # ¸³Öµ
10 a=879
11 echo "The value of \"a\" is $a."
12
13 # ʹÓÃlet¸³Öµ
14 let a=16+5
15 echo "The value of \"a\" is now $a."
16
17 echo
18
19 # ÔÚforÑ­»·ÖÐ
20 echo -n "Values of \"a\" in the loop are: "
21 for a in 7 8 9 11
22 do
23 echo -n "$a "
24 done
25
26 echo
27 echo
28
29 # ÔÚreadÃüÁî״̬ÖÐ
30 echo -n "Enter \"a\" "
31 read a
32 echo "The value of \"a\" is now $a."
33
34 echo
35
36 exit 0
################################End Script######################################
###
Example 4-3. ±äÁ¿¸³Öµ,Ò»°ãµÄºÍ±È½ÏÌØÊâµÄ
################################Start Script####################################
###
1 #!/bin/bash
2
3 a=23 # Simple case
4 echo $a
5 b=$a
6 echo $b
7
8 # ÏÖÔÚÈÃÎÒÃÇÀ´µãС±ä»¯
9
10 a=`echo Hello!` # °ÑechoÃüÁîµÄ½á¹û´«¸ø±äÁ¿a
11 echo $a
12 # ×¢Òâ,Èç¹ûÔÚÃüÁîÀ©Õ¹½á¹¹ÖÐʹÓÃÒ»¸ö(!)µÄ»°,ÔÚÃüÁîÐÐÖн«²»Äܹ¤×÷
13 #+ ÒòΪÕâ´¥·¢ÁËBashµÄ"ÀúÊ·»úÖÆ".
14 # µ«ÊÇ,ÔÚУ±¾Àï±ßʹÓõĻ°,ÀúÊ·¹¦ÄÜÊDZ»¹Ø±ÕµÄ,ËùÒÔ¾ÍÄܹ»Õý³£ÔËÐÐ.
15
16
17 a=`ls -l` # °Ñls -lµÄ½á¹û¸øa
18 echo $a # ±ðÍüÁË,ÕâôÒýÓõĻ°,lsµÄ½á¹ûÖеÄËùÓпհײ¿·Ö¶¼Ã»ÁË(°üÀ¨»»ÐÐ)
19 echo
20 echo "$a" # ÕâôÒýÓþÍÕý³£ÁË,±£ÁôÁË¿Õ°×
21 # (¾ßÌå²ÎÔÄÕ½Ú"ÒýÓÃ")
22
23 exit 0
################################End Script######################################
###
ʹÓÃ$(...)»úÖƽøÐеıäÁ¿¸³Öµ(³ýȥʹÓÃ``À´¸³ÖµµÄÁíÍâÒ»ÖÖз½·¨).ÊÂʵÉÏÕâÁ½ÖÖ·½·¨¶¼ÊÇ
ÃüÁîÌæ»»µÄÒ»ÖÖÐÎʽ.
# À´×ÔÓÚ/ect/rc.d/rc.local
R=$(cat /ect/redhat-release)
arch=$(uname -m)

4.3 Bash±äÁ¿ÊDz»·ÖÀàÐ͵Ä
------------------------
²»ÏñÆäËû³ÌÐòÓïÑÔÒ»Ñù,Bash²¢²»¶Ô±äÁ¿Çø·Ö"ÀàÐÍ".±¾ÖÊÉÏ,Bash±äÁ¿¶¼ÊÇ×Ö·û´®.
µ«ÊÇÒÀÀµÓÚÉÏÏÂÎÄ,BashÒ²ÔÊÐí±È½Ï²Ù×÷ºÍËãÊõ²Ù×÷.¾ö¶¨ÕâЩµÄ¹Ø¼üÒòËؾÍÊÇ,±äÁ¿ÖеÄÖµ
ÊÇ·ñÖ»ÓÐÊý×Ö.
Example 4-4 ÕûÐÍ»¹ÊÇstring?
################################Start Script####################################
###
1 #!/bin/bash
2 # int-or-string.sh: ÕûÐλ¹ÊÇstring?
3
4 a=2334 # ÕûÐÍ
5 let "a += 1"
6 echo "a = $a " # a = 2335
7 echo # »¹ÊÇÕûÐÍ
8
9
10 b=${a/23/BB} # ½«23Ìæ»»³ÉBB
11 # Õ⽫°Ñb±äÁ¿´ÓÕûÐͱäΪstring
12 echo "b = $b" # b = BB35
13 declare -i b # ¼´Ê¹Ê¹ÓÃdeclareÃüÁîÒ²²»»á¶Ô´ËÓÐÈκΰïÖú,9.4½ÚÓнâÊÍ
14 echo "b = $b" # b = BB35
15
16 let "b += 1" # BB35 + 1 =
17 echo "b = $b" # b = 1
18 echo
19
20 c=BB34
21 echo "c = $c" # c = BB34
22 d=${c/BB/23} # S½«BBÌæ»»³É23
23 # ÕâʹµÃ$d±äΪһ¸öÕûÐÎ
24 echo "d = $d" # d = 2334
25 let "d += 1" # 2334 + 1 =
26 echo "d = $d" # d = 2335
27 echo
28
29 # ¹ØÓÚ¿Õ±äÁ¿ÔõôÑù?
30 e=""
31 echo "e = $e" # e =
32 let "e += 1" # ËãÊõ²Ù×÷ÔÊÐíÒ»¸ö¿Õ±äÁ¿?
33 echo "e = $e" # e = 1
34 echo # ¿Õ±äÁ¿½«×ª»»³ÉÒ»¸öÕûÐͱäÁ¿
35
36 # ¹ØÓÚδÉùÃ÷µÄ±äÁ¿ÔõôÑù?
37 echo "f = $f" # f =
38 let "f += 1" # ËãÊõ²Ù×÷ÔÊÐíô?
39 echo "f = $f" # f = 1
40 echo # δÉùÃ÷µÄ±äÁ¿½«×ª»»³ÉÒ»¸öÕûÐͱäÁ¿
41
42
43
44 # ËùÒÔ˵BashÖеıäÁ¿¶¼ÊÇÎÞÀàÐ͵Ä.
45
46 exit 0
################################End Script######################################
###

4.4 ÌØÊâµÄ±äÁ¿ÀàÐÍ
------------------
local variables
ÕâÖÖ±äÁ¿Ö»ÓÐÔÚ´úÂë¿é»òÕßÊǺ¯ÊýÖвſɼû(¾ßÌå¼û23.2ºÍ23ÕÂ)
environmental variables
ÕâÖÖ±äÁ¿½«¸Ä±äÓû§½Ó¿ÚºÍshellµÄÐÐΪ.
ÔÚÒ»°ãµÄÉÏÏÂÎÄÖÐ,ÿ¸ö½ø³Ì¶¼ÓÐ×Ô¼ºµÄ»·¾³,¾ÍÊÇÒ»×é±£³Ö½ø³Ì¿ÉÄÜÒýÓõÄÐÅÏ¢µÄ
±äÁ¿.ÕâÖÖÇé¿öÏÂ,shellÓÚÒ»¸öÒ»°ã½ø³ÌÊÇÏàͬµÄ.
ÿ´Îµ±shellÆô¶¯Ê±,Ëü¶¼½«´´½¨×Ô¼ºµÄ»·¾³±äÁ¿.¸üлòÕßÌí¼ÓеĻ·¾³±äÁ¿,½«µ¼
ÖÂshell¸üÐÂËüµÄ»·¾³,ͬʱҲ»áÓ°ÏìËùÓм̳Ð×ÔÕâ¸ö»·¾³µÄËùÓÐ×Ó½ø³Ì(ÓÉÕâ¸öÃüÁî
µ¼ÖµÄ).
×¢Òâ:·ÖÅä¸ø»·¾³±äÁ¿µÄ¿Õ¼äÊÇÊÜÏÞµÄ.´´½¨Ì«¶àµÄ»·¾³±äÁ¿½«ÒýÆð¿Õ¼äÒç³ö,Õâ»áÒý
ÆðÎÊÌâ.
¹ØÓÚevalÃüÁî,¾ßÌå¼ûµÚ11ÕÂ
bash$ eval "`seq 10000 | sed -e 's/.*/export var&=ZZZZZZZZZZZZZZ
/'`"
bash$ du
bash: /usr/bin/du: Argument list too long
Èç¹ûÒ»¸ö½Å±¾ÉèÖÃÁË»·¾³±äÁ¿,ÐèÒªexportËü,À´Í¨Öª±¾½Å±¾µÄ»·¾³,ÕâÊÇexport
ÃüÁîµÄ¹¦ÄÜ,¹ØÓÚexportÃüÁî,¾ßÌå¼û11ÕÂ.
½Å±¾Ö»ÄܶÔËü²úÉúµÄ×Ó½ø³Ìexport±äÁ¿.Ò»¸ö´ÓÃüÁîÐб»µ÷ÓõĽű¾exportµÄ±äÁ¿,½«
²»ÄÜÓ°Ïìµ÷ÓÃÕâ¸ö½Å±¾µÄÄǸöÃüÁîÐÐshellµÄ»·¾³.
positional parameters
¾ÍÊÇ´ÓÃüÁîÐÐÖд«½øÀ´µÄ²ÎÊý,$0, $1, $2, $3...
$0¾ÍÊǽű¾ÎļþµÄÃû×Ö,$1ÊǵÚÒ»¸ö²ÎÊý,$2ΪµÚ2¸ö...,²Î¼û[1](ÓÐ$0µÄ˵Ã÷),$9
ÒÔºó¾ÍÐèÒª´òÀ¨ºÅÁË,Èç${10},${11},${12}...
Á½¸öÖµµÃ×¢ÒâµÄ±äÁ¿$*ºÍ$@(µÚ9ÕÂÓоßÌåµÄÃèÊö),±íʾËùÓеÄλÖòÎÊý.
Example 4-5 λÖòÎÊý
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ×÷ΪÓÃÀý,µ÷ÓÃÕâ¸ö½Å±¾ÖÁÉÙÐèÒª10¸ö²ÎÊý,Èç
4 # ./scriptname 1 2 3 4 5 6 7 8 9 10
5 MINPARAMS=10
6
7 echo
8
9 echo "The name of this script is \"$0\"."
10 # Ìí¼Ó./ÊÇΪÁ˵±Ç°Ä¿Â¼
11 echo "The name of this script is \"`basename $0`\"."
12 # È¥µôĿ¼ÐÅÏ¢,¾ßÌå¼û'basename'ÃüÁî
13
14 echo
15
16 if [ -n "$1" ] # ²âÊÔ±äÁ¿±»±»ÒýÓÃ
17 then
18 echo "Parameter #1 is $1" # "#"û±»×ªÒå
19 fi
20
21 if [ -n "$2" ]
22 then
23 echo "Parameter #2 is $2"
24 fi
25
26 if [ -n "$3" ]
27 then
28 echo "Parameter #3 is $3"
29 fi
30
31 # ...
32
33
34 if [ -n "${10}" ] # ´óÓÚ9µÄ²ÎÊý±ØÐë³öÏÖÔÚ{}ÖÐ.
35 then
36 echo "Parameter #10 is ${10}"
37 fi
38
39 echo "-----------------------------------"
40 echo "All the command-line parameters are: "$*""
41
42 if [ $# -lt "$MINPARAMS" ] #$#ÊÇ´«µ½½Å±¾ÀïµÄλÖòÎÊýµÄ¸öÊý
43 then
44 echo
45 echo "This script needs at least $MINPARAMS command-line arguments!"
46 fi
47
48 echo
49
50 exit 0
################################End Script######################################
###
{}±ê¼Ç·¨ÊÇÒ»ÖֺܺõÄʹÓÃλÖòÎÊýµÄ·½·¨.ÕâÒ²ÐèÒª¼ä½ÓÒýÓÃ(¼ûExample 34-2)
1 args=$# # λÖòÎÊýµÄ¸öÊý
2 lastarg=${!args}
3 # »ò: lastarg=${!#}
4 # ×¢Òâ lastarg=${!$#} ½«±¨´í
һЩ½Å±¾¿ÉÄÜ»áÒÀÀµÓÚʹÓò»Í¬µÄµ÷ÓÃÃû×Ö,¶ø±íÏÖ³ö²»Í¬µÄÐÐΪ,ÕâÑùÒ»°ã¶¼ÐèÒª
ÅжÏ$0,¶øÆäËûµÄÃû×Ö¶¼ÊÇͨ¹ýlnÃüÁî²úÉúµÄÁ´½Ó.(¾ßÌå²Î¼ûExample 12-2)
Èç¹û½Å±¾ÐèÒªÒ»¸öÃüÁîÐвÎÊý,¶øµ÷ÓõÄʱºò,ûÓÃÕâ¸ö²ÎÊý,Õâ¾ÍÓпÉÄÜÔì³É·ÖÅäÒ»¸ö
¿Õ±äÁ¿,ÕâÑù¹À¼Æ¾Í»áÒýÆðÎÊÌâ.Ò»ÖÖ½â¾ö°ì·¨¾ÍÊÇÔÚÕâ¸öλÖòÎÊý,ºÍÏà¹ØµÄ±äÁ¿ºó
±ß,¶¼Ìí¼ÓÒ»¸ö¶îÍâµÄ×Ö·û.¾ßÌå¼ûϱߵÄÀý×Ó.
################################Start Script####################################
###
1 variable1_=$1_ # ¶ø²»ÊÇ variable1=$1
2 # Õ⽫×èÖ¹Ò»¸ö´íÎó,¼´Ê¹ÔÚµ÷ÓÃʱûʹÓÃÕâ¸öλÖòÎÊý.
3
4 critical_argument01=$variable1_
5
6 # Õâ¸öÀ©Õ¹µÄ×Ö·ûÊÇ¿ÉÒÔ±»Ïû³ýµôµÄ,¾ÍÏñÕâÑù.
7 variable1=${variable1_/_/}
8 # ¸±×÷ÓþÍÊÇ$variable1_¶àÁËÒ»¸öÏ»®Ïß
9 # ÕâÀïʹÓÃÁËÒ»¸ö²ÎÊýÌ滻ģ°æ(ºó±ß»áÓоßÌåµÄÌÖÂÛ)
10 # (Leaving out the replacement pattern results in a deletion.)
10 # (ÔÚÒ»¸öɾ³ý¶¯×÷ÖÐ,½ÚÊ¡ÁËÒ»¸öÌ滻ģʽ)
11
12
13 # Ò»¸ö½â¾öÕâÖÖÎÊÌâµÄ¸ü¼òµ¥µÄ×ö·¨¾ÍÊÇ,ÅжÏÒ»ÏÂÕâ¸öλÖòÎÊýÊÇ·ñ´«µÝÏÂÀ´ÁË
14 if [ -z $1 ]
15 then
16 exit $E_MISSING_POS_PARAM
17 fi
18
19
20 # µ«ÊÇÉϱߵķ½·¨½«¿ÉÄܲúÉúÒ»¸öÒâÍâµÄ¸±×÷ÓÃ
21 # ²ÎÊýÌæ»»µÄ¸üºÃµÄ°ì·¨Ó¦¸ÃÊÇ:
22 # ${1:-$DefaultVal}
23 # ¾ßÌå²ì¿´"Parameter Substition"½Ú
24 #+ ÔÚµÚ9ÕÂ
################################End Script######################################
###

Example 4-6 wh,whois½ÚµãÃû×Ö²éѯ


################################Start Script####################################
###
1 #!/bin/bash
2 # ex18.sh
3
4 # Does a 'whois domain-name' lookup on any of 3 alternate servers:
5 # ripe.net, cw.net, radb.net
6
7 # °ÑÕâ¸ö½Å±¾ÖØÃüÃûΪ'wh',È»ºó·Åµ½/usr/local/binÏÂ
8
9 # ÐèÒª3¸ö·ûºÅÁ´½Ó
10 # ln -s /usr/local/bin/wh /usr/local/bin/wh-ripe
11 # ln -s /usr/local/bin/wh /usr/local/bin/wh-cw
12 # ln -s /usr/local/bin/wh /usr/local/bin/wh-radb
13
14 E_NOARGS=65
15
16
17 if [ -z "$1" ]
18 then
19 echo "Usage: `basename $0` [domain-name]"
20 exit $E_NOARGS
21 fi
22
23 # Check script name and call proper server.
23 # ¼ì²é½Å±¾Ãû×Ö,È»ºóµ÷ÓúÏÊʵķþÎñÆ÷
24 case `basename $0` in # Or: case ${0##*/} in
25 "wh" ) whois $1@whois.ripe.net;;
26 "wh-ripe") whois $1@whois.ripe.net;;
27 "wh-radb") whois $1@whois.radb.net;;
28 "wh-cw" ) whois $1@whois.cw.net;;
29 * ) echo "Usage: `basename $0` [domain-name]";;
30 esac
31
32 exit $?
################################End Script######################################
###
shift shiftÃüÁîÖØзÖÅäλÖòÎÊý,Æäʵ¾ÍÊÇÏò×óÒƶ¯Ò»¸öλÖÃ.
$1 <--- $2, $2 <--- $3, $3 <--- $4, µÈµÈ.
ÀϵÄ$1½«Ïûʧ,µ«ÊÇ$0(½Å±¾Ãû)ÊDz»»á¸Ä±äµÄ.Èç¹ûÄãʹÓÃÁË´óÁ¿µÄλÖòÎÊý,ÄÇô
shiftÃüÁîÔÊÐíÄã´æÈ¡³¬¹ý10¸ö²ÎÊý.ËäÈ»{}±íʾ·¨Ò²ÔÊÐíÕâÑù.

Example 4-7 ʹÓÃshift


################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓÃ'shift'À´´©¹ýËùÓеÄλÖòÎÊý.
3
4 # °ÑÕâ¸ö½Å±¾ÃüÃûΪshft,
5 #+ ²¢ÇÒʹÓÃһЩ²ÎÊýÀ´µ÷ÓÃËü,Èç:
6 # ./shft a b c def 23 skidoo
7
8 until [ -z "$1" ] # ÖªµÀËùÓвÎÊý¶¼Óùâ
9 do
10 echo -n "$1 "
11 shift
12 done
13
14 echo # ¶îÍâµÄ»»ÐÐ.
15
16 exit 0
################################End Script######################################
###
ÔÚ½«²ÎÊý´«µÝµ½º¯ÊýÖÐʱ,shiftµÄ¹¤×÷·½Ê½Ò²»ù±¾²î²»¶à.¾ßÌå¼ûExample 33-15
×¢ÒâÊÂÏî:
[1] ½ø³Ìµ÷ÓÃÉèÖÃ$0²ÎÊýµÄ½Å±¾.Ò»°ãµÄ,Õâ¸ö²ÎÊý¾ÍÊǽű¾Ãû×Ö.¾ßÌå²ì¿´execvµÄmanÒ³.

µÚ5Õ ÒýÓÃ(·­ÒëµÄ¿ÉÄÜÓÐÎÊÌâ,ÌØÖ¸ÒýºÅ)
======================================
ÒýºÅµÄÌØÊâЧ¹û¾ÍÊÇ,±£»¤×Ö·û´®ÖеÄÌØÊâ×Ö·û²»±»shell»òÕßÊÇshell½Å±¾ÖØнâÊÍ»òÕßÀ©Õ¹.
(ÎÒÃÇÕâÀïËù˵µÄ"ÌØÊâ"Ö¸µÄÊÇһЩ×Ö·ûÔÚshellÖоßÓеÄÌØÊâÒâÒå,±ÈÈç*)
Èç:
bash$ ls -l [Vv]*
-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT
-rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh
-rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.sh
bash$ ls -l '[Vv]*'
ls: [Vv]*: No such file or directory
ÔÚÎÒÃÇÒ»°ãµÄÉú»îÖÐ,ÒýºÅÄÚµÄÄÚÈÝÍùÍùÓÐÌØÊâµÄº¬Òå,¶øÔÚBashÖÐ,µ±ÎÒÃÇÒýÓÃÒ»¸ö×Ö·û´®,
ÎÒÃÇÊDZ£»¤ËüµÄ×ÖÃ溬Òå.
Ìض¨µÄ³ÌÐòºÍ¹¤¾ßÄܹ»ÖØнâÊÍ»òÀ©Õ¹ÌØÊâµÄ×Ö·û.ÒýÓõÄÒ»¸öÖØÒªµÄ×÷ÓþÍÊDZ£»¤ÃüÁîÐÐÖÐ
µÄ²ÎÊý,µ«»¹ÊÇÔÊÐíÕýÔÚµ÷ÓõijÌÐòÀ´À©Õ¹Ëü.
bash$ grep '[Ff]irst' *.txt
file1.txt:This is the first line of file1.txt.
file2.txt:This is the First line of file2.txt.
×¢Òâ grep [Ff]irst *.txtÔÚBashϵÄÐÐΪ(Æäʵ¾ÍÊÇÕýÔò±í´ïʽô),[1]
ÒýÓû¹¿ÉÒÔÒÖÖÆechoÃüÁîµÄ»»ÐÐ×÷ÓÃ.
bash$ echo $(ls -l)
total 8 -rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh -rw-rw-r-- 1 bozo bozo 7
8 Aug 21 12:57 t71.sh
bash$ echo "$(ls -l)"
total 8
-rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh
-rw-rw-r-- 1 bozo bozo 78 Aug 21 12:57 t71.sh

5.1 ÒýÓñäÁ¿
------------
ÔÚÒ»¸öË«ÒýºÅÖÐÖ±½ÓʹÓñäÁ¿Ãû,Ò»°ã¶¼ÊÇûÓÐÎÊÌâµÄ.Ëü×èÖ¹ÁËËùÓÐÔÚÒýºÅÖеÄÌØÊâ×Ö·ûµÄ
ÖØнâÊÍ--°üÀ¨±äÁ¿Ãû[2]--µ«ÊÇ$,`ºÍ\³ýÍâ.[3]±£Áô$,×÷ΪÌØÊâ×Ö·ûµÄÒâÒå,ÊÇΪÁËÄܹ»ÔÚË«
ÒýºÅÖÐÒ²Äܹ»Õý³£µØÒýÓñäÁ¿("$var").ÕâÑùÔÚ""ÖпÉÒÔʹÓñäÁ¿Ëù±í´ïµÄÖµ(Example 4-1).
ʹÓÃ""À´·ÀÖ¹µ¥´Ê·Ö¸î.[4]Èç¹ûÔÚ²ÎÊýÁбíÖÐʹÓÃË«ÒýºÅ,½«Ê¹µÃË«ÒýºÅÖеIJÎÊý×÷Ϊһ¸ö²Î
Êý.¼´Ê¹Ë«ÒýºÅÖеÄ×Ö·û´®°üº¬¶à¸öµ¥´Ê(Ò²¾ÍÊÇ°üº¬¿Õ°×²¿·Ö),Ò²²»»á±äΪ¶à¸ö²ÎÊý,Èç:
1 variable1="a variable containing five words"
2 COMMAND This is $variable1 # COMMAND½«ÒÔ7¸ö²ÎÊýÀ´Ö´ÐÐ
3 # "This" "is" "a" "variable" "containing" "five" "words"
4
5 COMMAND "This is $variable1" # COMMAND½«ÒÔ1¸ö²ÎÊýÀ´Ö´ÐÐ
6 # "This is a variable containing five words"
7
8
9 variable2="" # ¿ÕÖµ
10
11 COMMAND $variable2 $variable2 $variable2 # COMMAND½«²»´ø²ÎÊýÖ´ÐÐ
12 COMMAND "$variable2" "$variable2" "$variable2" # COMMAND½«ÒÔ3¸ö¿Õ²ÎÊýÀ´Ö´ÐÐ
13 COMMAND "$variable2 $variable2 $variable2" # COMMAND½«ÒÔ1¸ö²ÎÊýÀ´Ö´ÐÐ(2¿Õ¸ñ)
ÓÃË«ÒýºÅ°Ñ²ÎÊý·âµ½echoÖÐÊǺÜÓбØÒªµÄ,Ö»ÓÐÔÚµ¥´Ê·Ö¸ô»òʱ±£Áô¿Õ°×ʱµÄʱºò¿ÉÄÜ
ÓÐЩÎÊÌâ.
Example 5-1 echoһЩ¹îÒìµÄ±äÁ¿
################################Start Script####################################
###
1 #!/bin/bash
2 # weirdvars.sh: echo¹îÒìµÄ±äÁ¿
3
4 var="'(]\\{}\$\""
5 echo $var # '(]\{}$"
6 echo "$var" # '(]\{}$" ²¢Ã»ÓÐʲô²»Í¬
7
8 echo
9
10 IFS='\'
11 echo $var # '(] {}$" \ ת»»³É¿Õ¸ñÁË?Ã÷ÏÔºÍIFSÓйØϵô!ÓÖ²»Éµ!
12 echo "$var" # '(]\{}$"
13
14 exit 0
################################End Script######################################
###
µ¥ÒýºÅ²Ù×÷×ÜÌåÉϺÍ""ºÜÏñ,µ«²»ÔÊÐíÒýÓñäÁ¿.ÒòΪ$µÄÌØÊ⺬Òå±»¹Ø±ÕÁË.ÔÚ''ÖгýÁË',ÆäËû
×Ö·û¶¼Ã»ÓÐÌØÊâµÄº¬ÒåÁË.ËùÒÔµ¥ÒýºÅ±ÈË«ÒýºÅÑϸñ.
ÒòΪ¼´Ê¹ÊÇ\,ÔÚ''Öж¼±»¹Ø±ÕÁË,ËùÒÔÄãÏëÔÚ''ÖÐÏÔʾ'µÄº¬Òå,½«µÃ²»µ½Ô¤ÆÚµÄЧ¹û.
1 echo "Why can't I write 's between single quotes"
2
3 echo
4
5 # Ò»ÖÖÈÆÍäµÄ·½·¨
6 echo 'Why can'\''t I write '"'"'s between single quotes'
7 # |-------| |----------| |-----------------------|
8 # °üº¬ÁË2¸öµ¥ÒýºÅ×Ö·û,Ô­ÊéºÃÏñÓдíÎó
×¢ÒâÊÂÏî:
[1] ³ý·Çµ±Ç°Ä¿Â¼ÏÂ,ÕýºÃÓиö½ÐfirstµÄÎļþ.
[2] ¼´Ê¹ÊDZäÁ¿µÄÖµÒ²ÊÇÓи±×÷ÓõÄ(¼ûϱß)
[3] Èç¹ûÔÚ""Öаüº¬"!"µÄ»°,ÔÚÃüÁîÐÐÖн«»á³öÏÖ´íÎó.ÒòΪÕâ¸ö"!"±»µ±×÷ÀúÊ·ÃüÁîÀ´½âÊÍÁ
ÔÚÒ»¸ö½Å±¾ÖÐ,ÕâÖÖÇé¿öÊDz»»á·¢ÉúµÄ,ÒòΪÔڽű¾ÖÐ,BashÀúÊ·¼Ç¼±»¹Ø±ÕÁË.
ϱßÊÇһЩ¹ØÓÚ"\"һЩ²»Ð­µ÷µÄÐÐΪ.
bash$ echo hello\!
hello!
bash$ echo "hello\!"
hello\!
bash$ echo -e x\ty
xty
bash$ echo -e "x\ty"
x y
[4] "µ¥´Ê·Ö¸ô",ÔÚÕâ¸öÉÏÏÂÎÄÖÐÒâζ×Å,½«Ò»¸ö×Ö·û´®·Ö¸ôΪһЩ·ÖÀëµÄ²ÎÊý.

5.2 תÒå(\)
-----------
תÒåÊÇÒ»ÖÖÒýÓõ¥¸ö×Ö·ûµÄ·½·¨.Ò»¸ö¾ßÓÐÌØÊ⺬ÒåµÄ×Ö·ûÇ°±ß·ÅÉÏÒ»¸öתÒå·û(\)¾Í¸æËßshell
Õâ¸ö×Ö·ûʧȥÁËÌØÊâµÄº¬Òå.
ÖµµÃ×¢ÒâµÄÊÇ,ÔÚijЩÌض¨µÄÃüÁîºÍ¹¤¾ßÖÐ,±ÈÈçechoºÍsed,תÒå·ûÍùÍù»áÆðµ½Ïà·´µÄЧ¹û,
Ëü·´µ¹ÓпÉÄÜÒý·¢³öÕâ¸ö×Ö·ûÌØÊâµÄº¬Òå.
¶ÔÓÚÌض¨µÄתÒå·ûµÄÌØÊâµÄº¬Òå
ÔÚechoºÍsedÖÐËùʹÓõÄ
\n Òâζ×ÅеÄÒ»ÐÐ
\r »Ø³µ
\t tab¼ü
\v vertical tab(´¹Ö±tab),²éÇ°±ßµÄCtl-K
\b backspace,²éÇ°±ßµÄCtl-H
\a "alert"(Èçbeep»òflash)
\0xx ת»»³É8½øÖÆASCII½âÂë,µÈ¼ÛÓÚoxx
Example 5-2 תÒå·û
################################Start Script####################################
###
1 #!/bin/bash
2 # escaped.sh: תÒå·û
3
4 echo; echo
5
6 echo "\v\v\v\v" # Öð×ֵĴòÓ¡\v\v\v\v .
7 # ʹÓÃ-eÑ¡ÏîµÄechoÃüÁîÀ´´òӡתÒå·û
8 echo "============="
9 echo "VERTICAL TABS"
10 echo -e "\v\v\v\v" # Prints 4 vertical tabs.
11 echo "=============="
12
13 echo "QUOTATION MARK"
14 echo -e "\042" # ´òÓ¡" (ÒýºÅ, 8½øÖƵÄASCII Âë¾ÍÊÇ42).
15 echo "=============="
16
17 # The $'\X' construct makes the -e option unnecessary.
17 # Èç¹ûʹÓÃ$'\X'½á¹¹,ÄÇ-eÑ¡Ïî¾Í²»±ØÒªÁË
18 echo; echo "NEWLINE AND BEEP"
19 echo $'\n' # ÐÂÐÐ.
20 echo $'\a' # Alert (beep).
21
22 echo "==============="
23 echo "QUOTATION MARKS"
24 # °æ±¾2ÒÔºóBashÔÊÐíʹÓÃ$'\nnn'½á¹¹
25 # ×¢ÒâÕâÖÖÇé¿ö,'\nnn\ÊÇ8½øÖÆ
26 echo $'\t \042 \t' # Quote (") framed by tabs.
27
28 # µ±È»,Ò²¿ÉÒÔʹÓÃ16½øÖƵÄÖµ,ʹÓÃ$'\xhhh' ½á¹¹
29 echo $'\t \x22 \t' # Quote (") framed by tabs.
30
31 # ÔçÒ»µãµÄBash°æ±¾ÔÊÐí'\x022'ÕâÖÖÐÎʽ
32 echo "==============="
33 echo
34
35
36 # ·ÖÅäASCII×Ö·ûµ½±äÁ¿ÖÐ
37 # ---------------------
38 quote=$'\042' # \042ÊÇ",·ÖÅäµ½±äÁ¿ÖÐ
39 echo "$quote This is a quoted string, $quote and this lies outside the quotes
."
40
41 echo
42
43 # Concatenating ASCII chars in a variable.
43 # ±äÁ¿ÖеÄÁ¬ÐøµÄASCII char.
44 triple_underline=$'\137\137\137' # 137 ÊÇ8½øÖƵÄASCII Âë'_'.
45 echo "$triple_underline UNDERLINE $triple_underline"
46
47 echo
48
49 ABC=$'\101\102\103\010' # 101, 102, 103 ÊÇ8½øÖƵÄÂëA, B, C.
50 echo $ABC
51
52 echo; echo
53
54 escape=$'\033' # 033 ÊÇ8½øÖÆÂëfor escape.
55 echo "\"escape\" echoes as $escape"
56 #"escape" echoes as ûÓбäÁ¿±»Êä³ö
57
58 echo; echo
59
60 exit 0
################################End Script######################################
###
ÁíÒ»¸ö¹ØÓÚ$''×Ö·û´®À©Õ¹½á¹ûµÄÀý×Ó¼ûExample 34-1
\" ±í´ïÒýºÅ±¾Éí
1 echo "Hello" # Hello
2 echo "\"Hello\", he said." # "Hello", he said.
\$ $ºÅ±¾Éí,¸úÔÚ\$ºóµÄ±äÁ¿Ãû,½«²»ÄÜÀ©Õ¹
1 echo "\$variable01" # ½á¹ûÊÇ$variable01
\\ \ºÅ±¾Éí.
1 echo "\\" # ½á¹ûÊÇ\
2
3 # Ïà·´µÄ . . .
4
5 echo "\" # Õâ»á³öÏÖµÚ2¸öÃüÁîÌáʾ·û,˵°×Á˾ÍÊÇÌáʾÄãÃüÁȫ,ÄãÔÙ²¹¸ö"¾Í
6 # ºÃÁË.Èç¹ûÊÇÔڽű¾Àï,¾Í»á¸ø³öÒ»¸ö´íÎó.
×¢Òâ:\µÄÐÐΪÒÀÀµÓÚËüÊÇ·ñ±»×ªÒå,±»"",»òÕßÊÇ·ñÔÚ"ÃüÁîÌæ»»"ºÍ"here document"ÖÐ.
################################Start Script####################################
###
1 # ¼òµ¥µÄתÒåºÍ""
2 echo \z # z
3 echo \\z # \z
4 echo '\z' # \z
5 echo '\\z' # \\z
6 echo "\z" # \z
7 echo "\\z" # \z
8
9 # ÃüÁîÌæ»»
10 echo `echo \z` # z
11 echo `echo \\z` # z
12 echo `echo \\\z` # \z
13 echo `echo \\\\z` # \z
14 echo `echo \\\\\\z` # \z
15 echo `echo \\\\\\\z` # \\z
16 echo `echo "\z"` # \z
17 echo `echo "\\z"` # \z
18
19 # Here document
20 cat <<EOF
21 \z
22 EOF # \z
23
24 cat <<EOF
25 \\z
26 EOF # \z
################################End Script######################################
###
·ÖÅä¸ø±äÁ¿µÄ×Ö·û´®µÄÔªËØÒ²»á±»×ªÒå,µ«ÊÇÖ»°ÑÒ»¸öתÒå·û·ÖÅä¸ø±äÁ¿½«»á±¨´í.
################################Start Script####################################
###
1 variable=\
2 echo "$variable"
3 # Will not work - gives an error message:
3 # ½«²»ÄÜÕý³£¹¤×÷- ½«¸ø³öÒ»¸ö´íÎóÏûÏ¢:
4 # test.sh: : command not found
5 # Ò»¸ö"ÂãÌåµÄ" תÒå·û½«²»Äܹ»°²È«µÄ·ÖÅä¸ø±äÁ¿.
6 #
7 # What actually happens here is that the "\" escapes the newline and
7 # ÕâÀïÆäʵÕæÕý·¢ÉúµÄÊÇvariable=\,Õâ¾ä±»shellÈÏΪÊÇûÓÐÍê³É,\±»ÈÏΪÊÇÒ»¸öÐøÐзû
8 #+ ÕâÑù,ϱߵÄÕâ¾äecho,Ò²±»ÈÏΪÊÇÉÏÒ»ÐеIJ¹³ä.ËùÒÔ,×ܵÄÀ´Ëµ¾ÍÊÇÒ»¸ö·Ç·¨±äÁ¿·ÖÅä
9
10 variable=\
11 23skidoo
12 echo "$variable" # 23skidoo
13 # Õâ¾ä¾Í¿ÉÒÔʹÓÃ,ÒòΪÕâÊÇÒ»¸öºÏ·¨µÄ±äÁ¿·ÖÅä
14
15 variable=\
16 # \^ תÒåÒ»¸ö¿Õ¸ñ
17 echo "$variable" # ÏÔʾ¿Õ¸ñ
18
19 variable=\\
20 echo "$variable" # \
21
22 variable=\\\
23 echo "$variable"
24 # ²»ÄÜÕý³£¹¤×÷,¸ø³öÒ»¸ö´íÎó
25 # test.sh: \: command not found
26 #
27 # µÚÒ»¸öתÒå·û°ÑµÚ2¸ö\תÒåÁË,µ«ÊǵÚ3¸öÓÖ±ä³É"ÂãÌåµÄ"ÁË,
28 #+ ÓëÉϱߵÄÀý×ÓµÄÔ­ÒòÏàͬ
29
30 variable=\\\\
31 echo "$variable" # \\
32 # תÁËÁ½¸ö\
33 # ûÎÊÌâ
################################End Script######################################
###
תÒåÒ»¸ö¿Õ¸ñ,ÔÚÃüÁîÐвÎÊýÁбíÖн«»á×èÖ¹µ¥´Ê·Ö¸ôÎÊÌâ.
################################Start Script####################################
###
1 file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7"
2 # ÁгöµÄÎļþ¶¼×÷ΪÃüÁîµÄ²ÎÊý.
3
4 # Add two files to the list, and list all.
4 # ¼Ó2¸öÎļþµ½listÖÐ,²¢ÇÒÁгöÈ«²¿.
5 ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list
6
7 echo "-----------------------------------------------------------------------
--"
8
9 # Èç¹ûÎÒÃÇתÒå2¸ö¿Õ¸ñ,»á·¢Éúʲô?
10 ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list
11 # ´íÎó: ÒòΪǰ3¸ö·¾¶Ãû±»ºÏ²¢³ÉÒ»¸ö²ÎÊý´«¸øÁË'ls -l'
12 # ÒòΪ2¸öתÒå·û×èÖ¹Á˲ÎÊý(µ¥´Ê)·ÖÀë
################################End Script######################################
###
תÒå·ûÒ²ÌṩÐøÐй¦ÄÜ.Ò»°ã,ÿһÐж¼°üº¬Ò»¸ö²»Í¬µÄÃüÁî,µ«Èç¹ûÔÚÐÐβ¼ÓÉÏ\,ÄǾͻá½ÓÊÜ
ÐÂÐеÄÊäÈë,×÷ΪÕâÒ»ÐеIJ¹³ä.
1 (cd /source/directory && tar cf - . ) | \
2 (cd /dest/directory && tar xpvf -)
3 # Öظ´ÁË Alan CoxµÄĿ¼Ê÷¿½±´ÃüÁî
4 # ΪÁËÔö¼Ó¿É¶ÁÐÔ·Ö³É2ÐÐ.
5
6 # Ò²¿ÉÒÔʹÓÃÈçÏ·½Ê½:
7 tar cf - -C /source/directory . |
8 tar xpvf - -C /dest/directory
9 # ²ì¿´Ï±ߵÄ×¢ÒâÊÂÏî
×¢Òâ:Èç¹ûÒ»¸ö½Å±¾ÒÔ|(¹ÜµÀ×Ö·û)½áÊø.ÄÇôһ¸ö\(תÒå·û),¾Í²»Ó÷ǼÓÉϲ»¿ÉÁË.
µ«ÊÇÒ»¸öºÃµÄshell½Å±¾±àд·ç¸ñ,»¹ÊÇÓ¦¸ÃÔÚÐÐβ¼ÓÉÏ\,ÒÔÔö¼Ó¿É¶ÁÐÔ.
################################Start Script####################################
###
1 echo "foo
2 bar"
3 #foo
4 #bar
5
6 echo
7
8 echo 'foo
9 bar' # ûÇø±ð
10 #foo
11 #bar
12
13 echo
14
15 echo foo\
16 bar # ÐøÐÐ
17 #foobar
18
19 echo
20
21 echo "foo\
22 bar" # ÓëÉϱßÒ»Ñù,\»¹ÊÇ×÷ΪÐøÐзû
23 #foobar
24
25 echo
26
27 echo 'foo\
28 bar' # ÓÉÓÚÊÇÇ¿ÒýÓÃ,ËùÒÔ\û±»½âÊͳÉÐøÐзû
29 #foo\
30 #bar
################################End Script######################################
###

µÚ6Õ Í˳öºÍÍ˳ö״̬
====================
exitÃüÁî±»ÓÃÀ´½áÊø½Å±¾,¾ÍÏñCÓïÑÔÒ»Ñù.ËûÒ²»á·µ»ØÒ»¸öÖµÀ´´«¸ø¸¸½ø³Ì,¸¸½ø³Ì»áÅжÏÊÇ·ñ
¿ÉÓÃ.
ÿ¸öÃüÁ»á·µ»ØÒ»¸öexit״̬(ÓÐʱºòÒ²½Ðreturn״̬).³É¹¦·µ»Ø0,Èç¹û·µ»ØÒ»¸ö·Ç0Öµ,ͨ
³£Çé¿ö϶¼»á±»ÈÏΪÊÇÒ»¸ö´íÎóÂë.Ò»¸ö±àдÁ¼ºÃµÄUNIXÃüÁî,³ÌÐò,ºÍ¹¤¾ß¶¼»á·µ»ØÒ»¸ö0×÷Ϊ
Í˳öÂëÀ´±íʾ³É¹¦,ËäȻż¶ûÒ²»áÓÐÀýÍâ.
ͬÑùµÄ,½Å±¾Öеĺ¯ÊýºÍ½Å±¾±¾Éí¶¼»á·µ»ØÍ˳ö״̬.Ôڽű¾»òÕßÊǽű¾º¯ÊýÖÐÖ´ÐеÄ×îºóµÄÃü
Áî»á¾ö¶¨Í˳ö״̬.Ôڽű¾ÖÐ,exit nnnÃüÁ»á°ÑnnnÍ˳öÂë´«µÝ¸øshell(nnn±ØÐëÊÇ10½øÖÆÊý
0-255).
µ±Ò»¸ö½Å±¾ÒÔ²»´ø²ÎÊýexitÀ´½áÊøʱ,½Å±¾µÄÍ˳ö״̬¾ÍÓɽű¾ÖÐ×îºóÖ´ÐÐÃüÁîÀ´¾ö¶¨.
1 #!/bin/bash
2
3 COMMAND_1
4
5 . . .
6
7 # ½«ÒÔ×îºóµÄÃüÁîÀ´¾ö¶¨Í˳ö״̬
8 COMMAND_LAST
9
10 exit $?
1 #!/bin/bash
2
3 COMMAND1
4
5 . . .
6
7 # ½«ÒÔ×îºóµÄÃüÁîÀ´¾ö¶¨Í˳ö״̬
8 COMMAND_LAST
$?¶ÁÈ¡×îºóÖ´ÐÐÃüÁîµÄÍ˳öÂë.º¯Êý·µ»Øºó,$?¸ø³öº¯Êý×îºóÖ´ÐеÄÄÇÌõÃüÁîµÄÍ˳öÂë.ÕâÖÖ¸ø
º¯Êý·µ»ØÖµµÄ·½·¨ÊÇBashµÄ·½·¨.¶ÔÓڽű¾À´ËµÒ²Ò»Ñù.×ÜÖ®,Ò»°ãÇé¿öÏÂ,0Ϊ³É¹¦,·Ç0ʧ°ÜW.
Example 6-1 exit/exit״̬
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo hello
4 echo $? # ·µ»Ø0,ÒòΪִÐгɹ¦
5
6 lskdf # ²»ÈÏʶµÄÃüÁî.
7 echo $? # ·µ»Ø·Ç0Öµ,ÒòΪʧ°ÜÁË.
8
9 echo
10
11 exit 113 # ½«·µ»Ø113¸øshell.
12 # To verify this, type "echo $?" after script terminates.
12 # ΪÁËÑéÖ¤Õâ¸ö,Ôڽű¾½áÊøµÄµØ·½Ê¹ÓÃ"echo $?"
################################End Script######################################
###
$?¶ÔÓÚ²âÊԽű¾ÖеÄÃüÁîµÄ½á¹ûÌرðÓÐÓÃ(¼ûExample 12-32ºÍExample 12-17).
×¢Òâ: !Âß¼­·Ç²Ù×÷,½«»á·´×ªtestÃüÁîµÄ½á¹û,²¢ÇÒÕâ»áÓ°Ïìexit״̬.
Example 6-2 ·ñ¶¨Ò»¸öÌõ¼þʹÓÃ!
################################Start Script####################################
###
1 true # trueÊÇshellÄÚ½¨ÃüÁî,ʲôʶ¼²»×ö,¾ÍÊÇshell·µ»Ø0
2 echo "exit status of \"true\" = $?" # 0
3
4 ! true
5 echo "exit status of \"! true\" = $?" # 1
6 # ×¢Òâ:"!"ÐèÒªÒ»¸ö¿Õ¸ñ
7 # !true ½«µ¼ÖÂÒ»¸ö"command not found"´íÎó
8 #
9 # Èç¹ûÒ»¸öÃüÁîÒÔ'!'¿ªÍ·,ÄÇô½«Ê¹ÓÃBashµÄÀúÊ·»úÖÆ.¾ÍÊÇÏÔʾÕâ¸öÃüÁʹÓõÄÀúÊ·.
10
11 true
12 !true
13 # Õâ´Î¾ÍûÓдíÎóÁË.
14 # Ëû²»¹ýÊÇÖظ´ÁË֮ǰµÄÃüÁî(true).
################################End Script######################################
###
×¢ÒâÊÂÏî:
Ìض¨µÄÍ˳öÂ붼ÓÐÔ¤¶¨µÄº¬Òå(¼û¸½Â¼D),Óû§²»Ó¦¸ÃÔÚ×Ô¼ºµÄ½Å±¾ÖÐÖ¸¶¨Ëû.

µÚ7ÕÂ Tests
===========
ÿ¸öÍêÕûµÄºÏÀíµÄ±à³ÌÓïÑÔ¶¼¾ßÓÐÌõ¼þÅжϵŦÄÜ.Bash¾ßÓÐtestÃüÁî,²»Í¬µÄ[]ºÍ()²Ù×÷,ºÍ
if/then½á¹¹.

7.1 Test½á¹¹
------------
Ò»¸öif/then½á¹¹¿ÉÒÔ²âÊÔÃüÁîµÄ·µ»ØÖµÊÇ·ñΪ0(ÒòΪ0±íʾ³É¹¦),Èç¹ûÊǵĻ°,Ö´Ðиü¶àÃüÁî.
ÓÐÒ»¸öרÓÃÃüÁî"["(×óÖÐÀ¨ºÅ,ÌØÊâ×Ö·û).Õâ¸öÃüÁîÓëtestÃüÁîµÈ¼Û,µ«ÊdzöÓÚЧÂÊÉϵĿ¼ÂÇ,
ËüÊÇÒ»¸öÄÚ½¨ÃüÁî.Õâ¸öÃüÁî°ÑËüµÄ²ÎÊý×÷Ϊ±È½Ï±í´ïʽ»òÊÇÎļþ²âÊÔ,²¢ÇÒ¸ù¾Ý±È½ÏµÄ½á¹û,
·µ»ØÒ»¸öÍ˳öÂë.
ÔÚ°æ±¾2.02µÄBashÖÐ,ÍƳöÁËÒ»¸öеÄ[[...]]À©Õ¹testÃüÁî.ÒòΪÕâÖÖ±íÏÖÐÎʽ¿ÉÄܶÔijЩÓï
ÑԵijÌÐòÔ±À´Ëµ¸ü¼ÓÊìϤ.×¢Òâ"[["ÊÇÒ»¸ö¹Ø¼ü×Ö,²¢²»ÊÇÒ»¸öÃüÁî.
Bash°Ñ[[ $a -lt $b ]]¿´×÷Ò»¸öµ¥¶ÀµÄÔªËØ,²¢ÇÒ·µ»ØÒ»¸öÍ˳öÂë.
((...))ºÍlet...½á¹ûÒ²Äܹ»·µ»ØÒ»¸öÍ˳öÂë,µ±ËüÃÇËù²âÊÔµÄËãÊõ±í´ïʽµÄ½á¹ûΪ·Ç0µÄʱºò,
ËûÃǵÄÍ˳öÂ뽫·µ»Ø0.ÕâЩËãÊõÀ©Õ¹(¼ûµÚ15ÕÂ)½á¹¹±»ÓÃÀ´×öËãÊõ±È½Ï.
1 let "1<2" returns 0 (as "1<2" expands to "1")
2 (( 0 && 1 )) returns 1 (as "0 && 1" expands to "0")
ifÃüÁî¿ÉÒÔ²âÊÔÈκÎÃüÁî,²»½ö½öÊÇÀ¨ºÅÖеÄÌõ¼þ.
1 if cmp a b &> /dev/null # ×èÖ¹Êä³ö.
2 then echo "Files a and b are identical."
3 else echo "Files a and b differ."
4 fi
5
6 # ·Ç³£ÓÐÓõÄ"if-grep" ½á¹¹:
7 # ------------------------
8 if grep -q Bash file
9 then echo "File contains at least one occurrence of Bash."
10 fi
11
12 word=Linux
13 letter_sequence=inu
14 if echo "$word" | grep -q "$letter_sequence"
15 # "-q"Ñ¡ÏîÊÇÓÃÀ´×èÖ¹Êä³ö
16 then
17 echo "$letter_sequence found in $word"
18 else
19 echo "$letter_sequence not found in $word"
20 fi
21
22
23 if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED
24 then echo "Command succeeded."
25 else echo "Command failed."
26 fi
Ò»¸öif/then½á¹¹¿ÉÒÔ°üº¬¶à¼¶±È½ÏºÍtests.
1 if echo "Next *if* is part of the comparison for the first *if*."
2
3 if [[ $comparison = "integer" ]]
4 then (( a < b ))
5 else
6 [[ $a < $b ]]
7 fi
8
9 then
10 echo '$a is less than $b'
11 fi
Example 7-1 ʲôÇé¿öÏÂΪÕæ?
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ¼¼ÇÉ:
4 # Èç¹ûÄ㲻ȷ¶¨Ò»¸öÌض¨µÄÌõ¼þÈçºÎÅжÏ.
5 #+ ÔÚÒ»¸öif-test½á¹¹ÖвâÊÔËü.
6
7 echo
8
9 echo "Testing \"0\""
10 if [ 0 ] # zero
11 then
12 echo "0 is true."
13 else
14 echo "0 is false."
15 fi # 0 is true.
16
17 echo
18
19 echo "Testing \"1\""
20 if [ 1 ] # one
21 then
22 echo "1 is true."
23 else
24 echo "1 is false."
25 fi # 1 is true.
26
27 echo
28
29 echo "Testing \"-1\""
30 if [ -1 ] # -1
31 then
32 echo "-1 is true."
33 else
34 echo "-1 is false."
35 fi # -1 is true.
36
37 echo
38
39 echo "Testing \"NULL\""
40 if [ ] # NULL (¿Ø״̬)
41 then
42 echo "NULL is true."
43 else
44 echo "NULL is false."
45 fi # NULL is false.
46
47 echo
48
49 echo "Testing \"xyz\""
50 if [ xyz ] # ×Ö·û´®
51 then
52 echo "Random string is true."
53 else
54 echo "Random string is false."
55 fi # Random string is true.
56
57 echo
58
59 echo "Testing \"\$xyz\""
60 if [ $xyz ] # ²âÊÔ$xyzÊÇ·ñΪnull,µ«ÊÇ...(Ã÷ÏÔûÈ˶¨Òåô!)
61 # Ö»²»¹ýÊÇÒ»¸ö䶨ÒåµÄ±äÁ¿
62 then
63 echo "Uninitialized variable is true."
64 else
65 echo "Uninitialized variable is false."
66 fi # Uninitialized variable is false.
67
68 echo
69
70 echo "Testing \"-n \$xyz\""
71 if [ -n "$xyz" ] # ¸üѧ¾¿µÄµÄ¼ì²é
72 then
73 echo "Uninitialized variable is true."
74 else
75 echo "Uninitialized variable is false."
76 fi # Uninitialized variable is false.
77
78 echo
79
80
81 xyz= # ³õʼ»¯ÁË,µ«Êǽ«ÆäÉèΪ¿ÕÖµ
82
83 echo "Testing \"-n \$xyz\""
84 if [ -n "$xyz" ]
85 then
86 echo "Null variable is true."
87 else
88 echo "Null variable is false."
89 fi # Null variable is false.
90
91
92 echo
93
94
95 # ʲôʱºò"flase"Ϊtrue?
96
97 echo "Testing \"false\""
98 if [ "false" ] # ¿´ÆðÀ´"false"Ö»²»¹ýÊǸö×Ö·û´®¶øÒÑ.
99 then
100 echo "\"false\" is true." #+ ²¢ÇÒËütestµÄ½á¹û¾ÍÊÇtrue.
101 else
102 echo "\"false\" is false."
103 fi # "false" is true.
104
105 echo
106
107 echo "Testing \"\$false\"" # ÔÙÀ´Ò»¸ö,δÉùÃ÷µÄ±äÁ¿
108 if [ "$false" ]
109 then
110 echo "\"\$false\" is true."
111 else
112 echo "\"\$false\" is false."
113 fi # "$false" is false.
114 # ÏÖÔÚÎÒÃÇÖÕÓڵõ½ÁËÆÚÍûµÄ½á¹û
115
116 # Èç¹ûÎÒÃÇtestÕâ¸ö±äÁ¿"$true"»á·¢Éúʲô½á¹û?´ð°¸ÊǺÍ"$flase"Ò»Ñù,¶¼Îª¿Õ,ÒòΪÎÒ
117 #+ ÃDz¢Ã»Óж¨ÒåËü.
118 echo
119
120 exit 0
################################End Script######################################
###
Á·Ï°.½âÊÍÉϱßÀý×ÓµÄÐÐΪ(ÎÒÏëÎÒ½âÊ͵ÄÒѾ­¹»Çå³þÁË)
1 if [ condition-true ]
2 then
3 command 1
4 command 2
5 ...
6 else
7 # ¿ÉÑ¡µÄ(Èç¹û²»ÐèÒª¿ÉÒÔÊ¡È¥)
8 # Èç¹ûԭʼµÄÌõ¼þ²âÊÔ½á¹ûÊÇfalse,ÄÇôÌí¼ÓĬÈϵĴúÂëÀ´Ö´ÐÐ.
9 command 3
10 command 4
11 ...
12 fi
×¢Òâ:µ±ifºÍthenÔÚÒ»¸öÌõ¼þ²âÊÔµÄͬһÐÐÖеĻ°,±ØÐëʹÓÃ";"À´ÖÕÖ¹if±í´ïʽ.ifºÍthen¶¼ÊÇ
¹Ø¼ü×Ö.¹Ø¼ü×Ö(»òÕßÃüÁî)×÷Ϊһ¸ö±í´ïʽµÄ¿ªÍ·,²¢ÇÒÔÚÒ»¸öеıí´ïʽ¿ªÊ¼Ö®Ç°,±ØÐë
½áÊøÉÏÒ»¸ö±í´ïʽ.
1 if [ -x "$filename" ]; then
Else ifºÍelif
elif
elifÊÇelse ifµÄËõ¼õÐÎʽ.
1 if [ condition1 ]
2 then
3 command1
4 command2
5 command3
6 elif [ condition2 ]
7 # Same as else if
8 then
9 command4
10 command5
11 else
12 default-command
13 fi
ʹÓÃif test condition-trueÕâÖÖÐÎʽºÍif[condition-true]ÕâÖÖÐÎʽÊǵȼ۵Ä.ÏòÎÒÃÇÇ°±ß
Ëù˵µÄ"["ÊÇtestµÄ±ê¼Ç.²¢ÇÒÒÔ"]"½áÊø.ÔÚif/testÖв¢²»Ó¦¸ÃÕâôÑÏÀ÷,µ«ÊÇа汾µÄBash
ÐèÒªËü.
×¢Òâ:testÃüÁîÊÇBashµÄÄÚ½¨ÃüÁî,ÓÃÀ´²âÊÔÎļþÀàÐͺͱȽÏ×Ö·û´®.Òò´Ë,ÔÚBash½Å±¾ÖÐ,test
²¢²»µ÷ÓÃ/usr/bin/testµÄ¶þ½øÖÆ°æ±¾(ÕâÊÇsh-utils¹¤¾ß°üµÄÒ»²¿·Ö).ͬÑùµÄ,[²¢²»µ÷ÓÃ
/usr/bin/[,±»Á¬½Óµ½/usr/bin/test.
bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword
bash$ type ']'
bash: type: ]: not found
Example 7-2 ¼¸¸öµÈЧÃüÁîtest,/usr/bin/test,[],ºÍ/usr/bin/[
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo
4
5 if test -z "$1"
6 then
7 echo "No command-line arguments."
8 else
9 echo "First command-line argument is $1."
10 fi
11
12 echo
13
14 if /usr/bin/test -z "$1" # ÓëÄÚ½¨µÄtest½á¹ûÏàͬ
15 then
16 echo "No command-line arguments."
17 else
18 echo "First command-line argument is $1."
19 fi
20
21 echo
22
23 if [ -z "$1" ] # ÓëÉϱߴúÂëµÄ×÷ÓÃÏàͬ
24 # if [ -z "$1" Ó¦¸Ã¹¤×÷,µ«ÊÇ...
25 #+ BashÏàÓ¦Ò»¸öȱÉٹرÕÖÐÀ¨ºÅµÄ´íÎóÏûÏ¢.
26 then
27 echo "No command-line arguments."
28 else
29 echo "First command-line argument is $1."
30 fi
31
32 echo
33
34
35 if /usr/bin/[ -z "$1" ] # ÔÙÀ´Ò»¸ö,ÓëÉϱߴúÂëµÄ×÷ÓÃÏàͬ
36 # if /usr/bin/[ -z "$1" # ¹¤×÷,µ«ÊǸø¸ö´íÎóÏûÏ¢
37 # # ×¢Òâ:
38 # This has been fixed in Bash, version 3.x.
38 # ÔÚver 3.xÉÏ,Õâ¸öbugÒѾ­±»BashÐÞÕýÁË.
39 then
40 echo "No command-line arguments."
41 else
42 echo "First command-line argument is $1."
43 fi
44
45 echo
46
47 exit 0
###############################End Script#######################################
##
[[]]½á¹¹±ÈBashµÄ[]¸ü¼ÓÁé»î,ÕâÊÇÒ»¸öÀ©Õ¹µÄtestÃüÁî,´Óksh88¼Ì³Ð¹ýÀ´µÄ.
×¢Òâ:ÔÚ[[]]½á¹¹ÖÐ,½«Ã»ÓÐÎļþÀ©Õ¹»òÕßÊǵ¥´Ê·ÖÀë,µ«ÊǻᷢÉú²ÎÊýÀ©Õ¹ºÍÃüÁîÌæ»».
1 file=/etc/passwd
2
3 if [[ -e $file ]]
4 then
5 echo "Password file exists."
6 fi
×¢Òâ:ʹÓÃ[[]],¶ø²»ÊÇ[],Äܹ»×èÖ¹½Å±¾ÖеÄÐí¶àÂß¼­´íÎó.±ÈÈç,¾¡¹ÜÔÚ[]Öн«¸ø³öÒ»¸ö´íÎó,
µ«ÊÇ&&,||,<>²Ù×÷»¹ÊÇÄܹ»¹¤×÷ÔÚÒ»¸ö[[]]testÖ®ÖÐ.
×¢Òâ:ÔÚifºó±ß,testÃüÁîºÍ[]»ò[[]]¶¼²»ÊDZØÐëµÄ.ÈçÏÂ:
1 dir=/home/bozo
2
3 if cd "$dir" 2>/dev/null; then # "2>/dev/null" hides error message.
4 echo "Now in $dir."
5 else
6 echo "Can't change to $dir."
7 fi
ifÃüÁ·µ»Øifºó±ßµÄÃüÁîµÄÍ˳öÂë.
Óë´ËÏàËÆ,µ±ÔÚÒ»¸öÔÚʹÓÃÓë»òÁбí½á¹¹µÄʱºò,test»òÖÐÀ¨ºÅµÄʹÓÃ,Ò²²¢²»Ò»¶¨·ÇµÄÓÐif²»¿É
1 var1=20
2 var2=22
3 [ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
4
5 home=/home/bozo
6 [ -d "$home" ] || echo "$home directory does not exist."
(())½á¹¹À©Õ¹²¢¼ÆËãÒ»¸öËãÊõ±í´ïʽµÄ½á¹û.Èç¹û±í´ïʽµÄ½á¹ûΪ0,Ëü½«·µ»Ø1×÷ΪÍ˳öÂë,»ò
ÕßÊÇ"false".¶øÒ»¸ö·Ç0±í´ïʽµÄ½á¹û½«·µ»Ø0×÷ΪÍ˳öÂë,»òÕßÊÇ"true".
Example 7-3 ËãÊý²âÊÔʹÓÃ(( ))
################################Start Script####################################
###
1 #!/bin/bash
2 # ËãÊý²âÊÔ
3
4 # The (( ... )) construct evaluates and tests numerical expressions.
4 # (( ... ))½á¹¹¼ÆËã²¢²âÊÔËãÊý±í´ïʽµÄ½á¹û.
5 # Í˳öÂ뽫Óë[ ... ]½á¹¹Ïà·´!
6
7 (( 0 ))
8 echo "Exit status of \"(( 0 ))\" is $?." # 1
9
10 (( 1 ))
11 echo "Exit status of \"(( 1 ))\" is $?." # 0
12
13 (( 5 > 4 )) # true
14 echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
15
16 (( 5 > 9 )) # false
17 echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
18
19 (( 5 - 5 )) # 0
20 echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
21
22 (( 5 / 4 )) # ³ý·¨Ò²ÐÐ
23 echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
24
25 (( 1 / 2 )) # ³ö·¢½á¹û<1
26 echo "Exit status of \"(( 1 / 2 ))\" is $?." # ½á¹û½«Îª0
27 # 1
28
29 (( 1 / 0 )) 2>/dev/null # ³ýÊýΪ0µÄ´íÎó
30 # ^^^^^^^^^^^
31 echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1
32
33 # What effect does the "2>/dev/null" have?
33 # "2>/dev/null"µÄ×÷ÓÃÊÇʲô?
34 # Èç¹ûɾ³ý"2>dev/null"½«»á·¢Éúʲô?
35 # Try removing it, then rerunning the script.
35 # ³¢ÊÔɾ³ýËü,È»ºóÔÙÔËÐнű¾.
36
37 exit 0
################################End Script######################################
###

7.2 Îļþ²âÊÔ²Ù×÷
----------------
·µ»ØtrueÈç¹û...
-e Îļþ´æÔÚ
-a Îļþ´æÔÚ
Õâ¸öÑ¡ÏîµÄЧ¹ûÓë-eÏàͬ.µ«ÊÇËüÒѾ­±»ÆúÓÃÁË,²¢ÇÒ²»¹ÄÀøʹÓÃ
-f fileÊÇÒ»¸öregularÎļþ(²»ÊÇĿ¼»òÕßÉ豸Îļþ)
-s Îļþ³¤¶È²»Îª0
-d ÎļþÊǸöĿ¼
-b ÎļþÊǸö¿éÉ豸(ÈíÅÌ,cdromµÈµÈ)
-c ÎļþÊǸö×Ö·ûÉ豸(¼üÅÌ,modem,Éù¿¨µÈµÈ)
-p ÎļþÊǸö¹ÜµÀ
-h ÎļþÊǸö·ûºÅÁ´½Ó
-L ÎļþÊǸö·ûºÅÁ´½Ó
-S ÎļþÊǸösocket
-t ¹ØÁªµ½Ò»¸öÖÕ¶ËÉ豸µÄÎļþÃèÊö·û
Õâ¸öÑ¡ÏîÒ»°ã¶¼ÓÃÀ´¼ì²âÊÇ·ñÔÚÒ»¸ö¸ø¶¨½Å±¾ÖеÄstdin[-t0]»ò[-t1]ÊÇÒ»¸öÖÕ¶Ë
-r Îļþ¾ßÓжÁȨÏÞ(¶ÔÓÚÓû§ÔËÐÐÕâ¸ötest)
-w Îļþ¾ßÓÐдȨÏÞ(¶ÔÓÚÓû§ÔËÐÐÕâ¸ötest)
-x Îļþ¾ßÓÐÖ´ÐÐȨÏÞ(¶ÔÓÚÓû§ÔËÐÐÕâ¸ötest)
-g set-group-id(sgid)±êÖ¾µ½Îļþ»òĿ¼ÉÏ
Èç¹ûÒ»¸öĿ¼¾ßÓÐsgid±êÖ¾,ÄÇôһ¸ö±»´´½¨ÔÚÕâ¸öĿ¼ÀïµÄÎļþ,Õâ¸öĿ¼ÊôÓÚ´´½¨
Õâ¸öĿ¼µÄÓû§×é,²¢²»Ò»¶¨Óë´´½¨Õâ¸öÎļþµÄÓû§µÄ×éÏàͬ.¶ÔÓÚworkgroupµÄĿ¼
¹²ÏíÀ´Ëµ,Õâ·Ç³£ÓÐÓÃ.¼û<<UNIX»·¾³¸ß¼¶±à³ÌÖÐÎÄ°æ>>µÚ58Ò³.
-u set-user-id(suid)±êÖ¾µ½ÎļþÉÏ
Èç¹ûÔËÐÐÒ»¸ö¾ßÓÐrootȨÏÞµÄÎļþ,ÄÇôÔËÐнø³Ì½«È¡µÃrootȨÏÞ,¼´Ê¹ÄãÊÇÒ»¸öÆÕͨ
Óû§.[1]Õâ¶ÔÓÚÐèÒª´æȡϵͳӲ¼þµÄÖ´ÐвÙ×÷(±ÈÈçpppdºÍcdrecord)·Ç³£ÓÐÓÃ.Èç¹û
ûÓÐsuid±êÖ¾µÄ»°,ÄÇôÆÕͨÓû§(ûÓÐrootȨÏÞ)½«ÎÞ·¨ÔËÐÐÕâÖÖ³ÌÐò.
¼û<<UNIX»·¾³¸ß¼¶±à³ÌÖÐÎÄ°æ>>µÚ58Ò³.
-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd
¶ÔÓÚÉèÖÃÁËsuidµÄÎļþ,ÔÚËüµÄȨÏÞ±êÖ¾ÖÐÓÐ"s".
-k ÉèÖÃÕ³Ìùλ,¼û<<UNIX»·¾³¸ß¼¶±à³ÌÖÐÎÄ°æ>>µÚ65Ò³.
¶ÔÓÚ"sticky bit",save-text-mode±êÖ¾ÊÇÒ»¸öÎļþȨÏÞµÄÌØÊâÀàÐÍ.Èç¹ûÉèÖÃÁËÕâ
¸ö±êÖ¾,ÄÇôÕâ¸öÎļþ½«±»±£´æÔÚ½»»»Çø,ΪÁË´ïµ½¿ìËÙ´æÈ¡µÄÄ¿µÄ.Èç¹ûÉèÖÃÔÚĿ¼
ÖÐ,Ëü½«ÏÞÖÆдȨÏÞ.¶ÔÓÚÉèÖÃÁËsticky bitλµÄÎļþ»òĿ¼,ȨÏÞ±êÖ¾ÖÐÓÐ"t".
drwxrwxrwt 7 root 1024 May 19 21:26 tmp/
Èç¹ûÒ»¸öÓû§²¢²»Ê±¾ßÓÐstick bitλµÄĿ¼µÄÓµÓÐÕß,µ«ÊǾßÓÐдȨÏÞ,ÄÇôÓû§Ö»
ÄÜÔÚÕâ¸öĿ¼ÏÂɾ³ý×Ô¼ºËùÓµÓеÄÎļþ.Õ⽫·ÀÖ¹Óû§ÔÚÒ»¸ö¹«¿ªµÄĿ¼Öв»É÷¸²¸Ç
»òÕßɾ³ý±ðÈ˵ÄÎļþ,±ÈÈç/tmp(µ±È»root»òÕßÊÇĿ¼µÄËùÓÐÕß¿ÉÒÔËæ±ãɾ³ý»òÖØÃüÃû
ÆäÖеÄÎļþ).
-O ÄãÊÇÎļþµÄËùÓÐÕß.
-G ÎļþµÄgroup-idºÍÄãµÄÏàͬ.
-N ´ÓÎļþ×îºó±»ÔĶÁµ½ÏÖÔÚ,ÊÇ·ñ±»ÐÞ¸Ä.
f1 -nt f2
Îļþf1±Èf2ÐÂ
f1 -ot f2
f1±Èf2ÀÏ
f1 -ef f2
f1ºÍf2¶¼Ó²Á¬½Óµ½Í¬Ò»¸öÎļþ.
! ·Ç--·´×ªÉϱ߲âÊԵĽá¹û(Èç¹ûÌõ¼þȱϯ,½«·µ»Øtrue)
Example 7-4 testËÀµÄÁ´½ÓÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # broken-link.sh
3 # Written by Lee bigelow <ligelowbee@yahoo.com>
4 # Used with permission.
5
6 #Ò»¸öÕæÕýÓÐÓõÄshell½Å±¾À´ÕÒ³öËÀÁ´½ÓÎļþ²¢ÇÒÊä³öËüÃǵÄÒýÓÃ
7 #ÒÔ±ãÓÚËüÃÇ¿ÉÒÔ±»ÊäÈëµ½xargsÃüÁîÖнøÐд¦Àí :)
8 #±ÈÈç: broken-link.sh /somedir /someotherdir|xargs rm
9 #
10 #ÕâÀï,²»¹ÜÔõô˵,ÊÇÒ»ÖÖ¸üºÃµÄ·½·¨
11 #
12 #find "somedir" -type l -print0|\
13 #xargs -r0 file|\
14 #grep "broken symbolic"|
15 #sed -e 's/^\|: *broken symbolic.*$/"/g'
16 #
17 #µ«Õâ²»ÊÇÒ»¸ö´¿´âµÄbash,×îÆðÂëÏÖÔÚ²»ÊÇ.
18 #СÐÄ:СÐÄ/procÎļþϵͳºÍÈκεÄÑ­»·Á´½ÓÎļþ.
19 ##############################################################
20
21
22 #Èç¹ûû¶ÔÕâ¸ö½Å±¾´«µÝ²ÎÊý,ÄÇô¾ÍʹÓõ±Ç°Ä¿Â¼.
23 #·ñÔò¾ÍʹÓô«µÝ½øÀ´µÄ²ÎÊý×÷ΪĿ¼À´ËÑË÷.
24 #
25 ####################
26 [ $# -eq 0 ] && directorys=`pwd` || directorys=$@
27
28 #½¨Á¢º¯ÊýlinkchkÀ´¼ì²é´«½øÀ´µÄĿ¼»òÎļþÊÇ·ñÊÇÁ´½ÓºÍÊÇ·ñ´æÔÚ,
29 #²¢ÇÒ´òÓ¡³öËüÃǵÄÒýÓÃ
30 #Èç¹û´«½øÀ´µÄĿ¼ÓÐ×ÓĿ¼,
31 #ÄÇô°Ñ×ÓĿ¼Ҳ·¢Ë͵½linkchkº¯ÊýÖд¦Àí,¾ÍÊǵݹéĿ¼.
32 ##########
33 linkchk () {
34 for element in $1/*; do
35 [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
36 [ -d "$element" ] && linkchk $element
37 # Of course, '-h' tests for symbolic link, '-d' for directory.
37 # µ±È»'-h'ÊDzâÊÔÁ´½Ó,'-d'ÊDzâÊÔĿ¼.
38 done
39 }
40
41 #Èç¹ûÊǸö¿ÉÓÃĿ¼,ÄǾͰÑÿ¸ö´Ó½Å±¾´«µÝ½øÀ´µÄ²ÎÊý¶¼Ë͵½linkcheº¯ÊýÖÐ.
42 #Èç¹û²»ÊÇ,ÄǾʹòÓ¡³ö´íÎóÏûÏ¢ºÍʹÓÃÐÅÏ¢.
43 #
44 ################
45 for directory in $directorys; do
46 if [ -d $directory ]
47 then linkchk $directory
48 else
49 echo "$directory is not a directory"
50 echo "Usage: $0 dir1 dir2 ..."
51 fi
52 done
53
54 exit 0
################################End Script######################################
###
Example 28-1, Example 10-7, Example 10-3, Example 28-3, ºÍExample A-1 Ò²»á˵Ã÷Îļþ
²âÊÔ²Ù×÷µÄʹÓùý³Ì.
×¢ÒâÊÂÏî:
[1] СÐÄsuid,¿ÉÄÜÒýÆð°²È«Â©¶´,µ«ÊDz»»áÓ°Ïìshell½Å±¾.
[2] ÔÚµ±´úUNIXϵͳÖÐ,ÒѾ­²»Ê¹ÓÃsticky bitÁË,Ö»ÔÚĿ¼ÖÐʹÓÃ.

7.3 ÆäËû±È½Ï²Ù×÷
----------------
¶þÔª±È½Ï²Ù×÷·û,±È½Ï±äÁ¿»òÕ߱ȽÏÊý×Ö.×¢ÒâÊý×ÖÓë×Ö·û´®µÄÇø±ð.
ÕûÊý±È½Ï
-eq µÈÓÚ,Èç:if [ "$a" -eq "$b" ]
-ne ²»µÈÓÚ,Èç:if [ "$a" -ne "$b" ]
-gt ´óÓÚ,Èç:if [ "$a" -gt "$b" ]
-ge ´óÓÚµÈÓÚ,Èç:if [ "$a" -ge "$b" ]
-lt СÓÚ,Èç:if [ "$a" -lt "$b" ]
-le СÓÚµÈÓÚ,Èç:if [ "$a" -le "$b" ]
< СÓÚ(ÐèҪ˫À¨ºÅ),Èç:(("$a" < "$b"))
<= СÓÚµÈÓÚ(ÐèҪ˫À¨ºÅ),Èç:(("$a" <= "$b"))
> ´óÓÚ(ÐèҪ˫À¨ºÅ),Èç:(("$a" > "$b"))
>= ´óÓÚµÈÓÚ(ÐèҪ˫À¨ºÅ),Èç:(("$a" >= "$b"))
×Ö·û´®±È½Ï
= µÈÓÚ,Èç:if [ "$a" = "$b" ]
== µÈÓÚ,Èç:if [ "$a" == "$b" ],Óë=µÈ¼Û
×¢Òâ:==µÄ¹¦ÄÜÔÚ[[]]ºÍ[]ÖеÄÐÐΪÊDz»Í¬µÄ,ÈçÏÂ:
1 [[ $a == z* ]] # Èç¹û$aÒÔ"z"¿ªÍ·(ģʽƥÅä)ÄÇô½«Îªtrue
2 [[ $a == "z*" ]] # Èç¹û$aµÈÓÚz*(×Ö·ûÆ¥Åä),ÄÇô½á¹ûΪtrue
3
4 [ $a == z* ] # File globbing ºÍword splitting½«»á·¢Éú
5 [ "$a" == "z*" ] # Èç¹û$aµÈÓÚz*(×Ö·ûÆ¥Åä),ÄÇô½á¹ûΪtrue
Ò»µã½âÊÍ,¹ØÓÚFile globbingÊÇÒ»ÖÖ¹ØÓÚÎļþµÄËټǷ¨,±ÈÈç"*.c"¾ÍÊÇ,ÔÙÈç~Ò²ÊÇ.
µ«ÊÇfile globbing²¢²»ÊÇÑϸñµÄÕýÔò±í´ïʽ,ËäÈ»¾ø´ó¶àÊýÇé¿öϽṹ±È½ÏÏñ.
!= ²»µÈÓÚ,Èç:if [ "$a" != "$b" ]
Õâ¸ö²Ù×÷·û½«ÔÚ[[]]½á¹¹ÖÐʹÓÃģʽƥÅä.
< СÓÚ,ÔÚASCII×Öĸ˳ÐòÏÂ.Èç:
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
×¢Òâ:ÔÚ[]½á¹¹ÖÐ"<"ÐèÒª±»×ªÒå.
> ´óÓÚ,ÔÚASCII×Öĸ˳ÐòÏÂ.Èç:
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
×¢Òâ:ÔÚ[]½á¹¹ÖÐ">"ÐèÒª±»×ªÒå.
¾ßÌå²Î¿¼Example 26-11À´²é¿´Õâ¸ö²Ù×÷·ûÓ¦ÓõÄÀý×Ó.
-z ×Ö·û´®Îª"null".¾ÍÊdz¤¶ÈΪ0.
-n ×Ö·û´®²»Îª"null"
×¢Òâ:
ʹÓÃ-nÔÚ[]½á¹¹ÖвâÊÔ±ØÐëÒªÓÃ""°Ñ±äÁ¿ÒýÆðÀ´.ʹÓÃÒ»¸öδ±»""µÄ×Ö·û´®À´Ê¹ÓÃ! -z
»òÕß¾ÍÊÇδÓÃ""ÒýÓõÄ×Ö·û´®±¾Éí,·Åµ½[]½á¹¹ÖÐ(¼ûExample 7-6)ËäȻһ°ãÇé¿öÏ¿É
ÒÔ¹¤×÷,µ«ÕâÊDz»°²È«µÄ.Ï°¹ßÓÚʹÓÃ""À´²âÊÔ×Ö·û´®ÊÇÒ»ÖÖºÃÏ°¹ß.[1]
Example 7-5 Êý×ÖºÍ×Ö·û´®±È½Ï
################################Start Script####################################
###
1 #!/bin/bash
2
3 a=4
4 b=5
5
6 # ÕâÀïµÄ±äÁ¿aºÍb¼È¿ÉÒÔµ±×÷ÕûÐÍÒ²¿ÉÒÔµ±×÷ÊÇ×Ö·û´®.
7 # ÕâÀïÔÚËãÊõ±È½ÏºÍ×Ö·û´®±È½ÏÖ®¼äÓÐЩ»ìÏý,
8 #+ ÒòΪBash±äÁ¿²¢²»ÊÇÇ¿ÀàÐ͵Ä.
9
10 # BashÔÊÐí¶ÔÕûÐͱäÁ¿²Ù×÷ºÍ±È½Ï
11 #+ µ±È»±äÁ¿ÖÐÖ»°üº¬Êý×Ö×Ö·û.
12 # µ«ÊÇ»¹ÊÇÒª¿¼ÂÇÇå³þÔÙ×ö.
13
14 echo
15
16 if [ "$a" -ne "$b" ]
17 then
18 echo "$a is not equal to $b"
19 echo "(arithmetic comparison)"
20 fi
21
22 echo
23
24 if [ "$a" != "$b" ]
25 then
26 echo "$a is not equal to $b."
27 echo "(string comparison)"
28 # "4" != "5"
29 # ASCII 52 != ASCII 53
30 fi
31
32 # ÔÚÕâ¸öÌض¨µÄÀý×ÓÖÐ,"-ne"ºÍ"!="¶¼¿ÉÒÔ.
33
34 echo
35
36 exit 0
################################End Script######################################
###
Example 7-6 ²âÊÔ×Ö·û´®ÊÇ·ñΪnull
################################Start Script####################################
###
1 #!/bin/bash
2 # str-test.sh: ²âÊÔnull×Ö·û´®ºÍ·ÇÒýÓÃ×Ö·û´®,
3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .
4 #+ ÉϱßÕâ¾äû¿´¶®
5 # Using if [ ... ]
6
7
8 # Èç¹ûÒ»¸ö×Ö·û´®Ã»±»³õʼ»¯,ÄÇôËü¾ÍûÓж¨ÒåµÄÖµ(ÏñÕâÖÖ»°,×ܸоõÏñƨ»°)
9 # ÕâÖÖ״̬½Ð×ö"null"(Óëzero²»Í¬)
10
11 if [ -n $string1 ] # $string1 û±»ÉùÃ÷ºÍ³õʼ»¯
12 then
13 echo "String \"string1\" is not null."
14 else
15 echo "String \"string1\" is null."
16 fi
17 # ´íÎóµÄ½á¹û.
18 # ÏÔʾ$string1Ϊ·Ç¿Õ,ËäÈ»Ëûû±»³õʼ»¯.
19
20
21 echo
22
23
24 # ÈÃÎÒÃÇÔÙÊÔÒ»ÏÂ.
25
26 if [ -n "$string1" ] # Õâ´Î$string1±»ÒýÓÃÁË.
27 then
28 echo "String \"string1\" is not null."
29 else
30 echo "String \"string1\" is null."
31 fi # ""µÄ×Ö·û´®ÔÚ[]½á¹¹ÖÐ
32
33
34 echo
35
36
37 if [ $string1 ] # Õâ´Î$string1±ä³É"ÂãÌå"µÄÁË
38 then
39 echo "String \"string1\" is not null."
40 else
41 echo "String \"string1\" is null."
42 fi
43 # Õ⹤×÷µÃºÜºÃ.
44 # Õâ¸ö[]test²Ù×÷¼ì²âstringÊÇ·ñΪnull.
45 # È»¶ø,ʹÓÃ("$string1")ÊÇÒ»ÖֺܺõÄÏ°¹ß
46 #
47 # As Stephane Chazelas points out,
48 # if [ $string1 ] ÓÐ1¸ö²ÎÊý "]"
49 # if [ "$string1" ] ÓÐ2¸ö²ÎÊý,¿ÕµÄ"$string1"ºÍ"]"
50
51
52
53 echo
54
55
56
57 string1=initialized
58
59 if [ $string1 ] # ÔÙÀ´,$string1"ÂãÌåÁË"
60 then
61 echo "String \"string1\" is not null."
62 else
63 echo "String \"string1\" is null."
64 fi
65 # ÔÙÀ´,¸ø³öÁËÕýÈ·µÄ½á¹û.
66 # ²»¹ýÔõô˵("$string1")»¹ÊǺúܶà,ÒòΪ. . .
67
68
69 string1="a = b"
70
71 if [ $string1 ] # ÔÙÀ´,$string1 ÔÙ´ÎÂãÌåÁË.
72 then
73 echo "String \"string1\" is not null."
74 else
75 echo "String \"string1\" is null."
76 fi
77 # ·ÇÒýÓõÄ"$string1"ÏÖÔÚ¸ø³öÁËÒ»¸ö´íÎóµÄ½á¹û!
78
79 exit 0
80 # Thank you, also, Florian Wisser, for the "heads-up".
################################End Script######################################
###
Example 7-7 zmore
################################Start Script####################################
###
1 #!/bin/bash
2 # zmore
3
4 #ʹÓÃ'more'À´²é¿´gzipÎļþ
5
6 NOARGS=65
7 NOTFOUND=66
8 NOTGZIP=67
9
10 if [ $# -eq 0 ] # Óë if [ -z "$1" ]ͬÑùµÄЧ¹û
11 # Ó¦¸ÃÊÇ˵ǰ±ßµÄÄǾä×¢ÊÍÓÐÎÊÌâ,$1ÊÇ¿ÉÒÔ´æÔÚµÄ,±ÈÈç:zmore "" arg2 arg3
12 then
13 echo "Usage: `basename $0` filename" >&2
14 # ´íÎóÏûÏ¢µ½stderr
15 exit $NOARGS
16 # ½Å±¾·µ»Ø65×÷ΪÍ˳öÂë.
17 fi
18
19 filename=$1
20
21 if [ ! -f "$filename" ] # ½«$filename ""ÆðÀ´,À´ÔÊÐí¿ÉÄܵĿհ×
22 then
23 echo "File $filename not found!" >&2
24 # ´íÎóÏûÏ¢µ½stderr
25 exit $NOTFOUND
26 fi
27
28 if [ ${filename##*.} != "gz" ]
29 # ÔÚ±äÁ¿Ìæ»»ÖÐʹÓÃÖÐÀ¨ºÅ
30 then
31 echo "File $1 is not a gzipped file!"
32 exit $NOTGZIP
33 fi
34
35 zcat $1 | more
36
37 # ʹÓùýÂËÃüÁî'more'
38 # Èç¹ûÄãÏëµÄ»°Ò²¿ÉʹÓÃ'less'
39
40
41 exit $? # ½Å±¾½«·µ»ØpipeµÄ½á¹û×÷ΪÍ˳öÂë
42 # ÊÂʵÉÏ,²»Ó÷ǵÄÓÐ"exit $?",µ«ÊDz»¹ÜÔõô˵,ÓÐÁËÕâ¾ä,ÄÜÕý¹æһЩ
43 # ½«×îºóÒ»¾äÃüÁîµÄÖ´ÐÐ״̬×÷ΪÍ˳öÂë·µ»Ø
################################End Script######################################
###
»ìºÏ±È½Ï
-a Âß¼­Óë
exp1 -a exp2 Èç¹ûexp1ºÍexp2¶¼ÎªtrueµÄ»°,Õâ¸ö±í´ïʽ½«·µ»Øtrue
-o Âß¼­»ò
exp1 -o exp2 Èç¹ûexp1ºÍexp2ÖÐÓÐÒ»¸öΪtrueµÄ»°,ÄÇôÕâ¸ö±í´ïʽ¾Í·µ»Øtrue
ÕâÓëBashµÄ±È½Ï²Ù×÷·û&&ºÍ||ºÜÏàÏñ.ÔÚ[[]]ÖÐʹÓÃËü.
1 [[ condition1 && condition2 ]]
-oºÍ-aÒ»°ã¶¼ÊǺÍtestÃüÁî»òÕßÊÇ[]Ò»Æð¹¤×÷.
1 if [ "$exp1" -a "$exp2" ]
Çë²Î¿¼Example 8-3,Example 26-16ºÍExample A-28À´²é¿´»ìºÏ±È½Ï²Ù×÷µÄÐÐΪ.
×¢ÒâÊÂÏî:
[1] S.C.(Õâ¼Ò»ïÊǸöÈËÃû)Ö¸³ö,ÔÚʹÓûìºÏ±È½ÏµÄʱºò¼´Ê¹"$var"Ò²¿ÉÄÜ»á²úÉúÎÊÌâ.
Èç¹û$stringΪ¿ÕµÄ»°,[ -n "$string" -o "$a" = "$b" ]¿ÉÄÜÔÚijЩ°æ±¾µÄBashÖÐ
»áÓÐÎÊÌâ.ΪÁ˸½¼ÓÒ»¸ö¶îÍâµÄ×Ö·ûµ½¿ÉÄܵĿձäÁ¿ÖеÄÒ»ÖÖ°²È«µÄ°ì·¨ÊÇ,
[ "x$string" != x -o "x$a" = "x$b" ](the "x's" cancel out)(û¿´¶®).
cancel outÊǵÖÏûµÄÒâ˼.

7.4 ǶÌ×µÄif/thenÌõ¼þtest
-------------------------
¿ÉÒÔʹÓÃif/thenÀ´½øÐÐǶÌ×µÄÌõ¼þtest.×îÖյĽá¹ûºÍÉϱߵÄʹÓÃ&&»ìºÏ±È½Ï²Ù×÷ÊÇÏàͬµÄ.
1 if [ condition1 ]
2 then
3 if [ condition2 ]
4 then
5 do-something # ÕâÀïÖ»ÓÐÔÚcondition1ºÍcondition2¶¼¿ÉÓõÄʱºò²ÅÐÐ.
6 fi
7 fi
¾ßÌåÇë²é¿´Example 34-4.

7.5 ¼ì²éÄãµÄtest֪ʶ
--------------------
ϵͳ·¶Î§µÄxinitrcÎļþ¿ÉÒÔÓÃÀ´Æô¶¯X server.Õâ¸öÎļþÖаüº¬ÁËÏ൱¶àµÄif/then test,
¾ÍÏñϱߵĽÚÑ¡Ò»Ñù:
1 if [ -f $HOME/.Xclients ]; then
2 exec $HOME/.Xclients
3 elif [ -f /etc/X11/xinit/Xclients ]; then
4 exec /etc/X11/xinit/Xclients
5 else
6 # ¹ÊÕϱ£ÏÕÉèÖÃ,ËäÈ»ÎÒÃÇÓÀÔ¶¶¼²»»á×ßµ½ÕâÀ´.
7 # (ÎÒÃÇÔÚXclientsÖÐÒ²ÌṩÁËÏàͬµÄ»úÖÆ)Ëü²»»áÊÜÉ˵Ä.
8 xclock -geometry 100x100-5+5 &
9 xterm -geometry 80x50-50+150 &
10 if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then
11 netscape /usr/share/doc/HTML/index.html &
12 fi
13 fi
¶ÔÉϱߵÄ"test"½á¹¹½øÐнâÊÍ,È»ºó¼ì²éÕû¸öÎļþ,/etc/X11/xinit/xinitrc,²¢·ÖÎöif/then
test½á¹¹.Äã¿ÉÄÜÐèÒª²é¿´Ò»Ïºó±ß²ÅÄܽ²½âµ½µÄgrep,sedºÍÕýÔò±í´ïʽµÄ֪ʶ.

µÚ8Õ ²Ù×÷·ûºÍÏà¹ØµÄÖ÷Ìâ
========================
8.1 ²Ù×÷·û
----------
µÈºÅ²Ù×÷·û
±äÁ¿¸³Öµ
³õʼ»¯»òÕßÐ޸ıäÁ¿µÄÖµ
=
ÎÞÂÛÔÚËãÊõÔËË㻹ÊÇ×Ö·û´®ÔËËãÖÐ,¶¼ÊǸ³ÖµÓï¾ä.
1 var=27
2 category=minerals # No spaces allowed after the "=".
×¢Òâ:²»ÒªºÍ"="test²Ù×÷·û»ìÏý.
1 # = as a test operator
2
3 if [ "$string1" = "$string2" ]
4 # if [ "X$string1" = "X$string2" ] is safer,
5 # to prevent an error message should one of the variables be e
mpty.
6 # (The prepended "X" characters cancel out.)
7 then
8 command
9 fi
ËãÊõ²Ù×÷·û
+ ¼Ó·¨
- ¼õ·¨
* ³Ë·¨
/ ³ý·¨
** ÃÝÔËËã
1 # Bash, version 2.02, introduced the "**" exponentiation opera
tor.
2
3 let "z=5**3"
4 echo "z = $z" # z = 125
% È¡Ä£
bash$ expr 5 % 3
2
5/3=1Óà2
Ä£ÔËËã¾­³£ÓÃÔÚÆäËüµÄÊÂÇéÖÐ,±ÈÈç²úÉúÌض¨µÄ·¶Î§µÄÊý×Ö(Example 9-24,
Example 9-27)ºÍ¸ñʽ»¯³ÌÐòµÄÊä³ö(Example 26-15,Example A-6).ËüÉõÖÁ¿ÉÒÔÓÃÀ´
²úÉúÖÊÊý,(Example A-16).ÊÂʵÉÏÈ¡Ä£ÔËËãÔÚËãÊõÔËËãÖÐʹÓõÄƵÂʾªÈ˵ĸß.
Example 8-1 ×î´ó¹«Ô¼Êý
################################Start Script####################################
###
1 #!/bin/bash
2 # gcd.sh: ×î´ó¹«Ô¼Êý
3 # ʹÓÃEuclid's Ëã·¨
4
5 # ×î´ó¹«Ô¼Êý,¾ÍÊÇ2¸öÊýÄܹ»Í¬Ê±Õû³ýµÄ×î´óµÄÊý.
6 #
7
8 # Euclid'sËã·¨²ÉÓÃÁ¬Ðø³ý·¨.
9 # ÔÚÿ¸öÑ­»·ÖÐ
10 #+ ±»³ýÊý <--- ³ýÊý
11 #+ ³ýÊý <--- ÓàÊý
12 #+ Ö±µ½ÓàÊý= 0.
13 #+ ÔÚ×îºóµÄÑ­»·ÖÐThe gcd = ±»³ýÊý
14 #
15 # ¹ØÓÚÕâ¸öËã·¨¸ü¾«²ÊµÄÌÖÂÛ
16 # ¼ûJim Loy's site, http://www.jimloy.com/number/euclids.htm.
17
18
19 # ------------------------------------------------------
20 # ²ÎÊý¼ì²é
21 ARGS=2
22 E_BADARGS=65
23
24 if [ $# -ne "$ARGS" ]
25 then
26 echo "Usage: `basename $0` first-number second-number"
27 exit $E_BADARGS
28 fi
29 # ------------------------------------------------------
30
31
32 gcd ()
33 {
34
35 dividend=$1 # Ëæ±ã¸øÖµ
36 divisor=$2 #+ ¼´Ê¹$2´ó,Ҳû¹Øϵ.
37 # Why not?
38
39 remainder=1 # Èç¹ûÔÙÑ­»·ÖÐʹÓÃΪ³õʼ»¯µÄ±äÁ¿.
40 #+ Äǽ«ÔÚµÚÒ»´ÎÑ­»·ÖвúÉúÒ»¸ö´íÎóÏûÏ¢.
41
42
43 until [ "$remainder" -eq 0 ]
44 do
45 let "remainder = $dividend % $divisor"
46 dividend=$divisor # ÏÖÔÚʹÓÃ2¸ö×îСµÄÊýÖظ´.
47 divisor=$remainder
48 done # Euclid's algorithm
49
50 } # Last $dividend is the gcd.
50 } # ×îºóµÄ$dividend¾ÍÊÇgcd.
51
52
53 gcd $1 $2
54
55 echo; echo "GCD of $1 and $2 = $dividend"; echo
56
57
58 # Á·Ï°:
59 # --------
60 # ¼ì²éÃüÁîÐвÎÊýÀ´È·¶¨ËüÃǶ¼ÊÇÕûÊý,
61 #+ and exit the script with an appropriate error message if not.
61 #+ ·ñÔò¾ÍÑ¡ÔñºÏÊʵĴíÎóÏûÏ¢Í˳ö.
62
63 exit 0
################################End Script######################################
###
+= ¼ÓµÈÓÚ(ͨ¹ý³£Á¿Ôö¼Ó±äÁ¿)
let "var += 5" #var½«ÔÚ±¾ÉíÖµµÄ»ù´¡ÉÏÔö¼Ó5
-= ¼õµÈÓÚ
*= ³ËµÈÓÚ
let "var *= 4"
/= ³ýµÈÓÚ
%= È¡Ä£¸³Öµ,ËãÊõ²Ù×÷¾­³£Ê¹ÓÃexpr»òÕßlet±í´ïʽ.
Example 8-2 ʹÓÃËãÊõ²Ù×÷·û
################################Start Script####################################
###
1 #!/bin/bash
2 # Counting to 11 in 10 different ways.
3
4 n=1; echo -n "$n "
5
6 let "n = $n + 1" # let "n = n + 1" ÕâôдҲÐÐ
7 echo -n "$n "
8
9
10 : $((n = $n + 1))
11 # ":" ÊDZØÐëµÄ,ÕâÊÇÒòΪ,Èç¹ûûÓÐ":"µÄ»°,Bash½«
12 #+ ³¢ÊÔ°Ñ"$((n = $n + 1))"½âÊͳÉÒ»¸öÃüÁî
13 echo -n "$n "
14
15 (( n = n + 1 ))
16 # ¶ÔÓÚÉϱߵķ½·¨µÄÒ»¸ö¸ü¼òµ¥µÄÑ¡Ôò.
17 # Thanks, David Lombard, for pointing this out.
18 echo -n "$n "
19
20 n=$(($n + 1))
21 echo -n "$n "
22
23 : $[ n = $n + 1 ]
24 # ":" ÊDZØÐëµÄ,ÕâÊÇÒòΪ,Èç¹ûûÓÐ":"µÄ»°,Bash½«
25 #+ ³¢ÊÔ°Ñ"$[ n = $n + 1 ]" ½âÊͳÉÒ»¸öÃüÁî
26 # ¼´Ê¹"n"±»³õʼ»¯³ÉΪһ¸ö×Ö·û´®,Õâ¾äÒ²Äܹ¤×÷.
27 echo -n "$n "
28
29 n=$[ $n + 1 ]
30 # ¼´Ê¹"n"±»³õʼ»¯³ÉΪһ¸ö×Ö·û´®,Õâ¾äÒ²Äܹ¤×÷.
31 #* Avoid this type of construct, since it is obsolete and nonportable.
31 #* ¾¡Á¿±ÜÃâÕâÖÖÀàÐ͵Ľá¹û,ÒòΪÕâÒѾ­±»·ÏÆúÁË,²¢ÇÒ²»¾ß¿ÉÒÆÖ²ÐÔ.
32 # Thanks, Stephane Chazelas.
33 echo -n "$n "
34
35 # ÏÖÔÚÀ´¸öC·ç¸ñµÄÔöÁ¿²Ù×÷.
36 # Thanks, Frank Wang, for pointing this out.
37
38 let "n++" # let "++n" also works.
39 echo -n "$n "
40
41 (( n++ )) # (( ++n ) also works.
42 echo -n "$n "
43
44 : $(( n++ )) # : $(( ++n )) also works.
45 echo -n "$n "
46
47 : $[ n++ ] # : $[ ++n ]] also works
48 echo -n "$n "
49
50 echo
51
52 exit 0
################################End Script######################################
###
×¢Òâ:ÔÚBashÖеÄÕûÐͱäÁ¿ÊÂʵÉÏÊÇ32λµÄ,·¶Î§ÊÇ -2147483648 µ½2147483647.Èç¹û³¬¹ýÕâ¸ö
·¶Î§½øÐÐËãÊõ²Ù×÷,½«²»»áµÃµ½ÄãÆÚÍûµÄ½á¹û(¾ÍÊÇÒç³öô).
1 a=2147483646
2 echo "a = $a" # a = 2147483646
3 let "a+=1" # ¼Ó1 "a".
4 echo "a = $a" # a = 2147483647
5 let "a+=1" # ÔÙ¼Ó1 "a" ,½«³¬¹ýÉÏÏÞÁË.
6 echo "a = $a" # a = -2147483648
7 # ´íÎó (Òç³öÁË)
ÔÚBash 2.05b°æ±¾ÖÐ,BashÖ§³Ö64λÕûÐÍÁË.
×¢Òâ:Bash²¢²»ÄÜÀí½â¸¡µãÔËËã.Ëü°Ñ°üº¬µÄСÊýµã¿´×÷×Ö·û´®.
1 a=1.5
2
3 let "b = $a + 1.3" # ´íÎó.
4 # t2.sh: let: b = 1.5 + 1.3: ±í´ïʽµÄÓïÒå´íÎó(´íÎó±ê־Ϊ".5 + 1.3")
5
6 echo "b = $b" # b=1
Èç¹ûÕæÏë×ö¸¡µãÔËËãµÄ»°,ʹÓÃbc(¼û12.8½Ú),bc¿ÉÒÔ½øÐи¡µãÔËËã»òµ÷ÓÃÊýѧ¿âº¯Êý.
λ²Ù×÷·û.
(ÔÎ,ÓеãÇ¿´ó¹ý·ÖÁË°É,λ¼¶²Ù×÷¶¼Ö§³Ö.)
λ²Ù×÷·ûÔÚshell½Å±¾Öм«ÉÙʹÓÃ.ËüÃÇ×îÖ÷ÒªµÄÓÃ;¿´ÆðÀ´¾ÍÊDzÙ×÷ºÍtest´ÓsocketsÖÐ
¶Á³öµÄ±äÁ¿."Bit flipping"Óë±àÒëÓïÑÔµÄÁªÏµºÜ½ôÃÜ,±ÈÈçc/c++,ÔÚÕâÖÖÓïÑÔÖÐËü¿ÉÒÔ
ÔËÐеÃ×ã¹»¿ì.(Ô­ÎÄÓд¦on the fly,ÎÒ²éÁËÒ»ÏÂ,ºÃÏñÊÇûʸɵÄÒâ˼,ûÀí½â)
<< ×óÒÆ1λ(ÿ´Î×óÒƶ¼½«³Ë2)
<<= ×óÒƼ¸Î»,=ºÅºó±ß½«¸ø³ö×óÒƼ¸Î»
let "var <<= 2"¾ÍÊÇ×óÒÆ2λ(¾ÍÊdzË4)
>> ÓÒÒÆ1λ(ÿ´ÎÓÒÒƶ¼½«³ý2)
>>= ÓÒÒƼ¸Î»
& °´Î»Óë
&= °´Î»Ó븳ֵ
| °´Î»»ò
|= °´Î»»ò¸³Öµ
~ °´Î»·Ç
! °´Î»·ñ?(ûÀí½âºÍÉϱߵÄ~ÓÐʲôÇø±ð?),¸Ð¾õÊÇÓ¦¸Ã·Åµ½Ï±ߵÄÂß¼­²Ù×÷ÖÐ
^ °´Î»Òì»òXOR
^= Òì»ò¸³Öµ

Âß¼­²Ù×÷:
&& Âß¼­Óë
1 if [ $condition1 ] && [ $condition2 ]
2 # Óë: if [ $condition1 -a $condition2 ] Ïàͬ
3 # Èç¹ûcondition1ºÍcondition2¶¼Îªtrue,Äǽá¹û¾ÍΪtrue.
4
5 if [[ $condition1 && $condition2 ]] # Ò²¿ÉÒÔ.
6 # ×¢Òâ&&²»ÔÊÐí³öÏÖÔÚ[ ... ]ÖÐ.
×¢Òâ:&&Ò²¿ÉÒÔÓÃÔÚand listÖÐ(¼û25ÕÂ),µ«ÊÇʹÓõÄʱºòÐèÒªÒÀÀµÉÏÏÂÎÄ.
|| Âß¼­»ò
1 if [ $condition1 ] || [ $condition2 ]
2 # Óë: if [ $condition1 -o $condition2 ] Ïàͬ
3 # Èç¹ûcondition1»òcondition2Ϊtrue,Äǽá¹û¾ÍΪtrue.
4
5 if [[ $condition1 || $condition2 ]] # Ò²¿ÉÒÔ
6 # ×¢Òâ||²»ÔÊÐí³öÏÖÔÚ[ ... ]ÖÐ.
×¢Òâ:Bash½«testÿ¸öÁ¬½Óµ½Âß¼­²Ù×÷µÄ״̬µÄÍ˳ö״̬(¼ûµÚ6ÕÂ).

Example 8-3 ʹÓÃ&&ºÍ||½øÐлìºÏ״̬µÄtest


################################Start Script####################################
###
1 #!/bin/bash
2
3 a=24
4 b=47
5
6 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
7 then
8 echo "Test #1 succeeds."
9 else
10 echo "Test #1 fails."
11 fi
12
13 # ´íÎó: if [ "$a" -eq 24 && "$b" -eq 47 ]
14 #+ ³¢ÊÔÖ´ÐÐ' [ "$a" -eq 24 '
15 #+ ÒòΪûÕÒµ½']'ËùÒÔʧ°ÜÁË.
16 #
17 # ×¢Òâ: Èç¹û [[ $a -eq 24 && $b -eq 24 ]] Äܹ»¹¤×÷.
18 # ÄÇÕâ¸ö[[]]µÄtest½á¹¹¾Í±È[]½á¹¹¸üÁé»îÁË.
19 #
20 # (ÔÚ17ÐеÄ"&&"ÓëµÚ6ÐеÄ"&&"ÒâÒ岻ͬ)
21 # Thanks, Stephane Chazelas, for pointing this out.
22
23
24 if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
25 then
26 echo "Test #2 succeeds."
27 else
28 echo "Test #2 fails."
29 fi
30
31
32 # -aºÍ-oÑ¡ÏîÌṩÁË
33 #+ Ò»ÖÖ¿ÉÑ¡µÄ»ìºÏtest·½·¨.
34 # Thanks to Patrick Callahan for pointing this out.
35
36
37 if [ "$a" -eq 24 -a "$b" -eq 47 ]
38 then
39 echo "Test #3 succeeds."
40 else
41 echo "Test #3 fails."
42 fi
43
44
45 if [ "$a" -eq 98 -o "$b" -eq 47 ]
46 then
47 echo "Test #4 succeeds."
48 else
49 echo "Test #4 fails."
50 fi
51
52
53 a=rhino
54 b=crocodile
55 if [ "$a" = rhino ] && [ "$b" = crocodile ]
56 then
57 echo "Test #5 succeeds."
58 else
59 echo "Test #5 fails."
60 fi
61
62 exit 0
################################End Script######################################
###
&&ºÍ||²Ù×÷Ò²ÄÜÔÚËãÊõÔËËãµÄÉÏÏÂÎÄÖÐÕÒµ½.
bash$ echo $(( 1 && 2 )) $((3 && 0)) $((4 || 0)) $((0 || 0))
1 0 1 0
»ìÔÓ²Ù×÷:
, ¶ººÅ²Ù×÷·û
¶ººÅ²Ù×÷·û¿ÉÒÔÁ¬½Ó2¸ö»ò¶à¸öËãÊõÔËËã.ËùÓеIJÙ×÷¶¼»á±»Ö´ÐÐ,µ«ÊÇÖ»ÓÐ×îºóÒ»¸ö
²Ù×÷×÷Ϊ½á¹û.
1 let "t1 = ((5 + 3, 7 - 1, 15 - 4))"
2 echo "t1 = $t1" # t1 = 11
3
4 let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2".
5 echo "t2 = $t2 a = $a" # t2 = 5 a = 9
","Ö÷ÒªÓÃÔÚforÑ­»·ÖÐ,¾ßÌå¼ûExample 10-12.

8.2 Êý×Ö³£Á¿
------------
shell½Å±¾Ä¬È϶¼Êǽ«Êý×Ö×÷Ϊ10½øÖÆÊý´¦Àí,³ý·ÇÕâ¸öÊý×ÖijÖÖÌØÊâµÄ±ê¼Ç·¨»òǰ׺¿ªÍ·.
ÒÔ0¿ªÍ·¾ÍÊÇ8½øÖÆ.ÒÔ0x¿ªÍ·¾ÍÊÇ16½øÖÆÊý.ʹÓÃBASE#NUMBERÕâÖÖÐÎʽ¿ÉÒÔ±íʾÆäËü½øÖÆ
±íʾ·¨
Example 8-4 Êý×Ö³£Á¿µÄ´¦Àí
################################Start Script####################################
###
1 #!/bin/bash
2 # numbers.sh: Êý×Ö³£Á¿µÄ¼¸ÖÖ²»Í¬µÄ±íʾ·¨
3
4 # 10½øÖÆ: ĬÈÏ
5 let "dec = 32"
6 echo "decimal number = $dec" # 32
7 # Ò»Çж¼ºÜÕý³£
8
9
10 # 8½øÖÆ: ÒÔ'0'(Áã)¿ªÍ·
11 let "oct = 032"
12 echo "octal number = $oct" # 26
13 # ±í´ïʽµÄ½á¹ûÓÃ10½øÖƱíʾ.
14 #
15
16 # 16½øÖƱíʾ:Êý×ÖÒÔ'0x'»òÕß'0X'¿ªÍ·
17 let "hex = 0x32"
18 echo "hexadecimal number = $hex" # 50
19 # ±í´ïʽµÄ½á¹ûÓÃ10½øÖƱíʾ.
20
21 # ÆäËü½øÖÆ: BASE#NUMBER
22 # BASE between 2 and 64.
22 # 2µ½64½øÖƶ¼¿ÉÒÔ.
23 # NUMBER±ØÐëÔÚBASEµÄ·¶Î§ÄÚ,¾ßÌå¼ûϱß.
24
25
26 let "bin = 2#111100111001101"
27 echo "binary number = $bin" # 31181
28
29 let "b32 = 32#77"
30 echo "base-32 number = $b32" # 231
31
32 let "b64 = 64#@_"
33 echo "base-64 number = $b64" # 4031
34 # ÕâÖÖ64½øÖƵıíʾ·¨ÖеÄÿλÊý×Ö¶¼±ØÐëÔÚ64½øÖƱíʾ·¨µÄÏÞÖÆ×Ö·ûÄÚ.
35 # 10 ¸öÊý×Ö+ 26 ¸öСд×Öĸ+ 26 ¸ö´óд×Öĸ+ @ + _
36
37
38 echo
39
40 echo $((36#zz)) $((2#10101010)) $((16#AF16)) $((53#1aA))
41 # 1295 170 44822 3375
42
43
44 # ÖØÒªµÄ×¢ÒâÊÂÏî:
45 # ---------------
46 # Èç¹ûʹÓõÄÿλÊý×Ö³¬³öÁËÕâ¸ö½øÖƱíʾ·¨¹æ¶¨×Ö·ûµÄ·¶Î§µÄ»°,
47 #+ ½«¸ø³öÒ»¸ö´íÎóÏûÏ¢.
48
49 let "bad_oct = 081"
50 # (²¿·ÖµÄ) ´íÎóÏûÏ¢Êä³ö:
51 # bad_oct = 081: too great for base (error token is "081")
52 # Octal numbers use only digits in the range 0 - 7.
53
54 exit 0 # Thanks, Rich Bartell and Stephane Chazelas, for clarification.
################################End Script######################################
###
µÚÈý²¿·Ö ³¬Ô½»ù±¾
++++++++++++++++++++

µÚ9Õ ±äÁ¿ÖØÓÎ
================
Èç¹û±äÁ¿Ê¹ÓÃÇ¡µ±,½«»áÔö¼Ó½Å±¾µÄÄÜÁ¿ºÍÁé»îÐÔ.µ«ÊÇÇ°ÌáÊÇÕâÐèÒª×Ðϸѧϰ±äÁ¿µÄϸ½Ú֪ʶ.

9.1 ÄÚ²¿±äÁ¿
------------
Builtin variable
ÕâЩÄÚ½¨µÄ±äÁ¿,½«Ó°Ïìbash½Å±¾µÄÐÐΪ.
$BASH
Õâ¸ö±äÁ¿½«Ö¸ÏòBashµÄ¶þ½øÖÆÖ´ÐÐÎļþµÄλÖÃ.
bash$ echo $BASH
/bin/bash
$BASH_ENV
Õâ¸ö»·¾³±äÁ¿½«Ö¸ÏòÒ»¸öBashÆô¶¯Îļþ,Õâ¸öÆô¶¯Îļþ½«ÔÚµ÷ÓÃÒ»¸ö½Å±¾Ê±±»¶ÁÈ¡.
$BASH_SUBSHELL
Õâ¸ö±äÁ¿½«ÌáÐÑsubshellµÄ²ã´Î,ÕâÊÇÒ»¸öÔÚversion3²Å±»Ìí¼Óµ½BashÖеÄÐÂÌØÐÔ.
¼ûExample 20-1.
$BASH_VERSINFO[n]
¼Ç¼Bash°²×°ÐÅÏ¢µÄÒ»¸ö6ÔªËصÄÊý×é.ÓëϱߵÄ$BASH_VERSIONºÜÏñ,µ«Õâ¸ö¸ü¼ÓÏêϸ.
1 # Bash version info:
2
3 for n in 0 1 2 3 4 5
4 do
5 echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
6 done
7
8 # BASH_VERSINFO[0] = 3 # Ö÷°æ±¾ºÅ
9 # BASH_VERSINFO[1] = 00 # ´Î°æ±¾ºÅ
10 # BASH_VERSINFO[2] = 14 # Patch ´ÎÊý.
11 # BASH_VERSINFO[3] = 1 # Build version.
12 # BASH_VERSINFO[4] = release # Release status.
13 # BASH_VERSINFO[5] = i386-redhat-linux-gnu # Architecture
$BASH_VERSION
°²×°ÔÚϵͳÉϵÄBashµÄ°æ±¾ºÅ.
bash$ echo $BASH_VERSION
3.00.14(1)-release
tcsh% echo $BASH_VERSION
BASH_VERSION: Undefined variable.
ʹÓÃÕâ¸ö±äÁ¿¶ÔÓÚÅжÏϵͳÉϵ½µ×ÔËÐеÄÊÇÄǸöshllÀ´ËµÊÇÒ»Öַdz£ºÃµÄ°ì·¨.$SHELL
ÓÐʱ½«²»Äܸø³öÕýÈ·µÄ´ð°¸.
$DIRSTACK
ÔÚĿ¼ջÖÐ×îÉϱߵÄÖµ(½«Êܵ½pushdºÍpopdµÄÓ°Ïì).
Õâ¸öÄÚ½¨µÄ±äÁ¿ÓëdirsÃüÁîÊDZ£³ÖÒ»ÖµÄ,µ«ÊÇdirsÃüÁÏÔʾĿ¼ջµÄÕû¸öÄÚÈÝ.
$EDITOR
½Å±¾µ÷ÓõÄĬÈϱ༭Æ÷,Ò»°ãÊÇvi»òÕßÊÇemacs.
$EUID
"effective"Óû§IDºÅ.
µ±Ç°Óû§±»¼Ù¶¨µÄÈκÎidºÅ.¿ÉÄÜÔÚsuÃüÁîÖÐʹÓÃ.
×¢Òâ:$EUID²¢²»Ò»¶¨Óë$UIDÏàͬ.
$FUNCNAME
µ±Ç°º¯ÊýµÄÃû×Ö.
1 xyz23 ()
2 {
3 echo "$FUNCNAME now executing." # xyz23 ÏÖÔÚÕýÔÚ±»Ö´ÐÐ.
4 }
5
6 xyz23
7
8 echo "FUNCNAME = $FUNCNAME" # FUNCNAME =
9 # ³öÁ˺¯Êý¾Í±äΪNullÖµÁË.
$GLOBIGNORE
Ò»¸öÎļþÃûµÄģʽƥÅäÁбí,Èç¹ûÔÚfile globbingÖÐÆ¥Åäµ½µÄÎļþ°üº¬Õâ¸öÁбíÖеÄ
ij¸öÎļþ,ÄÇôÕâ¸öÎļþ½«±»´ÓÆ¥Åäµ½µÄÎļþÖÐÈ¥µô.
$GROUPS
µ±Ç°Óû§ÊôÓÚµÄ×é.
ÕâÊÇÒ»¸öµ±Ç°Óû§µÄ×éidÁбí(Êý×é),¾ÍÏñÔÚ/etc/passwdÖмǼµÄÒ»Ñù.
root# echo $GROUPS
0
root# echo ${GROUPS[1]}
1
root# echo ${GROUPS[5]}
6
$HOME
Óû§µÄhomeĿ¼,Ò»°ã¶¼ÊÇ/home/username(¼ûExample 9-14)
$HOSTNAME
hostnameÃüÁÔÚÒ»¸öinit½Å±¾ÖÐ,ÔÚÆô¶¯µÄʱºò·ÖÅäÒ»¸öϵͳÃû×Ö.
gethostname()º¯Êý½«ÓÃÀ´ÉèÖÃÕâ¸ö$HOSTNAMEÄÚ²¿±äÁ¿.(¼ûExample 9-14)
$HOSTTYPE
Ö÷»úÀàÐÍ
¾ÍÏñ$MACHTYPE,ʶ±ðϵͳµÄÓ²¼þ.
bash$ echo $HOSTTYPE
i686
$IFS
ÄÚ²¿Óò·Ö¸ô·û.
Õâ¸ö±äÁ¿ÓÃÀ´¾ö¶¨BashÔÚ½âÊÍ×Ö·û´®Ê±ÈçºÎʶ±ðÓò,»òÕßµ¥´Ê±ß½ç.
$IFSĬÈÏΪ¿Õ°×(¿Õ¸ñ,tab,ºÍÐÂÐÐ),µ«¿ÉÒÔÐÞ¸Ä,±ÈÈçÔÚ·ÖÎö¶ººÅ·Ö¸ôµÄÊý¾ÝÎļþʱ.
×¢Òâ:$*ʹÓÃ$IFSÖеĵÚÒ»¸ö×Ö·û,¾ßÌå¼ûExample 5-1.
bash$ echo $IFS | cat -vte
$
bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"'
w:x:y:o
×¢Òâ:$IFS²¢²»ÏñËü´¦ÀíÆäËü×Ö·ûÒ»Ñù´¦Àí¿Õ°×.
Example 9-1 $IFSºÍ¿Õ°×
################################Start Script####################################
###
1 #!/bin/bash
2 # $IFS ´¦Àí¿Õ°×µÄ·½·¨,Óë´¦ÀíÆäËü×Ö·û²»Í¬.
3
4 output_args_one_per_line()
5 {
6 for arg
7 do echo "[$arg]"
8 done
9 }
10
11 echo; echo "IFS=\" \""
12 echo "-------"
13
14 IFS=" "
15 var=" a b c "
16 output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
17 #
18 # [a]
19 # [b]
20 # [c]
21
22
23 echo; echo "IFS=:"
24 echo "-----"
25
26 IFS=:
27 var=":a::b:c:::" # ÓëÉϱߵÄÒ»Ñù,µ«ÊÇÓÃ" "Ìæ»»ÁË":"
28 output_args_one_per_line $var
29 #
30 # []
31 # [a]
32 # []
33 # [b]
34 # [c]
35 # []
36 # []
37 # []
38
39 # ͬÑùµÄÊÂÇéÒ²»á·¢ÉúÔÚawkÖеÄ"FS"Óò·Ö¸ô·û.
40
41 # Thank you, Stephane Chazelas.
42
43 echo
44
45 exit 0
################################End Script######################################
###
Example 12-37Ò²ÊÇʹÓÃ$IFSµÄÁíÒ»¸öÆô·¢ÐÔµÄÀý×Ó.
$IGNOREEOF
ºöÂÔEOF: ¸æËßshellÔÚlog out֮ǰҪºöÂÔ¶àÉÙÎļþ½áÊø·û(control-D).
$LC_COLLATE
³£ÔÚ.bashrc»ò/etc/profileÖÐÉèÖÃ,Õâ¸ö±äÁ¿ÓÃÀ´ÔÚÎļþÃûÀ©Õ¹ºÍģʽƥÅäУ¶Ô˳Ðò.
Èç¹û$LC_COLLATE±»´íÎóµÄÉèÖÃ,ÄÇô½«»áÔÚfilename globbingÖÐÒýÆð´íÎóµÄ½á¹û.
×¢Òâ:ÔÚ2.05ÒÔºóµÄBash°æ±¾ÖÐ,filename globbing½«²»ÔÚ¶Ô[]ÖеÄ×Ö·ûÇø·Ö´óСд.
±ÈÈç:ls [A-M]* ½«¼´Æ¥ÅäFile1.txtÒ²»áÆ¥Åäfile1.txt.ΪÁ˻ָ´[]µÄÏ°¹ßÓ÷
ÉèÖÃ$LC_COLLATEµÄֵΪc,ʹÓÃexport LC_COLLATE=c ÔÚ/etc/profile»òÕßÊÇ
~/.bashrcÖÐ.
$LC_CTYPE
Õâ¸öÄÚ²¿±äÁ¿ÓÃÀ´¿ØÖÆglobbingºÍģʽƥÅäµÄ×Ö·û´®½âÊÍ.
$LINENO
Õâ¸ö±äÁ¿¼Ç¼ËüËùÔÚµÄshell½Å±¾ÖÐËüËùÔÚÐеÄÐкÅ.Õâ¸ö±äÁ¿Ò»°ãÓÃÓÚµ÷ÊÔÄ¿µÄ.
1 # *** BEGIN DEBUG BLOCK ***
2 last_cmd_arg=$_ # Save it.
3
4 echo "At line number $LINENO, variable \"v1\" = $v1"
5 echo "Last command argument processed = $last_cmd_arg"
6 # *** END DEBUG BLOCK ***
$MACHTYPE
ϵͳÀàÐÍ
ÌáʾϵͳӲ¼þ
bash$ echo $MACHTYPE
i686
$OLDPWD
ÀϵŤ×÷Ŀ¼("OLD-print-working-directory",ÄãËùÔÚµÄ֮ǰµÄĿ¼)
$OSTYPE
²Ù×÷ϵͳÀàÐÍ.
bash$ echo $OSTYPE
linux
$PATH
Ö¸ÏòBashÍⲿÃüÁîËùÔÚµÄλÖÃ,Ò»°ãΪ/usr/bin,/usr/X11R6/bin,/usr/local/binµÈ.
µ±¸ø³öÒ»¸öÃüÁîʱ,Bash½«×Ô¶¯¶Ô$PATHÖеÄĿ¼×öÒ»ÕÅhash±í.$PATHÖÐÒÔ":"·Ö¸ôµÄ
Ŀ¼ÁÐ±í½«±»´æ´¢ÔÚ»·¾³±äÁ¿ÖÐ.Ò»°ãµÄ,ϵͳ´æ´¢µÄ$PATH¶¨ÒåÔÚ/ect/processed»ò
~/.bashrcÖÐ(¼ûAppendix G).
bash$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin
PATH=${PATH}:/opt/bin½«°Ñ/opt/binĿ¼¸½¼Óµ½$PATH±äÁ¿ÖÐ.Ôڽű¾ÖÐ,ÕâÊÇÒ»¸ö
Ìí¼ÓĿ¼µ½$PATHÖеıã½Ý·½·¨.ÕâÑùÔÚÕâ¸ö½Å±¾Í˳öµÄʱºò,$PATH½«»á»Ö¸´(ÒòΪÕâ¸ö
shellÊǸö×Ó½ø³Ì,ÏñÕâÑùµÄÒ»¸ö½Å±¾ÊDz»»á½«ËüµÄ¸¸½ø³ÌµÄ»·¾³±äÁ¿Ð޸ĵÄ)
×¢Òâ:µ±Ç°µÄ¹¤×÷Ŀ¼"./"Ò»°ã¶¼ÔÚ$PATHÖб»Ê¡È¥.
$PIPESTATUS
Êý×é±äÁ¿½«±£´æ×îºóÒ»¸öÔËÐеÄǰ̨¹ÜµÀµÄÍ˳öÂë.ÓÐȤµÄÊÇ,Õâ¸öÍ˳öÂëºÍ×îºóÒ»¸öÃüÁ
ÔËÐеÄÍ˳öÂë²¢²»Ò»¶¨Ïàͬ.
bash$ echo $PIPESTATUS
0
bash$ ls -al | bogus_command
bash: bogus_command: command not found
bash$ echo $PIPESTATUS
141
bash$ ls -al | bogus_command
bash: bogus_command: command not found
bash$ echo $?
127
$PIPESTATUSÊý×éµÄÿ¸ö³ÉÔ±¶¼»á±£´æÒ»¸ö¹ÜµÀÃüÁîµÄÍ˳öÂë,$PIPESTATUS[0]±£´æµÚ
Ò»¸ö¹ÜµÀÃüÁîµÄÍ˳öÂë,$PIPESTATUS[1]±£´æµÚ2¸ö,ÒÔ´ËÀàÍÆ.
×¢Òâ:$PIPESTATUS±äÁ¿ÔÚÒ»¸ölogin shellÖпÉÄÜ»á°üº¬Ò»¸ö´íÎóµÄ0Öµ(3.0ÒÔÏ°汾)
tcsh% bash
bash$ who | grep nobody | sort
bash$ echo ${PIPESTATUS[*]}
0
°üº¬Ôڽű¾ÖеÄÉϱßÕâÐн«»á²úÉúÒ»¸öÆÚÍûµÄÊä³ö0 1 0.
×¢Òâ:ÔÚijЩÉÏÏÂÎÄ$PIPESTATUS¿ÉÄܲ»»á¸ø³öÕýÈ·µÄ½á¹û.
bash$ echo $BASH_VERSION
3.00.14(1)-release
bash$ $ ls | bogus_command | wc
bash: bogus_command: command not found
0 0 0
bash$ echo ${PIPESTATUS[@]}
141 127 0
Chet Ramey°ÑÉϱßÊä³ö²»³ÉÈ·Ô­Òò¹é¾ÌÓÚlsµÄÐÐΪ.ÒòΪÈç¹û°ÑlsµÄ½á¹û·Åµ½¹ÜµÀÉÏ,
²¢ÇÒÕâ¸öÊä³öû±»¶ÁÈ¡,ÄÇôSIGPIPE½«»ákillµôËü,²¢ÇÒÍ˳öÂë±äΪ141,¶ø²»ÊÇÎÒÃÇÆÚ
ÍûµÄ0.ÕâÖÖÇé¿öÒ²»á·¢ÉúÔÚtrÃüÁîÖÐ.
×¢Òâ:$PIPESTATUSÊÇÒ»¸ö"volatile"±äÁ¿.ÔÚÈκÎÃüÁî²åÈë֮ǰ,²¢ÇÒÔÚpipeѯÎÊÖ®ºó,
Õâ¸ö±äÁ¿ÐèÒªÁ¢¼´±»²¶×½.
bash$ $ ls | bogus_command | wc
bash: bogus_command: command not found
0 0 0
bash$ echo ${PIPESTATUS[@]}
0 127 0
bash$ echo ${PIPESTATUS[@]}
0
$PPID
Ò»¸ö½ø³ÌµÄ$PPID¾ÍÊÇËüµÄ¸¸½ø³ÌµÄ½ø³Ìid(pid).[1]
ʹÓÃpidofÃüÁî¶Ô±ÈÒ»ÏÂ.
$PROMPT_COMMAND
Õâ¸ö±äÁ¿±£´æÒ»¸öÔÚÖ÷Ìáʾ·û($PS1)ÏÔʾ֮ǰÐèÒªÖ´ÐеÄÃüÁî.
$PS1
Ö÷Ìáʾ·û,¾ßÌå¼ûÃüÁîÐÐÉϵÄÏÔʾ.
$PS2
µÚ2Ìáʾ·û,µ±ÄãÐèÒª¶îÍâµÄÊäÈëµÄʱºò½«»áÏÔʾ,ĬÈÏΪ">".
$PS3
µÚ3Ìáʾ·û,ÔÚÒ»¸öselectÑ­»·ÖÐÏÔʾ(¼ûExample 10-29).
$PS4
µÚ4Ìáʾ·û,µ±Ê¹ÓÃ-xÑ¡Ïîµ÷Óýű¾Ê±,Õâ¸öÌáʾ·û½«³öÏÖÔÚÿÐеÄÊä³öÇ°±ß.
ĬÈÏΪ"+".
$PWD
¹¤×÷Ŀ¼(Ä㵱ǰËùÔÚµÄĿ¼).
ÓëpwdÄÚ½¨ÃüÁî×÷ÓÃÏàͬ.
################################Start Script####################################
###
1 #!/bin/bash
2
3 E_WRONG_DIRECTORY=73
4
5 clear # ÇåÆÁ.
6
7 TargetDirectory=/home/bozo/projects/GreatAmericanNovel
8
9 cd $TargetDirectory
10 echo "Deleting stale files in $TargetDirectory."
11
12 if [ "$PWD" != "$TargetDirectory" ]
13 then # ·ÀֹżȻɾ³ý´íÎóµÄĿ¼
14 echo "Wrong directory!"
15 echo "In $PWD, rather than $TargetDirectory!"
16 echo "Bailing out!"
17 exit $E_WRONG_DIRECTORY
18 fi
19
20 rm -rf *
21 rm .[A-Za-z0-9]* # Delete dotfiles.
21 rm .[A-Za-z0-9]* # ɾ³ý"."Îļþ(Òþº¬Îļþ).
22 # rm -f .[^.]* ..?* ΪÁËɾ³ýÒÔ¶à¸ö"."¿ªÍ·µÄÎļþ.
23 # (shopt -s dotglob; rm -f *) Ò²ÐÐ.
24 # Thanks, S.C. for pointing this out.
25
26 # ÎļþÃûÄܹ»°üº¬0-255·¶Î§µÄËùÓÐ×Ö·û,³ýÁË"/".
27 # ɾ³ýÒÔ¸÷ÖÖ¹îÒì×Ö·û¿ªÍ·µÄÎļþ½«×÷Ϊһ¸öÁ·Ï°Áô¸ø´ó¼Ò.
28
29 # ÕâÀïÔ¤Áô¸øÆäËûµÄ±ØÒª²Ù×÷.
30
31 echo
32 echo "Done."
33 echo "Old files deleted in $TargetDirectory."
34 echo
35
36
37 exit 0
################################End Script######################################
###
$REPLY
readÃüÁîÈç¹ûûÓиø±äÁ¿,ÄÇôÊäÈ뽫±£´æÔÚ$REPLYÖÐ.ÔÚselect²Ëµ¥ÖÐÒ²¿ÉÓÃ,µ«ÊÇÖ»
ÌṩѡÔñµÄ±äÁ¿µÄÏîÊý,¶ø²»ÊDZäÁ¿±¾ÉíµÄÖµ.
################################Start Script####################################
###
1 #!/bin/bash
2 # reply.sh
3
4 # REPLYÊÇ'read'ÃüÁî½á¹û±£´æµÄĬÈϱäÁ¿.
5
6 echo
7 echo -n "What is your favorite vegetable? "
8 read
9
10 echo "Your favorite vegetable is $REPLY."
11 # µ±ÇÒ½öµ±ÔÚûÓбäÁ¿Ìṩ¸ø"read"ÃüÁîʱ,
12 #+ REPLY²Å±£´æ×îºóÒ»¸ö"read"ÃüÁî¶ÁÈëµÄÖµ.
13
14 echo
15 echo -n "What is your favorite fruit? "
16 read fruit
17 echo "Your favorite fruit is $fruit."
18 echo "but..."
19 echo "Value of \$REPLY is still $REPLY."
20 # $REPLY»¹ÊDZ£´æ×ÅÉÏÒ»¸öreadÃüÁîµÄÖµ,
21 #+ ÒòΪ±äÁ¿$fruit±»´«Èëµ½ÁËÕâ¸öеÄ"read"ÃüÁîÖÐ.
22
23 echo
24
25 exit 0
################################End Script######################################
###
$SECONDS
Õâ¸ö½Å±¾ÒѾ­ÔËÐеÄʱ¼ä(µ¥Î»ÎªÃë).
################################Start Script####################################
###
1 #!/bin/bash
2
3 TIME_LIMIT=10
4 INTERVAL=1
5
6 echo
7 echo "Hit Control-C to exit before $TIME_LIMIT seconds."
8 echo
9
10 while [ "$SECONDS" -le "$TIME_LIMIT" ]
11 do
12 if [ "$SECONDS" -eq 1 ]
13 then
14 units=second
15 else
16 units=seconds
17 fi
18
19 echo "This script has been running $SECONDS $units."
20 # ÔÚһ̨±È½ÏÂýµÄ»òÕßÊǸºÔغܴóµÄ»úÆ÷ÉÏ,Õâ¸ö½Å±¾¿ÉÄÜ»áÌø¹ý¼¸´ÎÑ­»·
21 #+ ÔÚÒ»¸öwhileÑ­»·ÖÐ.
22 sleep $INTERVAL
23 done
24
25 echo -e "\a" # Beep!
26
27 exit 0
################################End Script######################################
###
$SHELLOPTS
Õâ¸ö±äÁ¿Àï±£´æshellÔÊÐíµÄÑ¡Ïî,Õâ¸ö±äÁ¿ÊÇÖ»¶ÁµÄ.
bash$ echo $SHELLOPTS
braceexpand:hashall:histexpand:monitor:history:interactive-comme
nts:emacs
$SHLVL
Shell²ã´Î,¾ÍÊÇshell²ãµþµÄ²ã´Î,Èç¹ûÊÇÃüÁîÐÐÄÇ$SHLVL¾ÍÊÇ1,Èç¹ûÃüÁîÐÐÖ´ÐеĽÅ
±¾ÖÐ,$SHLVL¾ÍÊÇ2,ÒÔ´ËÀàÍÆ.
$TMOUT
Èç¹û$TMOUT»·¾³±äÁ¿±»ÉèÖÃΪһ¸ö·ÇÁãµÄʱ¼äÖµ,ÄÇôÔÚ¹ýÁËÕâ¸öÖ¸¶¨µÄʱ¼äÖ®ºó,
shellÌáʾ·û½«»á³¬Ê±,Õâ»áÒýÆðÒ»¸ölogout.
ÔÚ2.05b°æ±¾µÄBashÖÐ,ÒѾ­Ö§³ÖÔÚÒ»¸ö´øÓÐreadÃüÁîµÄ½Å±¾ÖÐʹÓÃ$TMOUT±äÁ¿.
1 # ÐèҪʹÓÃBash v2.05b»òÕßÒÔºóµÄ°æ±¾ÉÏ
2
3 TMOUT=3 # Prompt times out at three seconds.
3 TMOUT=3 # ÉèÖó¬Ê±µÄʱ¼äΪ3Ãë
4
5 echo "What is your favorite song?"
6 echo "Quickly now, you only have $TMOUT seconds to answer!"
7 read song
8
9 if [ -z "$song" ]
10 then
11 song="(no answer)"
12 # ĬÈÏÏìÓ¦.
13 fi
14
15 echo "Your favorite song is $song."
ÕâÀïÓÐÒ»¸ö¸ü¸´Ôӵķ½·¨À´ÔÚÒ»¸ö½Å±¾ÖÐʵÏÖ³¬Ê±¹¦ÄÜ.Ò»ÖÖ°ì·¨¾ÍÊǽ¨Á¢Ò»¸öʱ¼äÑ­
»·,ÔÚ³¬Ê±µÄʱºò֪ͨ½Å±¾.²»¹ý,ÕâÒ²ÐèÒªÒ»¸öÐźŴ¦Àí»úÖÆ,ÔÚ³¬Ê±µÄʱºòÀ´²úÉúÖÐ
¶Ï.
(²Î¼ûExample 29-5)
Example 9-2 ʱ¼äÊäÈë
################################Start Script####################################
###
1 #!/bin/bash
2 # timed-input.sh
3
4 # TMOUT=3 ÔÚа汾µÄBashÉÏÒ²Äܹ¤×÷.
5
6
7 TIMELIMIT=3 # ÔÚÕâ¸öÀý×ÓÉÏÊÇ3Ãë,Ò²¿ÉÒÔÉèÆäËûµÄÖµ.
8
9 PrintAnswer()
10 {
11 if [ "$answer" = TIMEOUT ]
12 then
13 echo $answer
14 else # ±ðÏë»ìºÏ×ÅÁ½¸öÀý×Ó.
15 echo "Your favorite veggie is $answer"
16 kill $! # kill½«²»ÔÙÐèÒªTimerOnº¯ÊýÔËÐÐÔÚºǫ́.
17 # $! ÊÇÔËÐÐÔÚºǫ́µÄ×îºóÒ»¸ö¹¤×÷µÄPID.
18 fi
19
20 }
21
22
23
24 TimerOn()
25 {
26 sleep $TIMELIMIT && kill -s 14 $$ &
27 # µÈ´ý3Ãë,È»ºó·¢ËÍÒ»¸öÐźŸø½Å±¾.
28 }
29
30 Int14Vector()
31 {
32 answer="TIMEOUT"
33 PrintAnswer
34 exit 14
35 }
36
37 trap Int14Vector 14 # ΪÁËÎÒÃǵÄÄ¿µÄ,ʱ¼äÖжÏ(14)±»ÆÆ»µÁË.
38
39 echo "What is your favorite vegetable "
40 TimerOn
41 read answer
42 PrintAnswer
43
44
45 # ºÜÃ÷ÏÔµÄ,ÕâÊÇÒ»¸öÆ´´ÕµÄʵÏÖ.
46 #+ È»¶øʹÓÃ"-t"Ñ¡ÏîÀ´"read"µÄ»°,½«»á¼ò»¯Õâ¸öÈÎÎñ.
47 # ¼û"t-out.sh",ÔÚϱß.
48
49 # Èç¹ûÄãÐèÒªÒ»¸öÕæÕýµÄÓÄÑŵÄд·¨...
50 #+ ½¨ÒéÄãʹÓÃc/c++À´Ð´Õâ¸öÓ¦ÓÃ,
51 #+ ʹÓúÏÊʵĿâÀ´Íê³ÉÕâ¸öÈÎÎñ,±ÈÈç'alarm'ºÍ'setitimer'.
52
53 exit 0
################################End Script######################################
###
ʹÓÃsttyÒ²ÊÇÒ»ÖÖÑ¡Ôñ.
Example 9-3 ÔÙÀ´Ò»¸öʱ¼äÊäÈë
################################Start Script####################################
###
1 #!/bin/bash
2 # timeout.sh
3
4 # Stephane Chazelas±àд,
5 #+ ±¾Êé×÷Õß½øÐÐÁËһЩÐÞ¸Ä.
6
7 INTERVAL=5 # timeout¼ä¸ô
8
9 timedout_read() {
10 timeout=$1
11 varname=$2
12 old_tty_settings=`stty -g`
13 stty -icanon min 0 time ${timeout}0
14 eval read $varname # »òÕß¾ÍÊÇ read $varname
15 stty "$old_tty_settings"
16 # ²ì¿´"stty"µÄmanÒ³.
17 }
18
19 echo; echo -n "What's your name? Quick! "
20 timedout_read $INTERVAL your_name
21
22 # ÕâÖÖ·½·¨¿ÉÄܲ»ÊÇÿ¸öÖÕ¶ËÀàÐͶ¼¿ÉÒÔÕý³£Ê¹ÓõÄ.
23 # ×î´óµÄtimeoutÒÀÀµÓÚ¾ßÌåµÄÖÕ¶Ë.
24 #+ (Ò»°ã¶¼ÊÇ25.5Ãë).
25
26 echo
27
28 if [ ! -z "$your_name" ] # If name input before timeout...
29 then
30 echo "Your name is $your_name."
31 else
32 echo "Timed out."
33 fi
34
35 echo
36
37 # Õâ¸ö½Å±¾µÄÐÐΪ¿ÉÄÜÓë"timed-input.sh"Óе㲻ͬ.
38 # ÔÚÿ´Î°´¼üµÄʱºò,¼ÆÊýÆ÷¶¼»áÖØÖÃ.
39
40 exit 0
################################End Script######################################
###
»òÐí,×î¼òµ¥µÄ°ì·¨¾ÍÊÇʹÓÃ-tÑ¡ÏîÀ´readÁË.
Example 9-4 Timed read
################################Start Script####################################
###
1 #!/bin/bash
2 # t-out.sh
3 # "syngin seven"µÄÒ»¸öºÜºÃµÄÌáÒé (thanks).
4
5
6 TIMELIMIT=4 # 4 seconds
7
8 read -t $TIMELIMIT variable <&1
9 # ^^^
10 # ÔÚÕâ¸öÀý×ÓÖÐ,¶ÔÓÚBash 1.xºÍ2.x¾ÍÐèҪʹÓÃ"<&1"
11 # µ«¶ÔÓÚBash 3.x¾Í²»ÐèÒª.
12
13 echo
14
15 if [ -z "$variable" ] # Is null?
16 then
17 echo "Timed out, variable still unset."
18 else
19 echo "variable = $variable"
20 fi
21
22 exit 0
################################End Script######################################
###
$UID
Óû§IDºÅ.
µ±Ç°Óû§µÄidºÅ,ÔÚ/etc/passwdÖмǼ.
Õâ¸öÖµ²»»áÒòΪÓû§Ê¹ÓÃÁËsuÃüÁî¶ø¸Ä±ä.$UIDÊÇÖ»¶Á±äÁ¿,²»ÈÝÒ×ÔÚÃüÁîÐлòÕßÊǽÅ
±¾Öб»ÐÞ¸Ä,²¢ÇÒºÍÄÚ½¨µÄidÃüÁîºÜÏàÏñ.
Example 9-5 ÎÒÊÇroot?
################################Start Script####################################
###
1 #!/bin/bash
2 # am-i-root.sh: ÎÒÊDz»ÊÇrootÓû§?
3
4 ROOT_UID=0 # RootµÄ$UIDÊÇ0.
5
6 if [ "$UID" -eq "$ROOT_UID" ] # ÊÇ·ñÊÇrootÓû§,ÇëÕ¾³öÀ´.
7 then
8 echo "You are root."
9 else
10 echo "You are just an ordinary user (but mom loves you just the same)."
11 fi
12
13 exit 0
14
15
16 # ============================================================= #
17 # ϱߵĴúÂ뽫²»±»Ö´ÐÐ,ÒòΪ½Å±¾ÒѾ­Í˳öÁË.
18
19 # ¼ìÑéÊÇrootÓû§µÄÒ»ÖÖ¿ÉÑ¡·½·¨:
20
21 ROOTUSER_NAME=root
22
23 username=`id -nu` # Or... username=`whoami`
24 if [ "$username" = "$ROOTUSER_NAME" ]
25 then
26 echo "Rooty, toot, toot. You are root."
27 else
28 echo "You are just a regular fella."
29 fi
################################End Script######################################
###
¼ûÀý×ÓExample 2-3
×¢Òâ:±äÁ¿$ENV,$LOGNAME,$MAIL,$TERM,$USER,ºÍ$USERNAME²¢²»ÊÇBashµÄÄÚ½¨±äÁ¿.Ëü
ÃǾ­³£±»ÉèÖóɻ·¾³±äÁ¿,ËüÃÇÒ»°ã¶¼·ÅÔÚBashµÄ°²×°ÎļþÖÐ.$SHELL,Óû§µÇ¼µÄ
shellµÄÃû×Ö,¿ÉÄÜÊÇ´Ó/etc/passwdÉèÖõÄ,Ò²¿ÉÄÜÊÇÔÚÒ»¸ö"init"½Å±¾ÖÐÉèÖõÄ,ͬÑù
µÄ,ËüÒ²²»ÊÇBashµÄÄÚ½¨±äÁ¿.
tcsh% echo $LOGNAME
bozo
tcsh% echo $SHELL
/bin/tcsh
tcsh% echo $TERM
rxvt
bash$ echo $LOGNAME
bozo
bash$ echo $SHELL
/bin/tcsh
bash$ echo $TERM
rxvt
λÖòÎÊý
$0, $1, $2,µÈµÈ...
λÖòÎÊý,´ÓÃüÁîÐд«µÝ¸ø½Å±¾,»òÕßÊÇ´«µÝ¸øº¯Êý.»òÕ߸³Ö°¸øÒ»¸ö±äÁ¿.
(¾ßÌå¼ûExample 4-5ºÍExample 11-15)
$#
ÃüÁîÐлòÕßÊÇλÖòÎÊýµÄ¸öÊý.(¼ûExample 33-2)
$*
ËùÓеÄλÖòÎÊý,±»×÷Ϊһ¸öµ¥´Ê.
×¢Òâ:"$*"±ØÐë±»""ÒýÓÃ.
$@
Óë$*ͬÒå,µ«ÊÇÿ¸ö²ÎÊý¶¼ÊÇÒ»¸ö¶ÀÁ¢µÄ""ÒýÓÃ×Ö´®,Õâ¾ÍÒâζ×ŲÎÊý±»ÍêÕûµØ´«µÝ,
²¢Ã»Óб»½âÊͺÍÀ©Õ¹.ÕâÒ²Òâζ×Å,ÿ¸ö²ÎÊýÁбíÖеÄÿ¸ö²ÎÊý¶¼±»µ±³ÉÒ»¸ö¶ÀÁ¢µÄ
µ¥´Ê.
×¢Òâ:"$@"±ØÐë±»ÒýÓÃ.
Example 9-6 arglist:ͨ¹ý$*ºÍ$@ÁгöËùÓеIJÎÊý
################################Start Script####################################
###
1 #!/bin/bash
2 # arglist.sh
3 # ¶àʹÓü¸¸ö²ÎÊýÀ´µ÷ÓÃÕâ¸ö½Å±¾,±ÈÈç"one tow three".
4
5 E_BADARGS=65
6
7 if [ ! -n "$1" ]
8 then
9 echo "Usage: `basename $0` argument1 argument2 etc."
10 exit $E_BADARGS
11 fi
12
13 echo
14
15 index=1 # ³õʼ»¯ÊýÁ¿.
16
17 echo "Listing args with \"\$*\":"
18 for arg in "$*" # Èç¹û"$*"²»±»""ÒýÓÃ,ÄÇô½«²»ÄÜÕý³£µØ¹¤×÷
19 do
20 echo "Arg #$index = $arg"
21 let "index+=1"
22 done # $* sees all arguments as single word.
22 done # $* ÈÏΪËùÓеIJÎÊýΪһ¸öµ¥´Ê
23 echo "Entire arg list seen as single word."
24
25 echo
26
27 index=1 # ÖØÖÃÊýÁ¿.
28 # Èç¹ûÄãÍüÁËÕâ¾ä»á·¢Éúʲô?
29
30 echo "Listing args with \"\$@\":"
31 for arg in "$@"
32 do
33 echo "Arg #$index = $arg"
34 let "index+=1"
35 done # $@ ÈÏΪÿ¸ö²ÎÊý¶¼Ò»¸öµ¥¶ÀµÄµ¥´Ê.
36 echo "Arg list seen as separate words."
37
38 echo
39
40 index=1 # ÖØÖÃÊýÁ¿.
41
42 echo "Listing args with \$* (unquoted):"
43 for arg in $*
44 do
45 echo "Arg #$index = $arg"
46 let "index+=1"
47 done # δ""ÒýÓõÄ$*°Ñ²ÎÊý×÷Ϊ¶ÀÁ¢µÄµ¥´Ê.
48 echo "Arg list seen as separate words."
49
50 exit 0
################################End Script######################################
###
ÔÚshiftÃüÁîºó±ß,$@½«±£´æÃüÁîÐÐÖÐÊ£ÓàµÄ²ÎÊý,¶ø$1±»¶ªµôÁË.
1 #!/bin/bash
2 # ʹÓà ./scriptname 1 2 3 4 5 À´µ÷ÓÃÕâ¸ö½Å±¾
3
4 echo "$@" # 1 2 3 4 5
5 shift
6 echo "$@" # 2 3 4 5
7 shift
8 echo "$@" # 3 4 5
9
10 # ÿ¸ö"shift"¶¼¶ªÆú$1.
11 # "$@" ½«°üº¬Ê£ÏµIJÎÊý.
$@Ò²×÷ΪΪ¹¤¾ßʹÓÃ,ÓÃÀ´¹ýÂË´«¸ø½Å±¾µÄÊäÈë.
cat "$@"½á¹¹½ÓÊÜ´Óstdin´«À´µÄÊäÈë,Ò²½ÓÊÜ´Ó²ÎÊýÖÐÖ¸¶¨µÄÎļþ´«À´µÄÊäÈë.
¾ßÌå¼ûExample 12-21ºÍExample 12-22.
×¢Òâ:$*ºÍ$@µÄ²ÎÊýÓÐʱ»á²»Ò»ÖÂ,·¢ÉúÁîÈËÃÔ»óµÄÐÐΪ,ÕâÒÀÀµÓÚ$IFSµÄÉèÖÃ.
Example 9-7 ²»Ò»ÖµÄ$*ºÍ$@ÐÐΪ
################################Start Script####################################
###
1 #!/bin/bash
2
3 # "$*"ºÍ"$@"µÄ¹Å¹ÖÐÐΪ,
4 #+ ÒÀÀµÓÚËüÃÇÊÇ·ñ±»""ÒýÓÃ.
5 # µ¥´Ê²ð·ÖºÍ»»ÐеIJ»Ò»Ö´¦Àí.
6
7
8 set -- "First one" "second" "third:one" "" "Fifth: :one"
9 # ÉèÖÃÕâ¸ö½Å±¾²ÎÊý,$1,$2,µÈµÈ.
10
11 echo
12
13 echo 'IFS unchanged, using "$*"'
14 c=0
15 for i in "$*" # ÒýÓÃ
16 do echo "$((c+=1)): [$i]" # ÕâÐÐÔÚϱߵÄÿ¸öÀý×ÓÖж¼Ò»Ñù.
17 # Echo²ÎÊý.
18 done
19 echo ---
20
21 echo 'IFS unchanged, using $*'
22 c=0
23 for i in $* # δÒýÓÃ
24 do echo "$((c+=1)): [$i]"
25 done
26 echo ---
27
28 echo 'IFS unchanged, using "$@"'
29 c=0
30 for i in "$@"
31 do echo "$((c+=1)): [$i]"
32 done
33 echo ---
34
35 echo 'IFS unchanged, using $@'
36 c=0
37 for i in $@
38 do echo "$((c+=1)): [$i]"
39 done
40 echo ---
41
42 IFS=:
43 echo 'IFS=":", using "$*"'
44 c=0
45 for i in "$*"
46 do echo "$((c+=1)): [$i]"
47 done
48 echo ---
49
50 echo 'IFS=":", using $*'
51 c=0
52 for i in $*
53 do echo "$((c+=1)): [$i]"
54 done
55 echo ---
56
57 var=$*
58 echo 'IFS=":", using "$var" (var=$*)'
59 c=0
60 for i in "$var"
61 do echo "$((c+=1)): [$i]"
62 done
63 echo ---
64
65 echo 'IFS=":", using $var (var=$*)'
66 c=0
67 for i in $var
68 do echo "$((c+=1)): [$i]"
69 done
70 echo ---
71
72 var="$*"
73 echo 'IFS=":", using $var (var="$*")'
74 c=0
75 for i in $var
76 do echo "$((c+=1)): [$i]"
77 done
78 echo ---
79
80 echo 'IFS=":", using "$var" (var="$*")'
81 c=0
82 for i in "$var"
83 do echo "$((c+=1)): [$i]"
84 done
85 echo ---
86
87 echo 'IFS=":", using "$@"'
88 c=0
89 for i in "$@"
90 do echo "$((c+=1)): [$i]"
91 done
92 echo ---
93
94 echo 'IFS=":", using $@'
95 c=0
96 for i in $@
97 do echo "$((c+=1)): [$i]"
98 done
99 echo ---
100
101 var=$@
102 echo 'IFS=":", using $var (var=$@)'
103 c=0
104 for i in $var
105 do echo "$((c+=1)): [$i]"
106 done
107 echo ---
108
109 echo 'IFS=":", using "$var" (var=$@)'
110 c=0
111 for i in "$var"
112 do echo "$((c+=1)): [$i]"
113 done
114 echo ---
115
116 var="$@"
117 echo 'IFS=":", using "$var" (var="$@")'
118 c=0
119 for i in "$var"
120 do echo "$((c+=1)): [$i]"
121 done
122 echo ---
123
124 echo 'IFS=":", using $var (var="$@")'
125 c=0
126 for i in $var
127 do echo "$((c+=1)): [$i]"
128 done
129
130 echo
131
132 # ÓÃksh»òÕßzsh -yÀ´ÊÔÊÔÕâ¸ö½Å±¾.
133
134 exit 0
135
136 # This example script by Stephane Chazelas,
137 # and slightly modified by the document author.
################################End Script######################################
###
×¢Òâ:$@ºÍ$*ÖеIJÎÊýÖ»ÓÐÔÚ""ÖвŻ᲻ͬ.
Example 9-8 µ±$IFSΪ¿ÕʱµÄ$*ºÍ$@
################################Start Script####################################
###
1 #!/bin/bash
2
3 # Èç¹û$IFS±»ÉèÖÃΪ¿Õʱ,
4 #+ ÄÇô"$*" ºÍ"$@" ½«²»»áÏóÆÚÍûÄÇÑùecho³öλÖòÎÊý.
5
6 mecho () # Echo λÖòÎÊý.
7 {
8 echo "$1,$2,$3";
9 }
10
11
12 IFS="" # ÉèÖÃΪ¿Õ.
13 set a b c # λÖòÎÊý.
14
15 mecho "$*" # abc,,
16 mecho $* # a,b,c
17
18 mecho $@ # a,b,c
19 mecho "$@" # a,b,c
20
21 # µ±$IFSÉèÖÃΪ¿Õʱ,$* ºÍ$@ µÄÐÐΪÒÀÀµÓÚ
22 #+ ÕýÔÚÔËÐеÄBash»òÕßshµÄ°æ±¾.
23 # ËùÒÔÔڽű¾ÖÐʹÓÃÕâÖÖ"feature"²»ÊÇÃ÷ÖǵÄÐÐΪ.
24
25
26 # Thanks, Stephane Chazelas.
27
28 exit 0
################################End Script######################################
###
ÆäËûµÄÌØÊâ²ÎÊý
$-
´«µÝ¸ø½Å±¾µÄfalg(ʹÓÃsetÃüÁî).²Î¿¼Example 11-15.
×¢Òâ:ÕâÆð³õÊÇkshµÄÌØÕ÷,ºóÀ´±»Òý½øµ½BashÖÐ,µ«²»ÐÒµÄÊÇ,ÔÚBashÖÐËü¿´ÉÏÈ¥Ò²²»
ÄÜ¿É¿¿µÄ¹¤×÷.ʹÓÃËüµÄÒ»¸ö¿ÉÄܵķ½·¨¾ÍÊÇÈÃÕâ¸ö½Å±¾½øÐÐ×ÔÎÒ²âÊÔ(²é¿´ÊÇ·ñÊǽ»
»¥µÄ).
$!
ÔÚºǫ́ÔËÐеÄ×îºóµÄ¹¤×÷µÄPID(½ø³ÌID).
1 LOG=$0.log
2
3 COMMAND1="sleep 100"
4
5 echo "Logging PIDs background commands for script: $0" >> "$L
OG"
6 # ËùÒÔËüÃÇ¿ÉÒÔ±»¼à¿Ø,²¢ÇÒÔÚ±ØÒªµÄʱºòkillµô.
7 echo >> "$LOG"
8
9 # Logging ÃüÁî.
10
11 echo -n "PID of \"$COMMAND1\": " >> "$LOG"
12 ${COMMAND1} &
13 echo $! >> "$LOG"
14 # PID of "sleep 100": 1506
15
16 # Thank you, Jacques Lederer, for suggesting this.

1 possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &


> /dev/null; }
2 # Ç¿ÖƽáÊøÒ»¸öÆ·Ðв»Á¼µÄ³ÌÐò.
3 # ºÜÓÐÓÃ,±ÈÈçÔÚinit½Å±¾ÖÐ.
4
5 # Thank you,Sylvain Fourmanoit,for this creative use of the "!
" variable.
$_
±£´æ֮ǰִÐеÄÃüÁîµÄ×îºóÒ»¸ö²ÎÊý.
Example 9-9 Ï»®Ïß±äÁ¿
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo $_ # /bin/bash
4 # Ö»Êǵ÷ÓÃ/bin/bashÀ´ÔËÐÐÕâ¸ö½Å±¾.
5
6 du >/dev/null # ½«Ã»ÓÐÃüÁîµÄÊä³ö
7 echo $_ # du
8
9 ls -al >/dev/null # ûÓÐÃüÁîÊä³ö
10 echo $_ # -al (×îºóµÄ²ÎÊý)
11
12 :
13 echo $_ # :
################################End Script######################################
###
$?
ÃüÁî,º¯Êý»òÕ߽ű¾±¾ÉíµÄÍ˳ö״̬(¼ûExample 23-7)
$$
½Å±¾×ÔÉíµÄ½ø³ÌID.Õâ¸ö±äÁ¿¾­³£ÓÃÀ´¹¹ÔìÒ»¸ö"unique"µÄÁÙʱÎļþÃû.
(²Î¿¼Example A-13,Example 29-6,Example 12-28ºÍExample 11-25).
Õâͨ³£±Èµ÷ÓÃmktempÀ´µÃ¼òµ¥.
×¢ÒâÊÂÏî:
[1] µ±Ç°ÔËÐеĽű¾µÄPIDΪ$$.
[2] "argument"ºÍ"parameter"ÕâÁ½¸öµ¥´Ê¾­³£²»¼ÓÇø·ÖµÄʹÓÃ.ÔÚÕâÕû±¾ÊéÖÐ,ÕâÁ½¸ö
µ¥´ÊµÄÒâ˼ÍêÈ«Ïàͬ.(ÔÚ·­ÒëµÄʱºò¾Íδ¼ÓÇø·Ö,ͳͳ·­Òë³É²ÎÊý)

9.2 ²Ù×÷×Ö·û´®
--------------
BashÖ§³Ö³¬¶àµÄ×Ö·û´®²Ù×÷,²Ù×÷µÄÖÖÀàºÍÊýÁ¿ÁîÈ˾ªÒì.µ«²»ÐÒµÄÊÇ,ÕâЩ¹¤¾ßȱ·¦¼¯ÖÐÐÔ.
һЩÊDzÎÊýÌæ»»µÄ×Ó¼¯,µ«ÊÇÁíһЩÔòÊôÓÚUNIXµÄexprÃüÁî.Õâ¾Íµ¼ÖÂÁËÃüÁîÓï·¨µÄ²»Ò»ÖºÍ
¹¦ÄܵÄÖصþ,µ±È»Ò²»áÒýÆð»ìÂÒ.
×Ö·û´®³¤¶È
${#string}
expr length $string
expr "$string" : '.*'
1 stringZ=abcABC123ABCabc
2
3 echo ${#stringZ} # 15
4 echo `expr length $stringZ` # 15
5 echo `expr "$stringZ" : '.*'` # 15
Example 9-10 ÔÚÒ»¸öÎı¾ÎļþµÄ¶Î¼ä²åÈë¿ÕÐÐ
################################Start Script####################################
###
1 #!/bin/bash
2 # paragraph-space.sh
3
4 # ÔÚÒ»¸ö²»¿ÕÐеÄÎı¾ÎļþµÄ¶Î¼ä²åÈë¿ÕÐÐ.
5 # Usage: $0 <FILENAME
6
7 MINLEN=45 # ¿ÉÄÜÐèÒªÐÞ¸ÄÕâ¸öÖµ.
8 # ¼Ù¶¨Ðеij¤¶ÈСÓÚ$MINLENÖ¸¶¨µÄ³¤¶È
9 #+ $MINLENÖеÄÖµÓÃÀ´ÃèÊö¶àÉÙ¸ö×Ö·û½áÊøÒ»¸ö¶Î.
10
11 while read line # ¶ÔÓÚÐèÒª¶àÐÐÊäÈëµÄÎļþ»ù±¾¶¼ÊÇÕâ¸öÑù×Ó
12 do
13 echo "$line" # Êä³öline.
14
15 len=${#line}
16 if [ "$len" -lt "$MINLEN" ]
17 then echo # ÔÚ¶ÌÐкó±ßÌí¼ÓÒ»¸ö¿ÕÐÐ
18 fi
19 done
20
21 exit 0
################################End Script######################################
###
´Ó×Ö·û´®¿ªÊ¼µÄλÖÃÆ¥Åä×Ó´®µÄ³¤¶È
expr match "$string" '$substring'
$substringÊÇÒ»¸öÕýÔò±í´ïʽ
expr "$string" : '$substring'
$substringÊÇÒ»¸öÕýÔò±í´ïʽ
1 stringZ=abcABC123ABCabc
2 # |------|
3
4 echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
5 echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8
Ë÷Òý
expr index $string $substring
Æ¥Åäµ½×Ó´®µÄµÚÒ»¸ö×Ö·ûµÄλÖÃ.
1 stringZ=abcABC123ABCabc
2 echo `expr index "$stringZ" C12` # 6
3 # C position.
4
5 echo `expr index "$stringZ" 1c` # 3
6 # 'c' (in #3 position) matches before '1'.
ÔÚCÓïÑÔÖÐ×î½üµÄµÈ¼Ûº¯ÊýΪstrchr().
ÌáÈ¡×Ó´®
${string:position}
ÔÚstringÖдÓλÖÃ$position¿ªÊ¼ÌáÈ¡×Ó´®.
Èç¹û$stringΪ"*"»ò"@",ÄÇô½«ÌáÈ¡´ÓλÖÃ$position¿ªÊ¼µÄλÖòÎÊý,[1]
${string:position:length}
ÔÚstringÖдÓλÖÃ$position¿ªÊ¼ÌáÈ¡$length³¤¶ÈµÄ×Ó´®.
################################Start Script####################################
###
1 stringZ=abcABC123ABCabc
2 # 0123456789.....
3 # 0-based indexing.
4
5 echo ${stringZ:0} # abcABC123ABCabc
6 echo ${stringZ:1} # bcABC123ABCabc
7 echo ${stringZ:7} # 23ABCabc
8
9 echo ${stringZ:7:3} # 23A
10 # 3¸ö×Ö·û³¤¶ÈµÄ×Ó´®.
11
12
13
14 # ÓÐûÓпÉÄÜ´Ó×Ö·û½áβ¿ªÊ¼,·´ÏòÌáÈ¡×Ó´®?
15
16 echo ${stringZ:-4} # abcABC123ABCabc
17 # ÒÔ${parameter:-default}·½Ê½,ĬÈÏÊÇÌáÈ¡ÍêÕûµØ×Ö·û´®.
18 # È»¶ø . . .
19
20 echo ${stringZ:(-4)} # Cabc
21 echo ${stringZ: -4} # Cabc
22 # ÏÖÔÚ,Ëü¿ÉÒÔ¹¤×÷ÁË.
23 # ʹÓÃÔ²À¨ºÅ»òÕßÌí¼ÓÒ»¸ö¿Õ¸ñÀ´×ªÒåÕâ¸öλÖòÎÊý.
24
25 # Thank you, Dan Jacobson, for pointing this out.
################################End Script######################################
###
Èç¹û$string²ÎÊýΪ"*"»ò"@",Äǽ«×î´óµÄÌáÈ¡´Ó$position¿ªÊ¼µÄ$length¸öλÖòÎÊý.
1 echo ${*:2} # Echo³öµÚ2¸öºÍºó±ßËùÓеÄλÖòÎÊý.
2 echo ${@:2} # ÓëÇ°±ßÏàͬ.
3
4 echo ${*:2:3} # ´ÓµÚ2¸ö¿ªÊ¼,Echo³öºó±ß3¸öλÖòÎÊý.

expr substr $string $position $length


ÔÚstringÖдÓλÖÃ$position¿ªÊ¼ÌáÈ¡$length³¤¶ÈµÄ×Ó´®.
1 stringZ=abcABC123ABCabc
2 # 123456789......
3 # 1-based indexing.
4
5 echo `expr substr $stringZ 1 2` # ab
6 echo `expr substr $stringZ 4 3` # ABC
expr match "$string" '\($substring\)'
´Ó$stringµÄ¿ªÊ¼Î»ÖÃÌáÈ¡$substring,$substringÊÇÒ»¸öÕýÔò±í´ïʽ.
expr "$string" : '\($substring\)'
´Ó$stringµÄ¿ªÊ¼Î»ÖÃÌáÈ¡$substring,$substringÊÇÒ»¸öÕýÔò±í´ïʽ.
1 stringZ=abcABC123ABCabc
2 # =======
3
4 echo `expr match "$stringZ" '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1
5 echo `expr "$stringZ" : '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1
6 echo `expr "$stringZ" : '\(.......\)'` # abcABC1
7 # All of the above forms give an identical result.
×Ó´®Ï÷³ý
${string#substring}
´Ó$stringµÄ×ó±ß½ØµôµÚÒ»¸öÆ¥ÅäµÄ$substring
${string##substring}
´Ó$stringµÄ×ó±ß½Øµô×îºóÒ»¸ö¸öÆ¥ÅäµÄ$substring
1 stringZ=abcABC123ABCabc
2 # |----|
3 # |----------|
4
5 echo ${stringZ#a*C} # 123ABCabc
6 # ½Øµô'a'ºÍ'C'Ö®¼ä×î½üµÄÆ¥Åä.
7
8 echo ${stringZ##a*C} # abc
9 # ½Øµô'a'ºÍ'C'Ö®¼ä×îÔ¶µÄÆ¥Åä.

${string%substring}
´Ó$stringµÄÓұ߽صôµÚÒ»¸öÆ¥ÅäµÄ$substring
${string%%substring}
´Ó$stringµÄÓұ߽صô×îºóÒ»¸öÆ¥ÅäµÄ$substring
1 stringZ=abcABC123ABCabc
2 # ||
3 # |------------|
4
5 echo ${stringZ%b*c} # abcABC123ABCa
6 # ´Ó$stringZµÄºó±ß¿ªÊ¼½Øµô'b'ºÍ'c'Ö®¼äµÄ×î½üµÄÆ¥Åä
7
8 echo ${stringZ%%b*c} # a
9 # ´Ó$stringZµÄºó±ß¿ªÊ¼½Øµô'b'ºÍ'c'Ö®¼äµÄ×îÔ¶µÄÆ¥Åä
Example 9-11 ÀûÓÃÐÞ¸ÄÎļþÃû,À´×ª»»Í¼Æ¬¸ñʽ
################################Start Script####################################
###
1 #!/bin/bash
2 # cvt.sh:
3 # °ÑÒ»¸öĿ¼ÏµÄËùÓÐMacPaint¸ñʽµÄͼƬÎļþ¶¼×ª»»Îª"pbm"¸ñʽµÄͼƬÎļþ.
4
5 # ʹÓÃÀ´×Ô"netpbm"°üµÄ"macptopbm"³ÌÐò,
6 #+ Õâ¸ö³ÌÐòÖ÷ÒªÊÇÓÉBrian Henderson(bryanh@giraffe-data.com)À´Î¬»¤µÄ.
7 # NetpbmÊÇ´ó¶àÊýLinux·¢ÐаæµÄ±ê×¼²¿·Ö.
8
9 OPERATION=macptopbm
10 SUFFIX=pbm # еÄÎļþÃûºó׺
11
12 if [ -n "$1" ]
13 then
14 directory=$1 # Èç¹ûĿ¼Ãû×÷ΪµÚ1¸ö²ÎÊý¸ø³ö...
15 else
16 directory=$PWD # ·ñÔòʹÓõ±Ç°µÄ¹¤×÷Ŀ¼.
17 fi
18
19 # ¼ÙÉèÔÚÄ¿±êĿ¼ÖеÄËùÓÐÎļþ¶¼ÊÇMacPaint¸ñʽµÄͼƬÎļþ,
20 #+ ÒÔ".mac"ΪÎļþÃûµÄºó׺.
21
22 for file in $directory/* # Filename globbing.
23 do
24 filename=${file%.*c} # È¥µôÎļþÃûµÄ".mac"ºó׺
25 #+ ('.*c' matches everything
25 #+ ('.*c' ½«Æ¥Åä'.'ºÍ'c'Ö®¼äµÄÈκÎ×Ö·û´®).
26
27 $OPERATION $file > "$filename.$SUFFIX"
28 # ת»»ÎªÐµÄÎļþÃû.
29 rm -f $file # ת»»Íê±Ïºóɾ³ýÔ­ÓеÄÎļþ.
30 echo "$filename.$SUFFIX" # ´ÓstdoutÊä³ö·´À¡.
31 done
32
33 exit 0
34
35 # Á·Ï°:
36 # --------
37 # ¾ÍÏñËüÏÖÔÚÕâ¸öÑù×Ó,Õâ¸ö½Å±¾°Ñµ±Ç°Ä¿Â¼µÄËùÓÐÎļþ¶¼×ª»»ÁË.
38 #
39 # ÐÞ¸ÄÕâ¸ö½Å±¾,ÈÃËûֻת»»ÒÔ".mac"Ϊºó׺µÄÎļþ.
################################End Script######################################
###
Ò»¸ö¼òµ¥µÄÄ£ÄâgetoptÃüÁîµÄ°ì·¨¾ÍÊÇʹÓÃ×Ó´®ÌáÈ¡½á¹¹.
Example 9-12 Ä£·ÂgetoptÃüÁî
################################Start Script####################################
###
1 #!/bin/bash
2 # getopt-simple.sh
3 # Author: Chris Morgan
4 # ÊÚȨʹÓÃÔÚABS GuideÖÐ.
5
6
7 getopt_simple()
8 {
9 echo "getopt_simple()"
10 echo "Parameters are '$*'"
11 until [ -z "$1" ]
12 do
13 echo "Processing parameter of: '$1'"
14 if [ ${1:0:1} = '/' ]
15 then
16 tmp=${1:1} # È¥µô¿ªÍ·µÄ'/' . . .
17 parameter=${tmp%%=*} # ÌáÈ¡Ãû×Ö.
18 value=${tmp##*=} # ÌáÈ¡Öµ.
19 echo "Parameter: '$parameter', value: '$value'"
20 eval $parameter=$value
21 fi
22 shift
23 done
24 }
25
26 # ´«µÝËùÓеÄÑ¡Ïîµ½getopt_simple().
27 getopt_simple $*
28
29 echo "test is '$test'"
30 echo "test2 is '$test2'"
31
32 exit 0
33
34 ---
35
36 sh getopt_example.sh /test=value1 /test2=value2
37
38 Parameters are '/test=value1 /test2=value2'
39 Processing parameter of: '/test=value1'
40 Parameter: 'test', value: 'value1'
41 Processing parameter of: '/test2=value2'
42 Parameter: 'test2', value: 'value2'
43 test is 'value1'
44 test2 is 'value2'
################################End Script######################################
###
×Ó´®Ìæ»»
${string/substring/replacement}
ʹÓÃ$replacementÀ´Ìæ»»µÚÒ»¸öÆ¥ÅäµÄ$substring.
${string//substring/replacement}
ʹÓÃ$replacementÀ´Ìæ»»ËùÓÐÆ¥ÅäµÄ$substring.
1 stringZ=abcABC123ABCabc
2
3 echo ${stringZ/abc/xyz} # xyzABC123ABCabc
4 # ÓÃ'xyz'À´Ìæ»»µÚÒ»¸öÆ¥ÅäµÄ'abc'.
5
6 echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
7 # ÓÃ'xyz'À´Ìæ»»ËùÓÐÆ¥ÅäµÄ'abc'.
${string/#substring/replacement}
Èç¹û$substringÆ¥Åä$stringµÄ¿ªÍ·²¿·Ö,ÄÇô¾ÍÓÃ$replacementÀ´Ìæ»»$substring.
${string/%substring/replacement}
Èç¹û$substringÆ¥Åä$stringµÄ½áβ²¿·Ö,ÄÇô¾ÍÓÃ$replacementÀ´Ìæ»»$substring.
1 stringZ=abcABC123ABCabc
2
3 echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc
4 # ÓÃ'XYZ'Ìæ»»¿ªÍ·µÄ'abc'
5
6 echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ
7 # ÓÃ'XYZ'Ìæ»»½áβµÄ'abc'
9.2.1 ʹÓÃawkÀ´²Ù×÷×Ö·û´®
~~~~~~~~~~~~~~~~~~~~~~~~~
Bash½Å±¾Ò²¿ÉÒÔʹÓÃawkÀ´²Ù×÷×Ö·û´®.
Example 9-13 ÌáÈ¡×Ö·û´®µÄÒ»ÖÖ¿ÉÑ¡µÄ·½·¨
################################Start Script####################################
###
1 #!/bin/bash
2 # substring-extraction.sh
3
4 String=23skidoo1
5 # 012345678 Bash
6 # 123456789 awk
7 # ×¢Òâ,¶ÔÓÚawkºÍBashÀ´Ëµ,ËüÃÇʹÓõÄÊDz»Í¬µÄstringË÷Òýϵͳ:
8 # BashµÄµÚÒ»¸ö×Ö·ûÊÇ´Ó'0'¿ªÊ¼¼Ç¼µÄ.
9 # AwkµÄµÚÒ»¸ö×Ö·ûÊÇ´Ó'1'¿ªÊ¼¼Ç¼µÄ.
10
11 echo ${String:2:4} # λÖÃ3 (0-1-2), 4 ¸ö×Ö·û³¤
12 # skid
13
14 # awkÖеȼÛÓÚ${string:pos:length}µÄÃüÁîÊÇsubstr(string,pos,length).
15 echo | awk '
16 { print substr("'"${String}"'",3,4) # skid
17 }
18 '
19 # ʹÓÃÒ»¸ö¿ÕµÄ"echo"ͨ¹ý¹ÜµÀ¸øawkÒ»¸ö¼ÙµÄÊäÈë,
20 #+ ÕâÑù¿ÉÒÔ²»ÓÃÌṩһ¸öÎļþÃû.
21
22 exit 0
################################End Script######################################
###
9.2.2 ¸üÉîµÄÌÖÂÛ
~~~~~~~~~~~~~~~~
¹ØÓÚÔڽű¾ÖÐʹÓÃ×Ö·û´®¸üÉîµÄÌÖÂÛ,Çë²Î¿¼ 9.3½Ú,hºÍexprÃüÁîÁбíµÄÏà¹ØÕ½Ú.
¹ØÓڽű¾µÄÀý×Ó,¼û:
1 Example 12-9
2 Example 9-16
3 Example 9-17
4 Example 9-18
5 Example 9-20
×¢ÒâÊÂÏî:
[1] ÕâÊÊÓÃÓÚÃüÁîÐвÎÊýºÍº¯Êý²ÎÊý.

9.3 ²ÎÊýÌæ»»
------------
²Ù×÷ºÍÀ©Õ¹±äÁ¿
${parameter}
Óë$parameterÏàͬ,¾ÍÊÇparameterµÄÖµ.ÔÚÌض¨µÄÉÏÏÂÎÄÖÐ,Ö»ÓÐÉÙ²¿·Ö»á²úÉú
${parameter}µÄ»ìÏý.¿ÉÒÔ×éºÏÆðÀ´Ò»Æð¸³Ö¸¸ø×Ö·û´®±äÁ¿.
1 your_id=${USER}-on-${HOSTNAME}
2 echo "$your_id"
3 #
4 echo "Old \$PATH = $PATH"
5 PATH=${PATH}:/opt/bin #Add /opt/bin to $PATH for duration of
script.
6 echo "New \$PATH = $PATH"

${parameter-default},${parameter:-default}
Èç¹ûparameterû±»set,ÄÇô¾ÍʹÓÃdefault.
1 echo ${username-`whoami`}
2 # echo `whoami`µÄ½á¹û,Èç¹ûûset username±äÁ¿µÄ»°.
×¢Òâ:${parameter-default}ºÍ${parameter:-default}´ó²¿·ÖʱºòÊÇÏàͬµÄ.
¶îÍâµÄ":"ÔÚparameter±»ÉùÃ÷µÄʱºò(¶øÇÒ±»¸³¿ÕÖµ),»áÓÐһЩ²»Í¬.
################################Start Script####################################
###
1 #!/bin/bash
2 # param-sub.sh
3
4 # Ò»¸ö±äÁ¿ÊÇ·ñ±»ÉùÃ÷
5 #+ ½«»áÓ°ÏìĬÈÏÑ¡ÏîµÄ´¥·¢
6 #+ ÉõÖÁÓÚÕâ¸ö±äÁ¿±»ÉèΪ¿Õ.
7
8 username0=
9 echo "username0 has been declared, but is set to null."
10 echo "username0 = ${username0-`whoami`}"
11 # ½«²»»áecho.
12
13 echo
14
15 echo username1 has not been declared.
16 echo "username1 = ${username1-`whoami`}"
17 # ½«»áecho.
18
19 username2=
20 echo "username2 has been declared, but is set to null."
21 echo "username2 = ${username2:-`whoami`}"
22 # ^
23 # ½«»áechoÒòΪʹÓõÄÊÇ:-¶ø²»ÊÇ -.
24 # ºÍÇ°±ßµÄµÚÒ»¸öÀý×ӺúñȽÏÒ»ÏÂ.
25
26
27 #
28
29 # ÔÙÀ´Ò»¸ö:
30
31 variable=
32 # ±äÁ¿ÒѾ­±»ÉùÃ÷ÁË,µ«ÊDZ»ÉèÖÃΪ¿Õ.
33
34 echo "${variable-0}" # (no output)
35 echo "${variable:-1}" # 1
36 # ^
37
38 unset variable
39
40 echo "${variable-2}" # 2
41 echo "${variable:-3}" # 3
42
43 exit 0
################################End Script######################################
###
Èç¹û½Å±¾Öв¢Ã»Óд«ÈëÃüÁîÐвÎÊý,ÄÇôdefault parameter½«±»Ê¹ÓÃ.
1 DEFAULT_FILENAME=generic.data
2 filename=${1:-$DEFAULT_FILENAME}
3 # Èç¹ûûÓвÎÊý±»´«µÝ½øÀ´,ÄÇôϱߵÄÃüÁî¿ì½«²Ù×÷
4 #+ Îļþ"generic.data"
5 #
6 # ºóÐøÃüÁî.
ÁíÍâ²Î¼ûExample 3-4,Example 28-2,ºÍExample A-6.
Óë"ʹÓÃÒ»¸öÓëÁбíÀ´Ö§³ÖÒ»¸öĬÈϵÄÃüÁîÐвÎÊý"µÄ·½·¨Ïà±È½Ï.
${parameter=default},${parameter:=default}
Èç¹ûparameterδÉèÖÃ,ÄÇô¾ÍÉèÖÃΪdefault.
ÕâÁ½ÖÖ°ì·¨¾ø´ó¶àÊýʱºòÓ÷¨¶¼Ò»Ñù,Ö»ÓÐÔÚ$parameter±»ÉùÃ÷²¢ÉèÖÃΪ¿ÕµÄʱºò,
²Å»áÓÐÇø±ð,[1]ºÍÉϱߵÄÐÐΪһÑù.
1 echo ${username=`whoami`}
2 # Variable "username" is now set to `whoami`.
2 # ±äÁ¿"username"±»¸³ÖµÎª`whoami`.
${parameter+alt_value},${parameter:+alt_value}
Èç¹ûparameter±»setÁË,ÄǾÍʹÓÃalt_value,·ñÔò¾ÍʹÓÃnull×Ö·û´®.
ÕâÁ½ÖÖ°ì·¨¾ø´ó¶àÊýʱºòÓ÷¨¶¼Ò»Ñù,Ö»ÓÐÔÚ$parameter±»ÉùÃ÷²¢ÉèÖÃΪ¿ÕµÄʱºò,
»áÓÐÇø±ð,¼ûÏÂ.
################################Start Script####################################
###
1 echo "###### \${parameter+alt_value} ########"
2 echo
3
4 a=${param1+xyz}
5 echo "a = $a" # a =
6
7 param2=
8 a=${param2+xyz}
9 echo "a = $a" # a = xyz
10
11 param3=123
12 a=${param3+xyz}
13 echo "a = $a" # a = xyz
14
15 echo
16 echo "###### \${parameter:+alt_value} ########"
17 echo
18
19 a=${param4:+xyz}
20 echo "a = $a" # a =
21
22 param5=
23 a=${param5:+xyz}
24 echo "a = $a" # a =
25 # Óëa=${param5+xyz}Óв»Í¬µÄ½á¹û.
26
27 param6=123
28 a=${param6+xyz}
29 echo "a = $a" # a = xyz
################################End Script######################################
###
${parameter?err_msg}, ${parameter:?err_msg}
Èç¹ûparameter±»set,ÄǾÍÊÇÓÃsetµÄÖµ,·ñÔòprint err_msg.
ÕâÁ½ÖÖ°ì·¨¾ø´ó¶àÊýʱºòÓ÷¨¶¼Ò»Ñù,Ö»ÓÐÔÚ$parameter±»ÉùÃ÷²¢ÉèÖÃΪ¿ÕµÄʱºò,
»áÓÐÇø±ð,¼ûÉÏ.
Example 9-14 ʹÓòÎÊýÌæ»»ºÍerror messages
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ¼ì²éһЩϵͳµÄ»·¾³±äÁ¿.
4 # ÕâÊǸöºÃÏ°¹ß.
5 # ±ÈÈç,Èç¹û$USER(ÔÚconsoleÉϵÄÓû§Ãû)û±»set,
6 #+ ÄÇôϵͳ¾Í²»»áÈÏÄã.
7
8 : ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
9 echo
10 echo "Name of the machine is $HOSTNAME."
11 echo "You are $USER."
12 echo "Your home directory is $HOME."
13 echo "Your mail INBOX is located in $MAIL."
14 echo
15 echo "If you are reading this message,"
16 echo "critical environmental variables have been set."
17 echo
18 echo
19
20 # ------------------------------------------------------
21
22 # ${variablename?} ½á¹ûÒ²¿ÉÒÔÓÃÀ´
23 #+ ÔÚÒ»¸ö½Å±¾Öмì²é±äÁ¿ÊÇ·ñ±»set.
24
25 ThisVariable=Value-of-ThisVariable
26 # ×¢Òâ,˳±ãÌáÒ»ÏÂ,Õâ¸ö×Ö·û´®±äÁ¿¿ÉÄÜÔÚËüÃǵÄÃû×ÖÖлᱻÉèÖÃ
27 #+ ·Ç·¨×Ö·û
28 : ${ThisVariable?}
29 echo "Value of ThisVariable is $ThisVariable".
30 echo
31 echo
32
33
34 : ${ZZXy23AB?"ZZXy23AB has not been set."}
35 # Èç¹ûZZXy23ABû±»set,
36 #+ ÄÇôÕâ¸ö½Å±¾½«ÒÔÒ»¸öerror messageÖÕÖ¹.
37
38 # Äã¿ÉÒÔÖ¸¶¨´íÎóÏûÏ¢.
39 # : ${variablename?"ERROR MESSAGE"}
40
41
42 # ͬÑùµÄ½á¹û: dummy_variable=${ZZXy23AB?}
43 # dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."}
44 #
45 # echo ${ZZXy23AB?} >/dev/null
46
47 # ͬ"set -u"ÃüÁîÀ´±È½ÏÕâЩ¼ì²é±äÁ¿ÊÇ·ñ±»setµÄ·½·¨.
48 #
49
50
51
52 echo "You will not see this message, because script already terminated."
53
54 HERE=0
55 exit $HERE # Will NOT exit here.
56
57 # ÊÂʵÉÏ,Õâ¸ö½Å±¾½«·µ»ØÖµ1×÷ΪÍ˳ö״̬(echo $?).
################################End Script######################################
###
Example 9-15 ²ÎÊýÌæ»»ºÍ"usage"messages
################################Start Script####################################
###
1 #!/bin/bash
2 # usage-message.sh
3
4 : ${1?"Usage: $0 ARGUMENT"}
5 # Èç¹ûûÓÐÃüÁîÐвÎÊý,ÄÇô½Å±¾½«ÔÚ´Ë´¦Í˳ö.
6 #+ ²¢ÇÒ´ò³öÈçϵĴíÎóÏûÏ¢.
7 # usage-message.sh: 1: Usage: usage-message.sh ARGUMENT
8
9 echo "These two lines echo only if command-line parameter given."
10 echo "command line parameter = \"$1\""
11
12 exit 0 # Èç¹ûÓÐÃüÁîÐвÎÊý,ÄÇô½«ÔÚ´Ë´¦Í˳ö.
13
14 # ²âÊÔÕâ¸ö½Å±¾,µÚ1´Î²âÊÔ´ø²ÎÊý,µÚ2´Î²âÊÔ²»´ø²ÎÊý.
15 # Èç¹ûÓвÎÊý,ÄÇô"$?"¾ÍÊÇ0.
16 # Èç¹ûûÓÐ,ÄÇô"$?"¾ÍÊÇ1.
################################End Script######################################
###
²ÎÊýÌæ»»ºÍÀ©Õ¹
ϱߵıí´ïʽÊÇʹÓÃexpr×Ö·û´®Æ¥Åä²Ù×÷µÄ²¹³ä(¼ûExample 12-9).
ÕâЩÌض¨µÄʹÓ÷½·¨¾ø´ó¶àÊýÇé¿ö¶¼ÊÇÓÃÀ´·ÖÎöÎļþĿ¼Ãû.
±äÁ¿³¤¶È/×Ó´®É¾³ý
${#var}
×Ö·û´®³¤¶È($varµÄ×Ö·ûÊýÁ¿).¶ÔÓÚÒ»¸öÊý×é,${#array}ÊÇÊý×éÖеÚÒ»¸öÔªËصij¤¶È.
һЩÀýÍâ:
${#*}ºÍ${#@}½«¸ø³öλÖòÎÊýµÄ¸öÊý.
¶ÔÓÚÊý×éÀ´Ëµ${#array[*]}ºÍ${$#array[@]}½«¸ø³öÊý×éÔªËصĸöÊý.
Example 9-16 ±äÁ¿³¤¶È
################################Start Script####################################
###
1 #!/bin/bash
2 # length.sh
3
4 E_NO_ARGS=65
5
6 if [ $# -eq 0 ] # Õâ¸ödemo½Å±¾±ØÐëÓÐÃüÁîÐвÎÊý.
7 then
8 echo "Please invoke this script with one or more command-line arguments."
9 exit $E_NO_ARGS
10 fi
11
12 var01=abcdEFGH28ij
13 echo "var01 = ${var01}"
14 echo "Length of var01 = ${#var01}"
15 # ÏÖÔÚ,ÈÃÎÒÃÇÊÔÊÔÔÚÀï±ßǶÈëÒ»¸ö¿Õ¸ñ.
16 var02="abcd EFGH28ij"
17 echo "var02 = ${var02}"
18 echo "Length of var02 = ${#var02}"
19
20 echo "Number of command-line arguments passed to script = ${#@}"
21 echo "Number of command-line arguments passed to script = ${#*}"
22
23 exit 0
################################End Script######################################
###
${var#Pattern}, ${var##Pattern}
´Ó$var¿ªÍ·É¾³ý×î½ü»ò×îԶƥÅä$PatternµÄ×Ó´®.
À´×ÔExample A-7Àý×ÓµÄÒ»²¿·Ö.
1 # À´×Ô"days-between.sh"Àý×ÓµÄÒ»¸öº¯Êý.
2 # È¥µô´«µÝ½øÀ´µÄ²ÎÊý¿ªÍ·µÄ0.
3
4 strip_leading_zero () # È¥µô¿ªÍ·µÄ0
5 { #+ ´Ó´«µÝ½øÀ´µÄ²ÎÊýÖÐ.
6 return=${1#0} # "1"Ö¸µÄÊÇ"$1" -- ´«½øÀ´µÄ²ÎÊý.
7 } # "0"¾ÍÊÇÎÒÃÇÏë´Ó"$1"ÖÐɾ³ýµÄ×Ó´®.
ϱßÊÇManfred Schwarb's¶ÔÉϱߺ¯ÊýµÄÒ»¸ö¸Ä°æ.
1 strip_leading_zero2 () # È¥µô¿ªÍ·µÄ0,ÒòΪÈç¹û²»È¥µôµÄ»°
2 { # Bash½«»á°ÑÕâ¸öÖµ×÷Ϊ8½øÖƽâÊÍ.
3 shopt -s extglob # ´ò¿ªÀ©Õ¹globbing.
4 local val=${1##+(0)} # ʹÓþֲ¿±äÁ¿,Æ¥Åä×µÄÁ¬ÐøµÄ0.
5 shopt -u extglob # ´ò¿ªÀ©Õ¹globbing.
6 _strip_leading_zero2=${val:-0}
7 # Èç¹ûÊäÈëΪ0,ÄÇô·µ»Ø0À´´úÌæ"".
8 }
ÁíÒ»¸öÀý×Ó
1 echo `basename $PWD` # µ±Ç°¹¤×÷Ŀ¼µÄbasename.
2 echo "${PWD##*/}" # µ±Ç°¹¤×÷Ŀ¼µÄbasename.
3 echo
4 echo `basename $0` # ½Å±¾Ãû×Ö.
5 echo $0 # ½Å±¾Ãû×Ö.
6 echo "${0##*/}" # ½Å±¾Ãû×Ö.
7 echo
8 filename=test.data
9 echo "${filename##*.}" # data
${var%Pattern}, ${var%%Pattern}
´Ó$var½áβɾ³ý×î½ü»ò×îԶƥÅä$PatternµÄ×Ó´®.
Bash version2 Ìí¼ÓÁ˶îÍâµÄÑ¡Ïî.
Example 9-17 ²ÎÊýÌæ»»ÖеÄģʽƥÅä
################################Start Script####################################
###
1 #!/bin/bash
2 # patt-matching.sh
3
4 # ʹÓÃ# ## % %%À´½øÐвÎÊýÌæ»»²Ù×÷µÄģʽƥÅä.
5
6 var1=abcd12345abc6789
7 pattern1=a*c # * (ͨÅä·û) Æ¥Åäa - cÖ®¼äµÄÈκÎ×Ö·û.
8
9 echo
10 echo "var1 = $var1" # abcd12345abc6789
11 echo "var1 = ${var1}" # abcd12345abc6789
12 # (alternate form)
13 echo "Number of characters in ${var1} = ${#var1}"
14 echo
15
16 echo "pattern1 = $pattern1" # a*c (everything between 'a' and 'c')
17 echo "--------------"
18 echo '${var1#$pattern1} =' "${var1#$pattern1}" # d12345abc6789
19 # ×î¶ÌµÄ¿ÉÄÜÆ¥Åä, È¥µôabcd12345abc6789µÄÇ°3¸ö×Ö·û
20 # |-| ^^^
21 echo '${var1##$pattern1} =' "${var1##$pattern1}" # 6789
22 # ×îÔ¶µÄÆ¥Åä,È¥µôabcd12345abc6789µÄÇ°12¸ö×Ö·û.
23 # |----------| ^^^^
24
25 echo; echo; echo
26
27 pattern2=b*9 # 'b' µ½'9'Ö®¼äµÄÈκÎ×Ö·û
28 echo "var1 = $var1" # »¹ÊÇ abcd12345abc6789
29 echo
30 echo "pattern2 = $pattern2"
31 echo "--------------"
32 echo '${var1%pattern2} =' "${var1%$pattern2}" # abcd12345a
33 # ×î½üµÄÆ¥Åä, È¥µôabcd12345abc6789µÄ×îºó6¸ö×Ö·û
34 # |----| ^^^^
35 echo '${var1%%pattern2} =' "${var1%%$pattern2}" # a
36 # ×îԶƥÅä, È¥µôabcd12345abc6789µÄ×îºó12¸ö×Ö·û
37 # |-------------| ^^^^^^
38
39 # ¼Çס, # ºÍ## ´Ó×Ö·û´®µÄ×ó±ß¿ªÊ¼,²¢ÇÒÈ¥µô×ó±ßµÄ×Ö·û´®,
40 # % ºÍ %% ´Ó×Ö·û´®µÄÓұ߿ªÊ¼,²¢ÇÒÈ¥µôÓұߵÄ×Ó´®.
41
42 echo
43
44 exit 0
################################End Script######################################
###
Example 9-18 ÖØÃüÃûÎļþÀ©Õ¹Ãû
################################Start Script####################################
###
1 #!/bin/bash
2 # rfe.sh: ÖØÃüÃûÎļþÀ©Õ¹Ãû.
3 #
4 # Ó÷¨: rfe old_extension new_extension
5 #
6 # Àý×Ó:
7 # ½«Ö¸¶¨Ä¿Â¼µÄËùÓÐ *.gif Îļþ¶¼ÖØÃüÃûΪ *.jpg,
8 # Ó÷¨: rfe gif jpg
9
10
11 E_BADARGS=65
12
13 case $# in
14 0|1) # "|" ÔÚÕâÀïµÄÒâ˼ÊÇ»ò²Ù×÷.
15 echo "Usage: `basename $0` old_file_suffix new_file_suffix"
16 exit $E_BADARGS # Èç¹ûÖ»ÓÐ0¸ö»ò1¸ö²ÎÊý,ÄÇô¾ÍÍ˳ö.
17 ;;
18 esac
19
20
21 for filename in *.$1
22 # ÒÔµÚÒ»¸ö²ÎÊýΪÀ©Õ¹ÃûµÄÈ«²¿ÎļþµÄÁбí
23 do
24 mv $filename ${filename%$1}$2
25 # ´Óɸѡ³öµÄÎļþÖÐÏÈÈ¥µôÒÔµÚÒ»²ÎÊý½áβµÄÀ©Õ¹Ãû²¿ÃÅ,
26 #+ È»ºó×÷ΪÀ©Õ¹Ãû°ÑµÚ2¸ö²ÎÊýÌí¼ÓÉÏ.
27 done
28
29 exit 0
################################End Script######################################
###
±äÁ¿À©Õ¹/×Ó´®Ìæ»»
ÕâЩ½á¹¹¶¼ÊÇ´ÓkshÖÐÎüÊÕÀ´µÄ.
${var:pos}
±äÁ¿var´ÓλÖÃpos¿ªÊ¼À©Õ¹.
${var:pos:len}
´ÓλÖÃpos¿ªÊ¼,²¢À©Õ¹len³¤¶È¸ö×Ö·û.¼ûExample A-14(Õâ¸öÀý×ÓÀïÓÐÕâÖÖ²Ù×÷µÄÒ»¸ö
´´ÔìÐÔÓ÷¨)
${var/Pattern/Replacement}
ʹÓÃReplacementÀ´Ìæ»»varÖеĵÚÒ»¸öPatternµÄÆ¥Åä.
${var//Pattern/Replacement}
È«¾ÖÌæ»».ÔÚvarÖÐËùÓеÄÆ¥Åä,¶¼»áÓÃReplacementÀ´Ìæ»».
ÏòÉϱßËù˵,Èç¹ûReplacement±»ºöÂԵĻ°,ÄÇôËùÓÐÆ¥Åäµ½µÄPattern¶¼»á±»É¾³ý.
Example 9-19 ʹÓÃģʽƥÅäÀ´·ÖÎö±È½ÏÌØÊâµÄ×Ö·û´®
################################Start Script####################################
###
1 #!/bin/bash
2
3 var1=abcd-1234-defg
4 echo "var1 = $var1"
5
6 t=${var1#*-*}
7 echo "var1 (with everything, up to and including first - stripped out) = $t"
8 # t=${var1#*-} ÔÚÕâ¸öÀý×ÓÖÐ×÷ÓÃÊÇÒ»ÑùµÄ,
9 #+ ÒòΪ # Æ¥ÅäÕâ¸ö×î½üµÄ×Ö·û´®,
10 #+ ²¢ÇÒ * Æ¥ÅäÇ°±ßµÄÈκÎ×Ö·û´®,°üÀ¨Ò»¸ö¿Õ×Ö·û.
11 # (Thanks, Stephane Chazelas, for pointing this out.)
12
13 t=${var1##*-*}
14 echo "If var1 contains a \"-\", returns empty string... var1 = $t"
15
16
17 t=${var1%*-*}
18 echo "var1 (with everything from the last - on stripped out) = $t"
19
20 echo
21
22 # -------------------------------------------
23 path_name=/home/bozo/ideas/thoughts.for.today
24 # -------------------------------------------
25 echo "path_name = $path_name"
26 t=${path_name##/*/}
27 echo "path_name, stripped of prefixes = $t"
28 # ÔÚÕâ¸öÌض¨µÄÀý×ÓÖÐ,Óë t=`basename $path_name` µÄ×÷ÓÃÒ»ÖÂ.
29 # t=${path_name%/}; t=${t##*/} ÊÇÒ»¸ö¸üÒ»°ãµÄ½â¾ö°ì·¨,
30 #+ µ«ÓÐʱ»¹ÊDz»ÐÐ.
31 # Èç¹û $path_name ÒÔÒ»¸öÐÂÐнáÊø, ÄÇô`basename $path_name` ½«²»Äܹ¤×÷,
32 #+ µ«ÊÇÉϱßÕâ¸ö±í´ïʽ¿ÉÒÔ.
33 # (Thanks, S.C.)
34
35 t=${path_name%/*.*}
36 # Óë t=`dirname $path_name` Ч¹ûÏàͬ.
37 echo "path_name, stripped of suffixes = $t"
38 # ÔÚijЩÇé¿öϽ«Ê§Ð§,±ÈÈç "../", "/foo////", # "foo/", "/".
39 # ɾ³ýºó׺,ÓÈÆäÊÇÔÚbasenameûÓкó׺µÄʱºò,
40 #+ µ«ÊÇdirname»¹ÊÇ»áʹÎÊÌ⸴ÔÓ»¯.
41 # (Thanks, S.C.)
42
43 echo
44
45 t=${path_name:11}
46 echo "$path_name, with first 11 chars stripped off = $t"
47 t=${path_name:11:5}
48 echo "$path_name, with first 11 chars stripped off, length 5 = $t"
49
50 echo
51
52 t=${path_name/bozo/clown}
53 echo "$path_name with \"bozo\" replaced by \"clown\" = $t"
54 t=${path_name/today/}
55 echo "$path_name with \"today\" deleted = $t"
56 t=${path_name//o/O}
57 echo "$path_name with all o's capitalized = $t"
58 t=${path_name//o/}
59 echo "$path_name with all o's deleted = $t"
60
61 exit 0
################################End Script######################################
###
${var/#Pattern/Replacement}
Èç¹ûvarµÄǰ׺ƥÅäµ½ÁËPattern,ÄÇô¾ÍÓÃReplacementÀ´Ìæ»»Pattern.
${var/%Pattern/Replacement}
Èç¹ûvarµÄºó׺ƥÅäµ½ÁËPattern,ÄÇô¾ÍÓÃReplacementÀ´Ìæ»»Pattern.
Example 9-20 ¶Ô×Ö·û´®µÄǰ׺»òºó׺ʹÓÃÆ¥Åäģʽ
################################Start Script####################################
###
1 #!/bin/bash
2 # var-match.sh:
3 # ¶Ô×Ö·û´®µÄÇ°ºó׺ʹÓÃÆ¥ÅäÌæ»»µÄÒ»¸öÑù±¾
4
5 v0=abc1234zip1234abc # ԭʼ±äÁ¿.
6 echo "v0 = $v0" # abc1234zip1234abc
7 echo
8
9 # Æ¥Åä×Ö·û´®µÄǰ׺
10 v1=${v0/#abc/ABCDEF} # abc1234zip1234abc
11 # |-|
12 echo "v1 = $v1" # ABCDEF1234zip1234abc
13 # |----|
14
15 # Æ¥Åä×Ö·û´®µÄºó׺
16 v2=${v0/%abc/ABCDEF} # abc1234zip123abc
17 # |-|
18 echo "v2 = $v2" # abc1234zip1234ABCDEF
19 # |----|
20
21 echo
22
23 # ----------------------------------------------------
24 # ±ØÐëÔÚ¿ªÍ·»ò½áβƥÅä,·ñÔò,
25 #+ ½«²»»á²úÉúÌæ»»½á¹û.
26 # ----------------------------------------------------
27 v3=${v0/#123/000} # Æ¥ÅäÉÏÁË,µ«²»ÊÇÔÚ×Ö·û´®µÄ¿ªÍ·
28 echo "v3 = $v3" # abc1234zip1234abc
29 # ûÌæ»».
30 v4=${v0/%123/000} # Æ¥ÅäÉÏÁË,µ«²»ÊÇÔÚ×Ö·û´®½áβ.
31 echo "v4 = $v4" # abc1234zip1234abc
32 # ûÌæ»».
33
34 exit 0
################################End Script######################################
###
${!varprefix*}, ${!varprefix@}
ʹÓñäÁ¿µÄǰ׺À´Æ¥ÅäÇ°±ßËùÓÐÉùÃ÷¹ýµÄ±äÁ¿.
1 xyz23=whatever
2 xyz24=
3
4 a=${!xyz*} # ÒÔ"xyz"×÷Ϊǰ׺,Æ¥ÅäËùÓÐÇ°±ßÉùÃ÷¹ýµÄ±äÁ¿.
5 echo "a = $a" # a = xyz23 xyz24
6 a=${!xyz@} # ͬÉÏ.
7 echo "a = $a" # a = xyz23 xyz24
8
9 # Bash, version 2.04, Ìí¼ÓÁËÕâ¸öÌØÕ÷.
×¢ÒâÊÂÏî:
[1] Èç¹ûÔÚÒ»¸ö·Ç½»»¥½Å±¾ÖÐ,$parameterΪ¿ÕµÄ»°,ÄÇôÕâ¸ö½Å±¾½«ÒÔ127·µ»Ø.
(127Í˳öÂë¶ÔÓ¦µÄBash´íÎóÂëΪ"command not found").

9.4 Ö¸¶¨ÀàÐ͵ıäÁ¿:declare»òÕßtypeset
-------------------------------------
declare»òÕßtypesetÄÚ½¨ÃüÁî(ÕâÁ½¸öÃüÁîÊÇÍêÈ«Ò»ÑùµÄ)ÔÊÐíÖ¸¶¨±äÁ¿µÄ¾ßÌåÀàÐÍ.ÔÚijЩÌØ
¶¨µÄÓïÑÔÖÐ,ÕâÊÇÒ»ÖÖÖ¸¶¨ÀàÐ͵ĺÜÈõµÄÐÎʽ.declareÃüÁîÊÇÔÚBash°æ±¾2»òÖ®ºóµÄ°æ±¾²Å±»
¼ÓÈëµÄ.typesetÃüÁîÒ²¿ÉÒÔ¹¤×÷ÔÚksh½Å±¾ÖÐ.
declare/typeset Ñ¡Ïî
-r Ö»¶Á
1 declare -r var1
(declare -r var1Óëreadonly var1ÊÇÍêÈ«Ò»ÑùµÄ)
ÕâºÍCÓïÑÔÖеÄconst¹Ø¼ü×ÖÒ»Ñù,¶¼ÊÇÇ¿ÖÆÖ¸¶¨Ö»¶Á.Èç¹ûÄã³¢ÊÔÐÞ¸ÄÒ»¸öÖ»¶Á±äÁ¿
µÄÖµ,ÄÇôÄ㽫µÃµ½Ò»¸ö´íÎóÏûÏ¢.
-i ÕûÐÎ
1 declare -i number
2 # Õâ¸ö½Å±¾½«°Ñ±äÁ¿"number"ºó±ßµÄ¸³ÖµÊÓΪһ¸öÕûÐÎ.
3
4 number=3
5 echo "Number = $number" # Number = 3
6
7 number=three
8 echo "Number = $number" # Number = 0
9 # ³¢ÊÔ°Ñ"three"½âÊÍΪÕûÐÎ.
Èç¹û°ÑÒ»¸ö±äÁ¿Ö¸¶¨ÎªÕûÐÎ,ÄÇô¼´Ê¹Ã»ÓÐexprºÍletÃüÁî,Ò²ÔÊÐíʹÓÃÌض¨µÄËãÊõÔËËã
1 n=6/3
2 echo "n = $n" # n = 6/3
3
4 declare -i n
5 n=6/3
6 echo "n = $n" # n = 2
-a Êý×é
1 declae -a indices
±äÁ¿indices½«±»ÊÓΪÊý×é.
-f º¯Êý
1 declare -f
Èç¹ûʹÓÃdeclare -f¶ø²»´ø²ÎÊýµÄ»°,½«»áÁгöÕâ¸ö½Å±¾ÖÐ֮ǰ¶¨ÒåµÄËùÓк¯Êý.
1 declare -f function_name
Èç¹ûʹÓÃdeclare -f function_nameÕâÖÖÐÎʽµÄ»°,½«Ö»»áÁгöÕâ¸öº¯ÊýµÄÃû×Ö.
-x export
1 declare -x var3
ÕâÖÖʹÓ÷½Ê½,½«»á°Ñvar3 export³öÀ´.
Example 9-21 ʹÓÃdeclareÀ´Ö¸¶¨±äÁ¿µÄÀàÐÍ
################################Start Script####################################
###
1 #!/bin/bash
2
3 func1 ()
4 {
5 echo This is a function.
6 }
7
8 declare -f # Áгö֮ǰµÄËùÓк¯Êý.
9
10 echo
11
12 declare -i var1 # var1 ÊǸöÕûÐÎ.
13 var1=2367
14 echo "var1 declared as $var1"
15 var1=var1+1 # ±äÁ¿ÉùÃ÷²»ÐèʹÓÃ'let'ÃüÁî.
16 echo "var1 incremented by 1 is $var1."
17 # ³¢ÊÔ½«±äÁ¿ÐÞ¸ÄΪÕûÐÎ.
18 echo "Attempting to change var1 to floating point value, 2367.1."
19 var1=2367.1 # ½á¹û½«ÊÇÒ»¸ö´íÎóÏûÏ¢,²¢ÇÒ±äÁ¿²¢Ã»Óб»ÐÞ¸Ä.
20 echo "var1 is still $var1"
21
22 echo
23
24 declare -r var2=13.36 # 'declare' ÔÊÐíÉèÖñäÁ¿µÄÊôÐÔ,
25 #+ ²¢ÇÒͬʱ·ÖÅä±äÁ¿µÄÖµ.
26 echo "var2 declared as $var2" # ³¢ÊÔÐÞ¸ÄÖ»¶Á±äÁ¿.
27 var2=13.37 # ²úÉúÒ»¸ö´íÎóÏûÏ¢,²¢ÇҴӽű¾Í˳öÁË.
28
29 echo "var2 is still $var2" # ÕâÐн«²»»á±»Ö´ÐÐ.
30
31 exit 0 # ½Å±¾½«²»»áÔÚ´Ë´¦Í˳ö.
################################End Script######################################
###
×¢Òâ:ʹÓÃdeclareÄÚ½¨ÃüÁ»áÏÞÖƱäÁ¿µÄ×÷ÓÃÓò.
1 foo ()
2 {
3 FOO="bar"
4 }
5
6 bar ()
7 {
8 foo
9 echo $FOO
10 }
11
12 bar # Prints bar.
È»¶ø...
1 foo (){
2 declare FOO="bar"
3 }
4
5 bar ()
6 {
7 foo
8 echo $FOO
9 }
10
11 bar # Prints nothing.
12
13
14 # Thank you, Michael Iatrou, for pointing this out.
9.5 ±äÁ¿µÄ¼ä½ÓÒýÓÃ
------------------
¼ÙÉèÒ»¸ö±äÁ¿µÄÖµÊÇÁíÒ»¸ö±äÁ¿µÄÃû×Ö.ÎÒÃÇÓпÉÄÜ´ÓµÚÒ»¸ö±äÁ¿ÖÐÈ¡µÃµÚ2¸ö±äÁ¿µÄֵô?
±ÈÈç,Èç¹ûa=letter_of_alphabet½Ó×Åletter_of_alphabet=z,ÄÇôÎÒÃÇÄÜ´ÓaÖеõ½zô?
´ð°¸ÊÇ:µ±È»¿ÉÒÔ,²¢ÇÒÕâ±»³ÆΪ¼ä½ÓÒýÓÃ.ËüʹÓÃÒ»¸ö²»³£ÓõķûºÅeval var1=\$$var2.

Example 9-22 ¼ä½ÓÒýÓÃ


################################Start Script####################################
###
1 #!/bin/bash
2 # ind-ref.sh: ¼ä½Ó±äÁ¿ÒýÓÃ
3 # ´æÈ¡Ò»¸ö±äÁ¿µÄÖµµÄÖµ(ÕâÀï·­ÒëµÃÓеãÞÖ¿Ú,²»¹ý´ÕºÏ°É)
4
5 a=letter_of_alphabet # ±äÁ¿"a"µÄÖµÊÇÁíÒ»¸ö±äÁ¿µÄÃû×Ö.
6 letter_of_alphabet=z
7
8 echo
9
10 # Ö±½ÓÒýÓÃ.
11 echo "a = $a" # a = letter_of_alphabet
12
13 # ¼ä½ÓÒýÓÃ.
14 eval a=\$$a
15 echo "Now a = $a" # Now a = z
16
17 echo
18
19
20 # ÏÖÔÚ,ÈÃÎÒÃÇÊÔÊÔÐ޸ĵÚ2¸öÒýÓõÄÖµ.
21
22 t=table_cell_3
23 table_cell_3=24
24 echo "\"table_cell_3\" = $table_cell_3" # "table_cell_3" = 24
25 echo -n "dereferenced \"t\" = "; eval echo \$$t # ½âÒýÓà "t" = 24
26 # ÔÚÕâ¸ö¼òµ¥µÄÀý×ÓÖÐ,ϱߵıí´ïʽҲÄÜÕý³£¹¤×÷(Ϊʲô?).
27 # eval t=\$$t; echo "\"t\" = $t"
28
29 echo
30
31 t=table_cell_3
32 NEW_VAL=387
33 table_cell_3=$NEW_VAL
34 echo "Changing value of \"table_cell_3\" to $NEW_VAL."
35 echo "\"table_cell_3\" now $table_cell_3"
36 echo -n "dereferenced \"t\" now "; eval echo \$$t
37 # "eval" ½«»ñµÃÁ½¸ö²ÎÊý "echo" ºÍ "\$$t" (Óë$table_cell_3µÈ¼Û)
38
39 echo
40
41 # (Thanks, Stephane Chazelas, ³ÎÇåÁËÉϱߵÄÐÐΪ.)
42
43
44 # ÁíÒ»¸ö·½·¨ÊÇʹÓÃ${!t}·ûºÅ,¼û"Bash, °æ±¾2"С½Ú.
45 # Ò²Çë²ÎÔÄex78.sh.
46
47 exit 0
################################End Script######################################
###
¼ä½ÓÓ¦Óõ½µ×ÓÐʲôӦÓüÛÖµ?Ëü¸øBashÌí¼ÓÁËÒ»ÖÖÀàËÆÓÚCÓïÑÔÖ¸ÕëµÄ¹¦ÄÜ,ÔÚExample 34-3
ÖÐÓÐÀý×Ó.²¢ÇÒ,»¹ÓÐһЩÆäËüµÄÓÐȤµÄÓ¦ÓÃ....
Nils RadtkeչʾÁËÈçºÎ½¨Á¢Ò»¸ö"dynamic"±äÁ¿Ãû×Ö²¢ÇÒÈ¡³öÆäÖеÄÖµ.µ±sourcing(°üº¬)ÅäÖÃ
Îļþʱ,ÕâºÜÓÐÓÃ.
################################Start Script####################################
###
1 #!/bin/bash
2
3
4 # ---------------------------------------------
5 # Õⲿ·ÖÄÚÈÝ¿ÉÄÜÀ´×ÔÓÚµ¥¶ÀµÄÎļþ.
6 isdnMyProviderRemoteNet=172.16.0.100
7 isdnYourProviderRemoteNet=10.0.0.10
8 isdnOnlineService="MyProvider"
9 # ---------------------------------------------
10
11
12 remoteNet=$(eval "echo \$$(echo isdn${isdnOnlineService}RemoteNet)")
13 remoteNet=$(eval "echo \$$(echo isdnMyProviderRemoteNet)")
14 remoteNet=$(eval "echo \$isdnMyProviderRemoteNet")
15 remoteNet=$(eval "echo $isdnMyProviderRemoteNet")
16
17 echo "$remoteNet" # 172.16.0.100
18
19 # ================================================================
20
21 # ͬʱ,ËüÉõÖÁÄܸüºÃ.
21 #
22
23 # ¿¼ÂÇϱߵĽű¾,¸ø³öÁËÒ»¸ö±äÁ¿getSparc,
24 #+ µ«ÊÇû¸ø³ö±äÁ¿getIa64:
25
26 chkMirrorArchs () {
27 arch="$1";
28 if [ "$(eval "echo \${$(echo get$(echo -ne $arch |
29 sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch |
30 sed 's/^.\(.*\)/\1/g')):-false}")" = true ]
31 then
32 return 0;
33 else
34 return 1;
35 fi;
36 }
37
38 getSparc="true"
39 unset getIa64
40 chkMirrorArchs sparc
41 echo $? # 0
42 # True
43
44 chkMirrorArchs Ia64
45 echo $? # 1
46 # False
47
48 # ×¢Òâ:
49 # -----<rojy bug>
50 # Even the to-be-substituted variable name part is built explicitly.
51 # The parameters to the chkMirrorArchs calls are all lower case.
52 # The variable name is composed of two parts: "get" and "Sparc" . . .
################################End Script######################################
###
Example 9-23 ´«µÝÒ»¸ö¼ä½ÓÒýÓøøawk
################################Start Script####################################
###
1 #!/bin/bash
2
3 # "column totaler"½Å±¾µÄÁíÒ»¸ö°æ±¾
4 #+ Õâ¸ö°æ±¾ÔÚÄ¿±êÎļþÖÐÌí¼ÓÁËÒ»¸öÌØÊâµÄÁÐ(Êý×ÖµÄ).
5 # Õâ¸ö½Å±¾Ê¹ÓÃÁ˼ä½ÓÒýÓÃ.
6
7 ARGS=2
8 E_WRONGARGS=65
9
10 if [ $# -ne "$ARGS" ] # ¼ì²éÃüÁîÐвÎÊýÊÇ·ñÊǺÏÊʵĸöÊý.
11 then
12 echo "Usage: `basename $0` filename column-number"
13 exit $E_WRONGARGS
14 fi
15
16 filename=$1
17 column_number=$2
18
19 #===== ÉϱߵÄÕⲿ·Ö,ÓëÔ­À´µÄ½Å±¾Ò»Ñù =====#
20
21
22 # Ò»¸ö¶àÐеÄawk½Å±¾±»µ÷ÓÃ,ͨ¹ý ' ..... '
23
24
25 # awk ½Å±¾¿ªÊ¼.
26 # ------------------------------------------------
27 awk "
28
29 { total += \$${column_number} # ¼ä½ÓÒýÓÃ.
30 }
31 END {
32 print total
33 }
34
35 " "$filename"
36 # ------------------------------------------------
37 # awk ½Å±¾½áÊø.
38
39 # ¼ä½ÓµÄ±äÁ¿ÒýÓñÜÃâÁËÔÚÒ»¸öÄÚǶµÄawk½Å±¾ÖÐÒýÓÃ
40 #+ Ò»¸öshell±äÁ¿µÄÎÊÌâ.
41 # Thanks, Stephane Chazelas.
42
43
44 exit 0
################################End Script######################################
###
×¢Òâ: Õâ¸ö½Å±¾ÓÐЩ½Æ»«.Èç¹ûµÚ2¸ö±äÁ¿ÐÞ¸ÄÁËËüµÄÖµ,ÄÇôµÚÒ»¸ö±äÁ¿±ØÐë±»Êʵ±µÄ½âÒýÓÃ
(ÏñÉϱߵÄÀý×ÓÒ»Ñù).ÐÒÔ˵ÄÊÇ,ÔÚBash°æ±¾2ÖÐÒýÈëµÄ${!variable}(²Î¼ûExample 34-2)
Êǵļä½ÓÒýÓøü¼ÓÖ±¹ÛÁË.
×¢Òâ: Bash²¢²»Ö§³ÖÖ¸ÕëµÄËãÊõÔËËã,²¢ÇÒÕâÑϸñµÄÏÞÖÆÁ˼ä½ÓÒýÓõÄʹÓÃ.ÊÂʵÉÏ,Ôڽű¾ÓïÑÔ
ÖÐ,¼ä½ÓÒýÓñ¾À´¾ÍÊdzóªµÄ²¿·Ö.
9.6 $RANDOM: ²úÉúËæ»úÕûÊý
-------------------------
$RANDOMÊÇBashµÄÄÚ²¿º¯Êý(²¢²»Êdz£Á¿),Õâ¸öº¯Êý½«·µ»ØÒ»¸ö·¶Î§ÔÚ0 - 32767Ö®¼äµÄÒ»¸öα
Ëæ»úÕûÊý.Ëü²»Ó¦¸Ã±»ÓÃÀ´²úÉúÃܳ×.
Example 9-24 ²úÉúËæ»úÊý
################################Start Script####################################
###
1 #!/bin/bash
2
3 # $RANDOM ÔÚÿ´Îµ÷ÓõÄʱºò,·µ»ØÒ»¸ö²»Í¬µÄËæ»úÕûÊý.
4 # Ö¸¶¨µÄ·¶Î§ÊÇ: 0 - 32767 (ÓзûºÅµÄ16-bit ÕûÊý).
5
6 MAXCOUNT=10
7 count=1
8
9 echo
10 echo "$MAXCOUNT random numbers:"
11 echo "-----------------"
12 while [ "$count" -le $MAXCOUNT ] # ²úÉú10 ($MAXCOUNT) ¸öËæ»úÕûÊý.
13 do
14 number=$RANDOM
15 echo $number
16 let "count += 1" # ÊýÁ¿¼Ó1.
17 done
18 echo "-----------------"
19
20 # Èç¹ûÄãÐèÒªÔÚÒ»¸öÌض¨·¶Î§ÄÚ²úÉúÒ»¸öËæ»úint,ÄÇôʹÓÃ'modulo'(Ä£)²Ù×÷.
21 # Õ⽫·µ»ØÒ»¸ö³ý·¨²Ù×÷µÄÓàÊý.
22
23 RANGE=500
24
25 echo
26
27 number=$RANDOM
28 let "number %= $RANGE"
29 # ^^
30 echo "Random number less than $RANGE --- $number"
31
32 echo
33
34
35
36 # Èç¹ûÄãÐèÒª²úÉúÒ»¸ö±ÈÄãÖ¸¶¨µÄ×îС±ß½ç´óµÄËæ»úÊý,
37 #+ ÄÇô½¨Á¢Ò»¸ötestÑ­»·,À´¶ªÆúËùÓвúÉú¶Ô±ÈÕâ¸öÊýСµÄËæ»úÊý.
38
39 FLOOR=200
40
41 number=0 #initialize
42 while [ "$number" -le $FLOOR ]
43 do
44 number=$RANDOM
45 done
46 echo "Random number greater than $FLOOR --- $number"
47 echo
48
49 # ÈÃÎÒÃǶÔÉϱߵÄÑ­»·³¢ÊÔÒ»¸öС¸Ä¶¯,Ò²¾ÍÊÇ
50 # ÈÃ"number = $RANDOM + $FLOOR"
51 # Õ⽫²»ÔÙÐèÒªÄǸöwhileÑ­»·,²¢ÇÒÄܹ»ÔËÐеøü¿ì.
52 # µ«ÊÇ, Õâ¿ÉÄÜ»á²úÉúÒ»¸öÎÊÌâ.ÄÇôÕâ¸öÎÊÌâÊÇʲôÄØ?(ÒëÕß:ÕâºÜ¼òµ¥,ÓпÉÄÜÒç³ö)
53
54
55
56 # ½áºÏÉϱßÁ½¸öÀý×ӵļ¼Êõ,À´´ïµ½»ñµÃÔÚÖ¸¶¨µÄÉÏÏÂÏÞÖ®¼äÀ´²úÉúËæ»úÊý.
57 number=0 #initialize
58 while [ "$number" -le $FLOOR ]
59 do
60 number=$RANDOM
61 let "number %= $RANGE" # ÈÃ$numberÒÀ±ÈÀýÂäÔÚ$RANGE·¶Î§ÄÚ.
62 done
63 echo "Random number between $FLOOR and $RANGE --- $number"
64 echo
65
66
67
68 # ²úÉúÒ»¸ö¶þԪѡÔñ,¾ÍÊÇ"true"ºÍ"false"Á½¸öÖµ.
69 BINARY=2
70 T=1
71 number=$RANDOM
72
73 let "number %= $BINARY"
74 # ×¢Òâ,ÈÃ"number >>= 14" ½«¸ø³öÒ»¸ö¸üºÃµÄËæ»ú·ÖÅä
75 #+ (ÓÒÒÆ14뽫°ÑËùÓÐΪȫ²¿Çå¿Õ,³ýÁ˵Ú15λ,ÒòΪÓзûºÅ,ËùÒÔµÚ16λÊÇ·ûºÅλ).
76 if [ "$number" -eq $T ]
77 then
78 echo "TRUE"
79 else
80 echo "FALSE"
81 fi
82
83 echo
84
85
86 # Å×÷»×Ó
87 SPOTS=6 # Ä£6¸ø³öµÄ·¶Î§¾ÍÊÇ0-5.
88 # ¼Ó1¾Í»áµÃµ½ÆÚÍûµÄ·¶Î§1 - 6.
89 # Thanks, Paulo Marcel Coelho Aragao, for the simplification.
90 die1=0
91 die2=0
92 # ÊÇ·ñÈÃSPOTS=7±È¼Ó1¸üºÃÄØ?½âÊÍÐлòÕß²»ÐеÄÔ­Òò?
93
94 # ÿ´ÎÅ×÷»×Ó,¶¼»á¸ø³ö¾ùµÈµÄ»ú»á.
95
96 let "die1 = $RANDOM % $SPOTS +1" # Å×µÚÒ»´Î.
97 let "die2 = $RANDOM % $SPOTS +1" # Å×µÚ¶þ´Î.
98 # ÉϱߵÄÄǸöËãÊõ²Ù×÷,¾ßÓиü¸ßµÄÓÅÏȼ¶ÄØ --
99 #+ Ä£²Ù×÷(%)»¹ÊǼӷ¨²Ù×÷(+)?
100
101
102 let "throw = $die1 + $die2"
103 echo "Throw of the dice = $throw"
104 echo
105
106
107 exit 0
################################End Script######################################
###
Example 9-25 ´ÓÒ»¸±ÆË¿ËÅÆÖÐÈ¡³öÒ»ÕÅËæ»úµÄÅÆ
################################Start Script####################################
###
1 #!/bin/bash
2 # pick-card.sh
3
4 # ÕâÊÇÒ»¸ö´ÓÊý×éÖÐÈ¡³öËæ»úÔªËصÄÒ»¸öÀý×Ó.
5
6
7 # È¡³öÒ»ÕÅÅÆ,ÈκÎÒ»ÕÅ.
8
9 Suites="Clubs
10 Diamonds
11 Hearts
12 Spades"
13
14 Denominations="2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 10
23 Jack
24 Queen
25 King
26 Ace"
27
28 # ×¢Òâ±äÁ¿µÄ¶àÐÐÕ¹¿ª.
29
30
31 suite=($Suites) # ¶Áµ½Êý×é±äÁ¿ÖÐ.
32 denomination=($Denominations)
33
34 num_suites=${#suite[*]} # ¼ÆËãÓжàÉÙ¸öÔªËØ.
35 num_denominations=${#denomination[*]}
36
37 echo -n "${denomination[$((RANDOM%num_denominations))]} of "
38 echo ${suite[$((RANDOM%num_suites))]}
39
40
41 # $bozo sh pick-cards.sh
42 # Jack of Clubs
43
44
45 # Thank you, "jipe," for pointing out this use of $RANDOM.
46 exit 0
################################End Script######################################
###
JipeչʾÁËһϵÁеÄÔÚÒ»¶¨·¶Î§ÖвúÉúËæ»úÊýµÄ·½·¨.
1 # ÔÚ6µ½30Ö®¼ä²úÉúËæ»úÊý.
2 rnumber=$((RANDOM%25+6))
3
4 # »¹ÊDzúÉú6-30Ö®¼äµÄËæ»úÊý,
5 #+ µ«ÊÇÕâ¸öÊý×Ö±ØÐë±»3¾ù·Ö.
6 rnumber=$(((RANDOM%30/3+1)*3))
7
8 # ×¢Òâ,Õâ¿ÉÄܲ»»áÔÚËùÓÐʱºò¶¼ÄÜÕý³£µØÔËÐÐ.
9 # It fails if $RANDOM returns 0.
10
11 # Frank Wang ½¨ÒéÓÃÏ°àµÄ·½·¨À´È¡´ú:
12 rnumber=$(( RANDOM%27/3*3+6 ))
Bill Gradwohl Ìá³öÁËÒ»¸öÖØÒªµÄ¹æÔòÀ´²úÉúÕýÊý.
1 rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))
ÕâÀïBill¸ø³öÁËÒ»¸öͨÓú¯Êý,Õâ¸öº¯Êý·µ»ØÒ»¸öÔÚÁ½¸öÖ¸¶¨ÖµÖ®¼äµÄËæ»úÊý
Example 9-26 Á½¸öÖ¸¶¨ÖµÖ®¼äµÄËæ»úÊý
################################Start Script####################################
###
1 #!/bin/bash
2 # random-between.sh
3 # ÔÚÁ½¸öÖ¸¶¨ÖµÖ®¼äµÄËæ»úÊý.
4 # Bill Gradwohl±àдµÄ±¾½Å±¾,±¾ÎÄ×÷Õß×÷Á˽ÏСµÄÐÞ¸Ä.
5 # ÔÊÐíʹÓÃ.
6
7
8 randomBetween() {
9 # ²úÉúÒ»¸öÕýµÄ»òÕ߸ºµÄËæ»úÊý.
10 #+ ÔÚ$maxºÍ$maxÖ®¼ä
11 #+ ²¢Çҿɱ»$divisibleByÕû³ýµÄ.
12 # ¸ø³öÒ»¸öºÏÀíµÄËæ»ú·ÖÅäµÄ·µ»ØÖµ.
13 #
14 # Bill Gradwohl - Oct 1, 2003
15
16 syntax() {
17 # ÔÚº¯ÊýÖÐÄÚǶº¯Êý
18 echo
19 echo "Syntax: randomBetween [min] [max] [multiple]"
20 echo
21 echo "Expects up to 3 passed parameters, but all are completely opt
ional."
22 echo "min is the minimum value"
23 echo "max is the maximum value"
24 echo "multiple specifies that the answer must be a multiple of this
value."
25 echo " i.e. answer must be evenly divisible by this number."
26 echo
27 echo "If any value is missing, defaults area supplied as: 0 32767 1
"
28 echo "Successful completion returns 0, unsuccessful completion retu
rns"
29 echo "function syntax and 1."
30 echo "The answer is returned in the global variable randomBetweenAn
swer"
31 echo "Negative values for any passed parameter are handled correctl
y."
32 }
33
34 local min=${1:-0}
35 local max=${2:-32767}
36 local divisibleBy=${3:-1}
37 # ĬÈÏÖµ·ÖÅä,ÓÃÀ´´¦ÀíûÓвÎÊý´«µÝ½øÀ´µÄʱºò.
38
39 local x
40 local spread
41
42 # È·ÈÏdivisibleByÊÇÕýÖµ.
43 [ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy))
44
45 # ÍêÕûÐÔ¼ì²é.
46 if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]; then
47 syntax
48 return 1
49 fi
50
51 # ²ì¿´ÊÇ·ñminºÍmaxµßµ¹ÁË.
52 if [ ${min} -gt ${max} ]; then
53 # ½»»»ËüÃÇ.
54 x=${min}
55 min=${max}
56 max=${x}
57 fi
58
59 # Èç¹ûmin×Ô¼º²¢²»Äܹ»±»$divisibleByÕû³ý,
60 #+ ÄÇô¾Íµ÷ÕûminµÄÖµ,ʹÆäÄܹ»±»$divisibleByÕû³ý,Ç°ÌáÊDz»ÄÜ·Å´ó·¶Î§.
61 if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]; then
62 if [ ${min} -lt 0 ]; then
63 min=$((min/divisibleBy*divisibleBy))
64 else
65 min=$((((min/divisibleBy)+1)*divisibleBy))
66 fi
67 fi
68
69 # Èç¹ûmin×Ô¼º²¢²»Äܹ»±»$divisibleByÕû³ý,
70 #+ ÄÇô¾Íµ÷ÕûmaxµÄÖµ,ʹÆäÄܹ»±»$divisibleByÕû³ý,Ç°ÌáÊDz»ÄÜ·Å´ó·¶Î§.
71 if [ $((max/divisibleBy*divisibleBy)) -ne ${max} ]; then
72 if [ ${max} -lt 0 ]; then
73 max=$((((max/divisibleBy)-1)*divisibleBy))
74 else
75 max=$((max/divisibleBy*divisibleBy))
76 fi
77 fi
78
79 # ---------------------------------------------------------------------
80 # ÏÖÔÚ,À´×öÕæÕýµÄ¹¤×÷.
81
82 # ×¢Òâ,ΪÁ˵õ½¶ÔÓڶ˵ãÀ´ËµºÏÊʵķÖÅä,
83 #+ Ëæ»úÖµµÄ·¶Î§²»µÃ²»ÂäÔÚ
84 #+ 0 ºÍ abs(max-min)+divisibleByÖ®¼ä, ¶ø²»ÊÇ abs(max-min)+1.
85
86 # ¶ÔÓڶ˵ãÀ´Ëµ,
87 #+ Õâ¸öÉÙÁ¿µÄÔö¼Ó½«»á²úÉúºÏÊʵķÖÅä.
88
89 # ÐÞ¸ÄÕâ¸ö¹«Ê½,ʹÓÃabs(max-min)+1À´´úÌæabs(max-min)+divisibleByµÄ»°,
90 #+ Ò²Äܹ»²úÉúÕýÈ·µÄ´ð°¸, µ«ÊÇÔÚÕâÖÖÇé¿öÏÂÉú³ÉµÄËæ»úÖµ¶ÔÓÚÕýºÃΪ¶Ëµã±¶Êý
91 #+ µÄÕâÖÖÇé¿öÀ´Ëµ½«ÊDz»ÍêÃÀµÄ,ÒòΪÔÚÕýºÃΪ¶Ëµã±¶ÊýµÄÇé¿öµÄËæ»úÂʱȽϵÍ,
92 #+ ÒòΪÄã²Å¼Ó1¶øÒÑ,Õâ±ÈÕý³£µÄ¹«Ê½Ëù²úÉúµÄ»úÂÊҪСµÃ¶à(Õý³£Îª¼ÓdivisibleBy)
93 # ---------------------------------------------------------------------
94
95 spread=$((max-min))
96 [ ${spread} -lt 0 ] && spread=$((0-spread))
97 let spread+=divisibleBy
98 randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+min))
99
100 return 0
101
102 # È»¶ø,Paulo Marcel Coelho AragaoÖ¸³ö
103 #+ µ±$maxºÍ$min²»Äܱ»$divisibleByÕû³ýʱ,
104 #+ Õâ¸ö¹«Ê½½«»áʧ°Ü.
105 #
106 # Ëû½¨ÒéʹÓÃÈçÏµĹ«Ê½:
107 # rnumber = $(((RANDOM%(max-min+1)+min)/divisibleBy*divisibleBy))
108
109 }
110
111 # ÈÃÎÒÃDzâÊÔÒ»ÏÂÕâ¸öº¯Êý.
112 min=-14
113 max=20
114 divisibleBy=3
115
116
117 # ²úÉúÒ»¸öÊý×éanswers,answersµÄϱêÓÃÀ´±íʾÔÚ·¶Î§ÄÚ¿ÉÄܳöÏÖµÄÖµ,
118 #+ ¶øÄÚÈݼǼµÄÊǶÔÓÚÕâ¸öÖµ³öÏֵĴÎÊý,Èç¹ûÎÒÃÇÑ­»·×ã¹»¶à´Î,Ò»¶¨»áµÃµ½
119 #+ Ò»´Î³öÏÖ»ú»á.
120 declare -a answer
121 minimum=${min}
122 maximum=${max}
123 if [ $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]; then
124 if [ ${minimum} -lt 0 ]; then
125 minimum=$((minimum/divisibleBy*divisibleBy))
126 else
127 minimum=$((((minimum/divisibleBy)+1)*divisibleBy))
128 fi
129 fi
130
131
132 # Èç¹ûmaximum×Ô¼º²¢²»Äܹ»±»$divisibleByÕû³ý,
133 #+ ÄÇô¾Íµ÷ÕûmaximumµÄÖµ,ʹÆäÄܹ»±»$divisibleByÕû³ý,Ç°ÌáÊDz»ÄÜ·Å´ó·¶Î§.
134
135 if [ $((maximum/divisibleBy*divisibleBy)) -ne ${maximum} ]; then
136 if [ ${maximum} -lt 0 ]; then
137 maximum=$((((maximum/divisibleBy)-1)*divisibleBy))
138 else
139 maximum=$((maximum/divisibleBy*divisibleBy))
140 fi
141 fi
142
143
144 # ÎÒÃÇÐèÒª²úÉúÒ»¸öϱêȫΪÕýµÄÊý×é,
145 #+ ËùÒÔÎÒÃÇÐèÒªÒ»¸ödisplacementÀ´±£Õý¶¼ÎªÕýµÄ½á¹û.
146
147
148 displacement=$((0-minimum))
149 for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do
150 answer[i+displacement]=0
151 done
152
153
154 # ÏÖÔÚÎÒÃÇÑ­»·×ã¹»¶àµÄ´ÎÊýÀ´µÃµ½ÎÒÃÇÏëÒªµÄ´ð°¸.
155 loopIt=1000 # ½Å±¾×÷Õß½¨Òé 100000,
156 #+ µ«ÊÇÕâʵÔÚÊÇÐèҪ̫³¤µÄʱ¼äÁË.
157
158 for ((i=0; i<${loopIt}; ++i)); do
159
160 # ×¢Òâ,ÎÒÃÇÔÚÕâÀïµ÷ÓÃrandomBetweenº¯Êýʱ,¹ÊÒ⽫minºÍmaxµßµ¹Ë³Ðò
161 #+ ÎÒÃÇÊÇΪÁ˲âÊÔÔÚÕâÖÖÇé¿öÏÂ,´Ëº¯ÊýÊÇ·ñ»¹Äܵõ½ÕýÈ·µÄ½á¹û.
162
163 randomBetween ${max} ${min} ${divisibleBy}
164
165 # Èç¹û´ð°¸²»ÊÇÎÒÃÇËùÔ¤ÆÚµÄ,ÄÇô¾Í±¨¸æÒ»¸ö´íÎó.
166 [ ${randomBetweenAnswer} -lt ${min} -o ${randomBetweenAnswer} -gt ${max}
] && echo MIN or MAX error - ${randomBetweenAnswer}!
167 [ $((randomBetweenAnswer%${divisibleBy})) -ne 0 ] && echo DIVISIBLE BY er
ror - ${randomBetweenAnswer}!
168
169 # ½«Í³¼ÆÖµ´æµ½answerÖ®ÖÐ.
170 answer[randomBetweenAnswer+displacement]=$((answer[randomBetweenAnswer+di
splacement]+1))
171 done
172
173
174
175 # ÈÃÎÒÃDz쿴һϽá¹û
176
177 for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do
178 [ ${answer[i+displacement]} -eq 0 ] && echo "We never got an answer of $i
." || echo "${i} occurred ${answer[i+displacement]} times."
179 done
180
181
182 exit 0
################################End Script######################################
###
$RANDOMµ½µ×ÓжàËæ»ú?×îºÃµÄ°ì·¨¾ÍÊÇд¸ö½Å±¾À´²âÊÔÒ»ÏÂ.¸ú×ÙËæ»úÊýµÄ·ÖÅäÇé¿ö.
ÈÃÎÒÃÇÓÃËæ»úÊýÒ¡Ò»¸ö÷»×Ó.
Example 9-27 ʹÓÃËæ»úÊýÀ´Ò¡Ò»¸ö÷»×Ó
################################Start Script####################################
###
1 #!/bin/bash
2 # RANDOMµ½µ×Óжàrandom?
3
4 RANDOM=$$ # ʹÓýű¾µÄ½ø³ÌIDÀ´×÷ΪËæ»úÊýµÄ²úÉúÖÖ×Ó.
5
6 PIPS=6 # Ò»¸ö÷»×ÓÓÐ6Ãæ.
7 MAXTHROWS=600 # Èç¹ûÄãû±ðµÄʸÉ,ÄÇô¿ÉÒÔÔö¼ÓÕâ¸öÊýÖµ.
8 throw=0 # Å×÷»×ӵĴÎÊý.
9
10 ones=0 # ±ØÐë°ÑËùÓÐcount¶¼³õʼ»¯Îª0,
11 twos=0 #+ ÒòΪδ³õʼ»¯µÄ±äÁ¿Îªnull,²»ÊÇ0.
12 threes=0
13 fours=0
14 fives=0
15 sixes=0
16
17 print_result ()
18 {
19 echo
20 echo "ones = $ones"
21 echo "twos = $twos"
22 echo "threes = $threes"
23 echo "fours = $fours"
24 echo "fives = $fives"
25 echo "sixes = $sixes"
26 echo
27 }
28
29 update_count()
30 {
31 case "$1" in
32 0) let "ones += 1";; # ÒòΪ÷»×ÓûÓÐ0,ËùÒÔ¸ø1.
33 1) let "twos += 1";; # ¶Ôtows×öͬÑùµÄÊÂ.
34 2) let "threes += 1";;
35 3) let "fours += 1";;
36 4) let "fives += 1";;
37 5) let "sixes += 1";;
38 esac
39 }
40
41 echo
42
43
44 while [ "$throw" -lt "$MAXTHROWS" ]
45 do
46 let "die1 = RANDOM % $PIPS"
47 update_count $die1
48 let "throw += 1"
49 done
50
51 print_result
52
53 exit 0
54
55 # Èç¹ûRANDOMÊÇÕæÕýµÄËæ»ú,ÄÇôҡ³öÀ´½á¹ûÓ¦¸Ãƽ¾ùµÄ.
56 # $MAXTHROWSÉèΪ600,ÄÇôÿÃ涼Ӧ¸ÃΪ100,ÉÏϵijöÈë²»Ó¦¸Ã³¬¹ý20.
57 #
58 # ¼ÇסRANDOM±Ï¾¹Ö»ÊÇÒ»¸öαËæ»úÊý,
59 #+ ²¢ÇÒ²»ÊÇÊ®·ÖÍêÃÀµÄ.
60
61 # Ëæ»úÊýµÄ²úÉúÊÇÒ»¸öÉî°Â²¢¸´ÔÓµÄÎÊÌâ.
62 # ×ã¹»³¤µÄËæ»úÐòÁÐ,²»µ«»áÕ¹ÏÖÔÓÂÒÎÞÕµÄÒ»Ãæ,
63 #+ ¶øÇÒ»áÕ¹ÏÖ»ú»á¾ùµÈµÄÒ»Ãæ.
64
65 # Ò»¸öºÜ¼òµ¥µÄÁ·Ï°:
66 # -----------------
67 # ÖØдÕâ¸öÀý×Ó,×ö³ÉÅ×1000´ÎÓ²±ÒµÄÐÎʽ.
68 # ·ÖΪÕý·´Á½Ãæ.
################################End Script######################################
###
ÏñÎÒÃÇÔÚÉϱߵÄÀý×ÓÖп´µ½µÄ,×îºÃÔÚÿ´ÎËæ»úÊý²úÉúʱ¶¼Ê¹ÓÃеÄÖÖ×Ó.ӦΪÈç¹ûʹÓÃͬÑùµÄ
ÖÖ×ӵĻ°,ÄÇôËæ»úÊý½«²úÉúÏàͬµÄÐòÁÐ.[2](CÖÐrandom()º¯ÊýÒ²»áÓÐÕâÑùµÄÐÐΪ)
Example 9-28 ÖØзÖÅäËæ»úÊýÖÖ×Ó
################################Start Script####################################
###
1 #!/bin/bash
2 # seeding-random.sh: ÉèÖÃRANDOM±äÁ¿×÷ΪÖÖ×Ó.
3
4 MAXCOUNT=25 # ¾ö¶¨²úÉú¶àÉÙ¸öËæ»úÊý.
5
6 random_numbers ()
7 {
8 count=0
9 while [ "$count" -lt "$MAXCOUNT" ]
10 do
11 number=$RANDOM
12 echo -n "$number "
13 let "count += 1"
14 done
15 }
16
17 echo; echo
18
19 RANDOM=1 # ΪËæ»úÊýµÄ²úÉúÉèÖÃRANDOMÖÖ×Ó.
20 random_numbers
21
22 echo; echo
23
24 RANDOM=1 # ÉèÖÃͬÑùµÄÖÖ×Ó...
25 random_numbers # ...½«»áºÍÉϱ߲úÉúµÄËæ»úÊýÁÐÏàͬ.
26 #
27 # ¸´ÖÆÒ»¸öÏàͬµÄËæ»úÊýÐòÁÐÔÚʲôʱºòÓÐÓÃÄØ?
28
29 echo; echo
30
31 RANDOM=2 # ÔÙÊÔÒ»ÏÂ,µ«Õâ´ÎʹÓò»Í¬µÄÖÖ×Ó...
32 random_numbers # ½«¸ø³öÒ»¸ö²»Í¬µÄËæ»úÊýÐòÁÐ.
33
34 echo; echo
35
36 # RANDOM=$$ ʹÓýű¾µÄ½ø³Ìid ×÷ΪËæ»úÊýµÄÖÖ×Ó.
37 # ´Ó'time'»ò'date'ÃüÁîÖÐÈ¡µÃRANDOM×÷ΪÖÖ×ÓÒ²ÊǺܳ£Óõİ취.
38
39 # Ò»¸öÓÐÏëÏóÁ¦µÄ·½·¨...
40 SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
41 # Ê×ÏÈ´Ó/dev/urandom(ϵͳαËæ»úÉ豸Îļþ)ÖÐÈ¡³ö1ÐÐ,
42 #+ È»ºó×ÅÕâ¸ö¿É´òÓ¡ÐÐת»»Îª(8½øÖÆ)Êý,ͨ¹ýʹÓÃ"od"ÃüÁî,
43 #+ ×îºóʹÓÃ"awk"À´»ñµÃÒ»¸öÊý,
44 #+ Õâ¸öÊý½«×÷ΪËæ»úÊý²úÉúµÄÖÖ×Ó.
45 RANDOM=$SEED
46 random_numbers
47
48 echo; echo
49
50 exit 0
################################End Script######################################
###
×¢Òâ:/dev/urandomÉ豸ÎļþÌṩÁËÒ»Öֱȵ¥¶ÀʹÓÃ$RANDOM¸üºÃµÄ,ÄܲúÉú¸ü"Ëæ»ú"µÄËæ»úÊý
µÄ·½·¨.
dd if=/dev/urandom of=targetfile bs=1 count=XXÄܹ»²úÉúÒ»¸öºÜ·ÖÉ¢µÄΪËæ»úÊý.
È»¶ø,½«Õâ¸öÊý¸³Öµµ½Ò»¸ö½Å±¾ÎļþµÄ±äÁ¿ÖÐ,»¹ÐèÒª¿É²Ù×÷ÐÔ,±ÈÈçʹÓÃ"od"ÃüÁî
(¾ÍÏñÉϱߵÄÀý×Ó,¼ûExample 12-13),»òÕßʹÓÃddÃüÁî(¼ûExample 12-55),»òÕ߹ܵÀµ½
"md5sum"ÃüÁîÖÐ(¼ûExample 33-14).
µ±È»»¹ÓÐÆäËüµÄ²úÉúαËæ»úÊýµÄ·½·¨.Awk¾Í¿ÉÒÔÌṩһ¸ö·½±ãµÄ·½·¨.
Example 9-29 ʹÓÃawk²úÉúαËæ»úÊý
################################Start Script####################################
###
1 #!/bin/bash
2 # random2.sh: ²úÉúÒ»¸ö·¶Î§0 - 1µÄΪËæ»úÊý.
3 # ʹÓÃawkµÄrand()º¯Êý.
4
5 AWKSCRIPT=' { srand(); print rand() } '
6 # Command(s) / ´«µ½awkÖеIJÎÊý
7 # ×¢Òâ,srand()º¯ÊýÓÃÀ´²úÉúawkµÄËæ»úÊýÖÖ×Ó.
8
9
10 echo -n "Random number between 0 and 1 = "
11
12 echo | awk "$AWKSCRIPT"
13 # Èç¹ûÄãÊ¡È¥'echo'ÄÇô½«·¢Éúʲô?
14
15 exit 0
16
17
18 # Exercises:
18 # Á·Ï°:
19 # -----
20
21 # 1) ʹÓÃÑ­»·½á¹¹,´òÓ¡³ö10¸ö²»Í¬µÄËæ»úÊý.
22 # (Ìáʾ: ÔÚÑ­»·µÄÿ´ÎÖ´Ðйý³ÌÖÐ,Äã±ØÐëʹÓÃ"srand()"º¯ÊýÀ´Éú³É²»Í¬µÄ
23 #+ ÖÖ×Ó.Èç¹ûÄãû×öÕâ¼þÊÂÄÇô½«·¢Éúʲô?
24
25 # 2) ʹÓÃÒ»¸öÕûÊý³Ë·¨×÷Ϊһ¸ö·ÅËõÒò×Ó,ÔÚ10µ½100µÄ·¶Î§Ö®¼ä,
26 #+ À´²úÉúËæ»úÊý.
27
28 # 3) ͬÉϱߵÄÁ·Ï° #2,µ«Õâ´Î²úÉúËæ»úÕûÊý.
################################End Script######################################
###
"data"ÃüÁîÒ²¿ÉÒÔÓÃÀ´²úÉúαËæ»úÕûÊýÐòÁÐ.
×¢ÒâÊÂÏî:
[1] ÕæÕýµÄËæ»úʼþ(ÔÚËü´æÔڵķ¶Î§ÄÚ),Ö»·¢ÉúÔÚÌض¨µÄ¼¸¸öδ֪µÄ×ÔÈ»½çÏÖÏóÖÐ,±ÈÈç
·ÅÉäÐÔË¥±ä.¼ÆËã»úÖ»ÄܲúÉúÄ£ÄâµÄËæ»úʼþ,²¢ÇÒ¼ÆËã»ú²úÉúµÄ"Ëæ»ú"ÊýÒò´ËÖ»ÄܳÆ
ΪαËæ»úÊý.
[2] ¼ÆËã»ú²úÉúµÄαËæ»úÊýÐòÁÐÓõÄÖÖ×Ó¿ÉÒÔ±»¿´³ÉÊÇÒ»ÖÖ±êʶ±êÇ©.±ÈÈç,ʹÓÃÖÖ×Ó23Ëù
²úÉúµÄαËæ»úÊýÐòÁоͱ»³Æ×÷ÐòÁÐ#23.
Ò»¸öαËæ»úÐòÁеÄÌصã¾ÍÊÇÔÚÕâ¸öÐòÁпªÊ¼Öظ´Ö®Ç°µÄËùÓÐÔªËصĸöÊýµÄºÍ,Ò²¾ÍÊÇ
Õâ¸öÐòÁеij¤¶È.Ò»¸öºÃµÄαËæ»ú²úÉúËã·¨½«¿ÉÒÔ²úÉúÒ»¸ö·Ç³£³¤µÄ²»Öظ´µÄÐòÁÐ.

9.7 Ë«Ô²À¨ºÅ½á¹¹
----------------
((...))ÓëletÃüÁîºÜÏñ,ÔÊÐíËãÊõÀ©Õ¹ºÍ¸³Öµ.¾Ù¸ö¼òµ¥µÄÀý×Óa=$(( 5 + 3 )),½«°ÑaÉèΪ
"5+3"»òÕß8.È»¶ø,Ë«Ô²À¨ºÅÒ²ÊÇÒ»ÖÖÔÚBashÖÐÔÊÐíʹÓÃC·ç¸ñµÄ±äÁ¿´¦ÀíµÄ»úÖÆ.
Example 9-30 C·ç¸ñµÄ±äÁ¿´¦Àí
################################Start Script####################################
###
1 #!/bin/bash
2 # ´¦ÀíÒ»¸ö±äÁ¿,C·ç¸ñ,ʹÓÃ((...))½á¹¹.
3
4
5 echo
6
7 (( a = 23 )) # ¸øÒ»¸ö±äÁ¿¸³Öµ,´Ó"="Á½±ßµÄ¿Õ¸ñ¾ÍÄÜ¿´³öÕâÊÇc·ç¸ñµÄ´¦Àí.
8 echo "a (initial value) = $a"
9
10 (( a++ )) # ±äÁ¿'a'ºó¼Ó1,C·ç¸ñ.
11 echo "a (after a++) = $a"
12
13 (( a-- )) # ±äÁ¿'a'ºó¼õ1,C·ç¸ñ.
14 echo "a (after a--) = $a"
15
16
17 (( ++a )) # ±äÁ¿'a'Ô¤¼Ó1,C·ç¸ñ.
18 echo "a (after ++a) = $a"
19
20 (( --a )) # ±äÁ¿'a'Ô¤¼õ1,C·ç¸ñ.
21 echo "a (after --a) = $a"
22
23 echo
24
25 ########################################################
26 # ×¢Òâ:ÔÚCÓïÑÔÖÐ,Ô¤¼õºÍºó¼õ²Ù×÷
27 #+ »áÓÐЩ²»Í¬µÄ¸±×÷ÓÃ.
28
29 n=1; let --n && echo "True" || echo "False" # False
30 n=1; let n-- && echo "True" || echo "False" # True
31
32 # Thanks, Jeroen Domburg.
33 ########################################################
34
35 echo
36
37 (( t = a<45?7:11 )) # C·ç¸ñµÄ3Ôª²Ù×÷.
38 echo "If a < 45, then t = 7, else t = 11."
39 echo "t = $t " # Yes!
40
41 echo
42
43
44 # ----------------
45 # ¸´»î½Ú²Êµ°×¢Òâ!
46 # ----------------
47 # Chet Ramey ÏÔÈ»µÄ͵͵ÃþÃþµÄ×öÁËһЩδ¹«¿ªµÄC·ç¸ñµÄ½á¹¹
48 #+ ·ÅÔÚBashÖÐ(׼ȷµØ˵ÊǸù¾ÝkshÀ´¸ÄдµÄ,Õâ¸ü½Ó½üЩ)
49 # ÔÚBashÎĵµÖÐ,Rameyµ÷ÓÃ((...))shellËã·¨,
50 #+ µ«ÊÇËü¿ÉÒÔ×ߵøüÔ¶.
51 # ¶Ô²»Æð, Chet, ÏÖÔÚÃØÃܱ»¹«¿ªÁË.
52
53 # See also "for" and "while" loops using the ((...)) construct.
53 # Ò²²Î¿¼Ò»Ð©"for"ºÍ"while"Ñ­»·ÖÐʹÓÃ((...))½á¹¹µÄÀý×Ó.
54
55 # ÕâЩֻÄܹ¤×÷ÔÚ2.04»òÕ߸ü¸ß°æ±¾µÄBashÖÐ.
56
57 exit 0
################################End Script######################################
###
¼ûExample 10-12.

µÚ10Õ ѭ»·ºÍ·ÖÖ§
==================
¶Ô´úÂë¿é½øÐвÙ×÷ÊÇÓÐ×éÖ¯µÄ½á¹¹»¯µÄshell½Å±¾µÄ¹Ø¼ü.ΪÁË´ïµ½Õâ¸öÄ¿µÄ,Ñ­»·ºÍ·ÖÖ§Ìṩ
°ïÖú.

10.1 Ñ­»·
---------
Ñ­»·¾ÍÊÇÖظ´Ò»Ð©ÃüÁîµÄ´úÂë¿é,Èç¹ûÌõ¼þ²»Âú×ã¾ÍÍ˳öÑ­»·.
for loops
for arg in [list]
ÕâÊÇÒ»¸ö»ù±¾µÄÑ­»·½á¹¹.ËüÓëCµÄÏàËƽṹÓкܴó²»Í¬.
for arg in [list]
do
command(s)...
done
×¢Òâ:ÔÚÑ­»·µÄÿ´ÎÖ´ÐÐÖÐ,arg½«Ë³ÐòµÄ´æÈ¡listÖÐÁгöµÄ±äÁ¿.
1 for arg in "$var1" "$var2" "$var3" ... "$varN"
2 # ÔÚµÚ1´ÎÑ­»·ÖÐ, arg = $var1
3 # ÔÚµÚ2´ÎÑ­»·ÖÐ, arg = $var2
4 # ÔÚµÚ3´ÎÑ­»·ÖÐ, arg = $var3
5 # ...
6 # ÔÚµÚn´ÎÑ­»·ÖÐ, arg = $varN
7
8 # ÔÚ[list]ÖеIJÎÊý¼ÓÉÏË«ÒýºÅÊÇΪÁË×èÖ¹µ¥´Ê·ÖÀë.
listÖеIJÎÊýÔÊÐí°üº¬Í¨Åä·û.
Èç¹ûdoºÍforÏëÔÚͬһÐгöÏÖ,ÄÇôÔÚËüÃÇÖ®¼äÐèÒªÌí¼ÓÒ»¸ö";".
for arg in [list]; do
Example 10-1 Ñ­»·µÄÒ»¸ö¼òµ¥Àý×Ó
################################Start Script####################################
###
1 #!/bin/bash
2 # ÁгöËùÓÐÐÐÐÇ.
3
4 for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
5 do
6 echo $planet # Each planet on a separate line.
7 done
8
9 echo
10
11 for planet in "Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"
12 # ËùÓеÄÐÐÐǶ¼ÔÚͬһÐÐÉÏ.
13 # ÍêÕûµÄ'list'×÷Ϊһ¸ö±äÁ¿¶¼·âÔÚ""ÖÐ
14 do
15 echo $planet
16 done
17
18 exit 0
################################End Script######################################
###
×¢Òâ:ÿ¸ö[list]ÖеÄÔªËض¼¿ÉÄÜ°üº¬¶à¸ö²ÎÊý.ÔÚ´¦Àí²ÎÊý×éʱ,ÕâÊǷdz£ÓÐÓõÄ.
ÔÚÕâÖÖÇé¿öÏÂ,ʹÓÃsetÃüÁî(¼ûExample 11-15)À´Ç¿ÖƽâÎöÿ¸ö[list]ÖеÄÔªËØ,
²¢ÇÒ·ÖÅäÿ¸ö½âÎö³öÀ´µÄ²¿·Öµ½Ò»¸öλÖòÎÊýÖÐ.
Example 10-2 ÿ¸ö[list]ÔªËØ´øÁ½¸ö²ÎÊýµÄforÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2 # »¹ÊÇÐÐÐÇ.
3
4 # ·ÖÅäÐÐÐǵÄÃû×ÖºÍËü¾àÌ«ÑôµÄ¾àÀë.
5
6 for planet in "Mercury 36" "Venus 67" "Earth 93" "Mars 142" "Jupiter 483"
7 do
8 set -- $planet # ½âÎö±äÁ¿"planet"²¢ÇÒÉèÖÃλÖòÎÊý.
9 # "--" ½«·ÀÖ¹$planetΪ¿Õ,»òÕßÊÇÒÔÒ»¸öÆÆÕۺſªÍ·.
10
11 # ¿ÉÄÜÐèÒª±£´æԭʼµÄλÖòÎÊý,ÒòΪËüÃDZ»¸²¸ÇÁË.
12 # Ò»ÖÖ·½·¨¾ÍÊÇʹÓÃÊý×é,
13 # original_params=("$@")
14
15 echo "$1 $2,000,000 miles from the sun"
16 #-------two tabs---°Ñºó±ßµÄ0ºÍ$2Á¬½ÓÆðÀ´
17 done
18
19 # (Thanks, S.C., for additional clarification.)
20
21 exit 0
################################End Script######################################
###
¿ÉÒÔÔÚforÑ­»·ÖеÄ[list]λÖ÷ÅÈëÒ»¸ö±äÁ¿
Example 10-3 ÎļþÐÅÏ¢:¶Ô°üº¬ÔÚ±äÁ¿ÖеÄÎļþÁбí½øÐвÙ×÷
################################Start Script####################################
###
1 #!/bin/bash
2 # fileinfo.sh
3
4 FILES="/usr/sbin/accept
5 /usr/sbin/pwck
6 /usr/sbin/chroot
7 /usr/bin/fakefile
8 /sbin/badblocks
9 /sbin/ypbind" # Äã¹ØÐĵÄÎļþÁбí.
10 # ÈÓ½øÈ¥Ò»¸ö¼ÙÎļþ, /usr/bin/fakefile.
11
12 echo
13
14 for file in $FILES
15 do
16
17 if [ ! -e "$file" ] # ¼ì²éÎļþÊÇ·ñ´æÔÚ.
18 then
19 echo "$file does not exist."; echo
20 continue # ¼ÌÐøÏÂÒ»¸ö.
21 fi
22
23 ls -l $file | awk '{ print $9 " file size: " $5 }' # ´òÓ¡2¸öÓò.
24 whatis `basename $file` # ÎļþÐÅÏ¢.
25 # ×¢ÒâwhatisÊý¾Ý¿âÐèÒªÌáÇ°½¨Á¢ºÃ.
26 # ÒªÏë´ïµ½Õâ¸öÄ¿µÄ, ÒÔrootÉí·ÝÔËÐÐ/usr/bin/makewhatis.
27 echo
28 done
29
30 exit 0
################################End Script######################################
###
Èç¹ûÔÚforÑ­»·µÄ[list]ÖÐÓÐͨÅä·û(*ºÍ?),Äǽ«»á²úÉúÎļþÃûÀ©Õ¹,Ò²¾ÍÊÇfile globbing.
Example 10-4 ÔÚforÑ­»·ÖвÙ×÷Îļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # list-glob.sh: ²úÉú [list] ÔÚforÑ­»·ÖÐ, ʹÓà "globbing"
3
4 echo
5
6 for file in *
7 # ^ ÔÚ±í´ïʽÖÐʶ±ðfile globbingʱ,
8 #+ Bash ½«Ö´ÐÐÎļþÃûÀ©Õ¹
9 do
10 ls -l "$file" # ÁгöËùÓÐÔÚ$PWD(µ±Ç°Ä¿Â¼)ÖеÄËùÓÐÎļþ.
11 # »ØÏëÒ»ÏÂ,ͨÅä·û"*"Äܹ»Æ¥ÅäËùÓÐÎļþ,
12 #+ È»¶ø,ÔÚ"globbing"ÖÐ,ÊDz»ÄܱÈÅä"."ÎļþµÄ.
13
14 # If the pattern matches no file, it is expanded to itself.
14 # Èç¹ûûƥÅäµ½ÈκÎÎļþ,ÄÇËü½«À©Õ¹³É×Ô¼º.
15 # ΪÁ˲»ÈÃÕâÖÖÇé¿ö·¢Éú,ÄǾÍÉèÖÃnullglobÑ¡Ïî
16 #+ (shopt -s nullglob).
17 # Thanks, S.C.
18 done
19
20 echo; echo
21
22 for file in [jx]*
23 do
24 rm -f $file # ֻɾ³ýµ±Ç°Ä¿Â¼ÏÂÒÔ"j"»ò"x"¿ªÍ·µÄÎļþ.
25 echo "Removed file \"$file\"".
26 done
27
28 echo
29
30 exit 0
################################End Script######################################
###
ÔÚÒ»¸öforÑ­»·ÖкöÂÔ[list]µÄ»°,½«»áʹѭ»·²Ù×÷$@(´ÓÃüÁîÐд«µÝ¸ø½Å±¾µÄ²ÎÊýÁбí).
Ò»¸ö·Ç³£ºÃµÄÀý×Ó,¼ûExample A-16.
Example 10-5 ÔÚforÑ­»·ÖÐÊ¡ÂÔ[list]
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ʹÓÃÁ½ÖÖ·½·¨À´µ÷ÓÃÕâ¸ö½Å±¾,Ò»ÖÖÊÇ´ø²ÎÊýµÄÇé¿ö,ÁíÒ»ÖÖ²»´ø²ÎÊý.
4 #+ ¹Û²ì´Ë½Å±¾µÄÐÐΪ¸÷ÊÇʲôÑùµÄ?
5
6 for a
7 do
8 echo -n "$a "
9 done
10
11 # The 'in list' missing, therefore the loop operates on '$@'
11 # ûÓÐ[list],ËùÒÔÑ­»·½«²Ù×÷'$@'
12 #+ (°üÀ¨¿Õ°×µÄÃüÁî²ÎÊýÁбí).
13
14 echo
15
16 exit 0
################################End Script######################################
###
Ò²¿ÉÒÔʹÓÃÃüÁîÌæ»»À´²úÉúforÑ­»·µÄ[list].¾ßÌå¼ûExample 12-49,Example 10-10,
ºÍExample 12-43.
Example 10-6 ʹÓÃÃüÁîÌæ»»À´²úÉúforÑ­»·µÄ[list]
################################Start Script####################################
###
1 #!/bin/bash
2 # for-loopcmd.sh: ´ø[list]µÄforÑ­»·
3 #+ [list]ÊÇÓÉÃüÁîÌæ»»²úÉúµÄ.
4
5 NUMBERS="9 7 3 8 37.53"
6
7 for number in `echo $NUMBERS` # for number in 9 7 3 8 37.53
8 do
9 echo -n "$number "
10 done
11
12 echo
13 exit 0
################################End Script######################################
###
ϱßÊÇÒ»¸öÓÃÃüÁîÌæ»»À´²úÉú[list]µÄ¸ü¸´ÔÓµÄÀý×Ó.
Example 10-7 ¶ÔÓÚ¶þ½øÖÆÎļþµÄÒ»¸ögrepÌæ»»
################################Start Script####################################
###
1 #!/bin/bash
2 # bin-grep.sh: ÔÚÒ»¸ö¶þ½øÖÆÎļþÖж¨Î»Æ¥Åä×Ö´®.
3
4 # ¶ÔÓÚ¶þ½øÖÆÎļþµÄÒ»¸ögrepÌæ»»
5 # Óë"grep -a"µÄЧ¹ûÏàËÆ
6
7 E_BADARGS=65
8 E_NOFILE=66
9
10 if [ $# -ne 2 ]
11 then
12 echo "Usage: `basename $0` search_string filename"
13 exit $E_BADARGS
14 fi
15
16 if [ ! -f "$2" ]
17 then
18 echo "File \"$2\" does not exist."
19 exit $E_NOFILE
20 fi
21
22
23 IFS="\n" # ÓÉPaulo Marcel Coelho AragaoÌá³öµÄ½¨Òé.
24 for word in $( strings "$2" | grep "$1" )
25 # "strings" ÃüÁîÁгö¶þ½øÖÆÎļþÖеÄËùÓÐ×Ö·û´®.
26 # Êä³öµ½¹ÜµÀ½»¸ø"grep",È»ºóÓÉgrepÃüÁîÀ´¹ýÂË×Ö·û´®.
27 do
28 echo $word
29 done
30
31 # S.C. Ö¸³ö, ÐÐ23 - 29 ¿ÉÒÔ±»Ï±ߵÄÕâÐÐÀ´´úÌæ,
32 # strings "$2" | grep "$1" | tr -s "$IFS" '[\n*]'
33
34
35 # ÊÔÊÔÓÃ"./bin-grep.sh mem /bin/ls"À´ÔËÐÐÕâ¸ö½Å±¾.
36
37 exit 0
################################End Script######################################
###
´ó²¿·ÖÏàͬ.
Example 10-8 ÁгöϵͳÉϵÄËùÓÐÓû§
################################Start Script####################################
###
1 #!/bin/bash
2 # userlist.sh
3
4 PASSWORD_FILE=/etc/passwd
5 n=1 # User number
6
7 for name in $(awk 'BEGIN{FS=":"}{print $1}' < "$PASSWORD_FILE" )
8 # Óò·Ö¸ô = : ^^^^^^
9 # ´òÓ¡³öµÚÒ»¸öÓò ^^^^^^^^
10 # ´ÓpasswordÎļþÖÐÈ¡µÃÊäÈë ^^^^^^^^^^^^^^^^^
11 do
12 echo "USER #$n = $name"
13 let "n += 1"
14 done
15
16
17 # USER #1 = root
18 # USER #2 = bin
19 # USER #3 = daemon
20 # ...
21 # USER #30 = bozo
22
23 exit 0
24
25 # Á·Ï° :
26 # ------
27 # Ò»¸öÆÕͨÓû§(»òÕßÊÇÒ»¸öÆÕͨÓû§ÔËÐеĽű¾)
28 #+ ÔõôÄܶÁÈ¡/etc/passwordÄØ?
29 # ÕâÊÇ·ñÊÇÒ»¸ö°²È«Â©¶´? ΪʲôÊÇ?Ϊʲô²»ÊÇ?
################################End Script######################################
###
¹ØÓÚÓÃÃüÁîÌæ»»À´²úÉú[list]µÄ×îºóµÄÀý×Ó.
Example 10-9 ÔÚĿ¼µÄËùÓÐÎļþÖвéÕÒÔ´×Ö´®
################################Start Script####################################
###
1 #!/bin/bash
2 # findstring.sh:
3 # ÔÚÒ»¸öÖ¸¶¨Ä¿Â¼µÄËùÓÐÎļþÖвéÕÒÒ»¸öÌض¨µÄ×Ö·û´®.
4
5 directory=/usr/bin/
6 fstring="Free Software Foundation" # ²é¿´ÄǸöÎļþÖаüº¬FSF.
7
8 for file in $( find $directory -type f -name '*' | sort )
9 do
10 strings -f $file | grep "$fstring" | sed -e "s%$directory%%"
11 # ÔÚ"sed"±í´ïʽÖÐ,
12 #+ ÎÒÃDZØÐëÌæ»»µôÕý³£µÄÌæ»»·Ö¸ô·û"/",
13 #+ ÒòΪ"/"ÅöÇÉÊÇÎÒÃÇÐèÒª¹ýÂ˵Ä×Ö´®Ö®Ò».
14 # Èç¹û²»ÓÃ"%"´úÌæ"/"×÷Ϊ·Ö¸ô·û,ÄÇôÕâ¸ö²Ù×÷½«Ê§°Ü,²¢¸ø³öÒ»¸ö´íÎóÏûÏ¢.(ÊÔÊÔ)
15 done
16
17 exit 0
18
19 # Á·Ï° (easy):
20 # ------------
21 # ½«ÄÚ²¿ÓõÄ$directoryºÍ$fstring±äÁ¿,ÓôÓ
22 #+ ÃüÁîÐвÎÊý´úÌæ.
################################End Script######################################
###
forÑ­»·µÄÊä³öÒ²¿ÉÒÔͨ¹ý¹ÜµÀ´«µÝµ½Ò»¸ö»ò¶à¸öÃüÁîÖÐ.
Example 10-10 ÁгöĿ¼ÖÐËùÓеķûºÅÁ¬½ÓÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # symlinks.sh: ÁгöĿ¼ÖÐËùÓеķûºÅÁ¬½ÓÎļþ.
3
4
5 directory=${1-`pwd`}
6 # Èç¹ûûÓÐÆäËûµÄÌØÊâÖ¸¶¨,
7 #+ ĬÈÏΪµ±Ç°¹¤×÷Ŀ¼.
8 # ϱߵĴúÂë¿é,ºÍÉϱßÕâ¾äµÈ¼Û.
9 # ----------------------------------------------------------
10 # ARGS=1 # ÐèÒªÒ»¸öÃüÁîÐвÎÊý.
11 #
12 # if [ $# -ne "$ARGS" ] # Èç¹û²»ÊÇÒ»¸ö²ÎÊýµÄ»°...
13 # then
14 # directory=`pwd` # µ±Ç°¹¤×÷Ŀ¼
15 # else
16 # directory=$1
17 # fi
18 # ----------------------------------------------------------
19
20 echo "symbolic links in directory \"$directory\""
21
22 for file in "$( find $directory -type l )" # -type l ¾ÍÊÇ·ûºÅÁ¬½ÓÎļþ
23 do
24 echo "$file"
25 done | sort # ·ñÔòÁгöµÄÎļþ½«ÊÇδÅÅÐòµÄ
26 # ÑϸñÉÏ˵,´Ë´¦²¢²»Ò»¶¨·ÇÒªÒ»¸öÑ­»·²»¿É,
27 #+ ÒòΪ"find"ÃüÁîµÄ½á¹û½«±»À©Õ¹³ÉÒ»¸öµ¥´Ê.
28 # È»¶ø,ÕâÖÖ·½Ê½ºÜÈÝÒ×Àí½âºÍ˵Ã÷.
29
30 # Dominik 'Aeneas' Schnitzer Ö¸³ö,
31 #+ Èç¹ûû½« $( find $directory -type l )ÓÃ""ÒýÓÃÆðÀ´µÄ»°
32 #+ ÄÇô½«»á°ÑÒ»¸ö´øÓпհײ¿·ÖµÄÎļþÃû²ð³ÉÒÔ¿Õ°×·Ö¸ôµÄÁ½²¿·Ö(ÎļþÃûÖÐÔÊÐíÓпհ×).
33 # ¼´Ê¹ÕâÖ»½«È¡³öÿ¸ö²ÎÊýµÄµÚÒ»¸öÓò.
34
35 exit 0
36
37
38 # Jean Helou ½¨ÒéʹÓÃϱߵķ½·¨:
39
40 echo "symbolic links in directory \"$directory\""
41 # µ±Ç°IFSµÄ±¸·Ý.ҪСÐÄʹÓÃÕâ¸öÖµ.
42 OLDIFS=$IFS
43 IFS=:
44
45 for file in $(find $directory -type l -printf "%p$IFS")
46 do # ^^^^^^^^^^^^^^^^
47 echo "$file"
48 done|sort
################################End Script######################################
###
Ñ­»·µÄÊä³ö¿ÉÒÔÖض¨Ïòµ½ÎļþÖÐ,ÎÒÃǶÔÉϱߵÄÀý×Ó×öÁËÒ»µãÐÞ¸Ä.
Example 10-11 ½«Ä¿Â¼ÖеķûºÅÁ¬½ÓÎļþÃû±£´æµ½Ò»¸öÎļþÖÐ
################################Start Script####################################
###
1 #!/bin/bash
2 # symlinks.sh: ÁгöĿ¼ÖÐËùÓеķûºÅÁ¬½ÓÎļþ.
3
4 OUTFILE=symlinks.list # ±£´æµÄÎļþ
5
6 directory=${1-`pwd`}
7 # Èç¹ûûÓÐÆäËûµÄÌØÊâÖ¸¶¨,
8 #+ ĬÈÏΪµ±Ç°¹¤×÷Ŀ¼.
9
10
11 echo "symbolic links in directory \"$directory\"" > "$OUTFILE"
12 echo "---------------------------" >> "$OUTFILE"
13
14 for file in "$( find $directory -type l )" # -type l Ϊ·ûºÅÁ´½Ó
15 do
16 echo "$file"
17 done | sort >> "$OUTFILE" # Ñ­»·µÄÊä³ö
18 # ^^^^^^^^^^^^^ Öض¨Ïòµ½Ò»¸öÎļþÖÐ
19
20 exit 0
################################End Script######################################
###
ÓÐÒ»Öַdz£ÏñCÓïÑÔµÄforÑ­»·µÄÓï·¨ÐÎʽ.ÕâÐèҪʹÓÃ(()).
Example 10-12 Ò»¸öC·ç¸ñµÄforÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2 # Á½ÖÖÑ­»·µ½10µÄ·½·¨.
3
4 echo
5
6 # ±ê×¼Óï·¨.
7 for a in 1 2 3 4 5 6 7 8 9 10
8 do
9 echo -n "$a "
10 done
11
12 echo; echo
13
14 # +==========================================+
15
16 # ÏÖÔÚ, ÈÃÎÒÃÇÓÃC·ç¸ñµÄÓï·¨×öͬÑùµÄÊÂ.
17
18 LIMIT=10
19
20 for ((a=1; a <= LIMIT ; a++)) # Double parentheses, and "LIMIT" with no "$".
20 for ((a=1; a <= LIMIT ; a++)) # Ë«Ô²À¨ºÅ, ²¢ÇÒ"LIMIT"±äÁ¿Ç°±ßûÓÐ "$".
21 do
22 echo -n "$a "
23 done # ÕâÊÇÒ»¸ö½èÓÃ'ksh93'µÄ½á¹¹.
24
25 echo; echo
26
27 # +=========================================================================+
28
29 # ÈÃÎÒÃÇʹÓÃCµÄ¶ººÅ²Ù×÷·û,À´Í¬Ê±Ôö¼ÓÁ½¸ö±äÁ¿µÄÖµ.
30
31 for ((a=1, b=1; a <= LIMIT ; a++, b++)) # ¶ººÅ½«Í¬Ê±½øÐÐ2Ìõ²Ù×÷.
32 do
33 echo -n "$a-$b "
34 done
35
36 echo; echo
37
38 exit 0
################################End Script######################################
###
²Î¿¼Example 26-15,Example 26-16,ºÍExample A-6.
---
ÏÖÔÚÀ´Ò»¸öÏÖʵÉú»îÖÐʹÓõÄforÑ­»·.
Example 10-13 ÔÚbatch modeÖÐʹÓÃefax
################################Start Script####################################
###
1 #!/bin/bash
2 # Faxing ('fax' ±ØÐëÒѾ­±»°²×°¹ýÁË).
3
4 EXPECTED_ARGS=2
5 E_BADARGS=65
6
7 if [ $# -ne $EXPECTED_ARGS ]
8 # ¼ì²éÃüÁîÐвÎÊýµÄ¸öÊýÊÇ·ñÕýÈ·.
9 then
10 echo "Usage: `basename $0` phone# text-file"
11 exit $E_BADARGS
12 fi
13
14
15 if [ ! -f "$2" ]
16 then
17 echo "File $2 is not a text file"
18 exit $E_BADARGS
19 fi
20
21
22 fax make $2 # ´ÓÎı¾ÎļþÖд´½¨´«Õæ¸ñʽµÄÎļþ.
23
24 for file in $(ls $2.0*) # Á¬½Óת»»¹ýµÄÎļþ.
25 # ÔÚ±äÁ¿ÁбíÖÐʹÓÃͨÅä·û.
26 do
27 fil="$fil $file"
28 done
29
30 efax -d /dev/ttyS3 -o1 -t "T$1" $fil # ¸É»îµÄµØ·½.
31
32
33 # S.C. Ö¸³ö, ͨ¹ýϱߵÄÃüÁî¿ÉÒÔÊ¡È¥forÑ­»·.
34 # efax -d /dev/ttyS3 -o1 -t "T$1" $2.0*
35 # µ«Õâ²¢²»Ê®·ÖÓн²½âÒâÒå[ºÙºÙ].
36
37 exit 0
################################End Script######################################
###
while
ÕâÖֽṹÔÚÑ­»·µÄ¿ªÍ·ÅжÏÌõ¼þÊÇ·ñÂú×ã,Èç¹ûÌõ¼þÒ»Ö±Âú×ã,ÄǾÍһֱѭ»·ÏÂÈ¥(0ΪÍ˳ö
Âë).ÓëforÑ­»·µÄÇø±ðÊÇ,ÕâÖֽṹÊʺÏÓÃÔÚÑ­»·´ÎÊýδ֪µÄÇé¿öÏÂ.
while [condition]
do
command...
done
ºÍforÑ­»·Ò»Ñù,Èç¹ûÏë°ÑdoºÍÌõ¼þ·Åµ½Í¬Ò»ÐÐÉÏ»¹ÊÇÐèÒªÒ»¸ö";".
while [condition]; do
×¢ÒâÒ»ÏÂijÖÖÌض¨µÄwhileÑ­»·,±ÈÈçgetopts½á¹¹,ºÃÏñºÍÕâÀïËù½éÉܵÄÄ£°æÓеãÍѽÚ.
Example 10-14 ¼òµ¥µÄwhileÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2
3 var0=0
4 LIMIT=10
5
6 while [ "$var0" -lt "$LIMIT" ]
7 do
8 echo -n "$var0 " # -n ½«»á×èÖ¹²úÉúÐÂÐÐ.
9 # ^ ¿Õ¸ñ,Êý×ÖÖ®¼äµÄ·Ö¸ô.
10
11 var0=`expr $var0 + 1` # var0=$(($var0+1)) Ò²¿ÉÒÔ.
12 # var0=$((var0 + 1)) Ò²¿ÉÒÔ.
13 # let "var0 += 1" Ò²¿ÉÒÔ.
14 done # ʹÓÃÆäËûµÄ·½·¨Ò²ÐÐ.
15
16 echo
17
18 exit 0
################################End Script######################################
###
Example 10-15 ÁíÒ»¸öwhileÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo
4 # µÈ¼ÛÓÚ:
5 while [ "$var1" != "end" ] # while test "$var1" != "end"
6 do
7 echo "Input variable #1 (end to exit) "
8 read var1 # Ϊʲô²»Ê¹ÓÃ'read $var1'?
9 echo "variable #1 = $var1" # ÒòΪ°üº¬"#"×Ö·û,ËùÒÔÐèÒª""
10 # Èç¹ûÊäÈëΪ'end',ÄÇô¾ÍÔÚÕâÀïecho.
11 # ²»ÔÚÕâÀïÅжϽáÊø,ÔÚÑ­»·¶¥ÅжÏ.
12 echo
13 done
14
15 exit 0
################################End Script######################################
###
Ò»¸öwhileÑ­»·¿ÉÒÔÓжà¸öÅжÏÌõ¼þ,µ«ÊÇÖ»ÓÐ×îºóÒ»¸ö²ÅÄܾö¶¨ÊÇ·ñÍ˳öÑ­»·.È»¶øÕâÐè
ÒªÒ»ÖÖÓе㲻ͬµÄÑ­»·Óï·¨.
Example 10-16 ¶àÌõ¼þµÄwhileÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2
3 var1=unset
4 previous=$var1
5
6 while echo "previous-variable = $previous"
7 echo
8 previous=$var1
9 [ "$var1" != end ] # ¼Ç¼֮ǰµÄ$var1.
10 # Õâ¸ö"while"Ñ­»·ÖÐÓÐ4¸öÌõ¼þ, µ«ÊÇÖ»ÓÐ×îºóÒ»¸öÄÜ¿ØÖÆÑ­»·.
11 # Í˳ö״̬ÓɵÚ4¸öÌõ¼þ¾ö¶¨.
12 do
13 echo "Input variable #1 (end to exit) "
14 read var1
15 echo "variable #1 = $var1"
16 done
17
18 # ³¢ÊÔÀí½âÕâ¸ö½Å±¾µÄÔËÐйý³Ì.
19 # ÕâÀﻹÊÇÓеãС¼¼ÇɵÄ.
20
21 exit 0
################################End Script######################################
###
ÓëforÑ­»·Ò»Ñù,whileÑ­»·Ò²¿Éͨ¹ý(())À´Ê¹ÓÃC·ç¸ñÓï·¨.(¼ûExample 9-30)
Example 10-17 C·ç¸ñµÄwhileÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2 # wh-loopc.sh: Ñ­»·10´ÎµÄwhileÑ­»·.
3
4 LIMIT=10
5 a=1
6
7 while [ "$a" -le $LIMIT ]
8 do
9 echo -n "$a "
10 let "a+=1"
11 done # µ½Ä¿Ç°ÎªÖ¹¶¼Ã»Ê²Ã´ÁîÈ˾ªÆæµÄµØ·½.
12
13 echo; echo
14
15 # +=================================================================+
16
17 # ÏÖÔÚ, Öظ´C·ç¸ñµÄÓï·¨.
18
19 ((a = 1)) # a=1
20 # Ë«Ô²À¨ºÅÔÊÐí¸³ÖµÁ½±ßµÄ¿Õ¸ñ,¾ÍÏñCÓïÑÔÒ»Ñù.
21
22 while (( a <= LIMIT )) # Ë«Ô²À¨ºÅ, ±äÁ¿Ç°±ßûÓÐ"$".
23 do
24 echo -n "$a "
25 ((a += 1)) # let "a+=1"
26 # Yes, ¿´µ½ÁË°É.
27 # Ë«Ô²À¨ºÅÔÊÐíÏñC·ç¸ñµÄÓï·¨Ò»ÑùÔö¼Ó±äÁ¿µÄÖµ.
28 done
29
30 echo
31
32 # ÏÖÔÚ,C³ÌÐòÔ±¿ÉÒÔÔÚBashÖÐÕÒµ½»Ø¼ÒµÄ¸Ð¾õÁË°É.
33
34 exit 0
################################End Script######################################
###
×¢Òâ:whileÑ­»·µÄstdin¿ÉÒÔÓÃ<À´Öض¨Ïòµ½Îļþ.
whildÑ­»·µÄstdinÖ§³Ö¹ÜµÀ.
until
Õâ¸ö½á¹¹ÔÚÑ­»·µÄ¶¥²¿ÅжÏÌõ¼þ,²¢ÇÒÈç¹ûÌõ¼þһֱΪfalseÄǾÍһֱѭ»·ÏÂÈ¥.(Óëwhile
Ïà·´)
until [condition-is-true]
do
command...
done
×¢Òâ: untilÑ­»·µÄÅжÏÔÚÑ­»·µÄ¶¥²¿,ÕâÓëijЩ±à³ÌÓïÑÔÊDz»Í¬µÄ.
ÓëforÑ­»·Ò»Ñù,Èç¹ûÏë°ÑdoºÍÌõ¼þ·ÅÔÚÒ»ÐÐÀï,¾ÍʹÓÃ";".
until [condition-is-true]; do
Example 10-18 untilÑ­»·
################################Start Script####################################
###
1 #!/bin/bash
2
3 END_CONDITION=end
4
5 until [ "$var1" = "$END_CONDITION" ]
6 # ÔÚÑ­»·µÄ¶¥²¿ÅжÏÌõ¼þ.
7 do
8 echo "Input variable #1 "
9 echo "($END_CONDITION to exit)"
10 read var1
11 echo "variable #1 = $var1"
12 echo
13 done
14
15 exit 0
################################End Script######################################
###

10.2 ǶÌ×Ñ­»·
-------------
ǶÌ×Ñ­»·¾ÍÊÇÔÚÒ»¸öÑ­»·Öл¹ÓÐÒ»¸öÑ­»·,ÄÚ²¿Ñ­»·ÔÚÍⲿѭ»·ÌåÖÐ.ÔÚÍⲿѭ»·µÄÿ´ÎÖ´Ðйý
³ÌÖж¼»á´¥·¢ÄÚ²¿Ñ­»·,Ö±µ½ÄÚ²¿Ñ­»·Ö´ÐнáÊø.Íⲿѭ»·Ö´ÐÐÁ˶àÉÙ´Î,ÄÚ²¿Ñ­»·¾ÍÍê³É¶àÉÙ
´Î.µ±È»,²»ÂÛÊÇÍⲿѭ»·»òÄÚ²¿Ñ­»·µÄbreakÓï¾ä¶¼»á´ò¶Ï´¦Àí¹ý³Ì.
Example 10-19 ǶÌ×Ñ­»·
################################Start Script####################################
###
1 #!/bin/bash
2 # nested-loop.sh: ǶÌ×µÄ"for" Ñ­»·.
3
4 outer=1 # ÉèÖÃÍⲿѭ»·¼ÆÊý.
5
6 # ¿ªÊ¼Íⲿѭ»·.
7 for a in 1 2 3 4 5
8 do
9 echo "Pass $outer in outer loop."
10 echo "---------------------"
11 inner=1 # ÖØÉèÄÚ²¿Ñ­»·µÄ¼ÆÊý.
12
13 # ===============================================
14 # ¿ªÊ¼ÄÚ²¿Ñ­»·.
15 for b in 1 2 3 4 5
16 do
17 echo "Pass $inner in inner loop."
18 let "inner+=1" # Ôö¼ÓÄÚ²¿Ñ­»·¼ÆÊý.
19 done
20 # ÄÚ²¿Ñ­»·½áÊø.
21 # ===============================================
22
23 let "outer+=1" # Ôö¼ÓÍⲿѭ»·µÄ¼ÆÊý.
24 echo # ÿ´ÎÍⲿѭ»·Ö®¼äµÄ¼ä¸ô.
25 done
26 # Íⲿѭ»·½áÊø.
27
28 exit 0
################################End Script######################################
###

10.3 Ñ­»·¿ØÖÆ
-------------
Ó°ÏìÑ­»·ÐÐΪµÄÃüÁî
break,continue
breakºÍcontinueÕâÁ½¸öÑ­»·¿ØÖÆÃüÁî[1]ÓëÆäËüÓïÑÔµÄÀàËÆÃüÁîµÄÐÐΪÊÇÏàͬµÄ.break
ÃüÁ»áÌø³öÑ­»·,continueÃüÁ»áÌø¹ý±¾´ÎÑ­»·Ï±ߵÄÓï¾ä,Ö±½Ó½øÈëÏ´ÎÑ­»·.
Example 10-20 breakºÍcontinueÃüÁîÔÚÑ­»·ÖеÄЧ¹û
################################Start Script####################################
###
1 #!/bin/bash
2
3 LIMIT=19 # ÉÏÏÞ
4
5 echo
6 echo "Printing Numbers 1 through 20 (but not 3 and 11)."
7
8 a=0
9
10 while [ $a -le "$LIMIT" ]
11 do
12 a=$(($a+1))
13
14 if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # ³ýÁË3ºÍ11.
15 then
16 continue # Ìø¹ý±¾´ÎÑ­»·Ê£ÏµÄÓï¾ä.
17 fi
18
19 echo -n "$a " # ÔÚ$aµÈÓÚ3ºÍ11µÄʱºò,Õâ¾ä½«²»»áÖ´ÐÐ.
20 done
21
22 # Á·Ï°:
23 # Ϊʲôѭ»·»á´òÓ¡³ö20?
24
25 echo; echo
26
27 echo Printing Numbers 1 through 20, but something happens after 2.
28
29 ##################################################################
30
31 # Same loop, but substituting 'break' for 'continue'.
31 # ͬÑùµÄÑ­»·, µ«ÊÇÓÃ'break'À´´úÌæ'continue'.
32
33 a=0
34
35 while [ "$a" -le "$LIMIT" ]
36 do
37 a=$(($a+1))
38
39 if [ "$a" -gt 2 ]
40 then
41 break # ½«»áÌø³öÕû¸öÑ­»·.
42 fi
43
44 echo -n "$a "
45 done
46
47 echo; echo; echo
48
49 exit 0
################################End Script######################################
###
breakÃüÁî¿ÉÒÔ´øÒ»¸ö²ÎÊý.Ò»¸ö²»´ø²ÎÊýµÄbreakÑ­»·Ö»ÄÜÍ˳ö×îÄÚ²ãµÄÑ­»·,¶øbreak N
¿ÉÒÔÍ˳öN²ãÑ­»·.
Example 10-21 ¶à²ãÑ­»·µÄÍ˳ö
################################Start Script####################################
###
1 #!/bin/bash
2 # break-levels.sh: Í˳öÑ­»·.
3
4 # "break N" Í˳öN²ãÑ­»·.
5
6 for outerloop in 1 2 3 4 5
7 do
8 echo -n "Group $outerloop: "
9
10 # --------------------------------------------------------
11 for innerloop in 1 2 3 4 5
12 do
13 echo -n "$innerloop "
14
15 if [ "$innerloop" -eq 3 ]
16 then
17 break # ÊÔÊÔ break 2 À´¿´¿´·¢Éúʲô.
18 # (ÄÚ²¿Ñ­»·ºÍÍⲿѭ»·¶¼±»Í˳öÁË.)
19 fi
20 done
21 # --------------------------------------------------------
22
23 echo
24 done
25
26 echo
27
28 exit 0
################################End Script######################################
###
continueÃüÁîÒ²¿ÉÒÔ´øÒ»¸ö²ÎÊý.Ò»¸ö²»´ø²ÎÊýµÄcontinueÃüÁîֻȥµô±¾´ÎÑ­»·µÄÊ£Óà´úÂë
.¶øcontinue N½«»á°ÑN²ãÑ­»·Ê£ÓàµÄ´úÂ붼ȥµô,µ«ÊÇÑ­»·µÄ´ÎÊý²»±ä.
Example 10-22 ¶à²ãÑ­»·µÄcontinue
################################Start Script####################################
###
1 #!/bin/bash
2 # "continue N" ÃüÁî, ½«ÈÃN²ãµÄÑ­»·È«²¿±»continue.
3
4 for outer in I II III IV V # Íⲿѭ»·
5 do
6 echo; echo -n "Group $outer: "
7
8 # --------------------------------------------------------------------
9 for inner in 1 2 3 4 5 6 7 8 9 10 # ÄÚ²¿Ñ­»·
10 do
11
12 if [ "$inner" -eq 7 ]
13 then
14 continue 2 # continue 2²ã, Ò²¾ÍÊǵ½outerÑ­»·ÉÏ.
15 # ½«"continue 2"Ì滻Ϊһ¸öµ¥¶ÀµÄ"continue"
16 # À´¿´Ò»ÏÂÒ»¸öÕý³£Ñ­»·µÄÐÐΪ.
17 fi
18
19 echo -n "$inner " # 7 8 9 10 ½«²»»á±»echo
20 done
21 # --------------------------------------------------------------------
22 #ÒëÕß×¢:Èç¹ûÔÚ´Ë´¦Ìí¼ÓechoµÄ»°,µ±È»Ò²²»»áÊä³ö.
23 done
24
25 echo; echo
26
27 # Á·Ï°:
28 # ×¼±¸Ò»¸öÓÐÒâÒåµÄ"continue N"µÄʹÓÃ,·ÅÔڽű¾ÖÐ.
29
30 exit 0
################################End Script######################################
###
Example 10-23 ÔÚʵ¼ÊµÄÈÎÎñÖÐʹÓÃ"continue N"
################################Start Script####################################
###
1 # Albert Reiner ¸ø³öÁËÒ»¸ö¹ØÓÚʹÓÃ"continue N"µÄÀý×Ó:
2 # ---------------------------------------------------
3
4 # Suppose I have a large number of jobs that need to be run, with
5 #+ any data that is to be treated in files of a given name pattern in a
6 #+ directory. There are several machines that access this directory, and
7 #+ I want to distribute the work over these different boxen. Then I
8 #+ usually nohup something like the following on every box:
9
10 while true
11 do
12 for n in .iso.*
13 do
14 [ "$n" = ".iso.opts" ] && continue
15 beta=${n#.iso.}
16 [ -r .Iso.$beta ] && continue
17 [ -r .lock.$beta ] && sleep 10 && continue
18 lockfile -r0 .lock.$beta || continue
19 echo -n "$beta: " `date`
20 run-isotherm $beta
21 date
22 ls -alF .Iso.$beta
23 [ -r .Iso.$beta ] && rm -f .lock.$beta
24 continue 2
25 done
26 break
27 done
28
29 # The details, in particular the sleep N, are particular to my
30 #+ application, but the general pattern is:
31
32 while true
33 do
34 for job in {pattern}
35 do
36 {job already done or running} && continue
37 {mark job as running, do job, mark job as done}
38 continue 2
39 done
40 break # Or something like `sleep 600' to avoid termination.
41 done
42
43 # This way the script will stop only when there are no more jobs to do
44 #+ (including jobs that were added during runtime). Through the use
45 #+ of appropriate lockfiles it can be run on several machines
46 #+ concurrently without duplication of calculations [which run a couple
47 #+ of hours in my case, so I really want to avoid this]. Also, as search
48 #+ always starts again from the beginning, one can encode priorities in
49 #+ the file names. Of course, one could also do this without `continue 2',
50 #+ but then one would have to actually check whether or not some job
51 #+ was done (so that we should immediately look for the next job) or not
52 #+ (in which case we terminate or sleep for a long time before checking
53 #+ for a new job).
################################End Script######################################
###
×¢Òâ:continue N½á¹¹Èç¹û±»ÓÃÔÚÒ»¸öÓÐÒâÒåµÄÉÏÏÂÎÄÖеĻ°,ÍùÍù¶¼ºÜÄÑÀí½â,²¢ÇÒ¼¼ÇÉÐÔ
ºÜ¸ß.ËùÒÔ×îºÃµÄ·½·¨¾ÍÊǾ¡Á¿±ÜÃâËü.
×¢ÒâÊÂÏî:
[1] ÕâÁ½¸öÃüÁîÊÇshellµÄÄÚ½¨ÃüÁî,¶ø²»ÏñÆäËüµÄÑ­»·ÃüÁîÄÇÑù,±ÈÈçwhileºÍcase,ÕâÁ½¸ö
Êǹؼü×Ö.

10.4 ²âÊÔÓë·ÖÖ§(caseºÍselect½á¹¹)
---------------------------------
caseºÍselect½á¹¹ÔÚ¼¼ÊõÉÏ˵²»ÊÇÑ­»·,ÒòΪËüÃDz¢²»¶Ô¿ÉÖ´ÐеĴúÂë¿é½øÐеü´ú.µ«ÊǺÍÑ­»·
ÏàËƵÄÊÇ,ËüÃÇÒ²ÒÀ¿¿ÔÚ´úÂë¿éµÄ¶¥²¿»òµ×²¿µÄÌõ¼þÅжÏÀ´¾ö¶¨³ÌÐòµÄ·ÖÖ§.
ÔÚ´úÂë¿éÖпØÖƳÌÐò·ÖÖ§
case (in) / esac
ÔÚshellÖеÄcaseͬC/C++ÖеÄswitch½á¹¹ÊÇÏàͬµÄ.ËüÔÊÐíͨ¹ýÅжÏÀ´Ñ¡Ôñ´úÂë¿éÖжàÌõ
·¾¶ÖеÄÒ»Ìõ.
case "$variable" in
"$condition1")
command...
;;
"$condition1")
command...
;;
esac
×¢Òâ: ¶Ô±äÁ¿Ê¹ÓÃ""²¢²»ÊÇÇ¿ÖƵÄ,ÒòΪ²»»á·¢Éúµ¥´Ê·ÖÀë.
ÿ¾ä²âÊÔÐÐ,¶¼ÒÔÓÒСÀ¨ºÅ)½áβ.
ÿ¸öÌõ¼þ¿é¶¼ÒÔÁ½¸ö·ÖºÅ½áβ;;.
case¿éµÄ½áÊøÒÔesac(caseµÄ·´Ïòƴд)½áβ.
Example 10-24 ʹÓÃcase
################################Start Script####################################
###
1 #!/bin/bash
2 # ²âÊÔ×Ö·û´®·¶Î§
3
4 echo; echo "Hit a key, then hit return."
5 read Keypress
6
7 case "$Keypress" in
8 [[:lower:]] ) echo "Lowercase letter";;
9 [[:upper:]] ) echo "Uppercase letter";;
10 [0-9] ) echo "Digit";;
11 * ) echo "Punctuation, whitespace, or other";;
12 esac # Allows ranges of characters in [square brackets],
12 esac # ÔÊÐí×Ö·û´®µÄ·¶Î§³öÏÖÔÚ[]ÖÐ,
13 #+ or POSIX ranges in [[double square brackets.
13 #+ »òÕßPOSIX·¶Î§ÔÚ[[ÖÐ.
14
15 # ÔÚÕâ¸öÀý×ӵĵÚÒ»¸ö°æ±¾ÖÐ,
16 #+ ²âÊÔ´óдºÍСд×Ö·û´®Ê¹ÓõÄÊÇ
17 #+ [a-z] ºÍ [A-Z].
18 # ÕâÖÖÓ÷¨½«²»»áÔÚijЩÌض¨µÄ³¡ºÏ»òLinux·¢ÐаæÖÐÕý³£¹¤×÷.
19 # POSIX ¸ü¾ß¿ÉÒÆÖ²ÐÔ.
20 # ¸ÐлFrank Wang Ö¸³öÕâµã.
21
22 # Á·Ï°:
23 # -----
24 # ¾ÍÏñÕâ¸ö½Å±¾Ëù±íÏÖµÄ,ËüÖ»ÔÊÐíµ¥¸ö°´¼ü,È»ºó¾Í½áÊøÁË.
25 # ÐÞ¸ÄÕâ¸ö½Å±¾,ÈÃËüÄܹ»½ÓÊÜÖظ´ÊäÈë,
26 #+ ±¨¸æÿ¸ö°´¼ü,²¢ÇÒÖ»ÓÐÔÚ"X"±»¼üÈëʱ²Å½áÊø.
27 # °µÊ¾: ½«ÕâЩ´úÂ붼ÓÃ"while"Ñ­»·È¦ÆðÀ´.
28
29 exit 0
################################End Script######################################
###
Example 10-25 ʹÓÃcaseÀ´´´½¨²Ëµ¥
################################Start Script####################################
###
1 #!/bin/bash
2
3 # δ¾­´¦ÀíµÄµØÖ·×ÊÁÏ
4
5 clear # ÇåÆÁ.
6
7 echo " Contact List"
8 echo " ------- ----"
9 echo "Choose one of the following persons:"
10 echo
11 echo "[E]vans, Roland"
12 echo "[J]ones, Mildred"
13 echo "[S]mith, Julie"
14 echo "[Z]ane, Morris"
15 echo
16
17 read person
18
19 case "$person" in
20 # ×¢Òâ,±äÁ¿ÊDZ»ÒýÓõÄ.
21
22 "E" | "e" )
23 # ½ÓÊÜ´óд»òСдÊäÈë.
24 echo
25 echo "Roland Evans"
26 echo "4321 Floppy Dr."
27 echo "Hardscrabble, CO 80753"
28 echo "(303) 734-9874"
29 echo "(303) 734-9892 fax"
30 echo "revans@zzy.net"
31 echo "Business partner & old friend"
32 ;;
33 # ×¢Òâ,ÔÚÿ¸öÑ¡Ïîºó±ß¶¼ÐèÒªÒÔ;;½áβ.
34
35 "J" | "j" )
36 echo
37 echo "Mildred Jones"
38 echo "249 E. 7th St., Apt. 19"
39 echo "New York, NY 10009"
40 echo "(212) 533-2814"
41 echo "(212) 533-9972 fax"
42 echo "milliej@loisaida.com"
43 echo "Ex-girlfriend"
44 echo "Birthday: Feb. 11"
45 ;;
46
47 # ºó±ßµÄSmithºÍZaneµÄÐÅÏ¢ÔÚÕâÀï¾ÍÊ¡ÂÔÁË.
48
49 * )
50 # ĬÈÏÑ¡Ïî.
51 # ¿ÕÊäÈë(ÇÃRETURN).
52 echo
53 echo "Not yet in database."
54 ;;
55
56 esac
57
58 echo
59
60 # Á·Ï°:
61 # -----
62 # ÐÞ¸ÄÕâ¸ö½Å±¾,ÈÃËüÄܹ»½ÓÊܶàÊäÈë,
63 #+ ²¢ÇÒÄܹ»ÏÔʾ¶à¸öµØÖ·.
64
65 exit 0
################################End Script######################################
###
Ò»¸öcaseµÄÌØÊâÓ÷¨,ÓÃÀ´²âÊÔÃüÁîÐвÎÊý.
################################Start Script####################################
###
1 #! /bin/bash
2
3 case "$1" in
4 "") echo "Usage: ${0##*/} <filename>"; exit $E_PARAM;; # ûÓÐÃüÁîÐвÎÊý,
5 # »òÕßµÚÒ»¸ö²ÎÊýΪ¿Õ.
6 # Note that ${0##*/} is ${var##pattern} param substitution. Net result is $0.
6 # ×¢Òâ:${0##*/} ÊÇ${var##pattern} ÕâÖÖģʽµÄÌæ»». µÃµ½µÄ½á¹ûÊÇ$0.
7
8 -*) FILENAME=./$1;; # Èç¹û´«µÝ½øÀ´µÄÎļþÃû²ÎÊý($1)ÒÔÒ»¸öÆÆÕۺſªÍ·,
9 #+ ÄÇôÓÃ./$1À´´úÌæ
10 #+ ÕâÑùºó±ßµÄÃüÁ²»»á°ÑËü×÷Ϊһ¸öÑ¡ÏîÀ´½âÊÍ.
11
12 * ) FILENAME=$1;; # ·ñÔò, $1.
13 esac
################################End Script######################################
###
ÕâÊÇÒ»¸ö¸üÈÝÒ׶®µÄÃüÁîÐвÎÊý´¦ÀíµÄÒ»¸öÀý×Ó.
################################Start Script####################################
###
1 #! /bin/bash
2
3
4 while [ $# -gt 0 ]; do # Ö±µ½ÄãÓÃÍêËùÓеIJÎÊý...
5 case "$1" in
6 -d|--debug)
7 # "-d" or "--debug" parameter?
8 DEBUG=1
9 ;;
10 -c|--conf)
11 CONFFILE="$2"
12 shift
13 if [ ! -f $CONFFILE ]; then
14 echo "Error: Supplied file doesn't exist!"
15 exit $E_CONFFILE # Îļþû·¢ÏÖ´íÎó.
16 fi
17 ;;
18 esac
19 shift # ¼ì²éʣϵIJÎÊý.
20 done
21
22 # À´×ÔStefano FalsettoµÄ "Log2Rot" ½Å±¾,
23 #+ ËûµÄ"rottlog" °üµÄÒ»²¿·Ö.
24 # ÊÚȨʹÓÃ.
################################End Script######################################
###
Example 10-26 ʹÓÃÃüÁîÌæ»»À´²úÉúcase±äÁ¿
################################Start Script####################################
###
1 #!/bin/bash
2 # case-cmd.sh: ʹÓÃÃüÁîÌæ»»À´²úÉú"case"±äÁ¿
3
4 case $( arch ) in # "arch" ·µ»Ø»úÆ÷µÄÀàÐÍ.
5 # µÈ¼ÛÓÚ 'uname -m' ...
6 i386 ) echo "80386-based machine";;
7 i486 ) echo "80486-based machine";;
8 i586 ) echo "Pentium-based machine";;
9 i686 ) echo "Pentium2+-based machine";;
10 * ) echo "Other type of machine";;
11 esac
12
13 exit 0
################################End Script######################################
###
case½á¹¹Ò²¿ÉÒÔ¹ýÂËglobbingģʽµÄ×Ö·û´®.
Example 10-27 ¼òµ¥×Ö·û´®Æ¥Åä
################################Start Script####################################
###
1 #!/bin/bash
2 # match-string.sh: ¼òµ¥×Ö·û´®Æ¥Åä
3
4 match_string ()
5 {
6 MATCH=0
7 NOMATCH=90
8 PARAMS=2 # º¯ÊýÐèÒª2¸ö²ÎÊý.
9 BAD_PARAMS=91
10
11 [ $# -eq $PARAMS ] || return $BAD_PARAMS
12
13 case "$1" in
14 "$2") return $MATCH;;
15 * ) return $NOMATCH;;
16 esac
17
18 }
19
20
21 a=one
22 b=two
23 c=three
24 d=two
25
26
27 match_string $a # ²ÎÊý¸öÊý´íÎó.
28 echo $? # 91
29
30 match_string $a $b # ²»Æ¥Åä
31 echo $? # 90
32
33 match_string $b $d # Æ¥Åä
34 echo $? # 0
35
36
37 exit 0
################################End Script######################################
###
Example 10-28 ¼ì²éÊÇ·ñÊÇ×ÖĸÊäÈë
################################Start Script####################################
###
1 #!/bin/bash
2 # isalpha.sh: ʹÓÃ"case"½á¹¹À´¹ýÂË×Ö·û´®.
3
4 SUCCESS=0
5 FAILURE=-1
6
7 isalpha () # ¼ì²éÊäÈëµÄ*µÚÒ»¸ö×Ö·û*ÊDz»ÊÇ×Öĸ±íÉϵÄ×Ö·û.
8 {
9 if [ -z "$1" ] # ûÓвÎÊý´«½øÀ´?
10 then
11 return $FAILURE
12 fi
13
14 case "$1" in
15 [a-zA-Z]*) return $SUCCESS;; # ÒÔÒ»¸ö×Öĸ¿ªÍ·?
16 * ) return $FAILURE;;
17 esac
18 } # ͬCÓïÑÔµÄ"isalpha()"º¯ÊýÏà±È½Ï.
19
20
21 isalpha2 () # ²âÊÔÊÇ·ñ*Õû¸ö×Ö·û´®*Ϊ×Öĸ±í×Ö·û.
22 {
23 [ $# -eq 1 ] || return $FAILURE
24
25 case $1 in
26 *[!a-zA-Z]*|"") return $FAILURE;;
27 *) return $SUCCESS;;
28 esac
29 }
30
31 isdigit () # ²âÊÔÊÇ·ñ*Õû¸ö×Ö·û´®*¶¼ÊÇÊý×Ö.
32 { # »»¾ä»°Ëµ¾ÍÊDzâÊÔÊÇ·ñÊÇÕûÊý±äÁ¿.
33 [ $# -eq 1 ] || return $FAILURE
34
35 case $1 in
36 *[!0-9]*|"") return $FAILURE;;
37 *) return $SUCCESS;;
38 esac
39 }
40
41
42
43 check_var () # ²âÊÔ isalpha ().
44 {
45 if isalpha "$@"
46 then
47 echo "\"$*\" begins with an alpha character."
48 if isalpha2 "$@"
49 then # ²»ÐèÒª²âÊÔµÚÒ»¸ö×Ö·ûÊÇ·ñÊÇnon-alpha.
50 echo "\"$*\" contains only alpha characters."
51 else
52 echo "\"$*\" contains at least one non-alpha character."
53 fi
54 else
55 echo "\"$*\" begins with a non-alpha character."
56 # Èç¹ûûÓвÎÊý´«µÝ½øÀ´,Ò²ÊÇ"non-alpha".
57 fi
58
59 echo
60
61 }
62
63 digit_check () # ²âÊÔ isdigit ().
64 {
65 if isdigit "$@"
66 then
67 echo "\"$*\" contains only digits [0 - 9]."
68 else
69 echo "\"$*\" has at least one non-digit character."
70 fi
71
72 echo
73
74 }
75
76 a=23skidoo
77 b=H3llo
78 c=-What?
79 d=What?
80 e=`echo $b` # ÃüÁîÌæ»».
81 f=AbcDef
82 g=27234
83 h=27a34
84 i=27.34
85
86 check_var $a
87 check_var $b
88 check_var $c
89 check_var $d
90 check_var $e
91 check_var $f
92 check_var # ûÓвÎÊý´«½øÀ´,½«·¢Éúʲô?
93 #
94 digit_check $g
95 digit_check $h
96 digit_check $i
97
98
99 exit 0 # S.C¸Ä½ø¹ýÕâ¸ö½Å±¾.
100
101 # Exercise:
102 # --------
103 # ±àдһ¸ö 'isfloat ()'º¯ÊýÀ´²âÊÔ¸¡µãÊý.
104 # °µÊ¾: Õâ¸öº¯Êý»ù±¾ÉÏÓë'isdigit ()'Ò»Ñù,
105 #+ µ«ÊÇÒªÌí¼ÓÒ»²¿·ÖСÊýµãµÄ´¦Àí.
################################End Script######################################
###

select
select½á¹¹Êǽ¨Á¢²Ëµ¥µÄÁíÒ»ÖÖ¹¤¾ß,ÕâÖֽṹÊÇ´ÓkshÖÐÒýÈëµÄ.
select variable [in list]
do
command...
break
done
ÌáʾÓû§Ñ¡ÔñµÄÄÚÈݱÈÈç·ÅÔÚ±äÁ¿ÁбíÖÐ.×¢Òâ:selectÃüÁîʹÓÃPS3Ìáʾ·û[ĬÈÏΪ(#? )]
µ«ÊÇ¿ÉÒÔÐÞ¸ÄPS3.
Example 10-29 ÓÃselectÀ´´´½¨²Ëµ¥
################################Start Script####################################
###
1 #!/bin/bash
2
3 PS3='Choose your favorite vegetable: ' # ÉèÖÃÌáʾ·û×Ö´®.
4
5 echo
6
7 select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
8 do
9 echo
10 echo "Your favorite veggie is $vegetable."
11 echo "Yuck!"
12 echo
13 break # Èç¹ûÕâÀïûÓÐ'break'»á·¢Éúʲô?
14 done
15
16 exit 0
################################End Script######################################
###
Èç¹ûºöÂÔÁËin listÁбí,ÄÇôselectÃüÁʹÓô«µÝµ½½Å±¾µÄÃüÁîÐвÎÊý,»òÕßÊǺ¯Êý²ÎÊý
Ç°ÌáÊǽ«selectдµ½Õâ¸öº¯ÊýÖÐ.
Óëfor variable [in list]½á¹¹ÔÚºöÂÔ[in list]ʱµÄÐÐΪÏà±È½Ï.
Example 10-30 Óú¯ÊýÖÐselect½á¹¹À´´´½¨²Ëµ¥
################################Start Script####################################
###
1 #!/bin/bash
2
3 PS3='Choose your favorite vegetable: '
4
5 echo
6
7 choice_of()
8 {
9 select vegetable
10 # [in list] ±»ºöÂÔ, ËùÒÔ'select'Óô«µÝ¸øº¯ÊýµÄ²ÎÊý.
11 do
12 echo
13 echo "Your favorite veggie is $vegetable."
14 echo "Yuck!"
15 echo
16 break
17 done
18 }
19
20 choice_of beans rice carrots radishes tomatoes spinach
21 # $1 $2 $3 $4 $5 $6
22 # ´«µÝ¸øchoice_of() º¯ÊýµÄ²ÎÊý
23
24 exit 0
################################End Script######################################
###
²Î¼ûExample 34-3.

µÚ11Õ ÄÚ²¿ÃüÁîÓëÄÚ½¨
======================
ÄÚ½¨ÃüÁîÖ¸µÄ¾ÍÊÇ°üº¬ÔÚBash¹¤¾ß¼¯ÖеÄÃüÁî.ÕâÖ÷ÒªÊÇ¿¼Âǵ½Ö´ÐÐЧÂʵÄÎÊÌâ--ÄÚ½¨ÃüÁ
±ÈÍⲿÃüÁîµÄÖ´Ðеøü¿ì,ÍⲿÃüÁîͨ³£ÐèÒªfork³öÒ»¸öµ¥¶ÀµÄ½ø³ÌÀ´Ö´ÐÐ.ÁíÍâÒ»²¿·ÖÔ­Òò
ÊÇÌض¨µÄÄÚ½¨ÃüÁîÐèÒªÖ±½Ó´æÈ¡shellÄں˲¿·Ö.
µ±Ò»¸öÃüÁî»òÕßÊÇshell±¾ÉíÐèÒª³õʼ»¯(»òÕß´´½¨)Ò»¸öеÄ×Ó½ø³ÌÀ´Ö´ÐÐÒ»¸öÈÎÎñµÄʱºò,Õâ
ÖÖÐÐΪ±»³ÆΪforking.Õâ¸öвúÉúµÄ½ø³Ì±»½Ð×ö×Ó½ø³Ì,²¢ÇÒÕâ¸ö½ø³ÌÊÇ´Ó¸¸½ø³ÌÖзÖÀë³öÀ´
µÄ.µ±×Ó½ø³ÌÖ´ÐÐËüµÄÈÎÎñʱ,ͬʱ¸¸½ø³ÌÒ²ÔÚÔËÐÐ.
×¢Òâ:µ±¸¸½ø³ÌÈ¡µÃ×Ó½ø³ÌµÄ½ø³ÌIDµÄʱºò,¸¸½ø³Ì¿ÉÒÔ´«µÝ¸ø×Ó½ø³Ì²ÎÊý,¶ø·´¹ýÀ´Ôò²»ÐÐ.
Õ⽫²úÉú²»¿É˼ÒéµÄ²¢ÇÒºÜÄÑ×·×ÙµÄÎÊÌâ.
Example 11-1 Ò»¸öfork³ö¶à¸ö×Ô¼ºÊµÀýµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # spawn.sh
3
4
5 PIDS=$(pidof sh $0) # Õâ¸ö½Å±¾²»Í¬ÊµÀýµÄ½ø³ÌID.
6 P_array=( $PIDS ) # °ÑËüÃǷŵ½Êý×éÀï(Ϊʲô?).
7 echo $PIDS # ÏÔʾ¸¸½ø³ÌºÍ×Ó½ø³ÌµÄ½ø³ÌID.
8 let "instances = ${#P_array[*]} - 1" # ¼ÆËãÔªËظöÊý,ÖÁÉÙΪ1.
9 # Ϊʲô¼õ1?
10 echo "$instances instance(s) of this script running."
11 echo "[Hit Ctl-C to exit.]"; echo
12
13
14 sleep 1 # µÈ.
15 sh $0 # ÔÙÀ´Ò»´Î.
16
17 exit 0 # û±ØÒª: ½Å±¾ÓÀÔ¶²»»á×ßµ½ÕâÀï.
18 # Ϊʲô×ß²»µ½ÕâÀï?
19
20 # ÔÚʹÓÃCtl-CÍ˳öÖ®ºó,
21 #+ ÊÇ·ñËùÓвúÉúµÄ½ø³Ì¶¼»á±»killµô?
22 # Èç¹ûÊÇÕâÑùµÄ»°, Ϊʲô?
23
24 # ×¢Òâ:
25 # ----
26 # СÐÄ,²»ÒªÈÃÕâ¸ö½Å±¾ÔËÐÐÌ«³¤Ê±¼ä.
27 # Ëü×îºó½«³ÔµôÄã´ó²¿·ÖµÄϵͳ×ÊÔ´.
28
29 # ¶ÔÓÚÓýű¾²úÉú´óÁ¿µÄ×ÔÉíʵÀýÀ´Ëµ,
30 #+ ÊÇ·ñÓÐÊʵ±µÄ½Å±¾¼¼Êõ.
31 # ΪʲôÊÇΪʲô²»ÊÇ?
################################End Script######################################
###
Ò»°ãµÄ,½Å±¾ÖеÄÄÚ½¨ÃüÁîÔÚÖ´ÐÐʱ½«²»»áfork³öÒ»¸ö×Ó½ø³Ì.µ«Êǽű¾ÖеÄÍⲿ»ò¹ýÂËÃüÁî
ͨ³£»áforkÒ»¸ö×Ó½ø³Ì.
Ò»¸öÄÚ½¨ÃüÁîͨ³£ÓëÒ»¸öϵͳÃüÁîͬÃû,µ«ÊÇBashÔÚÄÚ²¿ÖØÐÂʵÏÖÁËÕâЩÃüÁî.±ÈÈç,BashµÄ
echoÃüÁîÓë/bin/echo¾Í²»¾¡Ïàͬ,ËäÈ»ËüÃǵÄÐÐΪ¾ø´ó¶àÊýÇé¿öÏÂÊÇÒ»ÑùµÄ.
1 #!/bin/bash
2
3 echo "This line uses the \"echo\" builtin."
4 /bin/echo "This line uses the /bin/echo system command."
¹Ø¼ü×ÖµÄÒâ˼¾ÍÊDZ£Áô×Ö.¶ÔÓÚshellÀ´Ëµ¹Ø¼ü×ÖÓÐÌØÊâµÄº¬Òå,²¢ÇÒÓÃÀ´¹¹½¨shellµÄÓï·¨½á¹¹.
±ÈÈç,"for","while","do"ºÍ"!"¶¼Êǹؼü×Ö.ÓëÄÚ½¨ÃüÁîÏàͬµÄÊÇ,¹Ø¼ü×ÖÒ²ÊÇBashµÄ¹Ç¸É²¿·Ö,
µ«ÊÇÓëÄÚ½¨ÃüÁͬµÄÊÇ,¹Ø¼ü×Ö×ÔÉí²¢²»ÊÇÃüÁî,¶øÊÇÒ»¸ö±È½Ï´óµÄÃüÁî½á¹¹µÄÒ»²¿·Ö.[1]

I/OÀà
echo
´òÓ¡(µ½stdout)Ò»¸ö±í´ïʽ»ò±äÁ¿(¼ûExample 4-1).
1 echo Hello
2 echo $a
echoÐèҪʹÓÃ-e²ÎÊýÀ´´òӡתÒÆ×Ö·û.¼ûExample 5-2.
Ò»°ãµÄÿ¸öechoÃüÁ»áÔÚÖÕ¶ËÉÏÐÂÆðÒ»ÐÐ,µ«ÊÇ-nÑ¡Ï»á×èÖ¹ÐÂÆðÒ»ÐÐ.
×¢Òâ:echoÃüÁî¿ÉÒÔÓÃÀ´×÷ΪһϵÁÐÃüÁîµÄ¹ÜµÀÊäÈë.
1 if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]]
2 then
3 echo "$VAR contains the substring sequence \"txt\""
4 fi
×¢Òâ:echoÃüÁîÓëÃüÁîÌæ»»Ïà×éºÏ¿ÉÒÔÓÃÀ´ÉèÖÃÒ»¸ö±äÁ¿.
a=`echo "HELLO" | tr A-Z a-z`
²Î¼ûExample 12-19,Example 12-3,Example 12-42,ºÍExample 12-43.
×¢Òâ:echo `command`½«»áɾ³ýÈκÎÓÐÃüÁî²úÉúµÄ»»Ðзû.
$IFS(ÄÚ²¿Óò·Ö¸ô·û)Ò»°ã¶¼»á½«\n(»»Ðзû)°üº¬ÔÚËüµÄ¿Õ°××Ö·û¼¯ºÏÖÐ.BashÒò´Ë»á¸ù¾Ý
²ÎÊýÖеĻ»ÐÐÀ´·ÖÀëÃüÁîµÄÊä³ö.È»ºóecho½«ÒÔ¿Õ¸ñ´úÌæ»»ÐÐÀ´Êä³öÕâЩ²ÎÊý.
bash$ ls -l /usr/share/apps/kjezz/sounds
-rw-r--r-- 1 root root 1407 Nov 7 2000 reflect.au
-rw-r--r-- 1 root root 362 Nov 7 2000 seconds.au

bash$ echo `ls -l /usr/share/apps/kjezz/sounds`


total 40 -rw-r--r-- 1 root root 716 Nov 7 2000 reflect.au -rw-r--r-- 1 r
oot root 362 Nov 7 2000 seconds.au

ËùÒÔ,ÎÒÃÇÔõô²ÅÄÜÔÚÒ»¸öÐèÒªecho³öÀ´µÄ×Ö·û´®ÖÐǶÈë»»ÐÐÄØ?
################################Start Script####################################
###
1 # ǶÈëÒ»¸ö»»ÐÐ?
2 echo "Why doesn't this string \n split on two lines?"
3 # ÉϱßÕâ¾äµÄ\n½«±»´òÓ¡³öÀ´.´ï²»µ½»»ÐеÄÄ¿µÄ.
4
5 # ÈÃÎÒÃÇÔÚÊÔÊÔÆäËû·½·¨.
6
7 echo
8
9 echo $"A line of text containing
10 a linefeed."
11 # ´òÓ¡³ö2¸ö¶ÀÁ¢µÄÐÐ,(DZÈë»»Ðгɹ¦ÁË).
12 # µ«ÊÇ,"$"ǰ׺ÊÇ·ñÊDZØÒªµÄ?
13
14 echo
15
16 echo "This string splits
17 on two lines."
18 # ²»Ó÷ǵÃÓÐ"$"ǰ׺.
19
20 echo
21 echo "---------------"
22 echo
23
24 echo -n $"Another line of text containing
25 a linefeed."
26 # ´òÓ¡³ö2¸ö¶ÀÁ¢µÄÐÐ,(DZÈë»»Ðгɹ¦ÁË).
27 # ¼´Ê¹-nÑ¡Ïî,ҲûÄÜ×èÖ¹»»ÐÐ(ÒëÕß:-n ×èÖ¹Á˵Ú2¸ö»»ÐÐ)
28
29 echo
30 echo
31 echo "---------------"
32 echo
33 echo
34
35 # È»¶ø,ϱߵĴúÂë¾ÍûÄÜÏñÆÚÍûµÄÄÇÑùÔËÐÐ.
36 # Why not? Hint: Assignment to a variable.
36 # Ϊʲôʧ°Ü? Ìáʾ: ÒòΪ·ÖÅäµ½Á˱äÁ¿.
37 string1=$"Yet another line of text containing
38 a linefeed (maybe)."
39
40 echo $string1
41 # Yet another line of text containing a linefeed (maybe).
42 # ^
43 # »»Ðбä³ÉÁË¿Õ¸ñ.
44
45 # Thanks, Steve Parker, for pointing this out.
################################End Script######################################
###
×¢Òâ: Õâ¸öÃüÁîÊÇshellµÄÒ»¸öÄÚ½¨ÃüÁî,Óë/bin/echo²»Í¬,ËäÈ»ÐÐΪÏàËÆ.
bash$ type -a echo
echo is a shell builtin
echo is /bin/echo
printf
printfÃüÁî,¸ñʽ»¯Êä³ö,ÊÇechoÃüÁîµÄÔöÇ¿.ËüÊÇCÓïÑÔprintf()¿âº¯ÊýµÄÒ»¸öÓÐÏ޵ıäÐÎ,
²¢ÇÒÔÚÓï·¨ÉÏÓÐЩ²»Í¬.
printf format-string... parameter...
ÕâÊÇBashµÄÄÚ½¨°æ±¾,Óë/bin/printf»ò/usr/bin/printfÃüÁͬ.Ïë¸üÉîÈëµÄÁ˽â,Çë
²ì¿´printf(ϵͳÃüÁî)µÄmanÒ³.
×¢Òâ:ÀÏ°æ±¾µÄBash¿ÉÄܲ»Ö§³Öprintf.
Example 11-2 printf
################################Start Script####################################
###
1 #!/bin/bash
2 # printf demo
3
4 PI=3.14159265358979
5 DecimalConstant=31373
6 Message1="Greetings,"
7 Message2="Earthling."
8
9 echo
10
11 printf "Pi to 2 decimal places = %1.2f" $PI
12 echo
13 printf "Pi to 9 decimal places = %1.9f" $PI # ¶¼ÄÜÕýÈ·µØ½áÊø.
14
15 printf "\n" # ´òÓ¡Ò»¸ö»»ÐÐ,
16 # µÈ¼ÛÓÚ 'echo' . . .
17
18 printf "Constant = \t%d\n" $DecimalConstant # ²åÈëÒ»¸ö tab (\t).
19
20 printf "%s %s \n" $Message1 $Message2
21
22 echo
23
24 # ==========================================#
25 # Ä£·ÂCº¯Êý, sprintf().
26 # ʹÓÃÒ»¸ö¸ñʽ»¯µÄ×Ö·û´®À´¼ÓÔØÒ»¸ö±äÁ¿.
27
28 echo
29
30 Pi12=$(printf "%1.12f" $PI)
31 echo "Pi to 12 decimal places = $Pi12"
32
33 Msg=`printf "%s %s \n" $Message1 $Message2`
34 echo $Msg; echo $Msg
35
36 # ÏòÎÒÃÇ¿´µ½µÄÒ»Ñù,ÏÖÔÚ'sprintf'º¯Êý¿ÉÒÔ
37 #+ ×÷Ϊһ¸ö¿É±»¼ÓÔصÄÄ£¿é
38 #+ µ«ÕâÊDz»¿ÉÒÆÖ²µÄ.
39
40 exit 0
################################End Script######################################
###
ʹÓÃprintfµÄ×îÖ÷ÒªµÄÓ¦ÓþÍÊǸñʽ»¯´íÎóÏûÏ¢.
1 E_BADDIR=65
2
3 var=nonexistent_directory
4
5 error()
6 {
7 printf "$@" >&2
8 # ¸ñʽ»¯´«µÝ½øÀ´µÄλÖòÎÊý,²¢°ÑËüÃÇË͵½stderr.
9 echo
10 exit $E_BADDIR
11 }
12
13 cd $var || error $"Can't cd to %s." "$var"
14
15 # Thanks, S.C.
read
´ÓstdinÖжÁÈ¡Ò»¸ö±äÁ¿µÄÖµ,Ò²¾ÍÊÇÓë¼üÅ̽»»¥È¡µÃ±äÁ¿µÄÖµ.ʹÓÃ-a²ÎÊý¿ÉÒÔÈ¡µÃÊý×é
±äÁ¿(¼ûExample 26-6).
Example 11-3 ʹÓÃread,±äÁ¿·ÖÅä
################################Start Script####################################
###
1 #!/bin/bash
2 # "Reading" ±äÁ¿.
3
4 echo -n "Enter the value of variable 'var1': "
5 # -nÑ¡Ïî,×èÖ¹»»ÐÐ.
6
7 read var1
8 # ×¢ÒâÔÚvar1Ç°ÃæûÓÐ'$',ÒòΪ±äÁ¿ÕýÔÚ±»ÉèÖÃ.
9
10 echo "var1 = $var1"
11
12
13 echo
14
15 # Ò»¸ö'read'ÃüÁî¿ÉÒÔÉèÖöà¸ö±äÁ¿.
16 echo -n "Enter the values of variables 'var2' and 'var3' (separated by a spac
e or tab): "
17 read var2 var3
18 echo "var2 = $var2 var3 = $var3"
19 # Èç¹ûÄãÖ»ÊäÈëÁËÒ»¸öÖµ,ÄÇôÆäËûµÄ±äÁ¿»¹ÊÇδÉèÖÃ(null).
20
21 exit 0
################################End Script######################################
###
Ò»¸ö²»´ø±äÁ¿²ÎÊýµÄreadÃüÁî,½«°ÑÀ´×Ô¼üÅ̵ÄÊäÈë´æÈ뵽רÓñäÁ¿$REPLYÖÐ.
Example 11-4 µ±Ê¹ÓÃÒ»¸ö²»´ø±äÁ¿²ÎÊýµÄreadÃüÁîʱ,½«»á·¢Éúʲô?
################################Start Script####################################
###
1 #!/bin/bash
2 # read-novar.sh
3
4 echo
5
6 # -------------------------- #
7 echo -n "Enter a value: "
8 read var
9 echo "\"var\" = "$var""
10 # µ½ÕâÀïΪֹ,¶¼ÓëÆÚÍûµÄÏàͬ.
11 # -------------------------- #
12
13 echo
14
15 # ------------------------------------------------------------------- #
16 echo -n "Enter another value: "
17 read # ûÓбäÁ¿·ÖÅä¸ø'read'ÃüÁî,Òò´Ë...
18 #+ ÊäÈ뽫·ÖÅä¸øĬÈϱäÁ¿,$REPLY.
19 var="$REPLY"
20 echo "\"var\" = "$var""
21 # Õⲿ·Ö´úÂëºÍÉϱߵĴúÂëµÈ¼Û.
22 # ------------------------------------------------------------------- #
23
24 echo
25
26 exit 0
################################End Script######################################
###
ͨ³£Çé¿öÏÂ,ÔÚʹÓÃreadÃüÁîʱ,ÊäÈëÒ»¸ö\È»ºó»Ø³µ,½«»á×èÖ¹²úÉúÒ»¸öÐÂÐÐ.-rÑ¡Ï»á
ÈÃ\תÒå.
Example 11-5 readÃüÁîµÄ¶àÐÐÊäÈë
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo
4
5 echo "Enter a string terminated by a \\, then press <ENTER>."
6 echo "Then, enter a second string, and again press <ENTER>."
7 read var1 # "\"½«»á×èÖ¹²úÉúÐÂÐÐ,µ±read $var1ʱ.
8 # first line \
9 # second line
10
11 echo "var1 = $var1"
12 # var1 = first line second line
13
14 # For each line terminated by a "\"
14 # ¶ÔÓÚÿ¸öÒ»¸ö"\"½áβµÄÐÐ
15 #+ Ä㶼»á¿´µ½Ò»¸öÏÂÒ»ÐеÄÌáʾ·û,ÈÃÄã¼ÌÐøÏòvar1ÊäÈëÄÚÈÝ.
16
17 echo; echo
18
19 echo "Enter another string terminated by a \\ , then press <ENTER>."
20 read -r var2 # -rÑ¡Ï»áÈÃ"\"תÒå.
21 # first line \
22
23 echo "var2 = $var2"
24 # var2 = first line \
25
26 # µÚÒ»¸ö<ENTER>¾Í»á½áÊøvar2±äÁ¿µÄ¼Èë.
27
28 echo
29
30 exit 0
################################End Script######################################
###
readÃüÁîÓÐЩÓÐȤµÄÑ¡Ïî,ÕâЩѡÏîÔÊÐí´òÓ¡³öÒ»¸öÌáʾ·û,È»ºóÔÚ²»ÊäÈë<ENTER>µÄÇé¿ö
ÏÂ,¿ÉÒÔ¶ÁÈëÄãµÄ°´¼ü×Ö·û.
1 # Read a keypress without hitting ENTER.
1 # ²»Çûسµ,¶ÁÈ¡Ò»¸ö°´¼ü×Ö·û.
2
3 read -s -n1 -p "Hit a key " keypress
4 echo; echo "Keypress was "\"$keypress\""."
5
6 # -s Ñ¡ÏîÒâζ×Ų»´òÓ¡ÊäÈë.
7 # -n N Ñ¡ÏîÒâζ×ÅÖ±½ÓÊÜN¸ö×Ö·ûµÄÊäÈë.
8 # -p Ñ¡ÏîÒâζ×ÅÔÚ¶ÁÈ¡ÊäÈë֮ǰ´òÓ¡³öºó±ßµÄÌáʾ·û.
9
10 # ʹÓÃÕâЩѡÏîÊÇÓм¼ÇɵÄ,ÒòΪÄãÐèҪʹÓÃÕýÈ·µÄÑ­ÐòÀ´Ê¹ÓÃËüÃÇ.
readµÄ-nÑ¡ÏîÒ²¿ÉÒÔ¼ì²â·½Ïò¼ü,ºÍһЩ¿ØÖÆ°´¼ü.
Example 11-6 ¼ì²â·½Ïò¼ü
################################Start Script####################################
###
1 #!/bin/bash
2 # arrow-detect.sh: ¼ì²â·½Ïò¼ü,ºÍһЩ·Ç´òÓ¡×Ö·ûµÄ°´¼ü.
3 # Thank you, Sandro Magi¸æËßÁËÎÒÔõô×ö.
4
5 # --------------------------------------------
6 # °´¼ü²úÉúµÄ×Ö·û±àÂë.
7 arrowup='\[A'
8 arrowdown='\[B'
9 arrowrt='\[C'
10 arrowleft='\[D'
11 insert='\[2'
12 delete='\[3'
13 # --------------------------------------------
14
15 SUCCESS=0
16 OTHER=65
17
18 echo -n "Press a key... "
19 # Èç¹û²»ÊÇÉϱßÁбíËùÁгöµÄ°´¼ü,¿ÉÄÜ»¹ÊÇÐèÒª°´»Ø³µ.(ÒëÕß:ÒòΪһ°ã°´¼üÊÇÒ»¸ö×Ö·û)
20 read -n3 key # ¶Á3¸ö×Ö·û.
21
22 echo -n "$key" | grep "$arrowup" #¼ì²éÊäÈë×Ö·ûÊÇ·ñÆ¥Åä.
23 if [ "$?" -eq $SUCCESS ]
24 then
25 echo "Up-arrow key pressed."
26 exit $SUCCESS
27 fi
28
29 echo -n "$key" | grep "$arrowdown"
30 if [ "$?" -eq $SUCCESS ]
31 then
32 echo "Down-arrow key pressed."
33 exit $SUCCESS
34 fi
35
36 echo -n "$key" | grep "$arrowrt"
37 if [ "$?" -eq $SUCCESS ]
38 then
39 echo "Right-arrow key pressed."
40 exit $SUCCESS
41 fi
42
43 echo -n "$key" | grep "$arrowleft"
44 if [ "$?" -eq $SUCCESS ]
45 then
46 echo "Left-arrow key pressed."
47 exit $SUCCESS
48 fi
49
50 echo -n "$key" | grep "$insert"
51 if [ "$?" -eq $SUCCESS ]
52 then
53 echo "\"Insert\" key pressed."
54 exit $SUCCESS
55 fi
56
57 echo -n "$key" | grep "$delete"
58 if [ "$?" -eq $SUCCESS ]
59 then
60 echo "\"Delete\" key pressed."
61 exit $SUCCESS
62 fi
63
64
65 echo " Some other key pressed."
66
67 exit $OTHER
68
69 # Á·Ï°:
70 # -----
71 # 1) ͨ¹ýʹÓÃ'case'½á¹¹À´´úÌæ'if'½á¹¹
72 #+ À´¼ò»¯Õâ¸ö½Å±¾.
73 # 2) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.
73 # 2) Ìí¼Ó"Home," "End," "PgUp," ºÍ "PgDn"ÕâЩ°´¼üµÄ¼ì²é.
################################End Script######################################
###
×¢Òâ: ¶ÔreadÃüÁîÀ´Ëµ,-n Ñ¡Ï²»»á¼ì²âENTER(ÐÂÐÐ)¼ü.
readÃüÁîµÄ-tÑ¡ÏîÔÊÐíʱ¼äÊäÈë(¼ûExample 9-4).
readÃüÁîÒ²¿ÉÒÔ´ÓÖض¨ÏòµÄÎļþÖжÁÈë±äÁ¿µÄÖµ.Èç¹ûÎļþÖеÄÄÚÈݳ¬¹ýÒ»ÐÐ,ÄÇôֻÓеÚ
Ò»Ðб»·ÖÅäµ½Õâ¸ö±äÁ¿ÖÐ.Èç¹ûreadÃüÁîÓг¬¹ýÒ»¸ö²ÎÊý,ÄÇôÿ¸ö±äÁ¿¶¼»á´ÓÎļþÖÐÈ¡µÃ
ÒÔ¶¨ÒåµÄ¿Õ°×·Ö¸ôµÄ×Ö·û´®×÷Ϊ±äÁ¿µÄÖµ.СÐÄ!
Example 11-7 ͨ¹ýÎļþÖض¨ÏòÀ´Ê¹ÓÃread
################################Start Script####################################
###
1 #!/bin/bash
2
3 read var1 <data-file
4 echo "var1 = $var1"
5 # var1½«»á°Ñdata-fileµÄµÚÒ»ÐеÄÈ«²¿ÄÚÈݶ¼×÷ΪËüµÄÖµ.
6
7 read var2 var3 <data-file
8 echo "var2 = $var2 var3 = $var3"
9 # ×¢Òâ,ÕâÀï"read"ÃüÁ»á²úÉúÒ»ÖÖ²»Ö±¹ÛµÄÐÐΪ.
10 # 1) ÖØдÓÎļþµÄ¿ªÍ·¿ªÊ¼¶ÁÈë±äÁ¿.
11 # 2) ÿ¸ö±äÁ¿¶¼ÉèÖóÉÁËÒÔ¿Õ°×·Ö¸îµÄ×Ö·û´®,
12 # ¶ø²»ÊÇ֮ǰµÄÒÔÕûÐеÄÄÚÈÝ×÷Ϊ±äÁ¿µÄÖµ.
13 # 3) ¶ø×îºóÒ»¸ö±äÁ¿½«»áÈ¡µÃµÚÒ»ÐÐÊ£ÓàµÄÈ«²¿²¿·Ö(²»¹ÜÊÇ·ñÒÔ¿Õ°×·Ö¸î).
14 # 4) Èç¹ûÐèÒª¸³ÖµµÄ±äÁ¿µÄ¸öÊý±ÈÎļþÖеÚÒ»ÐÐÒ»¿Õ°×·Ö¸îµÄ×Ö·û´®µÄ¸öÊý¶àµÄ»°,
15 # ÄÇôÕâЩ±äÁ¿½«»á±»¸³¿ÕÖµ.
16
17 echo "------------------------------------------------"
18
19 # ÈçºÎÓÃÑ­»·À´½â¾öÉϱßËùÌáµ½µÄÎÊÌâ:
20 while read line
21 do
22 echo "$line"
23 done <data-file
24 # Thanks, Heiner Steven for pointing this out.
25
26 echo "------------------------------------------------"
27
28 # ʹÓÃ$IFS (ÄÚ²¿Óò·Ö¸ô±äÁ¿)À´½«Ã¿ÐеÄÊäÈëµ¥¶ÀµÄ·Åµ½"read"ÖÐ,
29 # Èç¹ûÄã²»ÏëʹÓÃĬÈϿհ׵Ļ°.
30
31 echo "List of all users:"
32 OIFS=$IFS; IFS=: # /etc/passwd ʹÓà ":" ×÷ΪÓò·Ö¸ô·û.
33 while read name passwd uid gid fullname ignore
34 do
35 echo "$name ($fullname)"
36 done </etc/passwd # I/O Öض¨Ïò.
37 IFS=$OIFS # »Ö¸´Ô­Ê¼µÄ $IFS.
38 # Õâ¸ö´úÂëƬ¶ÎÒ²ÊÇHeiner StevenдµÄ.
39
40
41
42 # ÔÚÑ­»·ÄÚ²¿ÉèÖÃ$IFS±äÁ¿
43 #+ ¶ø²»ÓðÑԭʼµÄ$IFS
44 #+ ±£´æµ½ÁÙʱ±äÁ¿ÖÐ.
45 # Thanks, Dim Segebart, for pointing this out.
46 echo "------------------------------------------------"
47 echo "List of all users:"
48
49 while IFS=: read name passwd uid gid fullname ignore
50 do
51 echo "$name ($fullname)"
52 done </etc/passwd # I/O Öض¨Ïò.
53
54 echo
55 echo "\$IFS still $IFS"
56
57 exit 0
################################End Script######################################
###
×¢Òâ:¹ÜµÀÊä³öµ½Ò»¸öreadÃüÁîÖÐ,ʹÓùܵÀechoÊä³öµ½read»áʧ°Ü.
È»¶øʹÓùܵÀcatÊä³ö¿´ÆðÀ´Äܹ»Õý³£ÔËÐÐ.
1 cat file1 file2 |
2 while read line
3 do
4 echo $line
5 done
µ«ÊÇ,ÏñBjon ErikssonÖ¸³öµÄ:
Example 11-8 ¹ÜµÀÊä³öµ½readÖеÄÎÊÌâ
################################Start Script####################################
###
1 #!/bin/sh
2 # readpipe.sh
3 # Õâ¸öÀý×ÓÊÇBjon Eriksson¾èÏ×µÄ.
4
5 last="(null)"
6 cat $0 |
7 while read line
8 do
9 echo "{$line}"
10 last=$line
11 done
12 printf "\nAll done, last:$last\n"
13
14 exit 0 # ´úÂë½áÊø.
15 # ϱßÊÇÕâ¸ö½Å±¾µÄ²¿·ÖÊä³ö.
16 # ´òÓ¡³öÁ˶àÓàµÄ´óÀ¨ºÅ.
17
18 #############################################
19
20 ./readpipe.sh
21
22 {#!/bin/sh}
23 {last="(null)"}
24 {cat $0 |}
25 {while read line}
26 {do}
27 {echo "{$line}"}
28 {last=$line}
29 {done}
30 {printf "nAll done, last:$lastn"}
31
32
33 All done, last:(null)
34
35 ±äÁ¿(last)ÊÇÉèÖÃÔÚ×ÓshellÖеĶøûÉèÔÚÍâ±ß.
################################End Script######################################
###
ÔÚÐí¶àlinux·¢ÐаæÉÏ,gendiff½Å±¾Í¨³£ÔÚ/usr/binÏÂ,½«findµÄÊä³öʹÓùܵÀ´«µÝµ½Ò»¸ö
whileÑ­»·ÖÐ.
1 find $1 \( -name "*$2" -o -name ".*$2" \) -print |
2 while read f; do
3 . . .
ÎļþϵͳÀà
cd
cd,ÐÞ¸ÄĿ¼ÃüÁî,Ôڽű¾ÖÐÓõÃ×î¶àµÄʱºò¾ÍÊÇ,ÃüÁîÐèÒªÔÚÖ¸¶¨Ä¿Â¼ÏÂÔËÐÐʱ,ÐèÒªÓÃcd
Ð޸ĵ±Ç°¹¤×÷Ŀ¼.
1 (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpv
f -)
[֮ǰÓиöÀý×Ó,Alan CoxдµÄ]
-P(physical)Ñ¡ÏîµÄ×÷ÓÃÊǺöÂÔ·ûºÅÁ¬½Ó.
cd - ½«°Ñ¹¤×÷Ŀ¼¸ÄΪ$OLDPWD,¾ÍÊÇ֮ǰµÄ¹¤×÷Ŀ¼.
×¢Òâ:µ±ÎÒÃÇÓÃÁ½¸ö/À´×÷ΪcdÃüÁîµÄ²ÎÊýʱ,½á¹ûÈ´³öºõÎÒÃǵÄÒâÁÏ.
bash$ cd //
bash$ pwd
//
Êä³öÓ¦¸Ã,²¢ÇÒµ±È»ÊÇ/.ÎÞÂÛÔÚÃüÁîÐÐÏ»¹ÊÇÔڽű¾ÖÐ,ÕⶼÊǸöÎÊÌâ.
pwd
´òÓ¡µ±Ç°µÄ¹¤×÷Ŀ¼.Õ⽫¸øÓû§(»ò½Å±¾)µ±Ç°µÄ¹¤×÷Ŀ¼(¼ûExample 11-9).ʹÓÃÕâ¸ö
ÃüÁîµÄ½á¹ûºÍ´ÓÄÚ¼ü±äÁ¿$PWDÖжÁÈ¡µÄÖµÊÇÏàͬµÄ.
pushd, popd, dirs
Õ⼸¸öÃüÁî¿ÉÒÔʹµÃ¹¤×÷Ŀ¼ÊéÇ©»¯,¾ÍÊÇ¿ÉÒÔ°´Ë³ÐòÏòÇ°»òÏòºóÒƶ¯¹¤×÷Ŀ¼.
ѹջµÄ¶¯×÷¿ÉÒÔ±£´æ¹¤×÷Ŀ¼Áбí.Ñ¡Ïî¿ÉÒÔÔÊÐí¶ÔĿ¼ջ×÷²»Í¬µÄ²Ù×÷.
pushd dir-name °Ñ·¾¶dir-nameѹÈëĿ¼ջ,ͬʱÐ޸ĵ±Ç°Ä¿Â¼µ½dir-name.
popd ½«Ä¿Â¼Õ»ÖÐ×îÉϱߵÄĿ¼µ¯³ö,ͬʱÐ޸ĵ±Ç°Ä¿Â¼µ½µ¯³öÀ´µÄÄǸöĿ¼.
dirs ÁгöËùÓÐĿ¼ջµÄÄÚÈÝ(Óë$DIRSTACK±ãÁ½Ïà±È½Ï).Ò»¸ö³É¹¦µÄpushd»òÕßpopd½«»á
×Ô¶¯µÄµ÷ÓÃdirsÃüÁî.
¶ÔÓÚÄÇЩ²¢Ã»ÓжԵ±Ç°¹¤×÷Ŀ¼×öÓ²±àÂë,²¢ÇÒÐèÒª¶Ôµ±Ç°¹¤×÷Ŀ¼×öÁé»îÐ޸ĵĽű¾À´Ëµ
,ʹÓÃÕâЩÃüÁîÊÇÔٺò»¹ýµÄÁË.×¢ÒâÄÚ½¨$DIRSTACKÊý×é±äÁ¿,Õâ¸ö±äÁ¿¿ÉÒÔÔڽű¾ÄÚ´æÈ¡,
²¢ÇÒËüÃDZ£´æÁËĿ¼ջµÄÄÚÈÝ.
Example 11-9 Ð޸ĵ±Ç°µÄ¹¤×÷Ŀ¼
################################Start Script####################################
###
1 #!/bin/bash
2
3 dir1=/usr/local
4 dir2=/var/spool
5
6 pushd $dir1
7 # ½«»á×Ô¶¯ÔËÐÐÒ»¸ö 'dirs' (°ÑĿ¼ջµÄÄÚÈÝÁе½stdoutÉÏ).
8 echo "Now in directory `pwd`." # Uses back-quoted 'pwd'.
9
10 # ÏÖÔÚ¶Ô'dir1'×öһЩ²Ù×÷.
11 pushd $dir2
12 echo "Now in directory `pwd`."
13
14 # ÏÖÔÚ¶Ô'dir2'×öһЩ²Ù×÷.
15 echo "The top entry in the DIRSTACK array is $DIRSTACK."
16 popd
17 echo "Now back in directory `pwd`."
18
19 # ÏÖÔÚ,¶Ô'dir1'×ö¸ü¶àµÄ²Ù×÷.
20 popd
21 echo "Now back in original working directory `pwd`."
22
23 exit 0
24
25 # Èç¹ûÄ㲻ʹÓà 'popd'½«»á·¢Éúʲô -- È»ºóÍ˳öÕâ¸ö½Å±¾?
26 # Äã×îºó½«ÂäÔÚÄǸöĿ¼ÖÐ?Ϊʲô?
################################End Script######################################
###

±äÁ¿Àà
let
letÃüÁִÐбäÁ¿µÄËãÊõ²Ù×÷.ÔÚÐí¶àÇé¿öÏÂ,Ëü±»¿´×÷ÊǸ´ÔÓµÄexpr°æ±¾µÄÒ»¸ö¼ò»¯°æ.
Example 11-10 ÓÃ"let"ÃüÁîÀ´×÷ËãÊõ²Ù×÷.
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo
4
5 let a=11 # Óë 'a=11' Ïàͬ
6 let a=a+5 # µÈ¼ÛÓÚlet "a = a + 5"
7 # (Ë«ÒýºÅºÍ¿Õ¸ñÊÇÕâ¾ä»°¸ü¾ß¿É¶ÁÐÔ.)
8 echo "11 + 5 = $a" # 16
9
10 let "a <<= 3" # µÈ¼ÛÓÚlet "a = a << 3"
11 echo "\"\$a\" (=16) left-shifted 3 places = $a"
12 # 128
13
14 let "a /= 4" # µÈ¼ÛÓÚlet "a = a / 4"
15 echo "128 / 4 = $a" # 32
16
17 let "a -= 5" # µÈ¼ÛÓÚlet "a = a - 5"
18 echo "32 - 5 = $a" # 27
19
20 let "a *= 10" # µÈ¼ÛÓÚlet "a = a * 10"
21 echo "27 * 10 = $a" # 270
22
23 let "a %= 8" # µÈ¼ÛÓÚlet "a = a % 8"
24 echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"
25 # 6
26
27 echo
28
29 exit 0
################################End Script######################################
###
eval
eval arg1 [arg2] ... [argN]
½«±í´ïʽÖеIJÎÊý,»òÕß±í´ïʽÁбí,×éºÏÆðÀ´,²¢ÇÒÆÀ¹ÀËüÃÇ.°üº¬ÔÚ±í´ïʽÖеÄÈκαäÁ¿
¶¼½«±»À©Õ¹.½á¹û½«»á±»×ª»¯µ½ÃüÁîÖÐ.Õâ¶ÔÓÚ´ÓÃüÁîÐлòÕ߽ű¾ÖвúÉú´úÂëÊǺÜÓÐÓõÄ.
bash$ process=xterm
bash$ show_process="eval ps ax | grep $process"
bash$ $show_process
1867 tty1 S 0:02 xterm
2779 tty1 S 0:00 xterm
2886 pts/1 S 0:00 grep xterm
Example 11-11 ÏÔʾevalÃüÁîµÄЧ¹û
################################Start Script####################################
###
1 #!/bin/bash
2
3 y=`eval ls -l` # Óë y=`ls -l` ºÜÏàËÆ
4 echo $y #+ µ«ÊÇ»»Ðзû½«±»É¾³ý,ÒòΪechoµÄ±äÁ¿Î´±»""ÒýÓÃ.
5 echo
6 echo "$y" # ÓÃ""½«±äÁ¿ÒýÓÃ,»»Ðзû¾Í²»»á±»¿Õ¸ñÌæ»»ÁË.
7
8 echo; echo
9
10 y=`eval df` # Óë y=`df` ºÜÏàËÆ
11 echo $y #+ »»ÐзûÓÖ±»¿Õ¸ñÌæ»»ÁË.
12
13 # µ±Ã»ÓÐLF(»»Ðзû)³öÏÖʱ,¶ÔÓÚʹÓÃ"awk"ÕâÑùµÄ¹¤¾ßÀ´Ëµ,
14 #+ ¿ÉÄÜ·ÖÎöÊä³öµÄ½á¹û¸üÈÝÒ×һЩ.
15
16 echo
17 echo "==========================================================="
18 echo
19
20 # Now, showing how to "expand" a variable using "eval" . . .
20 # ÏÖÔÚ,À´¿´Ò»ÏÂÔõôÓÃ"eval"ÃüÁîÀ´À©Õ¹Ò»¸ö±äÁ¿. . .
21
22 for i in 1 2 3 4 5; do
23 eval value=$i
24 # value=$i ½«¾ßÓÐͬÑùµÄЧ¹û. "eval"²¢²»·ÇµÃÔÚÕâÀïʹÓÃ.
25 # Ò»¸öȱ·¦ÌØÊ⺬ÒåµÄ±äÁ¿½«±»ÆÀ¹ÀΪ×ÔÉí --
26 #+ Ò²¾ÍÊÇ˵,Õâ¸ö±äÁ¿³ýÁËÄܹ»±»À©Õ¹³É×ÔÉíËù±íʾµÄ×Ö·û,²»ÄÜÀ©Õ¹³ÉÈκÎÆäËûµÄº¬Òå.
27 echo $value
28 done
29
30 echo
31 echo "---"
32 echo
33
34 for i in ls df; do
35 value=eval $i
36 # value=$i has an entirely different effect here.
36 # value=$i ÔÚÕâÀï¾ÍÓëÉϱßÕâ¾äÓÐÁ˱¾ÖÊÉϵÄÇø±ð.
37 # "eval" ½«»áÆÀ¹ÀÃüÁî "ls" ºÍ "df" . . .
38 # ÊõÓï "ls" ºÍ "df" ¾Í¾ßÓÐÌØÊ⺬Òå,
39 #+ ÒòΪËüÃDZ»½âÊͳÉÃüÁî,
40 #+ ¶ø²»ÊÇ×Ö·û´®±¾Éí.
41 echo $value
42 done
43
44
45 exit 0
################################End Script######################################
###
Example 11-12 Ç¿ÖƵdzö(log-off)
################################Start Script####################################
###
1 #!/bin/bash
2 # ½áÊøppp½ø³ÌÀ´Ç¿ÖƵdzölog-off.
3
4 # ½Å±¾Ó¦¸ÃÒÔ¸ùÓû§µÄÉí·ÝÀ´ÔËÐÐ.
5
6 killppp="eval kill -9 `ps ax | awk '/ppp/ { print $1 }'`"
7 # -------- ppp µÄ½ø³ÌID -------
8
9 $killppp # Õâ¸ö±äÁ¿ÏÖÔÚ³ÉΪÁËÒ»¸öÃüÁî.
10
11
12 # ϱߵÄÃüÁî±ØÐëÒÔ¸ùÓû§µÄÉí·ÝÀ´ÔËÐÐ.
13
14 chmod 666 /dev/ttyS3 # »Ö¸´¶ÁдȨÏÞ,·ñÔòʲô?
15 # ÒòΪÔÚpppÉÏÖ´ÐÐÒ»¸öSIGKILL½«»áÐ޸Ĵ®¿ÚµÄȨÏÞ,
16 #+ ÎÒÃÇ°ÑȨÏÞ»Ö¸´µ½Ö®Ç°µÄ״̬.
17
18 rm /var/lock/LCK..ttyS3 # ɾ³ý´®¿ÚËöÎļþ.Ϊʲô?
19
20 exit 0
21
22 # Á·Ï°:
23 # -----
24 # 1) ±àдһ¸ö½Å±¾À´ÑéÖ¤ÊÇ·ñ¸úÓû§ÕýÔÚÔËÐÐËü.
25 # 2) ×öÒ»¸ö¼ì²é,¼ì²éһϽ«ÒªÉ±µôµÄ½ø³Ì
26 #+ ÔÙɱµôÕâ¸ö½ø³Ì֮ǰ,ËüÊÇ·ñÕýÔÚÔËÐÐ.
27 # 3) »ùÓÚ'fuser'À´±àд´ïµ½Õâ¸öÄ¿µÄµÄÁíÒ»¸ö°æ±¾µÄ½Å±¾
28 #+ if [ fuser -s /dev/modem ]; then . . .
################################End Script######################################
###
Example 11-13 ÁíÒ»¸ö"rot13"µÄ°æ±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓÃ'eval'µÄÒ»¸ö"rot13"µÄ°æ±¾,(ÒëÕß:rot13¾ÍÊÇ°Ñ26¸ö×Öĸ,´ÓÖмä·ÖΪ2°ê,¸÷13¸ö)
3 # Óë½Å±¾"rot13.sh" ±È½ÏÒ»ÏÂ.
4
5 setvar_rot_13() # "rot13" º¯Êý
6 {
7 local varname=$1 varvalue=$2
8 eval $varname='$(echo "$varvalue" | tr a-z n-za-m)'
9 }
10
11
12 setvar_rot_13 var "foobar" # ÓÃ"foobar" ´«µÝµ½rot13º¯ÊýÖÐ.
13 echo $var # ½á¹ûÊÇsbbone
14
15 setvar_rot_13 var "$var" # ´«µÝ"sbbone" µ½rot13º¯ÊýÖÐ.
16 # ÓÖ±ä³ÉÁËԭʼֵ.
17 echo $var # foobar
18
19 # Õâ¸öÀý×ÓÊÇSegebart Chazelas±àдµÄ.
20 # ×÷ÕßÓÖÐÞ¸ÄÁËÒ»ÏÂ.
21
22 exit 0
################################End Script######################################
###
Rory Winston¾èÏ×ÁËϱàµÄ½Å±¾,¹ØÓÚʹÓÃevalÃüÁî.
Example 11-14 ÔÚPerl½Å±¾ÖÐʹÓÃevalÃüÁîÀ´Ç¿ÖƱäÁ¿Ìæ»»
################################Start Script####################################
###
1 In the Perl script "test.pl":
2 ...
3 my $WEBROOT = <WEBROOT_PATH>;
4 ...
5
6 To force variable substitution try:
7 $export WEBROOT_PATH=/usr/local/webroot
8 $sed 's/<WEBROOT_PATH>/$WEBROOT_PATH/' < test.pl > out
9
10 But this just gives:
11 my $WEBROOT = $WEBROOT_PATH;
12
13 However:
14 $export WEBROOT_PATH=/usr/local/webroot
15 $eval sed 's%\<WEBROOT_PATH\>%$WEBROOT_PATH%' < test.pl > out
16 # ====
17
18 That works fine, and gives the expected substitution:
19 my $WEBROOT = /usr/local/webroot;
20
21
22 ### Correction applied to original example by Paulo Marcel Coelho Aragao.
################################End Script######################################
###
evalÃüÁîÊÇÓзçÏÕµÄ,Èç¹ûÓиüºÏÊʵķ½·¨À´ÊµÏÖ¹¦ÄܵĻ°,¾¡Á¿Òª±ÜÃâʹÓÃËü.
evalÃüÁִÐÐÃüÁîµÄÄÚÈÝ,Èç¹ûÃüÁîÖÐÓÐrm -rf*ÕâÖÖ¶«Î÷,¿ÉÄܾͲ»ÊÇÄãÏëÒªµÄÁË.
Èç¹ûÔÚÒ»¸ö²»ÊìϤµÄÈ˱àдµÄ½Å±¾ÖÐʹÓÃevalÃüÁÊÇΣÏÕµÄ.
set
setÃüÁîÓÃÀ´ÐÞ¸ÄÄÚ²¿½Å±¾±äÁ¿µÄÖµ.Ò»¸ö×÷ÓþÍÊÇ´¥·¢Ñ¡Ïî±ê־λÀ´°ïÖú¾ö¶¨½Å±¾µÄÐÐ
Ϊ.ÁíÒ»¸öÓ¦ÓþÍÊÇÒÔÒ»¸öÃüÁîµÄ½á¹û(set `command`)À´ÖØÐÂÉèÖýű¾µÄλÖòÎÊý.½Å±¾
½«»á´ÓÃüÁîµÄÊä³öÖÐÖØзÖÎö³öλÖòÎÊý.
Example 11-15 ʹÓÃsetÀ´¸Ä±ä½Å±¾µÄλÖòÎÊý
################################Start Script####################################
###
1 #!/bin/bash
2
3 # script "set-test"
4
5 # ʹÓÃ3¸öÃüÁîÐвÎÊýÀ´µ÷ÓÃÕâ¸ö½Å±¾,
6 # ±ÈÈç, "./set-test one two three".
7
8 echo
9 echo "Positional parameters before set \`uname -a\` :" #unameÃüÁî´òÓ¡²Ù×÷ϵͳÃû
10 echo "Command-line argument #1 = $1"
11 echo "Command-line argument #2 = $2"
12 echo "Command-line argument #3 = $3"
13
14
15 set `uname -a` # °Ñ`uname -a`µÄÃüÁîÊä³öÉèÖÃ
16 # ΪеÄλÖòÎÊý.
17
18 echo $_ # ÕâÒª¿´ÄãµÄunmae -aÊä³öÁË,Õâ¾ä´òÓ¡³öµÄ¾ÍÊÇÊä³öµÄ×îºóÒ»¸öµ¥´Ê.
19 # Ôڽű¾ÖÐÉèÖñêÖ¾.
20
21 echo "Positional parameters after set \`uname -a\` :"
22 # $1, $2, $3, µÈµÈ. ÕâЩλÖòÎÊý½«±»ÖØгõʼ»¯Îª`uname -a`µÄ½á¹û
23 echo "Field #1 of 'uname -a' = $1"
24 echo "Field #2 of 'uname -a' = $2"
25 echo "Field #3 of 'uname -a' = $3"
26 echo ---
27 echo $_ # ---
28 echo
29
30 exit 0
################################End Script######################################
###
²»Ê¹ÓÃÈκÎÑ¡Ïî»ò²ÎÊýÀ´µ÷ÓÃsetÃüÁîµÄ»°,½«»áÁгöËùÓеĻ·¾³±äÁ¿ºÍÆäËûËùÓеÄÒѾ­
³õʼ»¯¹ýµÄÃüÁî.
bash$ set
AUTHORCOPY=/home/bozo/posts
BASH=/bin/bash
BASH_VERSION=$'2.05.8(1)-release'
...
XAUTHORITY=/home/bozo/.Xauthority
_=/etc/bashrc
variable22=abc
variable23=xzy
ʹÓòÎÊý--À´µ÷ÓÃsetÃüÁîµÄ»°,½«»áÃ÷È·µÄ·ÖÅäλÖòÎÊý.Èç¹û--Ñ¡Ïîºó±ßûÓиú±äÁ¿Ãû
µÄ»°,ÄÇô½á¹û¾ÍʹËùÓÐλÖòÎÊý¶¼±ÈunsetÁË.
Example 11-16 ÖØзÖÅäλÖòÎÊý
################################Start Script####################################
###
1 #!/bin/bash
2
3 variable="one two three four five"
4
5 set -- $variable
6 # ½«Î»ÖòÎÊýµÄÄÚÈÝÉèΪ±äÁ¿"$variable"µÄÄÚÈÝ.
7
8 first_param=$1
9 second_param=$2
10 shift; shift # Shift past first two positional params.
11 remaining_params="$*"
12
13 echo
14 echo "first parameter = $first_param" # one
15 echo "second parameter = $second_param" # two
16 echo "remaining parameters = $remaining_params" # three four five
17
18 echo; echo
19
20 # ÔÙÀ´Ò»´Î.
21 set -- $variable
22 first_param=$1
23 second_param=$2
24 echo "first parameter = $first_param" # one
25 echo "second parameter = $second_param" # two
26
27 # ======================================================
28
29 set --
30 # Unsets positional parameters if no variable specified.
30 # Èç¹ûûָ¶¨±äÁ¿,ÄÇô½«»áunsetËùÓеÄλÖòÎÊý.
31
32 first_param=$1
33 second_param=$2
34 echo "first parameter = $first_param" # (null value)
35 echo "second parameter = $second_param" # (null value)
36
37 exit 0
################################End Script######################################
###
¼ûExample 10-2,ºÍExample 12-51.
unset
unsetÃüÁîÓÃÀ´É¾³ýÒ»¸öshell±äÁ¿,Ч¹û¾ÍÊÇ°ÑÕâ¸ö±äÁ¿ÉèΪnull.×¢Òâ:Õâ¸öÃüÁî¶ÔλÖÃ
²ÎÊýÎÞЧ.
bash$ unset PATH
bash$ echo $PATH

bash$
Example 11-17 UnsetÒ»¸ö±äÁ¿
################################Start Script####################################
###
1 #!/bin/bash
2 # unset.sh: UnsetÒ»¸ö±äÁ¿.
3
4 variable=hello # ³õʼ»¯.
5 echo "variable = $variable"
6
7 unset variable # Unset.
8 # Óë variable= µÄЧ¹ûÏàͬ.
9 echo "(unset) variable = $variable" # $variable ÉèΪ null.
10
11 exit 0
################################End Script######################################
###
export
exportÃüÁ»áʹµÃ±»exportµÄ±äÁ¿ÔÚÔËÐеĽű¾(»òshell)µÄËùÓеÄ×Ó½ø³ÌÖж¼¿ÉÓÃ.
²»ÐÒµÄÊÇ,ûÓа취½«±äÁ¿exportµ½¸¸½ø³Ì(¾ÍÊǵ÷ÓÃÕâ¸ö½Å±¾»òshellµÄ½ø³Ì)ÖÐ.
¹ØÓÚexportÃüÁîµÄÒ»¸öÖØÒªµÄʹÓþÍÊÇÓÃÔÚÆô¶¯ÎļþÖÐ,Æô¶¯ÎļþÊÇÓÃÀ´³õʼ»¯²¢ÇÒ
ÉèÖû·¾³±äÁ¿,ÈÃÓû§½ø³Ì¿ÉÒÔ´æÈ¡»·¾³±äÁ¿.
Example 11-18 ʹÓÃexportÃüÁî´«µÝÒ»¸ö±äÁ¿µ½Ò»¸öÄÚǶawkµÄ½Å±¾ÖÐ
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ÕâÊÇ"ÇóÁеĺÍ"½Å±¾µÄÁíÍâÒ»¸ö°æ±¾(col-totaler.sh)
4 #+ ÄǸö½Å±¾¿ÉÒÔ°ÑÄ¿±êÎļþÖеÄÖ¸¶¨µÄÁÐÉϵÄËùÓÐÊý×ÖÈ«²¿ÀÛ¼ÓÆðÀ´,ÇóºÍ.
5 # Õâ¸ö°æ±¾½«°ÑÒ»¸ö±äÁ¿Í¨¹ýexportµÄÐÎʽ´«µÝµ½'awk'ÖÐ . . .
6 #+ ²¢ÇÒ°Ñawk½Å±¾·Åµ½Ò»¸ö±äÁ¿ÖÐ.
7
8
9 ARGS=2
10 E_WRONGARGS=65
11
12 if [ $# -ne "$ARGS" ] # ¼ì²éÃüÁîÐвÎÊýµÄ¸öÊý.
13 then
14 echo "Usage: `basename $0` filename column-number"
15 exit $E_WRONGARGS
16 fi
17
18 filename=$1
19 column_number=$2
20
21 #===== ÉϱߵÄÕⲿ·Ö,Óëԭʼ½Å±¾ÍêÈ«Ò»Ñù =====#
22
23 export column_number
24 # ½«ÁкÅͨ¹ýexport³öÀ´,ÕâÑùºó±ßµÄ½ø³Ì¾Í¿ÉÓÃÁË.
25
26
27 # -----------------------------------------------
28 awkscript='{ total += $ENVIRON["column_number"] }
29 END { print total }'
30 # ÊǵÄ,Ò»¸ö±äÁ¿¿ÉÒÔ±£´æÒ»¸öawk½Å±¾.
31 # -----------------------------------------------
32
33 # ÏÖÔÚ,ÔËÐÐawk½Å±¾.
34 awk "$awkscript" "$filename"
35
36 # Thanks, Stephane Chazelas.
37
38 exit 0
################################End Script######################################
###
×¢Òâ:¿ÉÒÔÔÚÒ»¸ö²Ù×÷ÖÐͬʱ¸³ÖµºÍexport±äÁ¿,Èç: export var1=xxx.
È»¶ø,ÏñGreg KeraunenÖ¸³öµÄ,ÔÚijЩÇé¿öÏÂʹÓÃÉϱßÕâÖÖÐÎʽ,½«ÓëÏÈÉèÖñäÁ¿,È»ºó
export±äÁ¿Ð§¹û²»Í¬.
bash$ export var=(a b); echo ${var[0]}
(a b)

bash$ var=(a b); export var; echo ${var[0]}


a
declare, typeset
declareºÍtypesetÃüÁî±»ÓÃÀ´Ö¸¶¨»òÏÞÖƱäÁ¿µÄÊôÐÔ.
readonly
Óëdeclare -r×÷ÓÃÏàͬ,ÉèÖñäÁ¿µÄÖ»¶ÁÊôÐÔ,Ò²¿ÉÒÔÈÏΪÊÇÉèÖó£Á¿.ÉèÖÃÁËÕâÖÖÊôÐÔÖ®ºó
Èç¹ûÄ㻹ҪÐÞ¸ÄËü,ÄÇôÄ㽫µÃµ½Ò»¸ö´íÎóÏûÏ¢.ÕâÖÖÇé¿öÓëCÓïÑÔÖеÄconst³£Á¿ÀàÐ͵ÄÇé
¿öÊÇÏàͬµÄ.
getopts
¿ÉÒÔ˵ÕâÊÇ·ÖÎö´«µÝµ½½Å±¾µÄÃüÁîÐвÎÊýµÄ×îÇ¿Á¦¹¤¾ß.Õâ¸öÃüÁîÓëgetoptÍⲿÃüÁî,ºÍ
CÓïÑÔÖеĿ⺯ÊýgetoptµÄ×÷ÓÃÊÇÏàͬµÄ.ËüÔÊÐí´«µÝºÍÁ¬½Ó¶à¸öÑ¡Ïî[2]µ½½Å±¾ÖÐ,²¢ÄÜ·Ö
Åä¶à¸ö²ÎÊýµ½½Å±¾ÖÐ.
getopts½á¹¹Ê¹ÓÃÁ½¸öÒþº¬±äÁ¿.$OPTINDÊDzÎÊýÖ¸Õë(Ñ¡ÏîË÷Òý),ºÍ$OPTARG(Ñ¡Ïî²ÎÊý)
(¿ÉÑ¡µÄ)¿ÉÒÔÔÚÑ¡Ïîºó±ß¸½¼ÓÒ»¸ö²ÎÊý.ÔÚÉùÃ÷±êÇ©ÖÐ,Ñ¡ÏîÃûºó±ßµÄðºÅÓÃÀ´Ìáʾ
Õâ¸öÑ¡ÏîÃûÒѾ­·ÖÅäÁËÒ»¸ö²ÎÊý.
getopts½á¹¹Í¨³£¶¼×é³ÉÒ»×é·ÅÔÚÒ»¸öwhileÑ­»·ÖÐ,Ñ­»·¹ý³ÌÖÐÿ´Î´¦ÀíÒ»¸öÑ¡ÏîºÍ²ÎÊý,
È»ºóÔö¼ÓÒþº¬±äÁ¿$OPTINDµÄÖµ,ÔÙ½øÐÐÏÂÒ»´ÎµÄ´¦Àí.
×¢Òâ: 1.ͨ¹ýÃüÁîÐд«µÝµ½½Å±¾ÖеIJÎÊýÇ°±ß±ØÐë¼ÓÉÏÒ»¸ö¼õºÅ(-).ÕâÊÇÒ»¸öǰ׺,ÕâÑù
getoptsÃüÁ»áÈÏΪÕâ¸ö²ÎÊýÊÇÒ»¸öÑ¡Ïî.ÊÂʵÉÏ,getopts²»»á´¦Àí²»´ø"-"Ç°
µÄ²ÎÊý,Èç¹ûµÚÒ»¸ö²ÎÊý¾ÍûÓÐ"-",ÄÇô½«½áÊøÑ¡ÏîµÄ´¦Àí.
2.ʹÓÃgetoptsµÄwhileÑ­»·Ä£°æ»¹ÊÇÓë±ê×¼µÄwhileÑ­»·Ä£°æÓÐЩ²»Í¬.ûÓбê×¼
whileÑ­»·ÖеÄ[]ÅжÏÌõ¼þ.
3.getopts½á¹¹½«»áÈ¡´úgetoptÍⲿÃüÁî.
################################Start Script####################################
###
1 while getopts ":abcde:fg" Option
2 # Initial declaration.
2 # ¿ªÊ¼µÄÉùÃ÷.
3 # a, b, c, d, e, f, ºÍ g ±»ÈÏΪÊÇÑ¡Ïî(±êÖ¾).
4 # eÑ¡Ïîºó±ßµÄ:Ìáʾ,Õâ¸öÑ¡Ïî´øÒ»¸ö²ÎÊý.
5 do
6 case $Option in
7 a ) # Do something with variable 'a'.
7 a ) # ¶ÔÑ¡Ïî'a'×÷Щ²Ù×÷.
8 b ) # ¶ÔÑ¡Ïî'b'×÷Щ²Ù×÷.
9 ...
10 e) # Do something with 'e', and also with $OPTARG,
10 e) # ¶ÔÑ¡Ïî'e'×÷Щ²Ù×÷, ͬʱ´¦ÀíÒ»ÏÂ$OPTARG,
11 # which is the associated argument passed with option 'e'.
11 # Õâ¸ö±äÁ¿Àï±ß½«±£´æ´«µÝ¸øÑ¡Ïî"e"µÄ²ÎÊý.
12 ...
13 g ) # ¶ÔÑ¡Ïî'g'×÷Щ²Ù×÷.
14 esac
15 done
16 shift $(($OPTIND - 1))
17 # ½«²ÎÊýÖ¸ÕëÏòÏÂÒƶ¯.
18
19 # ËùÓÐÕâЩԶûÓÐËü¿´ÆðÀ´µÄÄÇô¸´ÔÓ.<ºÙºÙ>
20
################################End Script######################################
###
Example 11-19 ʹÓÃgetoptsÃüÁîÀ´¶ÁÈ¡´«µÝ¸ø½Å±¾µÄÑ¡Ïî/²ÎÊý.
(ÎÒ²âÊԵĽá¹ûÓë˵Ã÷²»Í¬,ÎÒʹÓà ./scriptname -mnp,µ«ÊÇ$OPTINDµÄÖµ¾ÓÈ»ÊÇ1 1 2)
################################Start Script####################################
###
1 #!/bin/bash
2 # Á·Ï° getopts ºÍ OPTIND
3 # ÔÚBill GradwohlµÄ½¨ÒéÏÂ,Õâ¸ö½Å±¾ÓÚ 10/09/03 ±»ÐÞ¸Ä.
4
5
6 # ÕâÀïÎÒÃǽ«Ñ§Ï° 'getopts'ÈçºÎ´¦Àí½Å±¾µÄÃüÁîÐвÎÊý.
7 # ²ÎÊý±»×÷Ϊ"Ñ¡Ïî"(±êÖ¾)±»½âÎö,²¢ÇÒ·ÖÅä²ÎÊý.
8
9 # ÊÔÒ»ÏÂͨ¹ýÈçÏ·½·¨À´µ÷ÓÃÕâ¸ö½Å±¾
10 # 'scriptname -mn'
11 # 'scriptname -oq qOption' (qOption ¿ÉÒÔÊÇÈÎÒâµÄÄÄÅÂÓÐЩ¹îÒì×Ö·ûµÄ×Ö·û´®.)
12 # 'scriptname -qXXX -r'
13 #
14 # 'scriptname -qr' - ÒâÍâµÄ½á¹û, "r" ½«±»¿´³ÉÊÇÑ¡Ïî "q" µÄ²ÎÊý.
15 # 'scriptname -q -r' - ÒâÍâµÄ½á¹û, ͬÉÏ.
16 # 'scriptname -mnop -mnop' - ÒâÍâµÄ½á¹û
17 # (OPTIND is unreliable at stating where an option came from).
18 #
19 # Èç¹ûÒ»¸öÑ¡ÏîÐèÒªÒ»¸ö²ÎÊý("flag:"),ÄÇôËüÓ¦¸Ã
20 #+ È¡µÃÔÚÃüÁîÐÐÉÏ°¤ÔÚËüºó±ßµÄÈκÎ×Ö·û.
21
22 NO_ARGS=0
23 E_OPTERROR=65
24
25 if [ $# -eq "$NO_ARGS" ] # ²»´øÃüÁîÐвÎÊý¾Íµ÷Óýű¾?
26 then
27 echo "Usage: `basename $0` options (-mnopqrs)"
28 exit $E_OPTERROR # Èç¹ûûÓвÎÊý´«½øÀ´,ÄǾÍÍ˳ö,²¢½âÊÍÓ÷¨.
29 fi
30 # Ó÷¨: ½Å±¾Ãû -Ñ¡ÏîÃû
31 # ×¢Òâ: ÆÆÕÛºÅ(-)ÊDZØÐëµÄ
32
33
34 while getopts ":mnopq:rs" Option
35 do
36 case $Option in
37 m ) echo "Scenario #1: option -m- [OPTIND=${OPTIND}]";;
38 n | o ) echo "Scenario #2: option -$Option- [OPTIND=${OPTIND}]";;
39 p ) echo "Scenario #3: option -p- [OPTIND=${OPTIND}]";;
40 q ) echo "Scenario #4: option -q-\
41 with argument \"$OPTARG\" [OPTIND=${OPTIND}]";;
42 # ×¢Òâ,Ñ¡Ïî'q'±ØÐë·ÖÅäÒ»¸ö²ÎÊý,
43 #+ ·ñÔòĬÈϽ«Ê§°Ü.
44 r | s ) echo "Scenario #5: option -$Option-";;
45 * ) echo "Unimplemented option chosen.";; # DEFAULT
46 esac
47 done
48
49 shift $(($OPTIND - 1))
50 # ½«²ÎÊýÖ¸Õë¼õ1,ÕâÑùËü½«Ö¸ÏòÏÂÒ»¸ö²ÎÊý.
51 # $1 ÏÖÔÚÒýÓõÄÊÇÃüÁîÐÐÉϵĵÚÒ»¸ö·ÇÑ¡Ïî²ÎÊý
52 #+ Èç¹ûÓÐÒ»¸öÕâÑùµÄ²ÎÊý´æÔڵĻ°.
53
54 exit 0
55
56 # Ïñ Bill Gradwohl Ëù˵,<rojy bug>
57 # "The getopts mechanism allows one to specify: scriptname -mnop -mnop
58 #+ but there is no reliable way to differentiate what came from where
59 #+ by using OPTIND."
################################End Script######################################
###
½Å±¾ÐÐΪ
source, . (µãÃüÁî)
Õâ¸öÃüÁîÔÚÃüÁîÐÐÉÏÖ´ÐеÄʱºò,½«»áÖ´ÐÐÒ»¸ö½Å±¾.ÔÚÒ»¸öÎļþÄÚÒ»¸ösource file-name
½«»á¼ÓÔØfile-nameÎļþ.sourceÒ»¸öÎļþ(»òµãÃüÁî)½«»áÔڽű¾ÖÐÒýÈë´úÂë,²¢¸½¼Óµ½½Å
±¾ÖÐ(ÓëCÓïÑÔÖеÄ#includeÖ¸ÁîµÄЧ¹ûÏàͬ).×îÖյĽá¹û¾ÍÏñÊÇÔÚʹÓÃ"sourced"ÐÐÉϲå
ÈëÁËÏàÓ¦ÎļþµÄÄÚÈÝ.ÕâÔÚ¶à¸ö½Å±¾ÐèÒªÒýÓÃÏàͬµÄÊý¾Ý,»òº¯Êý¿âʱ·Ç³£ÓÐÓÃ.
Example 11-20 "Including"Ò»¸öÊý¾ÝÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2
3 . data-file # ¼ÓÔØÒ»¸öÊý¾ÝÎļþ.
4 # Óë"source data-file"Ч¹ûÏàͬ,µ«ÊǸü¾ß¿ÉÒÆÖ²ÐÔ.
5
6 # Îļþ"data-file"±ØÐë´æÔÚÓÚµ±Ç°¹¤×÷Ŀ¼,
7 #+ ÒòΪÕâ¸öÎļþʱʹÓÃ'basename'À´ÒýÓõÄ.
8
9 # ÏÖÔÚ,ÒýÓÃÕâ¸öÊý¾ÝÎļþÖеÄһЩÊý¾Ý.
10
11 echo "variable1 (from data-file) = $variable1"
12 echo "variable3 (from data-file) = $variable3"
13
14 let "sum = $variable2 + $variable4"
15 echo "Sum of variable2 + variable4 (from data-file) = $sum"
16 echo "message1 (from data-file) is \"$message1\""
17 # ×¢Òâ : ½«Ë«ÒýºÅתÒå
18
19 print_message This is the message-print function in the data-file.
20
21
22 exit 0
################################End Script######################################
###
Example 11-20ʹÓõÄdata-file.¼ûÉϱß,Õâ¸öÎļþ±ØÐëºÍÉϱߵĽű¾·ÅÔÚͬһĿ¼ÏÂ.
################################Start Script####################################
###
1 # ÕâÊÇÐèÒª±»½Å±¾¼ÓÔصÄdata file.
2 # ÕâÖÖÎļþ¿ÉÒÔ°üº¬±äÁ¿,º¯Êý,µÈµÈ.
3 # Ôڽű¾ÖпÉÒÔͨ¹ý'source'»òÕß'.'ÃüÁîÀ´¼ÓÔØ.
4
5 # ÈÃÎÒÃdzõʼ»¯Ò»Ð©±äÁ¿.
6
7 variable1=22
8 variable2=474
9 variable3=5
10 variable4=97
11
12 message1="Hello, how are you?"
13 message2="Enough for now. Goodbye."
14
15 print_message ()
16 {
17 # Echo³ö´«µÝ½øÀ´µÄÈκÎÏûÏ¢.
18
19 if [ -z "$1" ]
20 then
21 return 1
22 # Èç¹ûûÓвÎÊýµÄ»°,³ö´í.
23 fi
24
25 echo
26
27 until [ -z "$1" ]
28 do
29 # Ñ­»·´¦Àí´«µÝµ½º¯ÊýÖеIJÎÊý.
30 echo -n "$1"
31 # ÿ´ÎEcho Ò»¸ö²ÎÊý, -n½ûÖ¹»»ÐÐ.
32 echo -n " "
33 # ÔÚ²ÎÊý¼ä²åÈë¿Õ¸ñ.
34 shift
35 # ÏÂÒ»¸ö.
36 done
37
38 echo
39
40 return 0
41 }
################################End Script######################################
###
Èç¹ûÒýÈëµÄÎļþ±¾Éí¾ÍÊÇÒ»¸ö¿ÉÖ´Ðнű¾µÄ»°,ÄÇôËü½«ÔËÐÐÆðÀ´,µ±ËüreturnµÄʱºò,¿ØÖÆ
ȨÓÖÖØлص½ÁËÒýÓÃËüµÄ½Å±¾ÖÐ.Ò»¸öÓÃsourceÒýÈëµÄ½Å±¾¿ÉÒÔʹÓÃreturn ÃüÁîÀ´´ïµ½Õâ
¸öÄ¿µÄ.
Ò²¿ÉÒÔÏòÐèÒªsourceµÄ½Å±¾Öд«µÝ²ÎÊý.ÕâЩ²ÎÊýÔÚsource½Å±¾Öб»ÈÏΪÊÇλÖòÎÊý.
1 source $filename $arg1 arg2
ÄãÉõÖÁ¿ÉÒÔÔڽű¾ÎļþÖÐsource½Å±¾Îļþ×ÔÉí,ËäÈ»¿´²»³öÓÐʲôʵ¼ÊµÄÓ¦ÓüÛÖµ.
Example 11-21 Ò»¸öûʲôÓõÄ,source×ÔÉíµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # self-source.sh: Ò»¸ö½Å±¾µÝ¹éµÄsource×ÔÉí.
3 # À´×ÔÓÚ"Stupid Script Tricks," ¾í II.
4
5 MAXPASSCNT=100 # source×ÔÉíµÄ×î´óÊýÁ¿.
6
7 echo -n "$pass_count "
8 # ÔÚµÚÒ»´ÎÔËÐеÄʱºò,Õâ¾äÖ»²»¹ýecho³ö2¸ö¿Õ¸ñ,
9 #+ ÒòΪ$pass_count»¹Ã»±»³õʼ»¯.
10
11 let "pass_count += 1"
12 # ¼Ù¶¨Õâ¸öΪ³õʼ»¯µÄ±äÁ¿ $pass_count
13 #+ ¿ÉÒÔÔÚµÚÒ»´ÎÔËÐеÄʱºò+1.
14 # Õâ¾ä¿ÉÒÔÕý³£¹¤×÷ÓÚBashºÍpdksh,µ«ÊÇ
15 #+ ËüÒÀÀµÓÚ²»¿ÉÒÆÖ²(²¢ÇÒ¿ÉÄÜΣÏÕ)µÄÐÐΪ.
16 # ¸üºÃµÄ·½·¨ÊÇÔÚʹÓÃ$pass_count֮ǰ,ÏÈ°ÑÕâ¸ö±äÁ¿³õʼ»¯Îª0.
17
18 while [ "$pass_count" -le $MAXPASSCNT ]
19 do
20 . $0 # ½Å±¾"sources" ×ÔÉí, ¶ø²»Êǵ÷ÓÃ×Ô¼º.
21 # ./$0 (Ó¦¸ÃÄܹ»Õý³£µÝ¹é) µ«ÊDz»ÄÜÔÚÕâÕý³£ÔËÐÐ. Ϊʲô?
22 done
23
24 # ÕâÀï·¢ÉúµÄ¶¯×÷²¢²»ÊÇÕæÕýµÄµÝ¹é,
25 #+ ÒòΪ½Å±¾³É¹¦µÄÕ¹¿ªÁË×Ô¼º,»»¾ä»°Ëµ,
26 #+ ÔÚÿ´ÎÑ­»·µÄ¹ý³ÌÖÐ
27 #+ ÔÚÿ¸ö'source'ÐÐ(µÚ20ÐÐ)ÉÏ
28 # ¶¼²úÉúÁËеĴúÂë.
29 #
30 # µ±È»,½Å±¾»á°Ñÿ¸öÐÂ'sourced'½øÀ´µÄÎļþµÄ"#!"ÐÐ
31 #+ ¶¼½âÊͳÉ×¢ÊÍ,¶ø²»»á°ÑËü¿´³ÉÊÇÒ»¸öеĽű¾.
32
33 echo
34
35 exit 0 # ×îÖÕµÄЧ¹û¾ÍÊÇ´Ó1Êýµ½100.
36 # ÈÃÈËÓ¡ÏóÉî¿Ì.
37
38 # Á·Ï°:
39 # -----
40 # ʹÓÃÕâ¸öС¼¼ÇɱàдһЩÕæÕýÄܸÉЩÊÂÇéµÄ½Å±¾.
################################End Script######################################
###
exit
¾ø¶ÔµÄÍ£Ö¹Ò»¸ö½Å±¾µÄÔËÐÐ.exitÃüÁîÓпÉÒÔËæ±ãÕÒÒ»¸öÕûÊý±äÁ¿×÷ΪÍ˳ö½Å±¾·µ»Øshell
ʱµÄÍ˳öÂë.ʹÓÃexit 0¶ÔÓÚÍ˳öÒ»¸ö¼òµ¥½Å±¾À´ËµÊÇÖÖºÃÏ°¹ß,±íÃ÷³É¹¦ÔËÐÐ.
×¢Òâ: Èç¹û²»´ø²ÎÊýµÄʹÓÃexitÀ´Í˳ö,ÄÇôÍ˳öÂ뽫Êǽű¾ÖÐ×îºóÒ»¸öÃüÁîµÄÍ˳öÂë.
µÈ¼ÛÓÚexit $?.
exec
Õâ¸öshellÄÚ½¨ÃüÁʹÓÃÒ»¸öÌض¨µÄÃüÁîÀ´È¡´úµ±Ç°½ø³Ì.Ò»°ãµÄµ±shellÓöµ½Ò»¸öÃüÁî,
Ëü»áfork offÒ»¸ö×Ó½ø³ÌÀ´ÕæÕýµÄÔËÐÐÃüÁî.ʹÓÃexecÄÚ½¨ÃüÁî,shell¾Í²»»áforkÁË,²¢
ÇÒÃüÁîµÄÖ´Ðн«»áÌæ»»µôµ±Ç°shell.Òò´Ë,µ±ÎÒÃÇÔڽű¾ÖÐʹÓÃËüʱ,µ±ÃüÁîʵÐÐÍê±Ï,
Ëü¾Í»áÇ¿ÖÆÍ˳ö½Å±¾.[3]
Example 11-22 execµÄЧ¹û
################################Start Script####################################
###
1 #!/bin/bash
2
3 exec echo "Exiting \"$0\"." # ½Å±¾½«ÔÚ´ËÍ˳ö.
4
5 # ----------------------------------
6 # ϱߵIJ¿·Ö½«Ö´Ðв»µ½.
7
8 echo "This echo will never echo."
9
10 exit 99 # ½Å±¾²»»áÔÚÕâÍ˳ö.
11 # ½Å±¾Í˳öºó¼ì²éÒ»ÏÂÍ˳öÂë
12 #+ ʹÓÃ'echo $?'ÃüÁî.
13 # ¿Ï¶¨²»ÊÇ99.
################################End Script######################################
###
Example 11-23 Ò»¸öexec×ÔÉíµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # self-exec.sh
3
4 echo
5
6 echo "This line appears ONCE in the script, yet it keeps echoing."
7 echo "The PID of this instance of the script is still $$."
8 # ÉϱßÕâ¾äÓÃÀ´¸ù±¾Ã»²úÉú×Ó½ø³Ì.
9
10 echo "==================== Hit Ctl-C to exit ===================="
11
12 sleep 1
13
14 exec $0 # ²úÉúÁ˱¾½Å±¾µÄÁíÒ»¸öʵÀý,
15 #+ ²¢ÇÒÕâ¸öʵÀý´úÌæÁË֮ǰµÄÄǸö.
16
17 echo "This line will never echo!" # µ±È»»áÕâÑù.
18
19 exit 0
################################End Script######################################
###
execÃüÁÄÜÓÃÓÚÖØзÖÅäÎļþÃèÊö·û.±ÈÈç: exec <zzz-file½«»áÓÃzzz-fileÀ´´úÌæ
stdin.
×¢Òâ: findÃüÁîµÄ -execÑ¡ÏîÓëshellÄÚ½¨µÄexecÃüÁîÊDz»Í¬µÄ.
shopt
Õâ¸öÃüÁîÔÊÐíshellÔÚ¿ÕÏÐʱÐÞ¸ÄshellÑ¡Ïî(¼ûExample 24-1ºÍExample 24-2).Ëü¾­³£³ö
ÏÖÔÚÆô¶¯½Å±¾ÖÐ,µ«ÊÇÔÚÒ»°ã½Å±¾ÖÐÒ²¿ÉÓÃ.ÐèÒªBash 2.0°æ±¾ÒÔÉÏ.
1 shopt -s cdspell
2 # Allows minor misspelling of directory names with 'cd'
2 # ʹÓÃ'cd'ÃüÁîʱ,ÔÊÐí²úÉúÉÙÁ¿µÄƴд´íÎó.
3
4 cd /hpme # àÞ! Ó¦¸ÃÊÇ'/home'.
5 pwd # /home
6 # ƴд´íÎó±»¾ÀÕýÁË.
caller
½«callerÃüÁî·Åµ½º¯ÊýÖÐ,½«»áÔÚstdoutÉÏ´òÓ¡³öº¯Êýµ÷ÓÃÕßµÄÐÅÏ¢.
1 #!/bin/bash
2
3 function1 ()
4 {
5 # ÔÚ function1 () ÄÚ²¿.
6 caller 0 # ÏÔʾµ÷ÓÃÕßÐÅÏ¢.
7 }
8
9 function1 # ½Å±¾µÄµÚ9ÐÐ.
10
11 # 9 main test.sh
12 # ^ º¯Êýµ÷ÓÃÕßËùÔÚµÄÐкÅ.
13 # ^^^^ ´Ó½Å±¾µÄ"main"²¿·Öµ÷ÓõÄ.
14 # ^^^^^^^ µ÷Óýű¾µÄÃû×Ö
15
16 caller 0 # ûЧ¹û,ÒòΪÕâ¸öÃüÁî²»ÔÙº¯ÊýÖÐ.
callerÃüÁîÒ²¿ÉÒÔ·µ»ØÔÚÒ»¸ö½Å±¾Öб»sourceµÄÁíÒ»¸ö½Å±¾µÄÐÅÏ¢.Ïóº¯ÊýÒ»Ñù,ÕâÊÇÒ»¸ö
"×ÓÀý³Ìµ÷ÓÃ",Äã»á·¢ÏÖÕâ¸öÃüÁîÔÚµ÷ÊÔµÄʱºòÌرðÓÐÓÃ.
ÃüÁîÀà
ture
Ò»¸ö·µ»Ø³É¹¦(¾ÍÊÇ·µ»Ø0)Í˳öÂëµÄÃüÁî,µ«Êdzý´ËÖ®ÍâʲôÊÂÒ²²»×ö.
1 # ËÀÑ­»·
2 while true # ÕâÀïµÄtrue¿ÉÒÔÓÃ":"Ìæ»»
3 do
4 operation-1
5 operation-2
6 ...
7 operation-n
8 # ÐèÒªÒ»ÖÖÊֶδÓÑ­»·ÖÐÌø³öÀ´,»òÕßÊÇÈÃÕâ¸ö½Å±¾¹ÒÆð.
9 done
flase
Ò»¸ö·µ»Øʧ°Ü(·Ç0)Í˳öÂëµÄÃüÁî,µ«Êdzý´ËÖ®ÍâʲôÊÂÒ²²»×ö.
1 # ²âÊÔ "false"
2 if false
3 then
4 echo "false evaluates \"true\""
5 else
6 echo "false evaluates \"false\""
7 fi
8 # ʧ°Ü»áÏÔʾ"false"
9
10
11 # while "false" Ñ­»· (¿ÕÑ­»·)
12 while false
13 do
14 # ÕâÀï±ßµÄ´úÂ뽫²»»á×ßµ½.
15 operation-1
16 operation-2
17 ...
18 operation-n
19 # ʲôʶ¼Ã»·¢Éú!
20 done
type[cmd]
ÓëwhichÀ©Õ¹ÃüÁîºÜÏàÏñ,type cmd½«¸ø³ö"cmd"µÄÍêÕû·¾¶.ÓëwhichÃüÁͬµÄÊÇ,typeÃü
ÁîÊÇBashÄÚ½¨ÃüÁî.Ò»¸öºÜÓÐÓõÄÑ¡ÏîÊÇ-aÑ¡Ïî,ʹÓÃÕâ¸öÑ¡Ïî¿ÉÒÔ¼ø±ðËùʶ±ðµÄ²ÎÊýÊǹØ
¼ü×Ö»¹ÊÇÄÚ½¨ÃüÁî,Ò²¿ÉÒÔ¶¨Î»Í¬ÃûµÄϵͳÃüÁî.
bash$ type '['
[ is a shell builtin
bash$ type -a '['
[ is a shell builtin
[ is /usr/bin/[
hash[cmds]
ÔÚshellµÄhash±íÖÐ[4],¼Ç¼ָ¶¨ÃüÁîµÄ·¾¶Ãû,ËùÒÔÔÚshell»ò½Å±¾ÖÐÔÚµ÷ÓÃÕâ¸öÃüÁîµÄ
»°,shell»ò½Å±¾½«²»ÐèÒªÔÙÔÚ$PATHÖÐÖØÐÂËÑË÷Õâ¸öÃüÁîÁË.Èç¹û²»´ø²ÎÊýµÄµ÷ÓÃhashÃü
Áî,Ëü½«ÁгöËùÓÐÒѾ­±»hashµÄÃüÁî.-rÑ¡Ïî»áÖØÐÂÉèÖÃhash±í.
bind
bindÄÚ½¨ÃüÁîÓÃÀ´ÏÔʾ»òÐÞ¸Äreadline[5]µÄ¼ü°ó¶¨.
help
»ñµÃshellÄÚ½¨ÃüÁîµÄÒ»¸öСµÄʹÓÃ×ܽá.ÕâÓëwhatisÃüÁî±È½ÏÏó,µ«ÊÇhelpÊÇÄÚ½¨ÃüÁî.
bash$ help exit
exit: exit [n]
Exit the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.

11.1 ×÷Òµ¿ØÖÆÃüÁî
-----------------
ϱߵÄ×÷Òµ¿ØÖÆÃüÁîÐèÒªÒ»¸ö"×÷Òµ±êʶ·û"×÷Ϊ²ÎÊý.¼ûÕâÕ½áβµÄ±í.
jobs
ÔÚºǫ́ÁгöËùÓÐÕýÔÚÔËÐеÄ×÷Òµ,¸ø³ö×÷ÒµºÅ.
×¢Òâ: ½ø³ÌºÍ×÷ÒµµÄ¸ÅÄîÌ«ÈÝÒ×»ìÏýÁË.Ìض¨µÄÄÚ½¨ÃüÁî,±ÈÈçkill,disownºÍwait¼´¿ÉÒÔ
½ÓÊÜÒ»¸ö×÷ÒµºÅ×÷Ϊ²ÎÊýÒ²¿ÉÒÔ½ÓÊÜÒ»¸ö×÷Ϊ²ÎÊý.µ«ÊÇfg,bgºÍjobsÃüÁîÖ»ÄܽÓÊÜ
×÷ÒµºÅ×÷Ϊ²ÎÊý.
bash$ sleep 100 &
[1] 1384
bash $ jobs
[1]+ Running sleep 100 &
×¢Òâ: "1"ÊÇ×÷ÒµºÅ(×÷ÒµÊDZ»µ±Ç°shellËùά»¤µÄ),¶ø"1384"Êǽø³ÌºÅ(½ø³ÌÊDZ»ÏµÍ³
ά»¤µÄ).ΪÁËkillµô×÷Òµ/½ø³Ì,»òÕßʹÓà kill %1ÃüÁî»òÕßʹÓÃkill 1384ÃüÁî
ÕâÁ½¸öÃüÁ¿ÉÒÔ.
¸Ðл,S.C.
disown
´ÓshellµÄµ±Ç°×÷Òµ±íÖÐ,ɾ³ý×÷Òµ.
fg,bg
fgÃüÁî¿ÉÒÔ°ÑÒ»¸öÔÚºǫ́ÔËÐеÄ×÷Òµ·Åµ½Ç°Ì¨À´ÔËÐÐ.¶øbgÃüÁ»áÖØÐÂÆô¶¯Ò»¸ö¹ÒÆðµÄ
×÷Òµ,²¢ÇÒÔÚºǫ́ÔËÐÐËü.Èç¹ûʹÓÃfg»òÕßbgÃüÁîµÄʱºòûָ¶¨×÷ÒµºÅ,ÄÇôĬÈϽ«¶Ôµ±Ç°
ÕýÔÚÔËÐеÄ×÷Òµ×ö²Ù×÷.
wait
Í£Ö¹½Å±¾µÄÔËÐÐ,Ö±µ½ºǫ́ÔËÐеÄËùÓÐ×÷Òµ¶¼½áÊøΪֹ,»òÕßÖ±µ½Ö¸¶¨×÷ÒµºÅ»ò½ø³ÌºÅΪѡ
ÏîµÄ×÷Òµ½áÊøΪֹ.
Äã¿ÉÒÔʹÓÃwaitÃüÁîÀ´·ÀÖ¹ÔÚºǫ́×÷ҵûÍê³É(Õâ»á²úÉúÒ»¸ö¹Â¶ù½ø³Ì)֮ǰÍ˳ö½Å±¾.
Example 11-24 ÔÚ¼ÌÐø´¦Àí֮ǰ,µÈ´ýÒ»¸ö½ø³ÌµÄ½áÊø
################################Start Script####################################
###
1 #!/bin/bash
2
3 ROOT_UID=0 # Ö»ÓÐ$UID Ϊ0µÄÓû§²ÅÓµÓÐrootȨÏÞ.
4 E_NOTROOT=65
5 E_NOPARAMS=66
6
7 if [ "$UID" -ne "$ROOT_UID" ]
8 then
9 echo "Must be root to run this script."
10 # "Run along kid, it's past your bedtime."
11 exit $E_NOTROOT
12 fi
13
14 if [ -z "$1" ]
15 then
16 echo "Usage: `basename $0` find-string"
17 exit $E_NOPARAMS
18 fi
19
20
21 echo "Updating 'locate' database..."
22 echo "This may take a while."
23 updatedb /usr & # ±ØÐëʹÓÃrootÉí·ÝÀ´ÔËÐÐ.
24
25 wait
26 # ½«²»»á¼ÌÐøÏòÏÂÔËÐÐ,³ý·Ç 'updatedb'ÃüÁîÖ´ÐÐÍê³É.
27 # ÄãÏ£ÍûÔÚ²éÕÒÎļþÃû֮ǰ¸üÐÂdatabase.
28
29 locate $1
30
31 # Èç¹ûûÓÐ'wait'ÃüÁîµÄ»°,¶øÇÒÔڱȽÏÔãµÄÇé¿öÏÂ,
32 #+ ½Å±¾¿ÉÄÜÔÚ'updatedb'ÃüÁÔÚÔËÐеÄʱºòÍ˳ö,
33 #+ Õ⽫»áµ¼ÖÂ'updatedb'³ÉΪһ¸ö¹Â¶ù½ø³Ì.
34
35 exit 0
################################End Script######################################
###
µ±È»,wait Ò²¿ÉÒÔ½ÓÊÜÒ»¸ö×÷Òµ±êʶ·û×÷Ϊ²ÎÊý,±ÈÈç,wait %1»òwait $PPID.¼û"×÷Òµ±êʶ
·û±í".
×¢Òâ: ÔÚÒ»¸ö½Å±¾ÖÐ,ʹÓÃÒ»¸öºǫ́ÔËÐеÄÃüÁî(ʹÓÃ&)¿ÉÄÜ»áʹÕâ¸ö½Å±¾¹ÒÆð,Ö±µ½ÇÃ
»Ø³µ,¹ÒÆð²Å»á±»»Ö¸´.¿´ÆðÀ´Ö»ÓÐÕâ¸öÃüÁîµÄ½á¹ûÐèÒªÊä³öµ½stdoutµÄʱºò²Å»á·¢
ÉúÕâÖÖÏÖÏó.Õâ»áÊÇÒ»¸öºÜ·³È˵ÄÏÖÏó.
1 #!/bin/bash
2 # test.sh
3
4 ls -l &
5 echo "Done."
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh
_

¿´ÆðÀ´ÔÚÕâ¸öºǫ́ÔËÐÐÃüÁîµÄºó±ß·ÅÉÏÒ»¸öwaitÃüÁî¿ÉÄÜ»á½â¾öÕâ¸öÎÊÌâ.
1 #!/bin/bash
2 # test.sh
3
4 ls -l &
5 echo "Done."
6 wait
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh
Èç¹û°ÑÕâ¸öºǫ́ÔËÐÐÃüÁîµÄÊä³öÖض¨Ïòµ½ÎļþÖлòÕßÖض¨Ïòµ½/dev/nullÖÐ,Ò²Äܽâ¾ö
Õâ¸öÎÊÌâ.
suspend
Õâ¸öÃüÁîµÄЧ¹ûÓëControl-ZºÜÏàÏñ,µ«ÊÇËü¹ÒÆðµÄÊÇÕâ¸öshell(Õâ¸öshellµÄ¸¸½ø³ÌÓ¦¸Ã
ÔÚºÏÊʵÄʱºòÖØлָ´Ëü).
logout
Í˳öÒ»¸öµÇ½µÄshell,Ò²¿ÉÒÔÖ¸¶¨Ò»¸öÍ˳öÂë.
times
¸ø³öÖ´ÐÐÃüÁîËùÕ¼µÄʱ¼ä,ʹÓÃÈçÏÂÐÎʽÊä³ö:
0m0.020s 0m0.020s
ÕâÊÇÒ»ÖÖºÜÓÐÏÞµÄÄÜÁ¦,ÒòΪÕâ²»³£³öÏÖÓÚshell½Å±¾ÖÐ.
kill
ͨ¹ý·¢ËÍÒ»¸öÊʵ±µÄ½áÊøÐźÅ,À´Ç¿ÖƽáÊøÒ»¸ö½ø³Ì(¼ûExample 13-6).
Example 11-25 Ò»¸ö½áÊø×ÔÉíµÄ½Å±¾.
################################Start Script####################################
###
1 #!/bin/bash
2 # self-destruct.sh
3
4 kill $$ # ½Å±¾½«ÔÚ´Ë´¦½áÊø×Ô¼ºµÄ½ø³Ì.
5 # Recall that "$$" is the script's PID.
5 # »ØÒäÒ»ÏÂ,"$$"¾ÍÊǽű¾µÄPID.
6
7 echo "This line will not echo."
8 # ¶øÇÒshell½«»á·¢ËÍÒ»¸ö"Terminated"ÏûÏ¢µ½stdout.
9
10 exit 0
11
12 # Ôڽű¾½áÊø×ÔÉí½ø³ÌÖ®ºó,
13 #+ Ëü·µ»ØµÄÍ˳öÂëÊÇʲô?
14 #
15 # sh self-destruct.sh
16 # echo $?
17 # 143
18 #
19 # 143 = 128 + 15
20 # ½áÊøÐźÅ
################################End Script######################################
###
×¢Òâ: kill -l½«ÁгöËùÓÐÐźÅ. kill -9 ÊÇ"±Øɱ"ÃüÁî,Õâ¸öÃüÁ»á½áÊøÄÄЩÍç¹ÌµÄ
²»Ïë±»killµôµÄ½ø³Ì.ÓÐʱºòkill -15Ò²¿ÉÒÔ¸ÉÕâ¸ö»î.Ò»¸ö½©Ê¬½ø³Ì²»Äܱ»µÇ½µÄ
Óû§killµô, -- ÒòΪÄã²»ÄÜɱµôһЩÒѾ­ËÀÁ˵Ķ«Î÷ -- ,µ«ÊÇinit½ø³Ì³ÙÔç»á
°ÑËüÇå³ý¸É¾».½©Ê¬½ø³Ì¾ÍÊÇ×Ó½ø³ÌÒѾ­½áÊøµô,¶ø¸¸½ø³ÌȴûkillµôÕâ¸ö×Ó½ø³Ì,
ÄÇôÕâ¸ö×Ó½ø³Ì¾Í³ÉΪ½©Ê¬½ø³Ì.
command
commandÃüÁî»á½ûÓñðÃûºÍº¯ÊýµÄ²éÕÒ.ËüÖ»²éÕÒÄÚ²¿ÃüÁîÒÔ¼°ËÑË÷·¾¶ÖÐÕÒµ½µÄ½Å±¾»ò¿É
Ö´ÐгÌÐò.(ÒëÕß,Ö»ÔÚÒªÖ´ÐеÄÃüÁîÓ뺯Êý»ò±ðÃûͬÃûʱʹÓÃ,ÒòΪº¯ÊýµÄÓÅÏȼ¶±ÈÄÚ½¨Ãü
ÁîµÄÓÅÏȼ¶¸ß)
(ÒëÕß:×¢ÒâÒ»ÏÂbashÖ´ÐÐÃüÁîµÄÓÅÏȼ¶:
1.±ðÃû
2.¹Ø¼ü×Ö
3.º¯Êý
4.ÄÚÖÃÃüÁî
5.½Å±¾»ò¿ÉÖ´ÐгÌÐò($PATH)
)

×¢Òâ: µ±ÏóÔËÐеÄÃüÁî»òº¯ÊýÓëÄÚ½¨ÃüÁîͬÃûʱ,ÓÉÓÚÄÚ½¨ÃüÁî±ÈÍⲿÃüÁîµÄÓÅÏȼ¶¸ß,¶ø
º¯Êý±ÈÄÚ½¨ÃüÁîÓÅÏȼ¶¸ß,ËùÒÔbash½«×Ü»áÖ´ÐÐÓÅÏȼ¶±È½Ï¸ßµÄÃüÁî.ÕâÑùÄã¾ÍûÓÐÑ¡
ÔñµÄÓàµØÁË.ËùÒÔBashÌṩÁË3¸öÃüÁîÀ´ÈÃÄãÓÐÑ¡ÔñµÄ»ú»á.commandÃüÁî¾ÍÊÇÕâ3¸öÃü
ÁîÖ®Ò».
ÁíÍâÁ½¸öÊÇbuiltinºÍenable.
builtin
ÔÚ"builtin"ºó±ßµÄÃüÁֻµ÷ÓÃÄÚ½¨ÃüÁî.ÔÝʱµÄ½ûÓÃͬÃûµÄº¯Êý»òÕßÊÇͬÃûµÄÀ©Õ¹ÃüÁî.
enable
Õâ¸öÃüÁî»òÕß½ûÓÃÄÚ½¨ÃüÁî»òÕ߻ָ´ÄÚ½¨ÃüÁî.Èç: enable -n kill½«½ûÓÃkillÄÚ½¨ÃüÁî,
ËùÒÔµ±ÎÒÃǵ÷ÓÃkillʱ,ʹÓõĽ«ÊÇ/bin/killÍⲿÃüÁî.
-aÑ¡Ï»á»Ö¸´ÏàÓ¦µÄÄÚ½¨ÃüÁî,Èç¹û²»´ø²ÎÊýµÄ»°,½«»á»Ö¸´ËùÓеÄÄÚ½¨ÃüÁî.
Ñ¡Ïî-f filename½«»á´ÓÊʵ±µÄ±àÒë¹ýµÄÄ¿±êÎļþ[6]ÖÐÒÔ¹²Ïí¿â(DLL)µÄÐÎʽÀ´¼ÓÔØÒ»¸ö
ÄÚ½¨ÃüÁî.
autoload
ÕâÊÇ´ÓkshµÄautoloaderÃüÁîÒÆÖ²¹ýÀ´µÄ.Ò»¸ö´øÓÐ"autoload"ÉùÃ÷µÄº¯Êý,ÔÚËüµÚÒ»´Î±»
µ÷ÓõÄʱºò²Å»á±»¼ÓÔØ.[7] ÕâÑù×ö»á½Úʡϵͳ×ÊÔ´.
×¢Òâ: autoloadÃüÁî²¢²»ÊÇBash°²×°Ê±ºòµÄºËÐÄÃüÁîµÄÒ»²¿·Ö.Õâ¸öÃüÁîÐèҪʹÓÃÃüÁî
enable -f(¼ûÉϱßenableÃüÁî)À´¼ÓÔØ.

Table 11-1 ×÷Òµ±êʶ·û


==========================================================
¼Ç·¨ | º¬Òå
==========================================================
%N | ×÷ÒµºÅ[N]
==========================================================
%S | ÒÔ×Ö·û´®S¿ªÍ·µÄ±»(ÃüÁîÐÐ)µ÷ÓõÄ×÷Òµ
==========================================================
%?S | °üº¬×Ö·û´®SµÄ±»(ÃüÁîÐÐ)µ÷ÓõÄ×÷Òµ
==========================================================
%% | µ±Ç°×÷Òµ(ǰ̨×îºó½áÊøµÄ×÷Òµ,»òºǫ́×îºóÆô¶¯µÄ×÷Òµ)
==========================================================
%+ | µ±Ç°×÷Òµ(ǰ̨×îºó½áÊøµÄ×÷Òµ,»òºǫ́×îºóÆô¶¯µÄ×÷Òµ)
==========================================================
%- | ×îºóµÄ×÷Òµ
==========================================================
$! | ×îºóµÄºǫ́½ø³Ì
==========================================================

×¢ÒâÊÂÏî:
[1] Ò»¸öÀýÍâ¾ÍÊÇtimeÃüÁî,Bash¹Ù·½ÎĵµËµÕâ¸öÃüÁîÊÇÒ»¸ö¹Ø¼ü×Ö.
[2] Ò»¸öÑ¡Ïî¾ÍÊÇÒ»¸öÐÐΪÉϱȽÏÏó±ê־λµÄ²ÎÊý,¿ÉÒÔÓÃÀ´´ò¿ª»ò¹Ø±Õ½Å±¾µÄijЩÐÐΪ.
¶øºÍij¸öÌض¨Ñ¡ÏîÏà¹ØµÄ²ÎÊý¾ÍÊÇÓÃÀ´¿ØÖÆÕâ¸öÑ¡ÏÄÜÊÇ¿ªÆô»¹ÊǹرյÄ.
[3] ³ý·Çexec±»ÓÃÀ´ÖØзÖÅäÎļþÃèÊö·û.
[4] hashÊÇÒ»ÖÖ´¦Àí´æ´¢ÔÚ±íÖÐÊý¾ÝµÄ·½·¨,ÕâÖÖ·½·¨¾ÍÊÇΪ±íÖеÄÊý¾Ý½¨Á¢²éÕÒ¼ü.
¶øÊý¾ÝÏî±¾ÉíÊDz»¹æÔòµÄ,ÕâÑù¾Í¿ÉÒÔͨ¹ýÒ»¸ö¼òµ¥µÄÊýѧËã·¨À´²úÉúÒ»¸öÊý×Ö,
Õâ¸öÊý×Ö±»ÓÃÀ´×÷Ϊ²éÕÒ¼ü.
ʹÓÃhashµÄÒ»¸ö×îÓÐÀûµÄµØ·½¾ÍÊÇÌá¸ßÁËËÙ¶È.¶øȱµã¾ÍÊÇ»á²úÉú"³åײ" -- Ò²¾ÍÊÇ
˵,¿ÉÄÜ»áÓжà¸öÊý¾ÝÔªËØʹÓÃͬһ¸öÖ÷¼ü.
¹ØÓÚhashµÄÀý×Ó¼û Example A-21 ºÍ Example A-22.
[5] ÔÚÒ»¸ö½»»¥µÄshellÖÐ,readline¿â¾ÍÊÇBashÓÃÀ´¶ÁÈ¡ÊäÈëµÄ.
(ÒëÕß: ±ÈÈçĬÈϵÄEmacs·ç¸ñµÄÊäÈë,µ±È»Ò²¿ÉÒÔ¸ÄΪvi·ç¸ñµÄÊäÈë)
[6] һЩ¿É¼ÓÔصÄÄÚ½¨ÃüÁîµÄCÔ´´úÂ붼·ÅÔÚ/usr/share/doc/bash-?.??/functionsÏÂ.
×¢Òâ: enableÃüÁîµÄ-fÑ¡Ïî²¢²»ÊǶÔËùÓÐϵͳ¶¼Ö§³ÖµÄ(¿´ÒÆûÒÆÖ²ÉÏ).
[7] typeset -fu¿ÉÒÔ´ïµ½ºÍautoloadÃüÁîÏàͬµÄ×÷ÓÃ.

µÚ12Õ Íⲿ¹ýÂËÆ÷,³ÌÐòºÍÃüÁî
=============================
±ê×¼µÄ UNIX ÃüÁîʹµÃ shell ½Å±¾¸ü¼ÓÁé»î.ͨ¹ý¼òµ¥µÄ±à³Ì½á¹¹°ÑshellÖ¸ÁîºÍϵͳÃüÁî½á
ºÏÆðÀ´,Õâ²ÅÊǽű¾ÄÜÁ¦µÄËùÔÚ.
12.1 »ù±¾ÃüÁî
-------------
ÐÂÊÖ±ØÐëÒªÕÆÎյijõ¼¶ÃüÁî
ls
»ù±¾µÄÁгöËùÓÐÎļþµÄÃüÁî.µ«ÊÇÍùÍù¾ÍÊÇÒòΪÕâ¸öÃüÁîÌ«¼òµ¥,ËùÒÔÎÒÃÇ×ÜÊǵ͹ÀËü.±ÈÈç
,Óà -R Ñ¡Ïî,ÕâÊǵݹéÑ¡Ïî,ls ½«»áÒÔĿ¼Ê÷µÄÐÎʽÁгöËùÓÐÎļþ, ÁíÒ»¸öºÜÓÐÓõÄÑ¡Ïî
ÊÇ -S ,½«»á°´ÕÕÎļþ³ß´çÁгöËùÓÐÎļþ, -t, ½«»á°´ÕÕÐÞ¸Äʱ¼äÀ´ÁгöÎļþ,-i Ñ¡Ïî»áÏÔ
ʾÎļþµÄinode(¼û Example 12-4).
Example 12-1 ʹÓÃlsÃüÁîÀ´´´½¨Ò»¸öÉÕ¼CDRµÄÄÚÈÝÁбí
################################Start Script####################################
###
1 #!/bin/bash
2 # ex40.sh (burn-cd.sh)
3 # ×Ô¶¯¿Ì¼CDRµÄ½Å±¾.
4
5
6 SPEED=2 # Èç¹ûÄãµÄÓ²¼þÖ§³ÖµÄ»°,Äã¿ÉÒÔÑ¡Óøü¸ßµÄËÙ¶È.
7 IMAGEFILE=cdimage.iso
8 CONTENTSFILE=contents
9 DEVICE=cdrom
10 # DEVICE="0,0" ΪÁËʹÓÃÀÏ°æ±¾µÄCDR
11 DEFAULTDIR=/opt # ÕâÊÇ°üº¬ÐèÒª±»¿Ì¼ÄÚÈݵÄĿ¼.
12 # ±ØÐ뱣֤Ŀ¼´æÔÚ.
13 # СÁ·Ï°: ²âÊÔÒ»ÏÂĿ¼ÊÇ·ñ´æÔÚ.
14
15 # Uses Joerg Schilling's "cdrecord" package:
15 # ʹÓà Joerg Schilling µÄ "cdrecord"°ü:
16 # http://www.fokus.fhg.de/usr/schilling/cdrecord.html
17
18 # Èç¹ûÒ»°ãÓû§µ÷ÓÃÕâ¸ö½Å±¾µÄ»°,¿ÉÄÜÐèÒªrootÉí·Ý
19 #+ chmod u+s /usr/bin/cdrecord
20 # µ±È», Õâ»á²úÉú°²È«Â©¶´, ËäÈ»ÕâÊÇÒ»¸ö±È½ÏСµÄ°²È«Â©¶´.
21
22 if [ -z "$1" ]
23 then
24 IMAGE_DIRECTORY=$DEFAULTDIR
25 # Èç¹ûÃüÁîÐÐûָ¶¨µÄ»°, ÄÇôÕâ¸ö¾ÍÊÇĬÈÏĿ¼.
26 else
27 IMAGE_DIRECTORY=$1
28 fi
29
30 # ´´½¨Ò»¸öÄÚÈÝÁбíÎļþ.
31 ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE
32 # "l" Ñ¡Ï¸ø³öÒ»¸ö"³¤"ÎļþÁбí.
33 # "R" Ñ¡ÏʹÕâ¸öÁбíµÝ¹é.
34 # "F" Ñ¡Ï±ê¼Ç³öÎļþÀàÐÍ (±ÈÈç: Ŀ¼ÊÇÒÔ /½áβ, ¶ø¿ÉÖ´ÐÐÎļþÒÔ *½áβ).
35 echo "Creating table of contents."
36
37 # ÔÚÉÕ¼µ½CDR֮ǰ´´½¨Ò»¸ö¾µÏñÎļþ.
38 mkisofs -r -o $IMAGEFILE $IMAGE_DIRECTORY
39 echo "Creating ISO9660 file system image ($IMAGEFILE)."
40
41 # ÉÕ¼CDR.
42 echo "Burning the disk."
43 echo "Please be patient, this will take a while."
44 cdrecord -v -isosize speed=$SPEED dev=$DEVICE $IMAGEFILE
45
46 exit $?
################################End Script######################################
###
cat, tac
cat, Êǵ¥´Ê concatenateµÄËõд, °ÑÎļþµÄÄÚÈÝÊä³öµ½stdout. µ±ÓëÖض¨Ïò²Ù×÷·û (> »ò
>>)½áºÏʹÓÃʱ, Ò»°ã¶¼ÊÇÓÃÀ´½«¶à¸öÎļþÁ¬½ÓÆðÀ´.
1 # Uses of 'cat'
2 cat filename # ´òÓ¡³öÎļþÄÚÈÝ.
3
4 cat file.1 file.2 file.3 > file.123 # °Ñ3¸öÎļþÁ¬½Óµ½Ò»¸öÎļþÖÐ.
cat ÃüÁîµÄ -n Ñ¡ÏîÊÇΪÁËÔÚÄ¿±êÎļþÖеÄËùÓÐÐÐÇ°±ß²åÈëÐкÅ. -b Ñ¡Ïî Óë -n Ñ¡ÏîÒ»
Ñù, Çø±ðÊDz»¶Ô¿ÕÐнøÐбàºÅ. -v Ñ¡Ïî¿ÉÒÔʹÓà ^ ±ê¼Ç·¨ À´echo ³ö²»¿É´òÓ¡×Ö·û.-sÑ¡
Ïî¿ÉÒ԰Ѷà¸ö¿ÕÐÐѹËõ³ÉÒ»¸ö¿ÕÐÐ.
¼û Example 12-25 ºÍ Example 12-21.
×¢Òâ: ÔÚÒ»¸ö ¹ÜµÀ ÖÐ, ¿ÉÄÜÓÐÒ»ÖÖ°Ñstdin Öض¨Ïò µ½Ò»¸öÎļþÖеĸüÓÐЧµÄ°ì·¨, Õâ
ÖÖ·½·¨±È catÎļþµÄ·½·¨¸üÓÐЧÂÊ.
1 cat filename | tr a-z A-Z
2
3 tr a-z A-Z < filename # Ч¹ûÏàͬ,µ«ÊÇ´¦Àí¸üÉÙ,
4 #+ ²¢ÇÒÁ¬¹ÜµÀ¶¼Ê¡µôÁË.
tac ÃüÁî, ¾ÍÊÇ catµÄ·´×ª, ½«´ÓÎļþµÄ½áβÁгöÎļþ.
rev
°ÑÿһÐÐÖеÄÄÚÈÝ·´×ª, ²¢ÇÒÊä³öµ½ stdoutÉÏ. Õâ¸öÃüÁîÓë tacÃüÁîµÄЧ¹ûÊDz»Í¬µÄ, Òò
ΪËü²¢²»·´×ªÐÐÐò, ¶øÊÇ°ÑÿÐеÄÄÚÈÝ·´×ª.
bash$ cat file1.txt
This is line 1.
This is line 2.

bash$ tac file1.txt


This is line 2.
This is line 1.

bash$ rev file1.txt


.1 enil si sihT
.2 enil si sihT
cp
ÕâÊÇÎļþ¿½±´ÃüÁî. cp file1 file2 °Ñ file1 ¿½±´µ½ file2, Èç¹û´æÔÚ file2 µÄ»°,ÄÇ
file2 ½«±»¸²¸Ç (¼û Example 12-6).
×¢Òâ: ÌرðÓÐÓõÄÑ¡Ïî¾ÍÊÇ -a ¹éµµ Ñ¡Ïî (ΪÁËcopyÒ»¸öÍêÕûµÄĿ¼Ê÷), -u ÊǸüÐÂÑ¡
Ïî, ºÍ -r Óë -R µÝ¹éÑ¡Ïî.
1 cp -u source_dir/* dest_dir
2 # "Synchronize" dest_dir to source_dir°ÑԴĿ¼"ͬ²½"µ½Ä¿±êĿ¼ÉÏ,
3 #+ Ò²¾ÍÊÇ¿½±´ËùÓиüеÄÎļþºÍ֮ǰ²»´æÔÚµÄÎļþ.
mv
ÕâÊÇÎļþÒƶ¯ÃüÁî. ËüµÈ¼ÛÓÚ cp Óë rm ÃüÁîµÄ×éºÏ. Ëü¿ÉÒ԰Ѷà¸öÎļþÒƶ¯µ½Ä¿Â¼ÖÐ,Éõ
ÖÁ½«Ä¿Â¼ÖØÃüÃû. Ïë²é¿´ mv Ôڽű¾ÖÐʹÓõÄÀý×Ó, ¼û Example 9-18 ºÍ Example A-2.
×¢Òâ: µ±Ê¹Ó÷ǽ»»¥½Å±¾Ê±,¿ÉÒÔʹÓà mv µÄ-f (Ç¿ÖÆ) Ñ¡ÏîÀ´±ÜÃâÓû§µÄÊäÈë.
µ±Ò»¸öĿ¼±»Òƶ¯µ½Ò»¸öÒÑ´æÔÚµÄĿ¼ʱ,ÄÇôËü½«³ÉΪĿ±êĿ¼µÄ×ÓĿ¼.
bash$ mv source_directory target_directory
bash$ ls -lF target_directory
total 1
drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/
rm
ɾ³ý(Çå³ý)Ò»¸ö»ò¶à¸öÎļþ. -f Ñ¡ÏǿÖÆɾ³ýÎļþ,¼´Ê¹Õâ¸öÎļþÊÇÖ»¶ÁµÄ.²¢ÇÒ¿ÉÒÔ
ÓÃÀ´±ÜÃâÓû§ÊäÈë(Ôڷǽ»»¥½Å±¾ÖÐʹÓÃ).
×¢Òâ: rm ½«ÎÞ·¨É¾³ýÒÔÆÆÕۺſªÍ·µÄÎļþ.
bash$ rm -badname
rm: invalid option -- b
Try `rm --help' for more information.
½â¾öÕâ¸öÎÊÌâµÄÒ»¸ö·½·¨¾ÍÊÇÔÚҪɾ³ýµÄÎļþµÄÇ°±ß¼ÓÉÏ"./".
bash$ rm ./-badname
ÁíÒ»ÖÖ½â¾öµÄ·½·¨ÊÇ ÔÚÎļþÃûÇ°±ß¼ÓÉÏ " -- ".
bash$ rm -- -badname
×¢Òâ: µ±Ê¹Óõݹé²ÎÊý -rʱ, rm ÃüÁ»áɾ³ýÕû¸öĿ¼Ê÷. Èç¹û²»É÷ʹÓà rm -rf *ÄÇÕû
¸öĿ¼Ê÷¾ÍÕæµÄÍêÁË.
rmdir
ɾ³ýĿ¼. µ«ÊÇÖ»ÓÐÕâ¸öĿ¼ÖÐûÓÐÎļþ -- µ±È»»á°üº¬²»¿É¼ûµÄ µãÎļþ [1] -- µÄ
ʱºòÕâ¸öÃüÁî²Å»á³É¹¦.
mkdir
Éú³ÉĿ¼, ´´½¨Ò»¸ö¿ÕĿ¼. ±ÈÈç, mkdir -p project/programs/December ½«»á´´½¨³ö
Õâ¸öÖ¸¶¨µÄĿ¼, ¼´Ê¹projectĿ¼ºÍprogramsĿ¼¶¼²»´æÔÚ. -p Ñ¡Ï»á×Ô¶¯²úÉú±ØÒª
µÄ¸¸Ä¿Â¼, ÕâÑùÒ²¾Íͬʱ´´½¨Á˶à¸öĿ¼.
chmod
ÐÞ¸ÄÒ»¸öÏÖ´æÎļþµÄÊôÐÔ (¼û Example 11-12).
1 chmod +x filename
2 # ʹµÃÎļþfilename¶ÔËùÓÐÓû§¶¼¿ÉÖ´ÐÐ.
3
4 chmod u+s filename
5 # ÉèÖÃ"filename"ÎļþµÄ"suid"λ.
6 # ÕâÑùÒ»°ãÓû§¾Í¿ÉÒÔÖ´ÐÐ"filename", Ëû½«ÓµÓкÍÎļþËÞÖ÷ÏàͬµÄȨÏÞ.
7 # (Õâ²¢²»ÊÊÓÃÓÚshell ½Å±¾)
1 chmod 644 filename
2 # Makes "filename" readable/writable to owner, readable to
3 # ÉèÖÃÎļþËÞÖ÷µÄ r/w ȨÏÞ,²¢¶ÔÒ»°ãÓû§
3 # ÉèÖöÁȨÏÞ.
4 # (8½øÖÆģʽ).
1 chmod 1777 directory-name
2 # ¶ÔÕâ¸öĿ¼ÉèÖÃr/w ºÍ¿ÉÖ´ÐÐȨÏÞ, ²¢¿ª·Å¸øËùÓÐÈË.
3 # ͬʱÉèÖà "Õ³Ìùλ".
4 # ÕâÒâζ×Å,¡¡Ö»ÓÐĿ¼ËÞÖ÷,
5 # ÎļþËÞÖ÷, µ±È», »¹ÓÐroot
6 # ¿ÉÒÔɾ³ýÕâ¸öĿ¼ÖеÄÈκÎÌض¨µÄÎļþ.
chattr
ÐÞ¸ÄÎļþÊôÐÔ. Õâ¸öÃüÁîÓëÉÏ±ßµÄ chmod ÃüÁîÏàÀàËÆ, µ«ÊÇÓв»Í¬µÄÑ¡ÏîºÍ²»Í¬µÄµ÷ÓÃÓï
·¨, ²¢ÇÒÕâ¸öÃüÁîÖ»Äܹ¤×÷ÔÚext2ÎļþϵͳÖÐ.
chattr ÃüÁîµÄÒ»¸öÌرðÓÐȤµÄÑ¡ÏîÊÇi. chattr +i filename ½«Ê¹µÃÕâ¸öÎļþ±»±ê¼ÇΪ
ÓÀÔ¶²»±ä. Õâ¸öÎļþ½«²»Äܱ»ÐÞ¸Ä, Á¬½Ó, »òɾ³ý, ¼´Ê¹ÊÇrootÒ²²»ÐÐ. Õâ¸öÎļþÊôÐÔÖ»
Äܱ»rootÉèÖúÍɾ³ý. ÀàËƵÄ, a Ñ¡Ï»á°ÑÎļþ±ê¼ÇΪֻÄÜ×·¼ÓÊý¾Ý.
root# chattr +i file1.txt

root# rm file1.txt
rm: remove write-protected regular file `file1.txt'? y
rm: cannot remove `file1.txt': Operation not permitted
Èç¹ûÎļþÉèÖÃÁËs(°²È«)ÊôÐÔ, ÄÇôµ±Õâ¸öÎļþ±»É¾³ýʱ,Õâ¸öÎļþËùÔÚ´ÅÅ̵Ŀ齫ȫ²¿±»0
Ìî³ä.
Èç¹ûÎļþÉèÖÃÁËu(²»¿Éɾ³ý)ÊôÐÔ, ÄÇôµ±Õâ¸öÎļþ±»É¾³ýºó, Õâ¸öÎļþµÄÄÚÈÝ»¹¿ÉÒÔ±»»Ö
¸´(²»¿Éɾ³ý).
Èç¹ûÎļþÉèÖÃÁËc(ѹËõ)ÊôÐÔ, ÄÇôµ±Õâ¸öÎļþÔÚ½øÐÐд²Ù×÷ʱ,Ëü½«×Ô¶¯±»Ñ¹Ëõ,²¢ÇÒÔÚ
¶ÁµÄʱºò, ×Ô¶¯½âѹ.
×¢Òâ: ʹÓÃÃüÁîchattr doÉèÖõÄÊôÐÔ, ½«²»»áÏÔʾÔÚÎļþÁбíÖÐ(ls -l).
ln
´´½¨ÎļþÁ´½Ó, Ç°ÌáÊÇÕâ¸öÎļþÊÇ´æÔÚµÄ. "Á´½Ó" ¾ÍÊÇÒ»¸öÎļþµÄÒýÓÃ, Ò²¾ÍÊÇÕâ¸öÎÄ
¼þµÄÁíÒ»¸öÃû×Ö. ln ÃüÁîÔÊÐí¶Ôͬһ¸öÎļþÒýÓöà¸öÁ´½Ó,²¢ÇÒÊDZÜÃâ»ìÏýµÄÒ»¸öºÜºÃµÄ
·½·¨ (¼û Example 4-6).
ln ¶ÔÓÚÎļþÀ´ËµÖ»²»¹ýÊÇ´´½¨ÁËÒ»¸öÒýÓÃ, Ò»¸öÖ¸Õë¶øÒÑ, ÒòΪ´´½¨³öÀ´µÄÁ¬½ÓÎļþÖ»ÓÐ
¼¸¸ö×Ö½Ú.
¾ø´ó¶àÊýʹÓÃln ÃüÁîʱʹÓÃÊÇ -s Ñ¡Ïî, ¿ÉÒÔ³ÆΪ·ûºÅÁ´½Ó, »òÈíÁ´½Ó.ʹÓà -s Ñ¡ÏîµÄ
Ò»¸öÓŵãÊÇËü¿ÉÒÔ´©Ô½ÎļþϵͳÀ´Á´½ÓĿ¼.
¹ØÓÚʹÓÃÕâ¸öÃüÁîµÄÓï·¨»¹ÊÇÓеãС¼¼ÇɵÄ. ±ÈÈç: ln -s oldfile newfile ½«¶ÔÀÏÎļþ
²úÉúÒ»¸öеÄÎļþÁ´½Ó.
×¢Òâ: Èç¹û֮ǰ¾Í´æÔÚnewfileµÄ»°, ÄÇô½«»á²úÉúÒ»¸ö´íÎóÏûÏ¢.
ʹÓÃÁ´½ÓÖеÄÄÄÖÖÀàÐÍ?
¾ÍÏñ John Macdonald ½âÊ͵ÄÄÇÑù:
²»ÂÛÊÇÄÇÖÖÀàÐ͵ÄÁ´½Ó, ¶¼ÌṩÁËÒ»ÖÖË«ÏòÒýÓõÄÊֶΠ-- Ò²¾ÍÊÇ˵, ²»¹ÜÄãÓÃÎļþ
µÄÄǸöÃû×Ö¶ÔÎļþÄÚÈݽøÐÐÐÞ¸Ä, ÄãÐ޸ĵÄЧ¹û¶¼¼´»á·´Ó³µ½Ô­Ê¼Ãû×ÖµÄÎļþ, Ò²»á
·´Ó³µ½Á´½ÓÃû×ÖµÄÎļþ.µ±Ä㹤×÷ÔÚ¸ü¸ß²ã´ÎµÄʱºò, ²Å»á·¢ÉúÈíÓ²Á´½ÓµÄ²»Í¬. Ó²Á´
½ÓµÄÓŵãÊÇ, ԭʼÎļþÓëÁ´½ÓÎļþÖ®¼äÊÇÏ໥¶ÀÁ¢µÄ -- Èç¹ûÄãɾ³ý»òÕßÖØÃüÃûÀÏÎÄ
¼þ, ÄÇôÕâÖÖ²Ù×÷½«²»»áÓ°ÏìÓ²Á´½ÓµÄÎļþ, Ó²Á´½ÓµÄÎļþ½²»¹ÊÇÔ­À´ÎļþµÄÄÚÈÝ.
È»¶øÈç¹ûÄãʹÓÃÈíÁ´½ÓµÄ, µ±Äã°ÑÀÏÎļþɾ³ý»òÖØÃüÃûºó, ÈíÁ´½Ó½«ÔÙÒ²ÕÒ²»µ½Ô­À´
ÎļþµÄÄÚÈÝÁË. ¶øÈíÁ´½ÓµÄÓŵãÊÇËü¿ÉÒÔ¿çÔ½Îļþϵͳ(ÒòΪËüÖ»²»¹ýÊÇÎļþÃûµÄÒ»¸ö
ÒýÓÃ, ¶ø²¢²»ÊÇÕæÕýµÄÊý¾Ý). ÓëÓ²Á´½ÓµÄÁíÒ»¸ö²»Í¬ÊÇ, Ò»¸ö·ûºÅÁ´½Ó¿ÉÒÔÖ¸ÏòÒ»¸ö
Ŀ¼.
Á´½Ó¸ø³öÁËÒ»ÖÖ¿ÉÒÔÓöà¸öÃû×ÖÀ´µ÷Óýű¾µÄÄÜÁ¦(µ±È»ÕâÒ²ÊÊÓÃÓÚÈκοÉÖ´ÐеÄÀàÐÍ),
²¢Çҽű¾µÄÐÐΪ½«ÒÀÀµÓڽű¾ÊÇÈçºÎ±»µ÷ÓõÄ.
Example 12-2 Hello or Good-bye
################################Start Script####################################
###
1 #!/bin/bash
2 # hello.sh: ÏÔʾ"hello" »¹ÊÇ "goodbye"
3 #+ ÒÀÀµÓڽű¾ÊÇÈçºÎ±»µ÷ÓõÄ.
4
5 # ÔÚµ±Ç°Ä¿Â¼ÏÂ($PWD)ΪÕâ¸ö½Å±¾´´½¨Ò»¸öÁ´½Ó:
6 # ln -s hello.sh goodbye
7 # ÏÖÔÚ, ͨ¹ýÈçÏÂÁ½ÖÖ·½·¨À´µ÷ÓÃÕâ¸ö½Å±¾:
8 # ./hello.sh
9 # ./goodbye
10
11
12 HELLO_CALL=65
13 GOODBYE_CALL=66
14
15 if [ $0 = "./goodbye" ]
16 then
17 echo "Good-bye!"
18 # µ±È», ÔÚÕâÀïÄãÒ²¿ÉÒÔÌí¼ÓһЩÆäËûµÄ goodbyeÀàÐ͵ÄÃüÁî.Some other goodbye-type commands,
19 exit $GOODBYE_CALL
20 fi
21
22 echo "Hello!"
23 # µ±È», ÔÚÕâÀïÄãÒ²¿ÉÒÔÌí¼ÓһЩÆäËûµÄ helloÀàÐ͵ÄÃüÁî.
24 exit $HELLO_CALL
################################End Script######################################
###
man, info
These ÕâÁ½¸öÃüÁîÓÃÀ´²é¿´ÏµÍ³ÃüÁî»ò°²×°¹¤¾ßµÄÊÖ²áºÍÐÅÏ¢.µ±Á½Õ߶¼¿ÉÓÃʱ, info Ò³
Ò»°ã±È manÒ²»á°üº¬¸ü¶àµÄϸ½ÚÃèÊö.
×¢ÒâÊÂÏî:
[1] Dotfiles ¾ÍÊÇÎļþÃûÒÔ"."¿ªÍ·µÄÎļþ, ±ÈÈç ~/.Xdefaults. ÕâÑùµÄÎļþÔÚÒ»°ãµÄ l
s ÃüÁîʹÓÃÖн«²»»á±»ÏÔʾ³öÀ´ (µ±È» ls -a ½«»áÏÔʾËüÃÇ), ²¢ÇÒËüÃÇÒ²²»»á±»Ò»
¸öÒâÍâµÄ rm -rf *ɾ³ý. ÔÚÓû§µÄhomeĿ¼ÖÐ,Dotfiles Ò»°ã±»ÓÃÀ´µ±×÷°²×°ºÍÅäÖÃ
Îļþ.

12.2 ¸´ÔÓÃüÁî
-------------
¸ü¸ß¼¶µÄÓû§ÃüÁî
find
-exec COMMAND \;
ÔÚÿһ¸öfind Æ¥Åäµ½µÄÎļþÖ´ÐÐ COMMAND ÃüÁî. ÃüÁîÐòÁÐÒÔ ; ½áÊø( ";" ÊÇ ×ªÒå·û ÒÔ
±£Ö¤ shell ´«µÝµ½findÃüÁîÖеÄ×Ö·û²»»á±»½âÊÍΪÆäËûµÄÌØÊâ×Ö·û).
bash$ find ~/ -name '*.txt'
/home/bozo/.kde/share/apps/karm/karmdata.txt
/home/bozo/misc/irmeyc.txt
/home/bozo/test-scripts/1.txt
Èç¹û COMMAND Öаüº¬ {}, ÄÇô find ÃüÁ»áÓÃËùÓÐÆ¥ÅäÎļþµÄ·¾¶ÃûÀ´Ìæ»» "{}" .
1 find ~/ -name 'core*' -exec rm {} \;
2 # ´ÓÓû§µÄ home Ŀ¼ÖÐɾ³ýËùÓÐµÄ core dumpÎļþ.
1 find /home/bozo/projects -mtime 1
2 # Áгö×îºóÒ»Ìì±»Ð޸ĵÄ
3 #+ ÔÚ/home/bozo/projectsĿ¼Ê÷ϵÄËùÓÐÎļþ.
4 #
5 # mtime = last modification time of the target file
6 # ctime = last status change time (via 'chmod' or otherwise)
7 # atime = last access time
8
9 DIR=/home/bozo/junk_files
10 find "$DIR" -type f -atime +5 -exec rm {} \;
11 # ^^
12 # ´óÀ¨ºÅ¾ÍÊÇ"find"ÃüÁîÓÃÀ´Ì滻Ŀ¼µÄµØ·½.
13 #
14 # ɾ³ýÖÁÉÙ5ÌìÄÚû±»´æÈ¡¹ýµÄ
15 #+ "/home/bozo/junk_files" ÖеÄËùÓÐÎļþ.
16 #
17 # "-type filetype", where
18 # f = regular file
19 # d = directory, etc.
20 # ('find' ÃüÁîµÄ manÒ³ÓÐÍêÕûµÄÑ¡ÏîÁбí.)
1 find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9]
[0-9]*' {} \;
2
3 # ÔÚ/etc Ŀ¼ÖеÄÎļþÕÒµ½ËùËùÓаüº¬ IP µØÖ·(xxx.xxx.xxx.xxx) µÄÎļþ.
4 # ¿ÉÄÜ»á²éÕÒµ½Ò»Ð©¶àÓàµÄÆ¥Åä. ÎÒÃÇÈçºÎÈ¥µôËüÃÇÄØ?
5
6 # »òÐí¿ÉÒÔʹÓÃÈçÏ·½·¨:
7
8 find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \
9 | grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'
10 #
11 # [:digit:] ÊÇÒ»ÖÖ×Ö·ûÀà.is one of the character classes
12 #+ ¹ØÓÚ×Ö·ûÀàµÄ½éÉܼû POSIX 1003.2 ±ê×¼»¯Îĵµ.
13
14 # Thanks, St¨¦phane Chazelas.
×¢Òâ: find ÃüÁîµÄ -exec Ñ¡Ïî²»Ó¦¸ÃÓëshellÖеÄÄÚ½¨ÃüÁî exec Ïà»ìÏý.
Example 12-3 ɾ³ýµ±Ç°Ä¿Â¼ÏÂÎļþÃûÖаüº¬Ò»Ð©ÌØÊâ×Ö·û(°üÀ¨¿Õ°×)µÄÎļþ..
################################Start Script####################################
###
1 #!/bin/bash
2 # badname.sh
3 # ɾ³ýµ±Ç°Ä¿Â¼ÏÂÎļþÃûÖаüº¬Ò»Ð©ÌØÊâ×Ö·ûµÄÎļþ.
4
5 for filename in *
6 do
7 badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p`
8 # badname=`echo "$filename" | sed -n '/[+{;"\=?~()<>&*|$]/p'` Õâ¾äÒ²ÐÐ.
9 # ɾ³ýÎļþÃû°üº¬ÕâЩ×Ö·ûµÄÎļþ: + { ; " \ = ? ~ ( ) < > & * | $
10 #
11 rm $badname 2>/dev/null
12 # ^^^^^^^^^^^ ´íÎóÏûÏ¢½«±»Å×Æú.
13 done
14
15 # ÏÖÔÚ, ´¦ÀíÎļþÃûÖÐÒÔÈκη½Ê½°üº¬¿Õ°×µÄÎļþ.
16 find . -name "* *" -exec rm -f {} \;
17 # "find"ÃüÁîÆ¥Åäµ½µÄĿ¼Ãû½«Ìæ»»µ½{}µÄλÖÃ.
18 # '\' ÊÇΪÁ˱£Ö¤ ';'±»ÕýÈ·µÄתÒå, ²¢Çҷŵ½ÃüÁîµÄ½áβ.
19
20 exit 0
21
22 #---------------------------------------------------------------------
23 # ÕâÐÐϱߵÄÃüÁ²»»áÔËÐÐ, ÒòΪ "exit" ÃüÁî.
24
25 # Õâ¾äÊÇÉϱ߽ű¾µÄÒ»¸ö¿ÉÑ¡·½·¨:
26 find . -name '*[+{;"\\=?~()<>&*|$ ]*' -exec rm -f '{}' \;
27 # (Thanks, S.C.)
################################End Script######################################
###
Example 12-4 ͨ¹ýÎļþµÄ inode ºÅÀ´É¾³ýÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # idelete.sh: ͨ¹ýÎļþµÄinodeºÅÀ´É¾³ýÎļþ.
3
4 # µ±ÎļþÃûÒÔÒ»¸ö·Ç·¨×Ö·û¿ªÍ·µÄʱºò, Õâ¾Í·Ç³£ÓÐÓÃÁË,
5 #+ ±ÈÈç ? »ò -.
6
7 ARGCOUNT=1 # ÎļþÃû²ÎÊý±ØÐë±»´«µÝµ½½Å±¾ÖÐ.
8 E_WRONGARGS=70
9 E_FILE_NOT_EXIST=71
10 E_CHANGED_MIND=72
11
12 if [ $# -ne "$ARGCOUNT" ]
13 then
14 echo "Usage: `basename $0` filename"
15 exit $E_WRONGARGS
16 fi
17
18 if [ ! -e "$1" ]
19 then
20 echo "File \""$1"\" does not exist."
21 exit $E_FILE_NOT_EXIST
22 fi
23
24 inum=`ls -i | grep "$1" | awk '{print $1}'`
25 # inum = inode (Ë÷Òý½Úµã) ºÅ.
26 # --------------------------------------------------------
27 # ÿ¸öÎļþ¶¼ÓÐÒ»¸öinodeºÅ, Õâ¸öºÅÓÃÀ´¼Ç¼ÎļþÎïÀíµØÖ·ÐÅÏ¢.
28 # --------------------------------------------------------
29
30 echo; echo -n "Are you absolutely sure you want to delete \"$1\" (y/n)? "
31 # 'rm' ÃüÁîµÄ '-v' Ñ¡ÏîÒ²»áÎÊÕâ¾ä»°.
32 read answer
33 case "$answer" in
34 [nN]) echo "Changed your mind, huh?"
35 exit $E_CHANGED_MIND
36 ;;
37 *) echo "Deleting file \"$1\".";;
38 esac
39
40 find . -inum $inum -exec rm {} \;
41 # ^^
42 # ´óÀ¨ºÅ¾ÍÊÇ"find"ÃüÁî
43 #+ ÓÃÀ´Ìæ»»Îı¾Êä³öµÄµØ·½.
44 echo "File "\"$1"\" deleted!"
45
46 exit 0
################################End Script######################################
###
¼û Example 12-27, Example 3-4, ºÍ Example 10-9 ÕâЩÀý×ÓչʾÁËʹÓà find ÃüÁî. ¶Ô
ÓÚÕâ¸ö¸´ÔÓ¶øÓÐÇ¿´óµÄÃüÁîÀ´Ëµ, ²é¿´manÒ³¿ÉÒÔ»ñµÃ¸ü¶àµÄϸ½Ú.
xargs
ÕâÊǸøÃüÁî´«µÝ²ÎÊýµÄÒ»¸ö¹ýÂËÆ÷, Ò²ÊÇ×éºÏ¶à¸öÃüÁîµÄÒ»¸ö¹¤¾ß.Ëü°ÑÒ»¸öÊý¾ÝÁ÷·Ö¸îΪ
һЩ×㹻СµÄ¿é, ÒÔ·½±ã¹ýÂËÆ÷ºÍÃüÁî½øÐд¦Àí. ÓÉ´ËÕâ¸öÃüÁîÒ²ÊǺóÖÃÒýÓõÄÒ»¸öÇ¿ÓÐ
Á¦µÄÌæ»». ÔÚÒ»°ãʹÓùý¶à²ÎÊýµÄÃüÁîÌ滻ʧ°ÜµÄʱºò,ÓÃxargs À´Ìæ»»ËüÒ»°ã¶¼Äܳɹ¦.
[1] ͨ³£Çé¿öÏÂ, xargs ´Ó¹ÜµÀ»òÕßstdinÖжÁÈ¡Êý¾Ý, µ«ÊÇËüÒ²Äܹ»´ÓÎļþµÄÊä³öÖжÁÈ¡
Êý¾Ý.
xargsµÄĬÈÏÃüÁîÊÇ echo. ÕâÒâζ×Åͨ¹ý¹ÜµÀ´«µÝ¸øxargsµÄÊäÈ뽫»á°üº¬»»ÐкͿհ×, ²»
¹ýͨ¹ýxargsµÄ´¦Àí, »»ÐкͿհ׽«±»¿Õ¸ñÈ¡´ú.
bash$ ls -l
total 0
-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1
-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file2

bash$ ls -l | xargs
total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bo
zo 0 Jan 29 23:58 file2

bash$ find ~/mail -type f | xargs grep "Linux"


./misc:User-Agent: slrn/0.9.8.1 (Linux)
./sent-mail-jul-2005: hosted by the Linux Documentation Project.
./sent-mail-jul-2005: (Linux Documentation Project Site, rtf version)
./sent-mail-jul-2005: Subject: Criticism of Bozo's Windows/Linux article
./sent-mail-jul-2005: while mentioning that the Linux ext2/ext3 filesyst
em
. . .
ls | xargs -p -l gzip ʹÓÃgzips ѹËõµ±Ç°Ä¿Â¼ÏµÄÿ¸öÎļþ, Ò»´ÎѹËõÒ»¸ö, ²¢ÇÒÔÚ
ÿ´ÎѹËõÇ°¶¼ÌáʾÓû§.
×¢Òâ: Ò»¸öÓÐȤµÄ xargs Ñ¡ÏîÊÇ -n NN, NN ÊÇÏÞÖÆÿ´Î´«µÝ½øÀ´²ÎÊýµÄ¸öÊý.
ls | xargs -n 8 echo ÒÔÿÐÐ8ÁеÄÐÎʽÁгöµ±Ç°Ä¿Â¼ÏµÄËùÓÐÎļþ.
×¢Òâ: ÁíÒ»¸öÓÐÓõÄÑ¡ÏîÊÇ -0, ʹÓà find -print0 »ò grep -lZ ÕâÁ½ÖÖ×éºÏ·½Ê½. ÕâÔÊ
Ðí´¦Àí°üº¬¿Õ°×»òÒýºÅµÄ²ÎÊý.
find / -type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f
grep -rliwZ GUI / | xargs -0 rm -f
ÉϱßÁ½Ðж¼¿ÉÓÃÀ´É¾³ýÈκΰüº¬ "GUI" µÄÎļþ. (Thanks, S.C.)
Example 12-5 Logfile: ʹÓà xargs À´¼à¿Øϵͳ log
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ´Ó /var/log/messagesGenerates µÄβ²¿¿ªÊ¼
4 # ²úÉúµ±Ç°Ä¿Â¼ÏµÄÒ»¸ölof Îļþ.
5
6 # ×¢Òâ: Èç¹ûÕâ¸ö½Å±¾±»Ò»¸öÒ»°ãÓû§µ÷ÓõĻ°,
7 # /var/log/messages ±ØÐëÊÇÈ«²¿¿É¶ÁµÄ.
8 # #root chmod 644 /var/log/messages
9
10 LINES=5
11
12 ( date; uname -a ) >>logfile
13 # ʱ¼äºÍ»úÆ÷Ãû
14 echo --------------------------------------------------------------------- >>
logfile
15 tail -$LINES /var/log/messages | xargs | fmt -s >>logfile
16 echo >>logfile
17 echo >>logfile
18
19 exit 0
20
21 # ×¢Òâ:
22 # -----
23 # Ïñ Frank Wang ËùÖ¸³ö,
24 #+ ÔÚÔ­ÎļþÖеÄÈκβ»Æ¥ÅäµÄÒýºÅ(°üÀ¨µ¥ÒýºÅºÍË«ÒýºÅ)
25 #+ ¶¼»á¸øxargsÔì³ÉÂé·³.
26 #
27 # Ëû½¨ÒéʹÓÃϱߵÄÕâÐÐÀ´Ìæ»»ÉϱߵĵÚ15ÐÐ:
28 # tail -$LINES /var/log/messages | tr -d "\"'" | xargs | fmt -s >>logfile
29
30
31
32 # Á·Ï°:
33 # -----
34 # ÐÞ¸ÄÕâ¸ö½Å±¾, ʹµÃÕâ¸ö½Å±¾Ã¿¸ö20·ÖÖÓ
35 #+ ¾Í¸ú×ÙһϠ/var/log/messages µÄÐ޸ļǼ.
36 # Ìáʾ: ʹÓà "watch" ÃüÁî.
################################End Script######################################
###
ÔÚfindÃüÁîÖÐ, Ò»¶Ô´óÀ¨ºÅ¾ÍÒ»¸öÎı¾Ìæ»»µÄλÖÃ.
Example 12-6 °Ñµ±Ç°Ä¿Â¼ÏµÄÎļþ¿½±´µ½ÁíÒ»¸öÎļþÖÐ
################################Start Script####################################
###
1 #!/bin/bash
2 # copydir.sh
3
4 # ¿½±´ (verbose) µ±Ç°Ä¿Â¼($PWD)ϵÄËùÓÐÎļþµ½
5 #+ ÃüÁîÐÐÖÐÖ¸¶¨µÄÁíÒ»¸öĿ¼ÏÂ.
6
7 E_NOARGS=65
8
9 if [ -z "$1" ] # Èç¹ûûÓвÎÊý´«µÝ½øÀ´ÄǾÍÍ˳ö.
10 then
11 echo "Usage: `basename $0` directory-to-copy-to"
12 exit $E_NOARGS
13 fi
14
15 ls . | xargs -i -t cp ./{} $1
16 # ^^ ^^ ^^
17 # -t ÊÇ "verbose" (Êä³öÃüÁîÐе½stderr) Ñ¡Ïî.
18 # -i ÊÇ"Ìæ»»×Ö·û´®"Ñ¡Ïî.
19 # {} ÊÇÊä³öÎı¾µÄÌæ»»µã.
20 # ÕâÓëÔÚ"find"ÃüÁîÖÐʹÓÃ{}µÄÇé¿öºÜÏàÏñ.
21 #
22 # Áгöµ±Ç°Ä¿Â¼ÏµÄËùÓÐÎļþ(ls .),
23 #+ ½« "ls" µÄÊä³ö×÷Ϊ²ÎÊý´«µÝµ½ "xargs"(-i -t Ñ¡Ïî) ÖÐ,
24 #+ È»ºó¿½±´(cp)ÕâЩ²ÎÊý({})µ½Ò»¸öÐÂĿ¼ÖÐ($1).
25 #
26 # ×îÖյĽá¹ûºÍϱߵÄÃüÁîµÈ¼Û,
27 #+ cp * $1
28 #+ ³ý·ÇÓÐÎļþÃûÖÐǶÈëÁË"¿Õ°×"×Ö·û.
29
30 exit 0
################################End Script######################################
###
Example 12-7 ͨ¹ýÃû×ÖKill½ø³Ì
################################Start Script####################################
###
1 #!/bin/bash
2 # kill-byname.sh: ͨ¹ýÃû×Ökill½ø³Ì.
3 # Óë½Å±¾kill-process.shÏà±È½Ï.
4
5 # ÀýÈç,
6 #+ ÊÔһϠ"./kill-byname.sh xterm" --
7 #+ ²¢ÇҲ鿴ÄãϵͳÉϵÄËùÓÐxterm¶¼½«Ïûʧ.
8
9 # ¾¯¸æ:
10 # -----
11 # ÕâÊÇÒ»¸ö·Ç³£Î£ÏյĽű¾.
12 # ÔËÐÐËüµÄʱºòÒ»¶¨ÒªÐ¡ÐÄ. (ÓÈÆäÊÇÒÔrootÉí·ÝÔËÐÐʱ)
13 #+ ÒòΪÔËÐÐÕâ¸ö½Å±¾¿ÉÄÜ»áÒýÆðÊý¾Ý¶ªÊ§»ò²úÉúÆäËûһЩ²»ºÃµÄЧ¹û.
14
15 E_BADARGS=66
16
17 if test -z "$1" # ûÓвÎÊý´«µÝ½øÀ´?
18 then
19 echo "Usage: `basename $0` Process(es)_to_kill"
20 exit $E_BADARGS
21 fi
22
23
24 PROCESS_NAME="$1"
25 ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&>/dev/nu
ll
26 # ^^ ^^
27
28 # -----------------------------------------------------------
29 # ×¢Òâ:
30 # -i ²ÎÊýÊÇxargsÃüÁîµÄ"Ìæ»»×Ö·û´®"Ñ¡Ïî.
31 # ´óÀ¨ºÅ¶ÔµÄµØ·½¾ÍÊÇÌæ»»µã.
32 # 2&>/dev/null ½«»á¶ªÆú²»ÐèÒªµÄ´íÎóÏûÏ¢.
33 # -----------------------------------------------------------
34
35 exit $?
################################End Script######################################
###
Example 12-8 ʹÓÃxargs·ÖÎöµ¥´Ê³öÏÖµÄƵÂÊ
################################Start Script####################################
###
1 #!/bin/bash
2 # wf2.sh: Crude word frequency analysis on a text file.
3
4 # ʹÓà 'xargs' ½«Îı¾ÐзֽâΪµ¥´Ê.
5 # ÓÚºó±ßµÄ "wf.sh" ½Å±¾Ïà±È½Ï.
6
7
8 # ¼ì²éÃüÁîÐÐÉÏÊäÈëµÄÎļþ.
9 ARGS=1
10 E_BADARGS=65
11 E_NOFILE=66
12
13 if [ $# -ne "$ARGS" ]
14 # ¾ÀÕý´«µÝµ½½Å±¾ÖеIJÎÊý¸öÊý?
15 then
16 echo "Usage: `basename $0` filename"
17 exit $E_BADARGS
18 fi
19
20 if [ ! -f "$1" ] # ¼ì²éÎļþÊÇ·ñ´æÔÚ.
21 then
22 echo "File \"$1\" does not exist."
23 exit $E_NOFILE
24 fi
25
26
27
28 #################################################################
29 cat "$1" | xargs -n1 | \
30 # ÁгöÎļþ, ÿÐÐÒ»¸öµ¥´Ê.
31 tr A-Z a-z | \
32 # ½«×Ö·ûת»»ÎªÐ¡Ð´.
33 sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
34 /g' | \
35 # ¹ýÂ˵ô¾äºÅºÍ¶ººÅ,
36 #+ ²¢ÇÒ½«µ¥´Ê¼äµÄ¿Õ¸ñÐÞ¸ÄΪ»»ÐÐ,
37 sort | uniq -c | sort -nr
38 # ×îºóͳ¼Æ³öÏÖ´ÎÊý,°ÑÊý×ÖÏÔʾÔÚµÚÒ»ÁÐ,È»ºóÏÔʾµ¥´Ê,²¢°´Êý×ÖÅÅÐò.
39 #################################################################
40
41 # Õâ¸öÀý×ÓµÄ×÷ÓÃÓë"wf.sh"µÄ×÷ÓÃÊÇÒ»ÑùµÄ,
42 #+ µ«ÊÇÕâ¸öÀý×ӱȽÏÓ·Ö×, ²¢ÇÒÔËÐÐÆðÀ´¸üÂýһЩ(Ϊʲô?).
43
44 exit 0
################################End Script######################################
###
expr
ͨÓÃÇóÖµ±í´ïʽ: ͨ¹ý¸ø¶¨µÄ²Ù×÷(²ÎÊý±ØÐëÒÔ¿Õ¸ñ·Ö¿ª)Á¬½Ó²ÎÊý,²¢¶Ô²ÎÊýÇóÖµ.¿ÉÒÔʹ
ËãÊõ²Ù×÷, ±È½Ï²Ù×÷, ×Ö·û´®²Ù×÷»òÕßÊÇÂß¼­²Ù×÷.
expr 3 + 5
·µ»Ø 8
expr 5 % 3
·µ»Ø 2
expr 1 / 0
·µ»Ø´íÎóÏûÏ¢, expr: division by zero
²»ÔÊÐí·Ç·¨µÄËãÊõ²Ù×÷.
expr 5 \* 3
·µ»Ø 15
ÔÚËãÊõ±í´ïʽexprÖÐʹÓó˷¨²Ù×÷ʱ, ³Ë·¨·ûºÅ±ØÐ뱻תÒå.
y=`expr $y + 1`
Ôö¼Ó±äÁ¿µÄÖµ, Óë let y=y+1 ºÍ y=$(($y+1)) µÄЧ¹ûÏàͬ. ÕâÊÇʹÓÃËãÊõ±í´ïʽµÄ
Ò»¸öÀý×Ó.
z=`expr substr $string $position $length`
ÔÚλÖÃ$positionÉÏÌáÈ¡$length³¤¶ÈµÄ×Ó´®.
Example 12-9 ʹÓà expr
################################Start Script####################################
###
1 #!/bin/bash
2
3 # չʾһЩ 'expr'µÄʹÓÃ
4 # =====================
5
6 echo
7
8 # ËãÊõ ²Ù×÷
9 # ---- ----
10
11 echo "Arithmetic Operators"
12 echo
13 a=`expr 5 + 3`
14 echo "5 + 3 = $a"
15
16 a=`expr $a + 1`
17 echo
18 echo "a + 1 = $a"
19 echo "(incrementing a variable)"
20
21 a=`expr 5 % 3`
22 # È¡Ä£²Ù×÷
23 echo
24 echo "5 mod 3 = $a"
25
26 echo
27 echo
28
29 # Âß¼­ ²Ù×÷
30 # ---- ----
31
32 # true·µ»Ø 1 ,false ·µ»Ø 0 ,
33 #+ ¶øBashµÄʹÓùßÀýÔòÏà·´.
34
35 echo "Logical Operators"
36 echo
37
38 x=24
39 y=25
40 b=`expr $x = $y` # ²âÊÔÏàµÈ.
41 echo "b = $b" # 0 ( $x -ne $y )
42 echo
43
44 a=3
45 b=`expr $a \> 10`
46 echo 'b=`expr $a \> 10`, therefore...'
47 echo "If a > 10, b = 0 (false)"
48 echo "b = $b" # 0 ( 3 ! -gt 10 )
49 echo
50
51 b=`expr $a \< 10`
52 echo "If a < 10, b = 1 (true)"
53 echo "b = $b" # 1 ( 3 -lt 10 )
54 echo
55 # Note escaping of operators.
56
57 b=`expr $a \<= 3`
58 echo "If a <= 3, b = 1 (true)"
59 echo "b = $b" # 1 ( 3 -le 3 )
60 # Ò²ÓÐ "\>=" ²Ù×÷ (´óÓÚµÈÓÚ).
61
62
63 echo
64 echo
65
66
67
68 # ×Ö·û´® ²Ù×÷
69 # ------ ----
70
71 echo "String Operators"
72 echo
73
74 a=1234zipper43231
75 echo "The string being operated upon is \"$a\"."
76
77 # ³¤¶È: ×Ö·û´®³¤¶È
78 b=`expr length $a`
79 echo "Length of \"$a\" is $b."
80
81 # Ë÷Òý: ´Ó×Ö·û´®µÄ¿ªÍ·²éÕÒÆ¥ÅäµÄ×Ó´®,
82 # ²¢È¡µÃµÚÒ»¸öÆ¥Åä×Ó´®µÄλÖÃ.
83 b=`expr index $a 23`
84 echo "Numerical position of first \"2\" in \"$a\" is \"$b\"."
85
86 # substr: ´ÓÖ¸¶¨Î»ÖÃÌáÈ¡Ö¸¶¨³¤¶ÈµÄ×Ö´®.
87 b=`expr substr $a 2 6`
88 echo "Substring of \"$a\", starting at position 2,\
89 and 6 chars long is \"$b\"."
90
91
92 # 'match' ²Ù×÷µÄĬÈÏÐÐΪ¾ÍÊÇ
93 #+ ´Ó×Ö·û´®µÄ¿ªÊ¼½øÐÐËÑË÷,²¢Æ¥ÅäµÚÒ»¸öÆ¥ÅäµÄ×Ö·û´®.
94 #
95 # ʹÓÃÕýÔò±í´ïʽ
96 b=`expr match "$a" '[0-9]*'` # Êý×ֵĸöÊý.
97 echo Number of digits at the beginning of \"$a\" is $b.
98 b=`expr match "$a" '\([0-9]*\)'` # ×¢ÒâÐèҪתÒåÀ¨ºÅ
99 # == == + ÕâÑù²ÅÄÜ´¥·¢×Ó´®µÄÆ¥Åä.
100 echo "The digits at the beginning of \"$a\" are \"$b\"."
101
102 echo
103
104 exit 0
################################End Script######################################
###
×¢Òâ: ":" ²Ù×÷¿ÉÒÔÌæ»» match. ±ÈÈç, b=`expr $a : [0-9]*`ÓëÉϱßËùʹÓÃµÄ b=`expr
match $a [0-9]*` ÍêÈ«µÈ¼Û.
################################Start Script####################################
###
1 #!/bin/bash
2
3 echo
4 echo "String operations using \"expr \$string : \" construct"
5 echo "==================================================="
6 echo
7
8 a=1234zipper5FLIPPER43231
9
10 echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"."
11 # תÒåÀ¨ºÅ¶Ô²Ù×÷. == ==
12
13 # ***************************
14 #+ תÒÆÀ¨ºÅ¶Ô
15 #+ ÓÃÀ´Æ¥ÅäÒ»¸ö×Ó´®
16 # ***************************
17
18
19 # Èç¹û²»×ªÒåÀ¨ºÅµÄ»°...
20 #+ ÄÇô 'expr' ½«°Ñstring²Ù×÷ת»»ÎªÒ»¸öÕûÊý.
21
22 echo "Length of \"$a\" is `expr "$a" : '.*'`." # ×Ö·û´®³¤¶È
23
24 echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`."
25
26 # ------------------------------------------------------------------------- #
27
28 echo
29
30 echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`."
31 # == ==
32 echo "The first 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`."
33 # ===== == ==
34 # ÔÙÀ´Ò»¸ö, תÒåÀ¨ºÅ¶ÔÇ¿ÖÆÒ»¸ö×Ó´®Æ¥Åä.
35 #
36 echo "The last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`."
37 # ==== end of string operator ^^
38 # (×îºóÕâ¸öģʽµÄÒâ˼ÊǺöÂÔÇ°±ßµÄÈκÎ×Ö·û,Ö±µ½×îºó7¸ö×Ö·û,
39 #+ ×îºó7¸öµã¾ÍÊÇÐèҪƥÅäµÄÈÎÒâ7¸ö×Ö·ûµÄ×Ö´®)
40
41 echo
42
43 exit 0
################################End Script######################################
###
ÉϱߵĽű¾Õ¹Ê¾ÁËexprÊÇÈçºÎʹÓÃתÒåµÄÀ¨ºÅ¶Ô -- \( ... \) -- ºÍ ÕýÔò±í´ïʽ Ò»ÆðÀ´·Ö
ÎöºÍÆ¥Åä×Ó´®. ϱßÊÇÁíÍâÒ»¸öÀý×Ó, Õâ´ÎµÄÀý×ÓÊÇÕæÕýµÄÓ¦ÓÃÓÃÀý.
1 # È¥µô×Ö·û´®¿ªÍ·ºÍ½áβµÄ¿Õ°×.
2 LRFDATE=`expr "$LRFDATE" : '[[:space:]]*\(.*\)[[:space:]]*$'`
3
4 # À´×ÔÓÚ Peter KnowleµÄ "booklistgen.sh" ½Å±¾
5 #+ ÓÃÀ´½«Îļþת»»ÎªSony Librie¸ñʽ.
6 # (http://booklistgensh.peterknowles.com)
Perl, sed, ºÍ awk ÊǸüÇ¿´óµÄ×Ö·û´®·ÖÎö¹¤¾ß. Ôڽű¾ÖÐǶÈëÒ»¶Î±È½Ï¶ÌµÄ sed »ò awk
²Ù×÷ (¼û Section 33.2) ±ÈʹÓà expr ¸ü¼ÓÓÐÎüÒýÁ¦.
¼û Section 9.2 ½«»áÓиü¶àʹÓà expr ½øÐÐ×Ö·û´®²Ù×÷µÄÀý×Ó.
×¢ÒâÊÂÏî:
[1] ¼´Ê¹ÔÚ²»±Ø·ÇµÃÇ¿ÖÆʹÓà xargs µÄʱºò, ʹÓà xargs Ò²¿ÉÒÔÃ÷ÏÔµØÌá¸ß¶àÎļþÅú´¦
ÀíÖ´ÐÐÃüÁîµÄËÙ¶È.

12.3 ʱ¼ä/ÈÕÆÚ ÃüÁî


-------------------
ʱ¼ä/ÈÕÆÚ ºÍ¼Æʱ
date
Ö±½Óµ÷ÓÃ, date ¾Í»á´òÓ¡ÈÕÆÚºÍʱ¼äµ½ stdout ÉÏ. Õâ¸öÃüÁîÓÐȤµÄµØ·½ÔÚÓÚËüµÄ¸ñʽ»¯
ºÍ·ÖÎöÑ¡ÏîÉÏ.
Example 12-10 ʹÓà date ÃüÁî
################################Start Script####################################
###
1 #!/bin/bash
2 # Á·Ï° 'date' ÃüÁî
3
4 echo "The number of days since the year's beginning is `date +%j`."
5 # ÐèÒªÔÚµ÷ÓøñʽµÄÇ°±ß¼ÓÉÏÒ»¸ö '+' ºÅ.
6 # %j ¸ø³ö½ñÌìÊDZ¾Äê¶ÈµÄµÚ¼¸Ìì.
7
8 echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
9 # %s ½«²úÉú´Ó "UNIX ÔªÄê" µ½ÏÖÔÚΪֹµÄÃëÊý,yields number of seconds since "UNIX epoch" be
10 #+ µ«ÊÇÕⶫÎ÷ÓÐÓÃô?
11
12 prefix=temp
13 suffix=$(date +%s) # 'date'ÃüÁîµÄ "+%s" Ñ¡ÏîÊÇ GNU-ÌØÐÔ.
14 filename=$prefix.$suffix
15 echo $filename
16 # ÕâÊÇÒ»Öַdz£ºÃµÄ²úÉú "Ψһ" µÄÁÙʱÎļþµÄ°ì·¨,
17 #+ ÉõÖÁ±ÈʹÓà $$ ¶¼Ç¿.
18
19 # Èç¹ûÏëÁ˽â 'date' ÃüÁîµÄ¸ü¶àÑ¡Ïî, Çë²éÔÄÕâ¸öÃüÁîµÄ man Ò³.
20
21 exit 0
################################End Script######################################
###
-u Ñ¡Ï¸ø³ö UTC (ÒëÕß: UTC ÊÇЭµ÷ÊÀ½çʱӢÎÄËõд) ʱ¼ä(Universal Coordinated T
ime).
bash$ date
Fri Mar 29 21:07:39 MST 2002

bash$ date -u
Sat Mar 30 04:07:42 UTC 2002
date ÃüÁîÓÐÐí¶àµÄÊä³öÑ¡Ïî. ±ÈÈç %N ½«ÒÔ10ÒÚ·Ö֮һΪµ¥Î»±íʾµ±Ç°Ê±¼ä. Õâ¸öÑ¡ÏîµÄ
Ò»¸öÓÐȤµÄÓ÷¨¾ÍÊÇÓÃÀ´²úÉúÒ»¸ö6λµÄËæ»úÊý.
1 date +%N | sed -e 's/000$//' -e 's/^0//'
2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 # È¥µô¿ªÍ·ºÍ½áβµÄ0.
µ±È»,»¹ÓÐÐí¶àÆäËüµÄÑ¡Ïî (Çë²é¿´ man date).
1 date +%j
2 # ÏÔʾ½ñÌìÊDZ¾Äê¶ÈµÄµÚ¼¸Ìì(´Ó1ÔÂ1ÈÕ¿ªÊ¼¼ÆËã).
3
4 date +%k%M
5 # ÏÔʾµ±Ç°Ð¡Ê±ÊýºÍ·ÖÖÓÊý.
6
7
8
9 # 'TZ' ²ÎÊýÔÊÐí¸Ä±äµ±Ç°µÄĬÈÏʱÇø.
10 date # Mon Mar 28 21:42:16 MST 2005
11 TZ=EST date # Mon Mar 28 23:42:16 EST 2005
12 # Thanks, Frank Kannemann and Pete Sjoberg, for the tip.
13
14
15 SixDaysAgo=$(date --date='6 days ago')
16 OneMonthAgo=$(date --date='1 month ago') # 4ÖÜÇ°(²»ÊÇÒ»¸öÔÂ).
17 OneYearAgo=$(date --date='1 year ago')
²Î¼û Example 3-4.
zdump
²é¿´Ìض¨Ê±ÇøµÄµ±Ç°Ê±¼ä.
bash$ zdump EST
EST Tue Sep 18 22:09:22 2001 EST
time
Êä³öͳ¼Æ³öÀ´µÄÃüÁîÖ´ÐеÄʱ¼ä.
time ls -l / ¸ø³öµÄÊä³ö´ó¸ÅÊÇÈçϸñʽ:
0.00user 0.01system 0:00.05elapsed 16%CPU (0avgtext+0avgdata 0maxresiden
t)k
0inputs+0outputs (149major+27minor)pagefaults 0swaps
²Î¼ûÇ°±ßÕ½ÚËù½²µÄÒ»¸öÀàËƵÄÃüÁî times .
×¢Òâ: ÔÚBashµÄ 2.0°æ±¾ ÖÐ, time ³ÉΪÁËshellµÄÒ»¸ö±£Áô×Ö, ²¢ÇÒÔÚÒ»¸ö´øÓйܵÀµÄ
ÃüÁîÐÐÖÐ,Õâ¸öÃüÁîµÄÐÐΪÓÐЩСµÄ±ä»¯.
touch
ÕâÊÇÒ»¸öÓÃÀ´¸üÐÂÎļþ±»´æÈ¡»òÐ޸ĵÄʱ¼äµÄ¹¤¾ß,Õâ¸öʱ¼ä¿ÉÒÔÊǵ±Ç°ÏµÍ³µÄʱ¼ä,Ò²¿É
ÒÔÊÇÖ¸¶¨µÄʱ¼ä, Õâ¸öÃüÁîÒ²ÓÃÀ´²úÉúÒ»¸öÐÂÎļþ.ÃüÁî touch zzz ½«²úÉúÒ»¸öÒÔzzzΪÃû
×ÖµÄ0×Ö½Ú³¤¶ÈÎļþ, µ±È»Ç°ÌáÊÇzzzÎļþ²»´æÔÚ. ΪÁ˴洢ʱ¼äÐÅÏ¢, ¾ÍÐèÒªÒ»¸öʱ¼ä´Á
Ϊ¿ÕµÄÎļþ, ±ÈÈçµ±ÄãÏë¸ú×ÙÒ»¸ö¹¤³ÌµÄÐÞ¸Äʱ¼äµÄʱºò,Õâ¾Í·Ç³£ÓÐÓÃÁË.
×¢Òâ: touch ÃüÁîµÈ¼ÛÓÚ : >> newfile »ò >> newfile (¶ÔÓÚÒ»¸öÆÕͨÎļþ).
at
at ÃüÁîÊÇÒ»¸ö×÷Òµ¿ØÖÆÃüÁî, ÓÃÀ´ÔÚÖ¸¶¨Ê±¼äÖ´Ðиø¶¨µÄÃüÁºÏ.ËüÓеãÏñ cron ÃüÁî,
È»¶ø, at ÃüÁîÖ÷Òª»¹ÊÇÓÃÀ´Ö´ÐÐÄÇÖÖÒ»´ÎÐÔÖ´ÐеÄÃüÁºÏ.
at 2pm January 15 ½«»áÌáʾÈÃÄãÊäÈëÐèÒªÔÚÕâ¸öʱ¼äÄãÒªÖ´ÐеÄÃüÁîÐòÁÐ. ÕâЩÃüÁîÓ¦
¸ÃÊÇ¿ÉÒÔºÍshell½Å±¾¼æÈݵÄ,ÒòΪ, ʵ¼ÊÉÏ, ÔÚÒ»¸ö¿ÉÖ´ÐеĽű¾ÖÐ, Óû§Ã¿´ÎÖ»ÄÜÇÃÒ»
ÐÐ. ÊäÈëÒÔ Ctl-D ½áÊø.
Äã¿ÉÒÔʹÓÃ-fÑ¡Ïî»òÕßʹÓà (<)Öض¨Ïò²Ù×÷·û, À´Èà at ÃüÁî´ÓÒ»¸öÎļþÖжÁÈ¡ÃüÁî
¼¯ºÏ. Õâ¸öÎļþÆäʵ¾ÍÒ»¸ö¿ÉÖ´ÐеĵĽű¾, ËäÈ»ËüÊÇÒ»¸ö²»¿É½»»¥µÄ½Å±¾. ÔÚÎļþÖаü
º¬Ò»¸ö run-parts ÃüÁî, ¶ÔÓÚÖ´ÐÐÒ»Ìײ»Í¬µÄ½Å±¾À´ËµÊǷdz£´ÏÃ÷µÄ×ö·¨.
bash$ at 2:30 am Friday < at-jobs.list
job 2 at 2000-10-27 02:30
batch
batch ×÷Òµ¿ØÖÆÃüÁîÓë at ÃüÁîµÄÐÐΪºÜÏàÏñ, µ« batch ÃüÁî±»ÓÃÀ´ÔÚϵͳƽ¾ùÔØÁ¿½µµ½
0.8 ÒÔÏÂʱִÐÐÒ»´ÎÐÔµÄÈÎÎñ. Óë at ÃüÁîÏàËƵÄÊÇ, ËüÒ²¿ÉÒÔʹÓà -f Ñ¡ÏîÀ´´ÓÎļþÖÐ
¶ÁÈ¡ÃüÁî.
cal
´ÓstdoutÖÐÊä³öÒ»¸ö¸ñʽ±È½ÏÕûÆëµÄÈÕÀú. Ò²¿ÉÒÔÖ¸¶¨ÄêºÍÔÂÀ´ÏÔʾÄǸöÔµÄÈÕÀú.
sleep
Õâ¸öÃüÁîÓëÒ»¸öµÈ´ýÑ­»·µÄЧ¹ûÒ»Ñù. Äã¿ÉÒÔÖ¸¶¨ÐèÒªÔÝÍ£µÄÃëÊý, Õâ¶Îʱ¼ä½«Ê²Ã´¶¼²»
¸É.µ±Ò»¸öºǫ́ÔËÐеĽø³ÌÐèҪż¶û¼ì²âÒ»¸öʼþʱ,Õâ¸ö¹¦ÄܺÜÓÐÓÃ. Ò²¿ÉÓÃÓÚ¼Æʱ. ²Î
¼û Example 29-6.
1 sleep 3 # Pauses 3 seconds.
×¢Òâ: sleep ÃüÁîĬÈÏΪÃë, µ«ÊÇÄãÒ²¿ÉÒÔÖ¸¶¨ÌìÊý, СʱÊý»ò·ÖÖÓÊý.
1 sleep 3 h # Pauses 3 hours!
×¢Òâ: Èç¹ûÄãÏëÿ¸ôÒ»¶Îʱ¼äÀ´ÔËÐÐÒ»¸öÃüÁîµÄ»°, ÄÇô watch ÃüÁ±È sleep ÃüÁîºÃ
µÃ¶à.
usleep
Microsleep ˯Ãß΢Ãë( "u" »á±»Ï£À°È˶Á³É "mu", »òÕßÊÇ micro- ǰ׺). ÓëÉÏ±ßµÄ sl
eep ÃüÁî×÷ÓÃÏàͬ, µ«Õâ¸öÃüÁîÊÇÒÔ°ÙÍò·ÖÖ®Ò»ÃëΪµ¥Î»µÄ. µ±ÐèÒª¾«È·¼Æʱ, »òÕßÐèÒª
·Ç³£Æµ·±µÄ¼à¿ØÒ»¸öÕýÔÚÔËÐеĽø³ÌµÄʱºò, Õâ¸öÃüÁî·Ç³£ÓÐÓÃ.
1 usleep 30 # ÔÝÍ£ 30 microseconds.
Õâ¸öÃüÁîÊÇ Red Hat initscripts / rc-scripts °üµÄÒ»²¿·Ö.
×¢Òâ: ÊÂʵÉÏ usleep ÃüÁî²¢²»ÄÜÌṩ·Ç³£¾«È·µÄ¼Æʱ, ËùÒÔÈç¹ûÄãÐèÒªÒ»¸öʵʱµÄÈÎÎñ
µÄ»°, Õâ¸öÃüÁî²¢²»ÊʺÏ.
hwclock, clock
hwclock ÃüÁî¿ÉÒÔ´æÈ¡»òµ÷ÕûÓ²¼þʱÖÓ. Õâ¸öÃüÁîµÄһЩѡÏîÐèÒª root ȨÏÞ. ÔÚϵͳÆô
¶¯µÄʱºò, /etc/rc.d/rc.sysinit Õâ¸öÆô¶¯Îļþ,»áʹÓà hwclock À´´ÓÓ²¼þʱÖÓÖжÁÈ¡
²¢ÉèÖÃϵͳʱ¼ä.clock at bootup.
clock ÃüÁîÓë hwclockÃüÁîÍêÈ«Ïàͬ.

12.4 Îı¾´¦ÀíÃüÁî
-----------------
´¦ÀíÎı¾ºÍÎı¾ÎļþµÄÃüÁî
sort
ÎļþÅÅÐò, ͨ³£ÓÃÔڹܵÀÖе±¹ýÂËÆ÷À´Ê¹ÓÃ. Õâ¸öÃüÁî¿ÉÒÔÒÀ¾ÝÖ¸¶¨µÄ¹Ø¼ü×Ö»òÖ¸¶¨µÄ×Ö
·ûλÖÃ, ¶ÔÎļþÐнøÐÐÅÅÐò. ʹÓà -m Ñ¡Ïî, Ëü½«»áºÏ²¢Ô¤ÅÅÐòµÄÊäÈëÎļþ. ÏëÁ˽âÕâ¸ö
ÃüÁîµÄÈ«²¿²ÎÊýÇë²Î¿¼Õâ¸öÃüÁîµÄ info Ò³. ¼û Example 10-9, Example 10-10, ºÍ
Example A-8.
tsort
ÍØÆËÅÅÐò ,¶ÁÈ¡ÒÔ¿Õ¸ñ·Ö¸ôµÄÓÐÐò¶Ô, ²¢ÇÒÒÀ¿¿ÊäÈëģʽ½øÐÐÅÅÐò.
uniq
Õâ¸ö¹ýÂËÆ÷½«»áɾ³ýÒ»¸öÒÑÅÅÐòÎļþÖеÄÖظ´ÐÐ.Õâ¸öÃüÁî¾­³£³öÏÖÔÚ sortÃüÁîµÄ¹ÜµÀºó
±ß.
1 cat list-1 list-2 list-3 | sort | uniq > final.list
2 # ½«3¸öÎļþÁ¬½ÓÆðÀ´,
3 # ½«ËüÃÇÅÅÐò,
4 # ɾ³ýÆäÖÐÖظ´µÄÐÐ,
5 # ×îºó½«½á¹ûÖض¨Ïòµ½Ò»¸öÎļþÖÐ.
-cÑ¡ÏîµÄÒâ˼ÊÇÔÚÊä³öÐÐÇ°Ãæ¼ÓÉÏÿÐÐÔÚÊäÈëÎļþÖгöÏֵĴÎÊý.
bash$ cat testfile
This line occurs only once.
This line occurs twice.
This line occurs twice.
This line occurs three times.
This line occurs three times.
This line occurs three times.

bash$ uniq -c testfile


1 This line occurs only once.
2 This line occurs twice.
3 This line occurs three times.

bash$ sort testfile | uniq -c | sort -nr


3 This line occurs three times.
2 This line occurs twice.
1 This line occurs only once.
sort INPUTFILE | uniq -c | sort -nr ÃüÁî ÏÈ¶Ô INPUTFILE ÅÅÐò, È»ºóͳ¼Æ ÿÐгö
ÏֵĴÎÊý, ×îºóµÄ(-nr Ñ¡Ï»á²úÉúÒ»¸öÊý×ֵķ´×ªÅÅÐò). ÕâÖÖÃüÁîÄ£°æÒ»°ã¶¼ÓÃÀ´·Ö
Îö log Îļþ»òÕßÓÃÀ´·ÖÎö×ÖµäÁбí, »òÕßÓÃÔÚÄÇЩÐèÒª¼ì²éÎı¾´Ê»ã½á¹¹µÄµØ·½.
Example 12-11 ·ÖÎöµ¥´Ê³öÏÖµÄƵÂÊ
################################Start Script####################################
###
1 #!/bin/bash
2 # wf.sh: ·ÖÎöÎı¾ÎļþÖÐ×ÔÈ»´Ê»ã³öÏÖµÄƵÂÊ.
3 # "wf2.sh" ÊÇÒ»¸öЧÂʸü¸ßµÄ°æ±¾.
4
5
6 # ´ÓÃüÁîÐÐÖмì²éÊäÈëµÄÎļþ.
7 ARGS=1
8 E_BADARGS=65
9 E_NOFILE=66
10
11 if [ $# -ne "$ARGS" ] # ¼ìÑé´«µÝµ½½Å±¾ÖвÎÊýµÄ¸öÊý.
12 then
13 echo "Usage: `basename $0` filename"
14 exit $E_BADARGS
15 fi
16
17 if [ ! -f "$1" ] # ¼ì²é´«ÈëµÄÎļþ²ÎÊýÊÇ·ñ´æÔÚ.
18 then
19 echo "File \"$1\" does not exist."
20 exit $E_NOFILE
21 fi
22
23
24
25 ########################################################
26 # main ()
27 sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
28 /g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
29 # =========================
30 # ¼ì²éµ¥´Ê³öÏÖµÄƵÂÊ
31
32 # ¹ýÂ˵ô¾äºÅºÍ¶ººÅ,
33 #+ ²¢ÇҰѵ¥´Ê¼äµÄ¿Õ¸ñת»¯Îª»»ÐÐ,
34 #+ È»ºóת»¯ÎªÐ¡Ð´,
35 #+ ×îºóͳ¼Æ³öÏÖµÄƵÂʲ¢°´ÆµÂÊÅÅÐò.
36
37 # Arun Giridhar ½¨Ò齫ÉϱߵĴúÂëÐÞ¸ÄΪ:
38 # . . . | sort | uniq -c | sort +1 [-f] | sort +0 -nr
39 # Õâ¾äÌí¼ÓÁ˵Ú2¸öÅÅÐòÖ÷¼ü, ËùÒÔ
40 #+ Õâ¸öÓëÉϱߵȼ۵ÄÀý×Ó½«°´ÕÕ×Öĸ˳Ðò½øÐÐÅÅÐò.
41 # ¾ÍÏñËûËù½âÊ͵Ä:
42 # "ÕâÊÇÒ»¸öÓÐЧµÄ¸ùÅÅÐò, Ê×ÏȶÔƵÂÊ×îÉÙµÄ
43 #+ ÁнøÐÐÅÅÐò
44 #+ (µ¥´Ê»òÕß×Ö·û´®, ºöÂÔ´óСд)
45 #+ È»ºó¶ÔƵÂÊ×î¸ßµÄÁнøÐÐÅÅÐò."
46 #
47 # Ïñ Frank Wang Ëù½âÊ͵ÄÄÇÑù, ÉϱߵĴúÂëµÈ¼ÛÓÚ:
48 #+ . . . | sort | uniq -c | sort +0 -nr
49 #+ ÓÃϱßÕâÐÐÒ²ÐÐ:
50 #+ . . . | sort | uniq -c | sort -k1nr -k
51 ########################################################
52
53 exit 0
54
55 # Á·Ï°:
56 # -----
57 # 1) ʹÓà 'sed' ÃüÁîÀ´¹ýÂËÆäËûµÄ±êµã·ûºÅ,
58 #+ ±ÈÈç·ÖºÅ.
59 # 2) ÐÞ¸ÄÕâ¸ö½Å±¾, Ìí¼ÓÄܹ»¹ýÂ˶à¸ö¿Õ¸ñ»òÕß
60 # ¿Õ°×µÄÄÜÁ¦.
################################End Script######################################
###
bash$ cat testfile
This line occurs only once.
This line occurs twice.
This line occurs twice.
This line occurs three times.
This line occurs three times.
This line occurs three times.

bash$ ./wf.sh testfile


6 this
6 occurs
6 line
3 times
3 three
2 twice
1 only
1 once
expand, unexpand
expand ½«»á°Ñÿ¸ötabת»¯ÎªÒ»¸ö¿Õ¸ñ.Õâ¸öÃüÁî¾­³£ÓÃÔڹܵÀÖÐ.
unexpand ½«»á°Ñÿ¸ö¿Õ¸ñת»¯ÎªÒ»¸ötab.Ч¹ûÓë expand Ïà·´.
cut
Ò»¸ö´ÓÎļþÖÐÌáÈ¡Ìض¨ÓòµÄ¹¤¾ß. Õâ¸öÃüÁîÓë awk ÖÐʹÓÃµÄ print $NÃüÁîºÜÏàËÆ, µ«ÊÇ
¸üÊÜÏÞ. Ôڽű¾ÖÐʹÓÃcutÃüÁî»á±ÈʹÓà awk ÃüÁîÀ´µÃÈÝÒ×һЩ. ×îÖØÒªµÄÑ¡Ïî¾ÍÊÇ -d
(×ֶ樽ç·û) ºÍ -f (Óò·Ö¸ô·û) Ñ¡Ïî.
ʹÓà cut À´»ñµÃËùÓÐmountÉϵÄÎļþϵͳµÄÁбí:
1 cut -d ' ' -f1,2 /etc/mtab
ʹÓà cut ÃüÁîÁгö OS ºÍ kernelµÄ°æ±¾:
1 uname -a | cut -d" " -f1,3,11,12
ʹÓà cut ÃüÁî´Ó e-mail ÖÐÌáÈ¡ÏûϢͷ:
bash$ grep '^Subject:' read-messages | cut -c10-80
Re: Linux suitable for mission-critical apps?
MAKE MILLIONS WORKING AT HOME!!!
Spam complaint
Re: Spam complaint
ʹÓà cut ÃüÁîÀ´·ÖÎöÒ»¸öÎļþ:
1 # ÁгöËùÓÐÔÚ/etc/passwdÖеÄÓû§.
2
3 FILENAME=/etc/passwd
4
5 for user in $(cut -d: -f1 $FILENAME)
6 do
7 echo $user
8 done
9
10 # Thanks, Oleg Philon for suggesting this.
cut -d ' ' -f2,3 filename µÈ¼ÛÓÚ awk -F'[ ]' '{ print $2, $3 }' filename
×¢Òâ:
ÄãÉõÖÁ¿ÉÒÔÖ¸¶¨»»Ðзû×÷Ϊ×ֶ樽ç·û. Õâ¸öС¼¿Á©Êµ¼ÊÉϾÍÊÇÔÚÃüÁîÐÐÉϲåÈëÒ»¸ö
»»ÐÐ(RETURN).(ÒëÕß: linuxʹÓÃlf×÷Ϊ»»ÐзûµÄ).
bash$ cut -d'
' -f3,7,19 testfile
This is line 3 of testfile.
This is line 7 of testfile.
This is line 19 of testfile.
Thank you, Jaka Kranjc, for pointing this out.
²Î¼û Example 12-43.
paste
½«¶à¸öÎļþ,ÒÔÿ¸öÎļþÒ»ÁеÄÐÎʽºÏ²¢µ½Ò»¸öÎļþÖÐ, ºÏ²¢ºóµÄÎļþûÁоÍÊÇÔ­À´µÄÒ»¸ö
Îļþ.¶ÔÓÚ´´½¨ÏµÍ³logÎļþÀ´Ëµ, ʹÓà cut ÃüÁîÓë paste ÃüÁîÏà½áºÏÊǷdz£ÓÐÓõÄ.
join
Õâ¸öÃüÁîÓë paste ÃüÁîÊôÓÚͬÀàÃüÁî, µ«ÊÇËüÄܹ»Íê³ÉijЩÌØÊâµÄÄ¿µØ. Õâ¸öÇ¿Á¦¹¤¾ßÄÜ
¹»ÒÔÒ»ÖÖÌØÊâµÄÐÎʽÀ´ºÏ²¢2¸öÎļþ, ÕâÖÖÌØÊâµÄÐÎʽ±¾ÖÊÉϾÍÊÇÒ»¸ö¹ØÁªÊý¾Ý¿âµÄ¼òµ¥°æ
±¾.
join ÃüÁîÖ»Äܹ»²Ù×÷2¸öÎļþ, Ëü¿ÉÒÔ½«ÄÇЩ¾ßÓÐÌض¨±ê¼ÇÓò(ͨ³£ÊÇÒ»¸öÊý×Ö±êÇ©)µÄÐÐ
ºÏ²¢ÆðÀ´, ²¢ÇÒ½«½á¹ûÊä³öµ½stdout. ±»¼ÓÈëµÄÎļþÓ¦¸ÃÊÂÏȸù¾Ý±ê¼ÇÓò½øÐÐÅÅÐòÒÔ±ãÓÚ
Äܹ»ÕýÈ·µÄÆ¥Åä.
1 File: 1.data
2
3 100 Shoes
4 200 Laces
5 300 Socks
1 File: 2.data
2
3 100 $40.00
4 200 $1.00
5 300 $2.00
bash$ join 1.data 2.data
File: 1.data 2.data
100 Shoes $40.00
200 Laces $1.00
300 Socks $2.00
×¢Òâ: ÔÚÊä³öÖбê¼ÇÓò½«Ö»»á³öÏÖÒ»´Î.
head
½«Ò»¸öÎļþµÄÍ·´òÓ¡µ½stdoutÉÏ ( ĬÈÏΪ10ÐÐ, ¿ÉÒÔ×Ô¼ºÐÞ¸Ä ). Õâ¸öÃüÁîÒ²ÓÐһЩÓÐȤ
µÄÑ¡Ïî.
Example 12-12 ÄǸöÎļþÊǽű¾?
################################Start Script####################################
###
1 #!/bin/bash
2 # script-detector.sh: ÔÚÒ»¸öĿ¼Öмì²éËùÓеĽű¾Îļþ.
3
4 TESTCHARS=2 # ²âÊÔÇ°Á½¸ö×Ö½Ú.
5 SHABANG='#!' # ½Å±¾¶¼ÊÇÒÔ "sha-bang." ¿ªÍ·µÄ.
6
7 for file in * # ±éÀúµ±Ç°Ä¿Â¼ÏµÄËùÓÐÎļþ.
8 do
9 if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]]
10 # head -c2 #!
11 # '-c' Ñ¡Ï´ÓÎļþÍ·Êä³öÖ¸¶¨¸öÊýµÄ×Ö·û,
12 #+ ¶ø²»ÊÇĬÈϵÄÐÐÊý.
13 then
14 echo "File \"$file\" is a script."
15 else
16 echo "File \"$file\" is *not* a script."
17 fi
18 done
19
20 exit 0
21
22 # Á·Ï°:
23 # -----
24 # 1) ½«Õâ¸ö½Å±¾ÐÞ¸ÄΪ¿ÉÒÔÖ¸¶¨Ä¿Â¼
25 #+ À´É¨ÃèĿ¼ÏµĽű¾.
26 #+ (¶ø²»ÊÇÖ»ËÑË÷µ±Ç°Ä¿Â¼).
27 #
28 # 2) ¾ÍÄ¿Ç°¿´À´, Õâ¸ö½Å±¾½«²»ÄÜÕýȷʶ±ð³ö
29 #+ Perl, awk, ºÍÆäËûһЩ½Å±¾ÓïÑԵĽű¾Îļþ.
30 # ÐÞÕýÕâ¸öÎÊÌâ.
################################End Script######################################
###
Example 12-13 ²úÉú10½øÖÆËæ»úÊý
################################Start Script####################################
###
1 #!/bin/bash
2 # rnd.sh: Êä³öÒ»¸ö10½øÖÆËæ»úÊý
3
4 # Script by Stephane Chazelas.
5
6 head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
7
8
9 # =================================================================== #
10
11 # ·ÖÎö
12 # ----
13
14 # head:
15 # -c4 Ñ¡ÏȡµÃÇ°4¸ö×Ö½Ú.
16
17 # od:
18 # -N4 Ñ¡ÏÏÞÖÆÊä³öΪ4¸ö×Ö½Ú.
19 # -tu4 Ñ¡ÏʹÓÃÎÞ·ûºÅ10½øÖƸñʽÀ´Êä³ö.
20
21 # sed:
22 # -n Ñ¡Ïî, ʹÓà "s" ÃüÁîÓë "p" ±êÖ¾×éºÏµÄ·½Ê½,
23 # ½«»áÖ»Êä³öÆ¥ÅäµÄÐÐ.
24
25
26
27 # ±¾½Å±¾×÷Õß½âÊÍ 'sed' ÃüÁîµÄÐÐΪÈçÏÂ.
28
29 # head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
30 # ----------------------------------> |
31
32 # ¼ÙÉèÒ»Ö±´¦Àíµ½ "sed"ÃüÁîʱµÄÊä³ö--> |
33 # Ϊ 0000000 1198195154\n
34
35 # sed ÃüÁʼ¶ÁÈ¡×Ö´®: 0000000 1198195154\n.
36 # ÕâÀïËü·¢ÏÖÒ»¸ö»»Ðзû,
37 #+ ËùÒÔ sed ×¼±¸´¦ÀíµÚÒ»ÐÐ (0000000 1198195154).
38 # sedÃüÁʼƥÅäËüµÄ <range> ºÍ <action>. µÚÒ»¸öÆ¥ÅäµÄ²¢ÇÒÖ»ÓÐÕâÒ»¸öÆ¥ÅäµÄ:
39
40 # range action
41 # 1 s/.* //p
42
43 # ÒòΪÐкÅÔÚrangeÖÐ, ËùÒÔ sed ¿ªÊ¼Ö´ÐÐ action:
44 #+ Ìæ»»µôÒÔ¿Õ¸ñ½áÊøµÄ×µÄ×Ö·û´®, ÔÚÕâÐÐÖÐÕâ¸ö×Ö·û´®ÊÇ
45 # ("0000000 ") ,ÓÿÕ×Ö·û´®(//)½«Õâ¸öÆ¥Åäµ½µÄ×Ö´®Ìæ»»µô, Èç¹û³É¹¦, ÄǾʹòÓ¡³ö½á¹û
46 # ("p" ÔÚÕâÀïÊÇ "s" ÃüÁîµÄ±êÖ¾, ÕâÓëµ¥¶ÀµÄ "p" ÃüÁîÊDz»Í¬µÄ).
47
48 # sed ÃüÁîÏÖÔÚ¿ªÊ¼¼ÌÐø¶ÁÈ¡ÊäÈë. (×¢ÒâÔÚ¼ÌÐø֮ǰ,
49 #+ continuing, Èç¹ûûʹÓà -n Ñ¡ÏîµÄ»°, sed ÃüÁÔÙ´Î
50 #+ ½«ÕâÐдòÓ¡Ò»±é).
51
52 # ÏÖÔÚ, sed ÃüÁî¶ÁÈ¡Ê£ÓàµÄ×Ö·û´®, ²¢ÇÒÕÒµ½ÎļþµÄ½áβ.
53 # sed ÃüÁʼ´¦ÀíµÚ2ÐÐ(ÕâÐÐÒ²±»±ê¼ÇΪ '$'
54 # ÒòΪÕâÒѾ­ÊÇ×îºóÒ»ÐÐ).
55 # ËùÒÔÕâÐÐû±»Æ¥Åäµ½ <range> ÖÐ, ÕâÑùsedÃüÁî¾Í½áÊøÁË.
56
57 # Õâ¸ö sed ÃüÁîµÄ¼ò¶ÌµÄ½âÊÍÊÇ:
58 # "ÔÚµÚÒ»ÐÐÖÐɾ³ýµÚÒ»¸ö¿Õ¸ñ×ó±ßÈ«²¿µÄ×Ö·û,
59 #+ È»ºó´òÓ¡³öÀ´."
60
61 # Ò»¸ö¸üºÃµÄÀ´´ïµ½Õâ¸öÄ¿µÄµÄ·½·¨ÊÇ:
62 # sed -e 's/.* //;q'
63
64 # ÕâÀï, <range> ºÍ <action> ·Ö±ðÊÇ (Ò²¿ÉÒÔд³É
65 # sed -e 's/.* //' -e q):
66
67 # range action
68 # nothing (matches line) s/.* //
69 # nothing (matches line) q (quit)
70
71 # ÕâÀï, sed ÃüÁîÖ»»á¶ÁÈ¡µÚÒ»ÐеÄÊäÈë.
72 # ½«»áÖ´ÐÐ2¸öÃüÁî, ²¢ÇÒ»áÔÚÍ˳ö֮ǰ´òÓ¡³ö(ÒѾ­Ìæ»»¹ýµÄ)ÕâÐÐ(ÒòΪ "q" action),
73 #+ ÒòΪûʹÓà "-n" Ñ¡Ïî.
74
75 # =================================================================== #
76
77 # Ò²¿ÉÒÔʹÓÃÈçÏÂÒ»¸ö¸ü¼òµ¥µÄÓï¾äÀ´´úÌæ:
78 # head -c4 /dev/urandom| od -An -tu4
79
80 exit 0
################################End Script######################################
###
²Î¼û Example 12-35.
tail
½«Ò»¸öÎļþµÄ½áβÊä³öµ½ stdout ÖÐ(ĬÈÏΪ 10 ÐÐ). ͨ³£ÓÃÀ´¸ú×ÙÒ»¸öϵͳ logfile
µÄÐÞ¸Ä×´¿ö, ʹÓà -f Ñ¡ÏîµÄ»°, tail ÃüÁ»á¼ÌÐøÏÔʾÌí¼Óµ½ÎļþÖеÄÐÐ.
Example 12-14 ʹÓà tail ÃüÁîÀ´¼à¿Øϵͳlog
################################Start Script####################################
###
1 #!/bin/bash
2
3 filename=sys.log
4
5 cat /dev/null > $filename; echo "Creating / cleaning out file."
6 # Èç¹ûÎļþ²»´æÔڵĻ°¾Í´´½¨Îļþ,
7 #+ È»ºó½«Õâ¸öÎļþÇå¿Õ.
8 # : > filename ºÍ > filename Ò²¿ÉÒÔÍê³ÉÕâ¸ö¹¤×÷.
9
10 tail /var/log/messages > $filename
11 # /var/log/messages ±ØÐë¾ßÓÐÈ«¾Ö¿É¶ÁȨÏÞ²ÅÐÐ.
12
13 echo "$filename contains tail end of system log."
14
15 exit 0
################################End Script######################################
###
×¢Òâ:
ΪÁËÁгöÒ»¸öÎı¾ÎļþÖеÄÖ¸¶¨ÐÐÊý, ¿ÉÒÔ½« head ÃüÁîµÄÊä³öͨ¹ý ¹ÜµÀ ´«µÝµ½
tail -1 ÖÐ . ±ÈÈç head -8 database.txt | tail -1 ½«»áÁгö database.txt ÎÄ
¼þµÄµÚ8ÐÐ.
ϱßÊǽ«Ò»¸öÎı¾ÎļþÖÐÖ¸¶¨·¶Î§µÄËùÓÐÐж¼±£´æµ½Ò»¸ö±äÁ¿ÖÐ:
1 var=$(head -$m $filename | tail -$n)
2
3 # filename = ÎļþÃû
4 # m = ´ÓÎļþ¿ªÍ·µ½ÏëÈ¡µÃµÄÖ¸¶¨·¶Î§µÄÐÐÊýµÄ×îºóÒ»ÐÐ
5 # n = È¡µÃÖ¸¶¨·¶Î§µÄÐÐÊý (´Ó¿é½áβ¿ªÊ¼½Ø¶Ï)
²Î¼û Example 12-5, Example 12-35 ºÍ Example 29-6.
grep
ʹÓà ÕýÔò±í´ïʽ µÄÒ»¸ö¶àÓÃ;Îı¾ËÑË÷¹¤¾ß. Õâ¸öÃüÁî±¾À´ÊÇ ed Ðб༭Æ÷ÖеÄÒ»¸öÃü
Áî/¹ýÂËÆ÷: g/re/p -- global - regular expression - print.
grep pattern [file...]
ÔÚÎļþÖÐËÑË÷ËùÓÐ pattern ³öÏÖµÄλÖÃ, pattern ¼È¿ÉÒÔÊÇÒªËÑË÷µÄ×Ö·û´®,Ò²¿ÉÒÔÊÇÒ»
¸öÕýÔò±í´ïʽ.
bash$ grep '[rst]ystem.$' osinfo.txt
The GPL governs the distribution of the Linux operating system.
Èç¹ûûÓÐÖ¸¶¨Îļþ²ÎÊý, grep ͨ³£ÓÃÔڹܵÀÖÐ¶Ô stdout ½øÐйýÂË.
bash$ ps ax | grep clock
765 tty1 S 0:00 xclock
901 pts/1 S 0:00 grep clock
-i Ñ¡ÏîÔÚËÑË÷ʱºöÂÔ´óСд.
-w Ñ¡ÏîÓÃÀ´Æ¥ÅäÕû´Ê.
-l Ñ¡Ïî½öÁгö·ûºÏÆ¥ÅäµÄÎļþ, ¶ø²»ÁгöÆ¥ÅäÐÐ.
-r (µÝ¹é) Ñ¡Ïî²»½öÔÚµ±Ç°¹¤×÷Ŀ¼ÏÂËÑË÷Æ¥Åä, ¶øÇÒËÑË÷×ÓĿ¼.
-n Ñ¡ÏîÁгöËùÓÐÆ¥ÅäÐÐ, ²¢ÏÔʾÐкÅ.
bash$ grep -n Linux osinfo.txt
2:This is a file containing information about Linux.
6:The GPL governs the distribution of the Linux operating system.
-v (»òÕß--invert-match) Ñ¡Ï»áÏÔʾËùÓв»Æ¥ÅäµÄÐÐ.
1 grep pattern1 *.txt | grep -v pattern2
2
3 # Æ¥ÅäÔÚ"*.txt"ÖÐËùÓаüº¬ "pattern1"µÄÐÐ,
4 # ¶ø²»ÏÔʾƥÅä°üº¬ "pattern2"µÄÐÐ.

-c (--count) Ñ¡Ïֻ»áÏÔʾƥÅäµ½µÄÐÐÊýµÄ×ÜÊý,¶ø²»»áÁгö¾ßÌåµÄÆ¥Åä.
1 grep -c txt *.sgml # (ÔÚ "*.sgml" ÎļþÖÐ, Æ¥Åä"txt"µÄÐÐÊýµÄ×ÜÊý.)
2
3
4 # grep -cz .
5 # ^ µã
6 # Òâ˼ÊǼÆÊý (-c) ËùÓÐÒÔ¿Õ×Ö·û·Ö¸î(-z) µÄÆ¥Åä "."µÄÏî
7 # "."ÊÇÕýÔò±í´ïʽµÄÒ»¸ö·ûºÅ, ±í´ïÆ¥ÅäÈÎÒâÒ»¸ö·Ç¿Õ×Ö·û(ÖÁÉÙÒª°üº¬Ò»¸ö×Ö·û).
8 #
9 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz . #
3
10 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '$' #
5
11 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '^' #
5
12 #
13 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -c '$' #
9
14 # ĬÈÏÇé¿öÏÂ, ÊÇʹÓû»Ðзû(\n)À´·Ö¸ôÆ¥ÅäÏî.
15
16 # ×¢Òâ -z Ñ¡ÏîÊÇ GNU "grep" Ìض¨µÄÑ¡Ïî.
17
18
19 # Thanks, S.C.
µ±Óжà¸öÎļþ²ÎÊýµÄʱºò, grep ½«»áÖ¸³öÄĸöÎļþÖаüº¬¾ßÌåµÄÆ¥Åä.
bash$ grep Linux osinfo.txt misc.txt
osinfo.txt:This is a file containing information about Linux.
osinfo.txt:The GPL governs the distribution of the Linux operating syste
m.
misc.txt:The Linux operating system is steadily gaining in popularity.
×¢Òâ: Èç¹ûÔÚ grep ÃüÁîÖ»ËÑË÷Ò»¸öÎļþµÄʱºò, ÄÇô¿ÉÒÔ¼òµ¥µÄ°Ñ /dev/null ×÷ΪµÚ2
¸öÎļþ²ÎÊý´«¸ø grep .
bash$ grep Linux osinfo.txt /dev/null
osinfo.txt:This is a file containing information about Linux.
osinfo.txt:The GPL governs the distribution of the Linux operati
ng system.
Èç¹û´æÔÚÒ»¸ö³É¹¦µÄÆ¥Åä, ÄÇô grep ÃüÁ»á·µ»Ø 0 ×÷Ϊ Í˳ö״̬ ,ÕâÑù¾Í¿ÉÒÔ½«
grep ÃüÁîµÄ½á¹û·ÅÔڽű¾µÄÌõ¼þ²âÊÔÖÐÀ´Ê¹ÓÃ, ÓÈÆäºÍ -q (½ûÖ¹Êä³ö)Ñ¡Ïî×éºÏʱÌرðÓÐ
ÓÃ.
1 SUCCESS=0 # Èç¹û grep Æ¥Åä³É¹¦
2 word=Linux
3 filename=data.file
4
5 grep -q "$word" "$filename" # "-q" Ñ¡ÏʹµÃʲô¶¼²»Êä³öµ½ stdout ÉÏ.
6
7 if [ $? -eq $SUCCESS ]
8 # if grep -q "$word" "$filename" Õâ¾ä»°¿ÉÒÔ´úÌæÐÐ 5 - 7.
9 then
10 echo "$word found in $filename"
11 else
12 echo "$word not found in $filename"
13 fi
Example 29-6 չʾÁËÈçºÎʹÓà grep ÃüÁîÀ´ÔÚÒ»¸öϵͳ logfile ÖнøÐÐÒ»¸öµ¥´ÊµÄģʽ
Æ¥Åä.
Example 12-15 ÔÚÒ»¸ö½Å±¾ÖÐÄ£·Â "grep" µÄÐÐΪ
################################Start Script####################################
###
1 #!/bin/bash
2 # grp.sh: Ò»¸ö·Ç³£´Ö²ÚµÄ 'grep' µÄʵÏÖ.
3
4 E_BADARGS=65
5
6 if [ -z "$1" ] # ¼ì²é´«µÝ¸ø½Å±¾µÄ²ÎÊý.
7 then
8 echo "Usage: `basename $0` pattern"
9 exit $E_BADARGS
10 fi
11
12 echo
13
14 for file in * # ±éÀú $PWD ϵÄËùÓÐÎļþ.
15 do
16 output=$(sed -n /"$1"/p $file) # ÃüÁîÌæ»».
17
18 if [ ! -z "$output" ] # Èç¹û"$output" ²»¼ÓË«ÒýºÅ½«»á·¢Éúʲô?
19 then
20 echo -n "$file: "
21 echo $output
22 fi # sed -ne "/$1/s|^|${file}: |p" Õâ¾äÓëÉϱßÕâ¶ÎµÈ¼Û.
23
24 echo
25 done
26
27 echo
28
29 exit 0
30
31 # Á·Ï°:
32 # -----
33 # 1) ÔÚÈκθø¶¨µÄÎļþÖÐ,Èç¹ûÓг¬¹ýÒ»¸öÆ¥ÅäµÄ»°, ÔÚÊä³öÖÐÌí¼ÓÐÂÐÐ.
34 # 2) Ìí¼ÓһЩÌØÕ÷.
################################End Script######################################
###
ÈçºÎʹÓà grep ÃüÁîÀ´ËÑË÷Á½¸ö(»òÁ½¸öÒÔÉÏ)¶ÀÁ¢µÄģʽ? Èç¹ûÄãÏëÏÔʾÔÚÒ»¸ö»ò¶à¸öÎÄ
¼þÖмÈÆ¥Åä"pattern1" ÓÖÆ¥Åä "pattern2"µÄËùÓÐÆ¥ÅäÐÐÓÖ¸ÃÈçºÎ×öÄØ?(ÒëÕß: ÕâÊÇÈ¡½»
¼¯µÄÇé¿ö, Èç¹ûÈ¡²¢¼¯¸ÃÔõô°ìÄØ?)
Ò»¸ö·½·¨ÊÇͨ¹ý ¹ÜµÀ À´½« grep pattern1 µÄ½á¹û´«µÝµ½ grep pattern2 ÖÐ .
ÀýÈç, ¸ø¶¨ÈçÏÂÎļþ:
1 # Filename: tstfile
2
3 This is a sample file.
4 This is an ordinary text file.
5 This file does not contain any unusual text.
6 This file is not unusual.
7 Here is some text.
ÏÖÔÚ, ÈÃÎÒÃÇÔÚÕâ¸öÎļþÖÐËÑË÷¼È°üº¬ "file" ÓÖ°üº¬ "text" µÄËùÓÐÐÐ
bash$ grep file tstfile
# Filename: tstfile
This is a sample file.
This is an ordinary text file.
This file does not contain any unusual text.
This file is not unusual.
bash$ grep file tstfile | grep text
This is an ordinary text file.
This file does not contain any unusual text.
--
egrep - À©Õ¹µÄ grep - Õâ¸öÃüÁîÓë grep -E µÈ¼Û. Õâ¸öÃüÁîÓÃÆðÀ´ÓÐЩ²»Í¬, ÓÉÓÚÕý
Ôò±í´ïʽÀ©Õ¹, ½«»áʹµÃËÑË÷¸ü¾ßÁé»îÐÔ.
fgrep - ¿ìËÙµÄ grep - Õâ¸öÃüÁîÓë grep -F µÈ¼Û. ÕâÊÇÒ»ÖÖ°´ÕÕ×Ö·û´®×ÖÃæÒâ˼½øÐÐ
µÄËÑË÷(¼´²»ÔÊÐíʹÓÃÕýÔò±í´ïʽ), ÕâÑùÓÐʱºò»áʹËÑË÷±äµÃÈÝÒ×һЩ.
×¢Òâ: ÔÚijЩlinux·¢ÐаæÖÐ, egrep ºÍ fgrep ¶¼ÊÇ grep ÃüÁîµÄ·ûºÅÁ¬½Ó»òÕßÊDZðÃû,
Ö»²»¹ýµ÷ÓõÄʱºò·Ö±ðʹÓà -E ºÍ -F Ñ¡Ïî°ÕÁË.
Example 12-16 ÔÚ1913ÄêµÄΤÊϴʵäÖвéÕÒ¶¨Òå
################################Start Script####################################
###
1 #!/bin/bash
2 # dict-lookup.sh
3
4 # Õâ¸ö½Å±¾ÔÚ1913ÄêµÄΤÊϴʵäÖвéÕÒ¶¨Òå.
5 # Õâ±¾¹«¹²´Êµä¿ÉÒÔͨ¹ý²»Í¬µÄ
6 #+ Õ¾µãÀ´ÏÂÔØ,°üÀ¨
7 #+ Project Gutenberg (http://www.gutenberg.org/etext/247).
8 #
9 # ÔÚͨ¹ý±¾½Å±¾Ê¹ÓÃ֮ǰ,
10 #+ ÏÈÒª½«Õâ±¾×ÖµäÓÉ DOS ¸ñʽת»»Îª UNIX¸ñʽ(Ö»ÒÔ LF ×÷ΪÐнáÊø·û).
11 # ½«Õâ¸öÎļþ´æ´¢Îª´¿Îı¾ÐÎʽ, ²¢ÇÒ±£Ö¤ÊÇδѹËõµÄ ASCII ¸ñʽ.
12 # ½«DEFAULT_DICTFILE ±äÁ¿ÒÔ path/filename ÐÎʽÉèÖúÃ.
13
14
15 E_BADARGS=65
16 MAXCONTEXTLINES=50 # ÏÔʾµÄ×î´óÐÐÊý.
17 DEFAULT_DICTFILE="/usr/share/dict/webster1913-dict.txt"
18 # ĬÈϵÄ·¾¶ºÍÎļþÃû.
19 # ÔÚ±ØÒªµÄʱºò¿ÉÒÔ½øÐÐÐÞ¸Ä.
20 # ×¢Òâ:
21 # -----
22 # Õâ¸öÌض¨µÄ1913Äê°æµÄΤÊϴʵä
23 #+ ÔÚÿ¸öÈë¿Ú¶¼ÊÇÒÔ´óд×Öĸ¿ªÍ·µÄ
24 #+ (Ê£ÓàµÄ×Ö·û¶¼ÊÇСд).
25 # Ö»ÓÐÿ²¿·ÖµÄµÚÒ»ÐÐÊÇÒÔÕâÖÖÐÎʽ¿ªÊ¼µÄ,
26 #+ ÕâÒ²¾ÍÊÇΪʲôËÑË÷Ëã·¨ÊÇϱߵÄÕâ¸öÑù×Ó.
27
28
29
30 if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]]
31 # ±ØÐëÖ¸¶¨Ò»¸öÒª²éÕҵĵ¥´Ê,
32 #+ ²¢ÇÒÕâ¸öµ¥´Ê±ØÐëÒÔ´óд×Öĸ¿ªÍ·.
33 then
34 echo "Usage: `basename $0` Word-to-define [dictionary-file]"
35 echo
36 echo "Note: Word to look up must start with capital letter,"
37 echo "with the rest of the word in lowercase."
38 echo "--------------------------------------------"
39 echo "Examples: Abandon, Dictionary, Marking, etc."
40 exit $E_BADARGS
41 fi
42
43
44 if [ -z "$2" ] # Ò²¿ÉÒÔÖ¸¶¨²»Í¬µÄ´Êµä
45 #+ ×÷ΪÕâ¸ö½Å±¾µÄµÚ2¸ö²ÎÊý´«µÝ½øÀ´.
46 then
47 dictfile=$DEFAULT_DICTFILE
48 else
49 dictfile="$2"
50 fi
51
52 # ---------------------------------------------------------
53 Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile")
54 # ÒÔ "Word \..." ÕâÖÖÐÎʽ¶¨Òå
55 #
56 # µ±È», ¼´Ê¹ËÑË÷Ò»¸öÌرð´óµÄÎı¾ÎļþµÄʱºò
57 #+ "fgrep" Ò²ÊÇ×ã¹»¿ìµÄ.
58
59
60 # ÏÖÔÚ, ¼ôµô¶¨Òå¿é.
61
62 echo "$Definition" |
63 sed -n '1,/^[A-Z]/p' |
64 # ´ÓÊä³öµÄµÚÒ»ÐÐ
65 #+ ´òÓ¡µ½ÏÂÒ»²¿·ÖµÄµÚÒ»ÐÐ.
66 sed '$d' | sed '$d'
67 # ɾ³ýÊä³öµÄ×îºóÁ½ÐÐDelete last two lines of output
68 #+ (¿ÕÐкÍÏÂÒ»²¿·ÖµÄµÚÒ»ÐÐ).
69 # ---------------------------------------------------------
70
71 exit 0
72
73 # Á·Ï°:
74 # -----
75 # 1) ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃËü¾ß±¸Äܹ»´¦ÀíÈκÎ×Ö·ûÐÎʽµÄÊäÈë
76 # + (´óд, Сд, »ò´óСд»ìºÏ), È»ºó½«Æäת»»Îª
77 # + Äܹ»´¦ÀíµÄͳһÐÎʽ.
78 #
79 # 2) ½«Õâ¸ö½Å±¾×ª»¯ÎªÒ»¸ö GUI Ó¦ÓÃ,
80 # + ʹÓÃһЩ±ÈÈçÏñ "gdialog"µÄ¶«Î÷ . . .
81 # ÕâÑùµÄ»°, ½Å±¾½«²»ÔÙ´ÓÃüÁîÐÐÖÐ
82 # + È¡µÃÕâЩ²ÎÊý.
83 #
84 # 3) ÐÞ¸ÄÕâ¸ö½Å±¾ÈÃËü¾ß±¸Äܹ»·ÖÎöÁíÍâÒ»¸ö
85 # + ¹«¹²´ÊµäµÄÄÜÁ¦,±ÈÈç U.S. Census Bureau Gazetteer.
################################End Script######################################
###
agrep (½üËÆ grep) À©Õ¹ÁË grep ½üËÆÆ¥ÅäµÄÄÜÁ¦. ËÑË÷µÄ×Ö·û´®¿ÉÄÜ»áÓë×îÖÕÆ¥Åä½á¹û
ËùÕÒµ½×Ö·û´®ÓÐЩ²»Í¬.Õâ¸ö¹¤¾ß²¢²»ÊǺËÐÄ Linux ·¢ÐаæµÄÒ»²¿·Ö.
×¢Òâ: ΪÁËËÑË÷ѹËõÎļþ, ӦʹÓà zgrep, zegrep, »ò zfgrep. ÕâЩÃüÁîÒ²¿ÉÒÔ¶ÔδѹËõ
µÄÎļþ½øÐÐËÑË÷, Ö»²»¹ý»á±ÈÒ»°ãµÄ grep, egrep, ºÍ fgrep ÂýÉÏһЩ. µ±È», ÔÚÄã
ÒªËÑË÷µÄÎļþÖÐÈç¹û»ìºÏÁËѹËõºÍδѹËõµÄÎļþµÄ»°, ÄÇôʹÓÃÕâЩÃüÁîÊǷdz£·½±ã
µÄ.
Èç¹ûÒªËÑË÷ bzipped ÀàÐ͵ÄÎļþ, ʹÓà bzgrep.
look
ÃüÁî look ÓëÃüÁî grep ºÜÏàËÆ, µ«ÊÇÕâ¸öÃüÁîÖ»ÄÜ×ö×Öµä²éѯ, Ò²¾ÍÊÇËüËùËÑË÷µÄÎļþ
±ØÐëÒѾ­ÅŹýÐòµÄµ¥´ÊÁбí. ĬÈÏÇé¿öÏÂ, Èç¹ûûÓÐÖ¸¶¨ËÑË÷ÄǸöÎļþ, ÄǾÍĬÈÏËÑË÷
/usr/dict/wordsÎļþ(ÒëÕß: ¸Ð¾õºÃÏñÓ¦¸ÃÊÇ/usr/share/dict/words), µ±È»Ò²¿ÉÒÔÖ¸¶¨
ÆäËûĿ¼ÏµÄÎļþ½øÐÐËÑË÷.
Example 12-17 ¼ì²éÁбíÖе¥´ÊµÄÕýÈ·ÐÔ
################################Start Script####################################
###
1 #!/bin/bash
2 # lookup: ¶ÔÖ¸¶¨Êý¾ÝÎļþÖеÄÿ¸öµ¥´Ê¶¼×öÒ»±é×Öµä²éѯ..
3
4 file=words.data # Ö¸¶¨µÄÒªËÑË÷µÄÊý¾ÝÎļþ.
5
6 echo
7
8 while [ "$word" != end ] # Êý¾ÝÎļþÖÐ×îºóÒ»¸öµ¥´Ê.
9 do
10 read word # ´ÓÊý¾ÝÎļþÖжÁ, ÒòΪÔÚÑ­»·µÄºó±ßÖض¨ÏòÁË.
11 look $word > /dev/null # ²»Ï뽫×ÖµäÎļþÖеÄÐÐÏÔʾ³öÀ´.
12 lookup=$? # 'look' ÃüÁîµÄÍ˳ö״̬.
13
14 if [ "$lookup" -eq 0 ]
15 then
16 echo "\"$word\" is valid."
17 else
18 echo "\"$word\" is invalid."
19 fi
20
21 done <"$file" # ½« stdin Öض¨Ïòµ½ $file, ËùÒÔ "reads" À´×ÔÓÚ $file.
22
23 echo
24
25 exit 0
26
27 # ----------------------------------------------------
28 # ϱߵĴúÂëÐн«²»»áÖ´ÐÐ, ÒòΪÉϱßÒѾ­ÓÐ "exit"ÃüÁîÁË.
29
30
31 # Stephane Chazelas ½¨ÒéʹÓÃϱ߸ü¼ò½àµÄ·½·¨:
32
33 while read word && [[ $word != end ]]
34 do if look "$word" > /dev/null
35 then echo "\"$word\" is valid."
36 else echo "\"$word\" is invalid."
37 fi
38 done <"$file"
39
40 exit 0
################################End Script######################################
###
sed, awk
Õâ¸öÁ½¸öÃüÁÊǶÀÁ¢µÄ½Å±¾ÓïÑÔ, ÓÈÆäÊʺϷÖÎöÎı¾ÎļþºÍÃüÁîÊä³ö. ¼È¿ÉÒÔµ¥¶ÀʹÓÃ,
Ò²¿ÉÒÔ½áºÏ¹ÜµÀºÍÔÚshell½Å±¾ÖÐʹÓÃ.
sed
·Ç½»»¥Ê½µÄ "Á÷±à¼­Æ÷", ÔÚÅúÁ¿Ä£Ê½ÏÂ, ÔÊÐíʹÓÃÐí¶à ex ÃüÁî.Äã»á·¢ÏÖËüÔÚshell½Å±¾
Öзdz£ÓÐÓÃ.
awk
¿É±à³ÌµÄÎļþÌáÈ¡Æ÷ºÍÎļþ¸ñʽ»¯¹¤¾ß, Ôڽṹ»¯µÄÎı¾ÎļþÖÐ,´¦Àí»òÌáÈ¡Ìض¨Óò(Ìض¨
ÁÐ)¾ßÓзdz£ºÃµÄ±íÏÖ.ËüµÄÓï·¨Óë C ÓïÑÔºÜÀàËÆ.
wc
wc ¿ÉÒÔͳ¼ÆÎļþ»ò I/O Á÷Öеĵ¥´ÊÊýÁ¿.
bash $ wc /usr/share/doc/sed-4.1.2/README
13 70 447 README
[13 lines 70 words 447 characters]
wc -w ͳ¼Æµ¥´ÊÊýÁ¿.
wc -l ͳ¼ÆÐÐÊýÁ¿.
wc -c ͳ¼Æ×Ö½ÚÊýÁ¿.
wc -m ͳ¼Æ×Ö·ûÊýÁ¿.
wc -L ¸ø³öÎļþÖÐ×Ðеij¤¶È.
ʹÓà wc ÃüÁîÀ´Í³¼Æµ±Ç°¹¤×÷Ŀ¼ÏÂÓжàÉÙ¸ö .txt Îļþ.
1 $ ls *.txt | wc -l
2 # ÒòΪÁгöµÄÎļþÃû¶¼ÊÇÒÔ»»ÐзûÇø·ÖµÄ,ËùÒÔʹÓà -l À´Í³¼Æ.
3
4 # ÁíÒ»ÖÖ´ïµ½Õâ¸öÄ¿µÄµÄ·½·¨:
5 # find . -maxdepth 1 -name \*.txt -print0 | grep -cz .
6 # (shopt -s nullglob; set -- *.txt; echo $#)
7
8 # Thanks, S.C.
ʹÓà wc ÃüÁîÀ´Í³¼ÆËùÓÐÒÔ d - h ¿ªÍ·µÄÎļþµÄ´óС.
bash$ wc [d-h]* | grep total | awk '{print $3}'
71832
ʹÓà wc ÃüÁîÀ´²é¿´Ö¸¶¨ÎļþÖаüº¬ "Linux" µÄÐÐÒ»¹²ÓжàÉÙ.
bash$ grep Linux abs-book.sgml | wc -l
50
²Î¼û Example 12-35 ºÍ Example 16-8.
ijЩÃüÁîµÄijЩѡÏîÆäʵÒѾ­°üº¬ÁË wc ÃüÁîµÄ²¿·Ö¹¦ÄÜ.
1 ... | grep foo | wc -l
2 # Õâ¸öÃüÁîʹÓõ÷dz£Æµ·±, µ«ÊÂʵÉÏËüÓиü¼ò±ãµÄд·¨.
3
4 ... | grep -c foo
5 # ֻҪʹÓà grep ÃüÁîµÄ "-c" (»ò "--count")Ñ¡Ïî¾ÍÄܴﵽͬÑùµÄÄ¿µÄ.
6
7 # Thanks, S.C.
tr
×Ö·ûת»»¹ýÂËÆ÷.
×¢Òâ: ±ØÐëʹÓÃÒýÓûòÖÐÀ¨ºÅ, ÕâÑù×ö²ÅÊǺÏÀíµÄ. ÒýÓÿÉÒÔ×èÖ¹ shell ÖØнâÊͳöÏÖÔÚ
tr ÃüÁîÐòÁÐÖеÄÌØÊâ×Ö·û.ÖÐÀ¨ºÅÓ¦¸Ã±»ÒýÓÃÆðÀ´·ÀÖ¹±»shellÀ©Õ¹.
ÎÞÂÛ tr "A-Z" "*" <filename »¹ÊÇ tr A-Z \* <filename ¶¼¿ÉÒÔ½« filename ÖеĴó
д×Ö·ûÐÞ¸ÄΪÐǺÅ(дµ½ stdout).µ«ÊÇÔÚijЩϵͳÉÏ¿ÉÄܾͲ»ÄÜÕý³£¹¤×÷ÁË, ¶ø tr A-Z '
[**]' ÔÚÈκÎϵͳÉ϶¼¿ÉÒÔÕý³£¹¤×÷.
-d Ñ¡Ïîɾ³ýÖ¸¶¨·¶Î§µÄ×Ö·û.
1 echo "abcdef" # abcdef
2 echo "abcdef" | tr -d b-d # aef
3
4
5 tr -d 0-9 <filename
6 # ɾ³ý "filename" ÖÐËùÓеÄÊý×Ö.
--squeeze-repeats (»ò -s) Ñ¡ÏîÓÃÀ´ÔÚÖظ´×Ö·ûÐòÁÐÖгýÈ¥³ýµÚÒ»¸ö×Ö·ûÒÔÍâµÄËùÓÐ×Ö
·û. Õâ¸öÑ¡ÏîÔÚɾ³ý¶àÓàµÄwhitespace µÄʱºò·Ç³£ÓÐÓÃ.
bash$ echo "XXXXX" | tr --squeeze-repeats 'X'
X
-c "complement" Ñ¡Ï»á ·´×ª Æ¥ÅäµÄ×Ö·û¼¯. ͨ¹ýÕâ¸öÑ¡Ïî, tr ½«Ö»»á¶ÔÄÇЩ ²»
Æ¥ÅäµÄ×Ö·ûÆð×÷ÓÃ.
bash$ echo "acfdeb123" | tr -c b-d +
+c+d+b++++
×¢Òâ tr ÃüÁîÖ§³Ö POSIX ×Ö·ûÀà. [1]
bash$ echo "abcd2ef1" | tr '[:alpha:]' -
----2--1
Example 12-18 ת»»´óд: °ÑÒ»¸öÎļþµÄÄÚÈÝÈ«²¿×ª»»Îª´óд.
################################Start Script####################################
###
1 #!/bin/bash
2 # °ÑÒ»¸öÎļþµÄÄÚÈÝÈ«²¿×ª»»Îª´óд.
3
4 E_BADARGS=65
5
6 if [ -z "$1" ] # ¼ì²éÃüÁîÐвÎÊý.
7 then
8 echo "Usage: `basename $0` filename"
9 exit $E_BADARGS
10 fi
11
12 tr a-z A-Z <"$1"
13
14 # ÓëÉϱߵÄ×÷ÓÃÏàͬ, µ«ÊÇʹÓÃÁË POSIX ×Ö·û¼¯±ê¼Ç·½·¨:
15 # tr '[:lower:]' '[:upper:]' <"$1"
16 # Thanks, S.C.
17
18 exit 0
19
20 # Á·Ï°:
21 # ÖØдÕâ¸ö½Å±¾, ͨ¹ýÑ¡Ïî¿ÉÒÔ¿ØÖƽű¾»òÕß
22 #+ ת»»Îª´óд»òÕßת»»ÎªÐ¡Ð´.
################################End Script######################################
###
Example 12-19 ת»»Ð¡Ð´: ½«µ±Ç°Ä¿Â¼ÏµÄËùÓÐÎÄÈ«²¿×ª»»ÎªÐ¡Ð´.
################################Start Script####################################
###
1 #!/bin/bash
2 #
3 # ½«µ±Ç°Ä¿Â¼ÏµÄËùÓÐÎÄÈ«²¿×ª»»ÎªÐ¡Ð´.
4 #
5 # Áé¸ÐÀ´×ÔÓÚ John Dubois µÄ½Å±¾,
6 #+ ת»»Îª Bash ½Å±¾,
7 #+ È»ºó±»±¾Êé×÷Õß¾«¼òÁËÒ»ÏÂ.
8
9
10 for filename in * # ±éÀúµ±Ç°Ä¿Â¼ÏµÄËùÓÐÎļþ.
11 do
12 fname=`basename $filename`
13 n=`echo $fname | tr A-Z a-z` # ½«Ãû×ÖÐÞ¸ÄΪСд.
14 if [ "$fname" != "$n" ] # Ö»¶ÔÄÇЩÎļþÃû²»ÊÇСдµÄÎļþ½øÐÐÖØÃüÃû.
15 then
16 mv $fname $n
17 fi
18 done
19
20 exit $?
21
22
23 # ϱߵĴúÂ뽫²»»á±»Ö´ÐÐ, ÒòΪÉÏ±ßµÄ "exit".
24 #-------------------------------------------#
25 # ɾ³ýÉϱߵÄÄÚÈÝ,À´ÔËÐÐϱߵÄÄÚÈÝ.
26
27 # ¶ÔÓÚÄÇЩÎļþÃûÖаüº¬¿Õ°×ºÍÐÂÐеÄÎļþ, ÉϱߵĽű¾¾Í²»Äܹ¤×÷ÁË.
28 # Stephane Chazelas Òò´Ë½¨ÒéʹÓÃϱߵķ½·¨:
29
30
31 for filename in * # ²»±Ø·ÇµÃʹÓà basename ÃüÁî,
32 # ÒòΪ "*" ²»»á·µ»ØÈκΰüº¬ "/" µÄÎļþ.
33 do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
34 # POSIX ×Ö·û¼¯±ê¼Ç·¨.
35 # Ìí¼ÓµÄбÏßÊÇΪÁËÔÚÎļþÃû½áβ»»Ðв»»á±»
36 # ÃüÁîÌ滻ɾµô.
37 # ±äÁ¿Ìæ»»:
38 n=${n%/} # ´ÓÎļþÃûÖн«ÉϱßÌí¼ÓÔÚ½áβµÄбÏßɾ³ýµô.
39 [[ $filename == $n ]] || mv "$filename" "$n"
40 # ¼ì²éÎļþÃûÊÇ·ñÒѾ­ÊÇСд.
41 done
42
43 exit $?
################################End Script######################################
###
Example 12-20 Du: DOS µ½ UNIX Îı¾ÎļþµÄת»».
################################Start Script####################################
###
1 #!/bin/bash
2 # Du.sh: DOS µ½ UNIX Îı¾ÎļþµÄת»».
3
4 E_WRONGARGS=65
5
6 if [ -z "$1" ]
7 then
8 echo "Usage: `basename $0` filename-to-convert"
9 exit $E_WRONGARGS
10 fi
11
12 NEWFILENAME=$1.unx
13
14 CR='\015' # »Ø³µCarriage return.
15 # 015 ÊÇ 8 ½øÖÆµÄ ASCII ÂëµÄ»Ø³µ.
16 # DOS ÖÐÎı¾ÎļþµÄÐнáÊø·ûÊÇ CR-LF.
17 # UNIX ÖÐÎı¾ÎļþµÄÐнáÊø·ûÖ»ÊÇ LF.
18
19 tr -d $CR < $1 > $NEWFILENAME
20 # ɾ³ý»Ø³µ²¢ÇÒдµ½ÐÂÎļþÖÐ.
21
22 echo "Original DOS text file is \"$1\"."
23 echo "Converted UNIX text file is \"$NEWFILENAME\"."
24
25 exit 0
26
27 # Á·Ï°:
28 # -----
29 # ÐÞ¸ÄÉϱߵĽű¾Íê³É´ÓUNIX µ½ DOS µÄת»».
################################End Script######################################
###
Example 12-21 rot13: rot13, ÈõÖǼÓÃÜ.
################################Start Script####################################
###
1 #!/bin/bash
2 # rot13.sh: µäÐ굀 rot13 Ëã·¨,
3 # ʹÓÃÕâÖÖ·½·¨¼ÓÃÜ¿ÉÄÜ¿ÉÒÔÓÞŪһÏÂ3ËêСº¢.
4
5 # Ó÷¨: ./rot13.sh filename
6 # »ò ./rot13.sh <filename
7 # »ò ./rot13.sh and supply keyboard input (stdin)
8
9 cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" ±äΪ "n", "b" ±äΪ "o", µÈµÈ.
10 # 'cat "$@"' ½á¹¹
11 #+ ÔÊÐí´Óstdin»òÕß´ÓÎļþÖлñµÃÊäÈë.
12
13 exit 0
################################End Script######################################
###
Example 12-22 Generating "Crypto-Quote" Puzzles
################################Start Script####################################
###
1 #!/bin/bash
2 # crypto-quote.sh: ¼ÓÃÜ
3
4 # ʹÓõ¥ÂëÌæ»»(µ¥Ò»×ÖĸÌæ»»·¨)À´½øÐмÓÃÜ.
5 # The result is similar to the "Crypto Quote" puzzles
6 #+ seen in the Op Ed pages of the Sunday paper. <rojy bug>(²»Ì«Á˽âÕâ¾äµÄÄÚÈÝ, Ó¦¸ÃÊÇÓÐÌض
7
8
9 key=ETAOINSHRDLUBCFGJMQPVWZYXK
10 # "key" ²»¹ýÊÇÒ»¸öÂÒÐòµÄ×Öĸ±í.
11 # ÐÞ¸Ä "key" ¾Í»áÐ޸ļÓÃܵĽá¹û.
12
13 # The 'cat "$@"' construction gets input either from stdin or from files.
14 # Èç¹ûʹÓÃstdin, ÄÇôҪÏë½áÊøÊäÈë¾ÍʹÓà Control-D.
15 # ·ñÔò¾ÍÒªÔÚÃüÁîÐÐÉÏÖ¸¶¨ÎļþÃû.
16
17 cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
18 # | ת»¯Îª´óд | ¼ÓÃÜ
19 # Сд, ´óд, »ò»ìºÏ´óСд, ¶¼¿ÉÒÔÕý³£¹¤×÷.
20 # µ«ÊÇ´«µÝ½øÀ´µÄ·Ç×Öĸ×Ö·û½«²»»áÆðÈκα仯.
21
22
23 # ÓÃϱߵÄÓï¾äÊÔÊÔÕâ¸ö½Å±¾:
24 # "Nothing so needs reforming as other people's habits."
25 # --Mark Twain
26 #
27 # Êä³öΪ:
28 # "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ."
29 # --BEML PZERC
30
31 # ½âÃÜ:
32 # cat "$@" | tr "$key" "A-Z"
33
34
35 # Õâ¸ö¼òµ¥µÄÃÜÂë¿ÉÒÔÇáÒ׵ı»Ò»¸ö12ËêµÄСº¢
36 #+ ÓÃǦ±ÊºÍÖ½Æƽâ.
37
38 exit 0
39
40 # Á·Ï°:
41 # -----
42 # ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃËü¿ÉÒÔÓÃÃüÁîÐвÎÊý
43 #+ À´¾ö¶¨¼ÓÃÜ»ò½âÃÜ.
################################End Script######################################
###
×¢Òâ: tr µÄ²»Í¬°æ±¾
tr ¹¤¾ßÔÚÀúÊ·ÉÏÓÐ2¸öÖØÒª°æ±¾. BSD °æ±¾²»ÐèҪʹÓÃÖÐÀ¨ºÅ (tr a-z A-Z), µ«ÊÇ
SysV °æ±¾ÔòÐèÒªÖÐÀ¨ºÅ (tr '[a-z]' '[A-Z]'). GNU °æ±¾µÄ tr ÃüÁîÓë BSD °æ±¾±È
½ÏÏàÏñ, ËùÒÔʹÓÃÖÐÀ¨ºÅÀ´ÒýÓÃ×Ö·û·¶Î§ÊÇÇ¿ÖÆÐÔµÄ(ÒëÕß: ¸Ð¾õÕâ¾ä˵·´ÁË, ¶ÁÕß¿É
×ÔÐвÎÕÕÔ­ÎÄ).
fold
½«ÊäÈë°´ÕÕÖ¸¶¨¿í¶È½øÐÐÕÛÐÐ. ÕâÀïÓÐÒ»¸ö·Ç³£ÓÐÓõÄÑ¡Ïî -s ,Õâ¸öÑ¡Ïî¿ÉÒÔʹÓÿոñ½ø
ÐжÏÐÐ.(ÒëÕß: ÊÂʵÉÏÖ»ÓÐÍâÎIJÅÐèҪʹÓÿոñ¶ÏÐÐ, ÖÐÎÄÊDz»ÐèÒªµÄ) (²Î¼û Example
12-23 ºÍ Example A-1).
fmt
Ò»¸ö¼òµ¥µÄÎļþ¸ñʽÆ÷, ͨ³£ÓÃÔڹܵÀÖÐ, ½«Ò»¸ö±È½Ï³¤µÄÎı¾ÐÐÊä³ö½øÐÐÕÛÐÐ.
Example 12-23 ¸ñʽ»¯ÎļþÁбí.
################################Start Script####################################
###
1 #!/bin/bash
2
3 WIDTH=40 # ÉèΪ 40 Áпí.
4
5 b=`ls /usr/local/bin` # È¡µÃÎļþÁбí...
6
7 echo $b | fmt -w $WIDTH
8
9 # Ò²¿ÉÒÔʹÓÃÈçÏ·½·¨,×÷ÓÃÏàͬ
10 # echo $b | fold - -s -w $WIDTH
11
12 exit 0
################################End Script######################################
###
²Î¼û Example 12-5.
×¢Òâ: Èç¹ûÏëÕÒµ½Ò»¸ö¸üÇ¿Á¦µÄ fmt ¹¤¾ß¿ÉÒÔÑ¡Ôñ Kamil Toman µÄ par ¹¤¾ß, Õâ¸ö¹¤
¾ß¿ÉÒÔ´Óºó±ßµÄÕâ¸öÍøÖ·È¡µÃhttp://www.cs.berkeley.edu/~amc/Par/.
col
Õâ¸öÃüÁîÓÃÀ´Â˳ý±ê×¼ÊäÈëµÄ·´Ïò»»ÐзûºÅ. Õâ¸ö¹¤¾ß»¹¿ÉÒÔ½«¿Õ°×ÓÃµÈ¼ÛµÄ tab À´Ìæ
»». col ¹¤¾ß×îÖ÷ÒªµÄÓ¦Óû¹ÊÇ´ÓÌض¨µÄÎı¾´¦Àí¹¤¾ßÖйýÂËÊä³ö, ±ÈÈç groff ºÍ tbl.
(ÒëÕß: Ö÷ÒªÓÃÀ´½«manҳת»¯ÎªÎı¾)
column
Áиñʽ»¯¹¤¾ß. Õâ¸ö¹ýÂ˹¤¾ß½«»á½«ÁÐÀàÐ͵ÄÎı¾×ª»¯Îª"Ò×ÓÚ´òÓ¡"µÄ±í¸ñʽ½øÐÐÊä³ö,
ͨ¹ýÔÚºÏÊʵÄλÖòåÈëtab.
Example 12-24 ʹÓà column À´¸ñʽ»¯Ä¿Â¼Áбí
################################Start Script####################################
###
1 #!/bin/bash
2 # ÕâÊÇ"column" manÒ³ÖеÄÒ»¸öÀý×Ó, ×÷Õ߶ÔÕâ¸öÀý×Ó×öÁ˺ÜСµÄÐÞ¸Ä.
3
4
5 (printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
6 ; ls -l | sed 1d) | column -t
7
8 # ¹ÜµÀÖÐµÄ "sed 1d" ɾ³ýÊä³öµÄµÚÒ»ÐÐ,
9 #+ µÚÒ»Ðн«ÊÇ "total N",
10 #+ ÆäÖÐ "N" ÊÇ "ls -l" ÕÒµ½µÄÎļþ×ÜÊý.
11
12 # "column" ÖÐµÄ -t Ñ¡ÏîÓÃÀ´×ª»¯ÎªÒ×ÓÚ´òÓ¡µÄ±íÐÎʽ.
13
14 exit 0
################################End Script######################################
###
colrm
ÁÐɾ³ý¹ýÂËÆ÷. Õâ¸ö¹¤¾ß½«»á´ÓÎļþÖÐɾ³ýÖ¸¶¨µÄÁÐ(ÁÐÖеÄ×Ö·û´®)²¢ÇÒдµ½ÎļþÖÐ, Èç
¹ûÖ¸¶¨µÄÁв»´æÔÚ,ÄÇô¾Í»Øµ½ stdout. colrm 2 4 <filename ½«»áÔÚfilenameÎļþÖжÔ
ÿÐÐɾ³ýµÚ2µ½µÚ4ÁÐÖ®¼äµÄËùÓÐ×Ö·û.
×¢Òâ: Èç¹ûÕâ¸öÎļþ°üº¬tabºÍ²»¿É´òÓ¡×Ö·û, Äǽ«»áÒýÆ𲻿ÉÔ¤ÆÚµÄÐÐΪ. ÔÚÕâÖÖÇé¿öÏÂ
, Ó¦¸Ãͨ¹ý¹ÜµÀµÄÊÖ¶ÎʹÓà expand ºÍ unexpand ÃüÁîÀ´Ô¤´¦Àí colrm.
nl
¼ÆËãÐкŹýÂËÆ÷. nl filename ½«»áÔÚ stdout ÖÐÁгöÎļþµÄËùÓÐÄÚÈÝ, µ«ÊÇ»áÔÚÿ¸ö·Ç
¿ÕÐеÄÇ°Ãæ¼ÓÉÏÁ¬ÐøµÄÐкÅ. Èç¹ûûÓÐ filename ²ÎÊý, ÄÇô¾Í²Ù×÷ stdin.
nl ÃüÁîµÄÊä³öÓë cat -n ·Ç³£ÏàËÆ, È»¶ø, ĬÈÏÇé¿öÏ nl ²»»áÁгö¿ÕÐÐ.
Example 12-25 nl: Ò»¸ö×Ô¼º¼ÆËãÐкŵĽű¾.
################################Start Script####################################
###
1 #!/bin/bash
2 # line-number.sh
3
4 # Õâ¸ö½Å±¾½«»á echo ×ÔÉíÁ½´Î, ²¢ÏÔʾÐкÅ.
5
6 # 'nl' ÃüÁîÏÔʾµÄʱºòÄ㽫»á¿´µ½, ±¾ÐÐÊǵÚ4ÐÐ, ÒòΪËü²»¼Æ¿ÕÐÐ.
7 # 'cat -n' ÃüÁîÏÔʾµÄʱºòÄ㽫»á¿´µ½, ±¾ÐÐÊǵÚ6ÐÐ.
8
9 nl `basename $0`
10
11 echo; echo # ϱß, ÈÃÎÒÃÇÊÔÊÔ 'cat -n'
12
13 cat -n `basename $0`
14 # Çø±ð¾ÍÊÇ 'cat -n' ¶Ô¿ÕÐÐÒ²½øÐмÆÊý.
15 # ×¢Òâ 'nl -ba' Ò²»áÕâô×ö.
16
17 exit 0
18 # -----------------------------------------------------------------
################################End Script######################################
###
pr
¸ñʽ»¯´òÓ¡¹ýÂËÆ÷. Õâ¸öÃüÁî»á½«Îļþ(»òstdout)·ÖÒ³, ½«ËüÃǷֳɺÏÊʵÄС¿éÒÔ±ãÓÚÓ²
¿½±´´òÓ¡»òÕßÔÚÆÁÄ»ÉÏä¯ÀÀ.ʹÓÃÕâ¸öÃüÁîµÄ²»Í¬µÄ²ÎÊý¿ÉÒÔÍê³ÉºÃ¶àÈÎÎñ, ±ÈÈç¶ÔÐкÍÁÐ
µÄ²Ù×÷,¼ÓÈëÐÐ, ÉèÖÃÒ³±ß, ¼ÆËãÐкÅ, Ìí¼Óҳü, ºÏ²¢ÎļþµÈµÈ. pr ÃüÁºÏÁËÐí¶àÃü
ÁîµÄ¹¦ÄÜ, ±ÈÈç nl, paste, fold, column, ºÍ expand.
pr -o 5 --width=65 fileZZZ | more Õâ¸öÃüÁî¶ÔfileZZZ½øÐÐÁ˱ȽϺõķÖÒ³,²¢ÇÒ´òÓ¡
µ½ÆÁÄ»ÉÏ.ÎļþµÄËõ½ø±»ÉèÖÃΪ5, ×Ü¿í¶ÈÉèÖÃΪ65.
Ò»¸öÌض¨µÄʹÓÃÑ¡Ïî -d, Ç¿ÖƸôÐдòÓ¡ (Óë sed -G Ч¹ûÏàͬ).
gettext
GNU gettext °üÊÇרÃÅÓÃÀ´½«³ÌÐòµÄÊä³ö·­Òë»òÕß±¾µØ»¯Îª²»Í¬¹ú¼ÒÓïÑԵŤ¾ß¼¯.ÔÚ×
ʼµÄʱºò½ö½öÖ§³ÖC ÓïÑÔ, ÏÖÔÚÒѾ­Ö§³ÖÁËÏ൱ÊýÁ¿µÄÆäËü³ÌÐòÓïÑԺͽű¾ÓïÑÔ.
ÒªÏë²é¿´ gettext ³ÌÐò ÈçºÎÔÚshell½Å±¾Öй¤×÷. ²Î¼û info Ò³.
msgfmt
Ò»¸ö²úÉú2½øÖÆÏûϢĿ¼µÄ³ÌÐò. Õâ¸öÃüÁîÖ÷ÒªÓÃÀ´ ±¾µØ»¯.
iconv
Ò»¸ö¿ÉÒÔ½«Îļþת»¯Îª²»Í¬±àÂë¸ñʽ(×Ö·û¼¯)µÄ¹¤¾ß. Õâ¸öÃüÁîÖ÷ÒªÓÃÀ´ ±¾µØ»¯.
1 # ½«×Ö·û·û´®ÓÉ UTF-8 ¸ñʽת»»Îª UTF-16 ²¢ÇÒ´òÓ¡µ½ BookList ÖÐ
2 function write_utf8_string {
3 STRING=$1
4 BOOKLIST=$2
5 echo -n "$STRING" | iconv -f UTF8 -t UTF16 | cut -b 3- | tr -d \\
n >> "$BOOKLIST"
6 }
7
8 # À´×ÔÓÚ Peter Knowles' "booklistgen.sh" ½Å±¾
9 #+ Ä¿µÄÊÇ°ÑÎļþת»»Îª Sony Librie ¸ñʽ.
10 # (http://booklistgensh.peterknowles.com)
recode
¿ÉÒÔÈÏΪÕâ¸öÃüÁîʱÉ챧 iconv ÃüÁîµÄÒ»¸ö¿ÕÏë¼Ò°æ±¾. Õâ¸ö·Ç³£Áé»îµÄ²¢¿ÉÒÔ°ÑÕû¸öÎÄ
¼þ¶¼×ª»»Îª²»Í¬±àÂë¸ñʽµÄ¹¤¾ß²¢²»ÊÇLinux ±ê×¼°²×°µÄÒ»²¿·Ö.
TeX, gs
TeX ºÍ Postscript ¶¼ÊÇÎı¾±ê¼ÇÓïÑÔ, ÓÃÀ´¶Ô´òÓ¡ºÍ¸ñʽ»¯µÄÊÓƵÏÔʾ½øÐÐÔ¤¿½±´.
TeX ÊÇ Donald Knuth ¾«ÐÄÖÆ×÷µÄÅÅ°æϵͳ. ͨ³£Çé¿öÏÂ, ͨ¹ý±àд½Å±¾µÄÊÖ¶ÎÀ´°ÑËùÓÐ
µÄÑ¡ÏîºÍ²ÎÊý·â×°ÆðÀ´Ò»Æð´«µ½±ê¼ÇÓïÑÔÖÐÊÇÒ»¼þºÜ·½±ãµÄÊÂÇé.
Ghostscript (gs) ÊÇÒ»¸ö ×ñÑ­ GPL µÄPostscript ½âÊÍÆ÷.
enscript
½«´¿Îı¾Îļþת»»Îª PostScript µÄ¹¤¾ß
±ÈÈç, enscript filename.txt -p filename.ps ²úÉúÒ»¸ö PostScript Êä³öÎļþ
filename.ps.
groff, tbl, eqn
ÁíÒ»ÖÖÎı¾±ê¼ÇºÍÏÔʾ¸ñʽ»¯ÓïÑÔÊÇ groff. ÕâÊÇÒ»¸ö¶Ô´«Í³ UNIX roff/troff ÏÔʾºÍÅÅ
°æ°üµÄ GNU ÔöÇ¿°æ±¾.ManÒ³ ʹÓõľÍÊÇ groff.
tbl ±í´¦Àí¹¤¾ß¿ÉÒÔÈÏΪÊÇ groff µÄÒ»²¿·Ö, ËüµÄ¹¦ÄܾÍÊǽ«±í±ê¼Çת»¯µ½ groff
ÃüÁîÖÐ.
eqn µÈʽ´¦Àí¹¤¾ßÒ²ÊÇ groff µÄÒ»²¿·Ö, ËüµÄ¹¦ÄÜÊǽ«µÈʽ±ê¼Çת»¯µ½ groff ÃüÁîÖÐ.
Example 12-26 manview: ²é¿´¸ñʽ»¯µÄmanÒ³
################################Start Script####################################
###
1 #!/bin/bash
2 # manview.sh: ½«manÒ³Ô´Îļþ¸ñʽ»¯ÒÔ·½±ã²é¿´.
3
4 # µ±ÄãÏëÔĶÁmanÒ³µÄʱºò, Õâ¸ö½Å±¾¾ÍÓÐÓÃÁË.
5 # ËüÔÊÐíÄãÔÚÔËÐеÄʱºò²é¿´
6 #+ Öмä½á¹û.
7
8 E_WRONGARGS=65
9
10 if [ -z "$1" ]
11 then
12 echo "Usage: `basename $0` filename"
13 exit $E_WRONGARGS
14 fi
15
16 # ---------------------------
17 groff -Tascii -man $1 | less
18 # À´×ÔÓÚ groff manÒ³.
19 # ---------------------------
20
21 # Èç¹ûmanÒµÖаüÀ¨±í»òÕßµÈʽ,
22 #+ ÄÇôÉϱߵĴúÂë¾Í¹»ÇºÁË.
23 # ϱߵÄÕâÐдúÂë¿ÉÒÔ½â¾öÉϱߵÄÕâ¸öÎÊÌâ.
24 #
25 # gtbl < "$1" | geqn -Tlatin1 | groff -Tlatin1 -mtty-char -man
26 #
27 # Thanks, S.C.
28
29 exit 0
################################End Script######################################
###
lex, yacc
lex ÊÇÓÃÓÚģʽƥÅäµÄ´Ê»ã·ÖÎö²úÉú³ÌÐò. ÔÚLinuxϵͳÉÏÕâ¸öÃüÁîÒѾ­±» flex È¡´úÁË.
yacc ¹¤¾ß»ùÓÚһϵÁеÄÓï·¨¹æ·¶Éú³ÉÓï·¨·ÖÎö³ÌÐò. ÔÚLinuxϵͳÉÏÕâ¸öÃüÁîÒѾ­±»
bison È¡´úÁË.
×¢ÒâÊÂÏî:
[1] ¶ÔÓÚ GNU °æ±¾µÄ tr ÃüÁîÀ´ËµÕâÊÇΨһһ´¦±ÈÄÇЩÉÌÒµ UNIX ϵͳÉϵÄÒ»°ã°æ±¾ºÏÊÊ
µÄµØ·½.

12.5 ÎļþÓë¹éµµÃüÁî
-------------------
¹éµµÃüÁî
tar
±ê×¼µÄ UNIX ¹éµµ¹¤¾ß. [1] Æð³õÕâÖ»ÊÇÒ»¸ö ´Å´ø ¹éµµ ³ÌÐò, ¶øÏÖÔÚÕâ¸ö¹¤¾ßÒѾ­±»¿ª
·¢ÎªÍ¨Óôò°ü³ÌÐò, ËüÄܹ»´¦ÀíËùÓÐÉ豸µÄËùÓÐÀàÐ͵Ĺ鵵Îļþ, °üÀ¨´Å´øÉ豸, Õý³£ÎÄ
¼þ, ÉõÖÁÊÇ stdout (²Î¼ûExample 3-4). GNU µÄtar¹¤¾ßÏÖÔÚ¿ÉÒÔ½ÓÊܲ»Í¬ÖÖÀàµÄѹËõ¹ý
ÂËÆ÷, ±ÈÈçtar czvf archive_name.tar.gz *, ²¢ÇÒ¿ÉÒԵݹéµÄ´¦Àí¹éµµÎļþ, »¹¿ÉÒÔÓÃ
gzip ѹËõĿ¼ÏµÄËùÓÐÎļþ, ³ýÁ˵±Ç°Ä¿Â¼ÏÂ($PWD)µÄ µãÎļþ . [2]
һЩÓÐÓÃµÄ tar ÃüÁîÑ¡Ïî:
1. -c ´´½¨ (Ò»¸öеĹ鵵Îļþ)
2. -x ½âѹÎļþ (´Ó´æÔڵĹ鵵ÎļþÖÐ)
3. --delete ɾ³ýÎļþ (´Ó´æÔڵĹ鵵ÎļþÖÐ)
×¢Òâ: Õâ¸öÑ¡Ïî²»ÄÜÓÃÓÚ´Å´øÀàÐÍÉ豸.
4. -r ½«ÎļþÌí¼Óµ½ÏÖ´æµÄ¹éµµÎļþµÄβ²¿
5. -A ½« tar ÎļþÌí¼Óµ½ÏÖ´æµÄ¹éµµÎļþµÄβ²¿
6. -t ÁгöÏÖ´æµÄ¹éµµÎļþÖаüº¬µÄÄÚÈÝ
7. -u ¸üй鵵Îļþ
8. -d ʹÓÃÖ¸¶¨µÄÎļþϵͳ ±È½Ï¹éµµÎļþ
9. -z Óà gzip ѹËõ¹éµµÎļþ
(ѹËõ»¹Êǽâѹ, ÒÀÀµÓÚÊÇ·ñ×éºÏÁË -c »ò -x)Ñ¡Ïî
10. -j Óà bzip2 ѹËõ¹éµµÎļþ
×¢Òâ: Èç¹ûÏë´ÓË𻵵ÄÓà gzip ѹËõ¹ýµÄ tar ÎļþÖÐÈ¡µÃÊý¾Ý, Äǽ«ÊǺÜÀ§ÄѵÄ. ËùÓе±
ÎÒÃǹ鵵ÖØÒªµÄÎļþµÄʱºò, Ò»¶¨Òª±£Áô¶à¸ö±¸·Ý.
shar
Shell ¹éµµ¹¤¾ß. ´æÔÚÓÚ shell ¹éµµÎļþÖеÄËùÓÐÎļþ¶¼ÊÇδ¾­Ñ¹ËõµÄ, ²¢ÇÒ±¾ÖÊÉÏÊÇÒ»
¸öshell ½Å±¾,ÒÔ #!/bin/sh ¿ªÍ·, ²¢ÇÒ°üº¬ËùÓбØÒªµÄ½âµµÃüÁî. Shar ¹éµµÎļþ ÖÁ½ñ
»¹ÔÚ Internet ÐÂÎÅ×éÖÐʹÓÃ, ·ñÔòµÄ»° sharÔç¾Í±» tar/gzip ËùÈ¡´úÁË. unshar ÃüÁî
ÓÃÀ´½âµµ shar ¹éµµÎļþ.
ar
´´½¨ºÍ²Ù×÷¹éµµÎļþµÄ¹¤¾ß, Ö÷ÒªÔÚ¶Ô2½øÖÆÄ¿±êÎļþ´ò°ü³É¿âʱ²Å»áÓõ½.
rpm
Red Hat °ü¹ÜÀíÆ÷, »òÕß˵ rpm ¹¤¾ßÌṩÁËÒ»ÖÖ¶ÔÔ´Îļþ»ò2½øÖÆÎļþ½øÐдò°üµÄ·½·¨.
³ý´ËÖ®Íâ, Ëü»¹°üÀ¨°²×°ÃüÁî, ²¢ÇÒ»¹¼ì²é°üµÄÍêÕûÐÔ.
Ò»¸ö¼òµ¥µÄ rpm -i package_name.rpm ÃüÁî¶ÔÓÚ°²×°Ò»¸ö°üÀ´Ëµ¾Í×ã¹»ÁË, ËäÈ»Õâ¸öÃü
ÁÓкöàÆäËüµÄÑ¡Ïî.
×¢Òâ: rpm -qf ÁгöÒ»¸öÎļþÊôÓÚÄǸö°ü.
bash$ rpm -qf /bin/ls
coreutils-5.2.1-3
×¢Òâ: rpm -qa ½«»áÁгö¸ø¶¨ÏµÍ³ÉÏËùÓа²×°Á赀 rpm °ü. rpm -qa package_name ÃüÁî
½«»áÁгöÓÚ¸ø¶¨Ãû×ÖÆ¥ÅäµÄ°ü.
bash$ rpm -qa
redhat-logos-1.1.3-1
glibc-2.2.4-13
cracklib-2.7-12
dosfstools-2.7-1
gdbm-1.8.0-10
ksymoops-2.4.1-1
mktemp-1.5-11
perl-5.6.0-17
reiserfs-utils-3.x.0j-2
...

bash$ rpm -qa docbook-utils


docbook-utils-0.6.9-2

bash$ rpm -qa docbook | grep docbook


docbook-dtd31-sgml-1.0-10
docbook-style-dsssl-1.64-3
docbook-dtd30-sgml-1.0-10
docbook-dtd40-sgml-1.0-11
docbook-utils-pdf-0.6.9-2
docbook-dtd41-sgml-1.0-10
docbook-utils-0.6.9-2
cpio
Õâ¸öÌØÊâµÄ¹éµµ¿½±´ÃüÁî(¿½±´ÊäÈëºÍÊä³ö)ÏÖÔÚÒѾ­ºÜÉÙÄܼûµ½ÁË, ÒòΪËüÒѾ­±» tar/gz
ip ËùÌæ´úÁË.ÏÖÔÚÕâ¸öÃüÁîÖ»ÔÚһЩ±È½ÏÌØÊâµÄµØ·½»¹ÔÚʹÓÃ,±ÈÈ翽±´Ò»¸öĿ¼Ê÷.
Example 12-27 ʹÓà cpio À´¿½±´Ò»¸öĿ¼Ê÷
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ʹÓà 'cpio' ¿½±´Ä¿Â¼Ê÷.
4
5 # ʹÓà 'cpio' µÄÓŵã:
6 # ¼ÓËÙ¿½±´. ±Èͨ¹ý¹ÜµÀʹÓà 'tar' ÃüÁî¿ìһЩ.
7 # ºÜÊʺϿ½±´Ò»Ð© 'cp' ÃüÁî
8 #+ ¸ã²»¶¨µÄµÄÌØÊâÎļþ(±ÈÈçÃû×ֽРpipes µÄÎļþ, µÈµÈ)
9
10 ARGS=2
11 E_BADARGS=65
12
13 if [ $# -ne "$ARGS" ]
14 then
15 echo "Usage: `basename $0` source destination"
16 exit $E_BADARGS
17 fi
18
19 source=$1
20 destination=$2
21
22 find "$source" -depth | cpio -admvp "$destination"
23 # ^^^^^ ^^^^^
24 # ÔĶÁ 'find' ºÍ 'cpio' µÄman Ò³À´Á˽âÕâЩѡÏîµÄÒâÒå.
25
26
27 # Á·Ï°:
28 # -----
29
30 # Ìí¼ÓһЩ´úÂëÀ´¼ì²é 'find | cpio' ¹ÜµÀÃüÁîµÄÍ˳öÂë($?)
31 #+ ²¢ÇÒÈç¹û³öÏÖ´íÎóµÄʱºòÊä³öºÏÊʵĴíÎóÂë.
32
33 exit 0
################################End Script######################################
###
rpm2cpio
Õâ¸öÃüÁî¿ÉÒÔ´Ó rpm ¹éµµÎļþÖнâ³öÒ»¸ö cpio ¹éµµÎļþ.
Example 12-28 ½â°üÒ»¸ö rpm ¹éµµÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # de-rpm.sh: ½â°üÒ»¸ö 'rpm' ¹éµµÎļþ
3
4 : ${1?"Usage: `basename $0` target-file"}
5 # ±ØÐëÖ¸¶¨ 'rpm' ¹éµµÎļþÃû×÷Ϊ²ÎÊý.
6
7
8 TEMPFILE=$$.cpio # Tempfile ±ØÐëÊÇÒ»¸ö"Ψһ"µÄÃû×Ö.
9 # $$ ÊÇÕâ¸ö½Å±¾µÄ½ø³Ì ID.
10
11 rpm2cpio < $1 > $TEMPFILE # ½« rpm ¹éµµÎļþת»»Îª cpio ¹éµµÎļþ.
12 cpio --make-directories -F $TEMPFILE -i # ½â°ü cpio ¹éµµÎļþ.
13 rm -f $TEMPFILE # ɾ³ý cpio ¹éµµÎļþ.
14
15 exit 0
16
17 # Á·Ï°:
18 # Ìí¼ÓһЩ´úÂëÀ´¼ì²é 1) "target-file" ÊÇ·ñ´æÔÚ
19 #+ 2) Õâ¸öÎļþÊÇ·ñÊÇÒ»¸ö rpm ¹éµµÎļþ.
20 # °µÊ¾: ·ÖÎö 'file' ÃüÁîµÄÊä³ö.
################################End Script######################################
###
ѹËõÃüÁî
gzip
±ê×¼µÄ GNU/UNIX ѹËõ¹¤¾ß, È¡´úÁ˱ȽϲîµÄ compress ÃüÁî. ÏàÓ¦µÄ½âѹÃüÁîÊÇgunzip,
gzip -d Êǵȼ۵Ä.
zcat ¹ýÂËÆ÷¿ÉÒÔ½«Ò»¸ö gzip Îļþ½âѹµ½ stdout, ËùÒÔ¾¡¿ÉÄܵÄʹÓùܵÀºÍÖض¨Ïò. Õâ
¸öÃüÁîÊÂʵÉϾÍÊÇÒ»¸ö¿ÉÒÔ¹¤×÷ÓÚѹËõÎļþ(°üÀ¨Ò»Ð©µÄʹÓÃÀ쵀 compress ¹¤¾ßѹËõµÄÎÄ
¼þ)µÄ cat ÃüÁî. zcat ÃüÁîµÈ¼ÛÓÚ gzip -dc.
×¢Òâ: ÔÚijЩÉÌÒµµÄ UNIX ϵͳÉÏ, zcat Óë uncompress -c µÈ¼Û, ²¢ÇÒ²»Äܹ¤×÷ÓÚ
gzip Îļþ.
²Î¼û Example 7-7.
bzip2
ÓÃÀ´Ñ¹ËõµÄÒ»¸ö¿ÉÑ¡µÄ¹¤¾ß, ͨ³£±È gzip ÃüÁîѹËõÂʸü¸ß(ËùÒÔ¸üÂý), ÊÊÓÃÓڱȽϴóµÄ
Îļþ. ÏàÓ¦µÄ½âѹÃüÁîÊÇ bunzip2.
×¢Òâ: а汾µÄ tar ÃüÁîÒѾ­Ö±½ÓÖ§³Ö bzip2 ÁË.
compress, uncompress
ÕâÊÇÒ»¸öÀϵÄ, ˽ÓеÄѹËõ¹¤¾ß, Ò»°ãµÄÉÌÒµ UNIX ·¢Ðа涼»áÓÐÕâ¸ö¹¤¾ß. ¸üÓÐЧÂʵÄ
gzip ¹¤¾ßÔç¾Í°ÑÕâ¸ö¹¤¾ßÌæ»»µôÁË. Linux ·¢ÐаæÒ»°ãÒ²»á°üº¬Ò»¸ö¼æÈÝµÄ compress Ãü
Áî, ËäÈ» gunzip Ò²¿ÉÒÔ¼ÓѹÓà compress ¹¤¾ßѹËõµÄÎļþ.
×¢Òâ: znew ÃüÁî¿ÉÒÔ½« compress ѹËõµÄÎļþת»»Îª gzip ѹËõµÄÎļþ.
sq
ÁíÒ»ÖÖѹËõ¹¤¾ß, Ò»¸öÖ»Äܹ¤×÷ÓÚÅŹýÐòµÄ ASCII µ¥´ÊÁбíµÄ¹ýÂËÆ÷.Õâ¸öÃüÁîʹÓùýÂË
Æ÷±ê×¼µÄµ÷ÓÃÓï·¨, sq < input-file > output-file. ËٶȺܿì, µ«ÊÇЧÂÊÔ¶²»¼°
gzip. ÏàÓ¦µÄ½âѹÃüÁîΪ unsq, µ÷Ó÷½·¨Óë sq Ïàͬ.
×¢Òâ: sq µÄÊä³ö¿ÉÒÔͨ¹ý¹ÜµÀ´«µÝ¸ø gzip ÒÔ±ãÓÚ½øÒ»²½µÄѹËõ.
zip, unzip
¿çƽ̨µÄÎļþ¹éµµºÍѹËõ¹¤¾ß, Óë DOS Ï嵀 pkzip.exe ¼æÈÝ. zip ¹éµµÎļþ¿´ÆðÀ´ÔÚ»¥
ÁªÍøÉÏ±È tar °ü¸üÁ÷ÐÐ.
unarc, unarj, unrar
ÕâЩ Linux ¹¤¾ß¿ÉÒÔÓÃÀ´½âµµÄÇЩÓà DOS Ï嵀 arc.exe, arj.exe, ºÍ rar.exe ³ÌÐò½ø
Ðй鵵µÄÎļþ.
ÎļþÐÅÏ¢
file
È·¶¨ÎļþÀàÐ͵Ť¾ß. ÃüÁî file file-name ½«»áÓà ascii Îı¾»òÊý¾ÝµÄÐÎʽ·µ»Ø
file-name ÎļþµÄÏêϸÃèÊö. Õâ¸öÃüÁî»áʹÓà /usr/share/magic, /etc/magic, »ò
/usr/lib/magic Öж¨ÒåµÄ ħ·¨Êý×Ö À´±êʶ°üº¬Ä³ÖÖħ·¨Êý×ÖµÄÎļþ, ÉϱßËù¾Ù³öµÄÕâ
3¸öÎļþÐèÒªÒÀÀµÓÚ¾ßÌåµÄ Linux/UNIX ·¢Ðаæ.
-f Ñ¡Ï»áÈà file ÃüÁîÔËÐÐÓÚÅú´¦Àíģʽ, Ò²¾ÍÊÇ˵Ëü»á·ÖÎö -f ºó±ßËùÖ¸¶¨µÄÎļþ,
´ÓÖжÁÈ¡ÐèÒª´¦ÀíµÄÎļþÁбí, È»ºóÒÀ´ÎÖ´ÐÐ file ÃüÁî. -z Ñ¡Ïî, µ±¶ÔѹËõ¹ýµÄÄ¿±êÎÄ
¼þʹÓÃʱ, ½«»áÇ¿ÖÆ·ÖÎöѹËõµÄÎļþÀàÐÍ.
bash$ file test.tar.gz
test.tar.gz: gzip compressed data, deflated, last modified: Sun Sep 16
13:34:51 2001, os: Unix
bash file -z test.tar.gz
test.tar.gz: GNU tar archive (gzip compressed data, deflated, last modi
fied: Sun Sep 16 13:34:51 2001, os: Unix)

1 # ÔÚ¸ø¶¨µÄĿ¼ÖÐÕÒ³öshºÍBash½Å±¾Îļþ:
2
3 DIRECTORY=/usr/local/bin
4 KEYWORD=Bourne
5 # Bourne ºÍ Bourne-Again shell ½Å±¾
6
7 file $DIRECTORY/* | fgrep $KEYWORD
8
9 # Êä³ö:
10
11 # /usr/local/bin/burn-cd: Bourne-Again shell script text exe
cutable
12 # /usr/local/bin/burnit: Bourne-Again shell script text exe
cutable
13 # /usr/local/bin/cassette.sh: Bourne shell script text executabl
e
14 # /usr/local/bin/copy-cd: Bourne-Again shell script text exe
cutable
15 # . . .
Example 12-29 ´Ó C ÎļþÖÐÈ¥µô×¢ÊÍ
################################Start Script####################################
###
1 #!/bin/bash
2 # strip-comment.sh: È¥µôC ³ÌÐòÖеÄ×¢ÊÍ (/* ×¢ÊÍ */)
3
4 E_NOARGS=0
5 E_ARGERROR=66
6 E_WRONG_FILE_TYPE=67
7
8 if [ $# -eq "$E_NOARGS" ]
9 then
10 echo "Usage: `basename $0` C-program-file" >&2 # ½«´íÎóÏûÏ¢·¢µ½ stderr.
11 exit $E_ARGERROR
12 fi
13
14 # ¼ì²éÎļþÀàÐÍÊÇ·ñÕýÈ·.
15 type=`file $1 | awk '{ print $2, $3, $4, $5 }'`
16 # "file $1" echoe ³öÎļþÀàÐÍ . . .
17 # È»ºó awk »áɾµôµÚÒ»¸öÓò, ¾ÍÊÇÎļþÃû . . .
18 # È»ºó½á¹û½«»á´«µÝµ½±äÁ¿ "type" ÖÐ.
19 correct_type="ASCII C program text"
20
21 if [ "$type" != "$correct_type" ]
22 then
23 echo
24 echo "This script works on C program files only."
25 echo
26 exit $E_WRONG_FILE_TYPE
27 fi
28
29
30 # Ï൱ÒþÃØµÄ sed ½Å±¾:
31 #--------
32 sed '
33 /^\/\*/d
34 /.*\*\//d
35 ' $1
36 #--------
37 # Èç¹ûÄ㻨Éϼ¸¸öСʱÀ´Ñ§Ï° sed Óï·¨µÄ»°, ÉϱßÕâ¸öÃüÁÊǺܺÃÀí½âµÄ.
38
39
40 # Èç¹û×¢ÊͺʹúÂëÔÚͬһÐÐÉÏ, ÉϱߵĽű¾¾Í²»ÐÐÁË.
41 #+ ËùÒÔÐèÒªÌí¼ÓһЩ´úÂëÀ´´¦ÀíÕâÖÖÇé¿ö.
42 # ÕâÊÇÒ»¸öºÜÖØÒªµÄÁ·Ï°.
43
44 # µ±È», ÉϱߵĴúÂëÒ²»áɾ³ý´øÓÐ "*/" µÄ·Ç×¢ÊÍÐÐ --
45 #+ ÕâÒ²²»ÊÇÒ»¸öÁîÈËÂúÒâµÄ½á¹û.
46
47 exit 0
48
49
50 # ----------------------------------------------------------------
51 # ϱߵĴúÂë²»»áÖ´ÐÐ, ÒòΪÉϱßÒѾ­ 'exit 0' ÁË.
52
53 # Stephane Chazelas ½¨ÒéʹÓÃϱߵķ½·¨:
54
55 usage() {
56 echo "Usage: `basename $0` C-program-file" >&2
57 exit 1
58 }
59
60 WEIRD=`echo -n -e '\377'` # or WEIRD=$'\377'
61 [[ $# -eq 1 ]] || usage
62 case `file "$1"` in
63 *"C program text"*) sed -e "s%/\*%${WEIRD}%g;s%\*/%${WEIRD}%g" "$1" \
64 | tr '\377\n' '\n\377' \
65 | sed -ne 'p;n' \
66 | tr -d '\n' | tr '\377' '\n';;
67 *) usage;;
68 esac
69
70 # Èç¹ûÊÇÏÂÁеÄÕâЩÇé¿ö, »¹ÊǺÜÔã¸â:
71 # printf("/*");
72 # or
73 # /* /* buggy embedded comment */
74 #
75 # ΪÁË´¦ÀíÉϱßËùÓÐÕâЩÌØÊâÇé¿ö(×Ö·û´®ÖеÄ×¢ÊÍ, º¬ÓÐ \", \\" ...
76 #+ µÄ×Ö·û´®ÖеÄ×¢ÊÍ) ΨһµÄ·½·¨»¹ÊÇдһ¸ö C ·ÖÎöÆ÷
77 #+ (»òÐí¿ÉÒÔʹÓÃlex »òÕß yacc ?).
78
79 exit 0
################################End Script######################################
###
which
which command-xxx ½«»á¸ø³ö "command-xxx" µÄÍêÕû·¾¶. µ±ÄãÏëÔÚϵͳÖÐ׼ȷ¶¨Î»Ò»¸ö
Ìض¨µÄÃüÁî»ò¹¤¾ßµÄʱºò, Õâ¸öÃüÁî¾Í·Ç³£ÓÐÓÃÁË.
$bash which rm
/usr/bin/rm
whereis
ÓëÉÏ±ßµÄ which ºÜÏàËÆ, whereis command-xxx ²»Ö»»á¸ø³ö "command-xxx" µÄÍêÕû·¾¶,
¶øÇÒ»¹»á¸ø³öÕâ¸öÃüÁîµÄ manÒ³ µÄÍêÕû·¾¶.
$bash whereis rm
rm: /bin/rm /usr/share/man/man1/rm.1.bz2
whatis
whatis filexxx ½«»áÔÚ whatis Êý¾Ý¿âÖвéѯ "filexxx". µ±ÄãÏëÈ·ÈÏϵͳÃüÁîºÍÖØÒªµÄ
ÅäÖÃÎļþµÄʱºò, Õâ¸öÃüÁî¾Í·Ç³£ÖØÒªÁË. ¿ÉÒÔ°ÑÕâ¸öÃüÁîÈÏΪÊÇÒ»¸ö¼òµ¥µÄ man ÃüÁî.
$bash whatis whatis
whatis (1) - search the whatis database for complete wor
ds
Example 12-30 Exploring /usr/X11R6/bin
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ÔÚ /usr/X11R6/bin ÖеÄËùÓÐÉñÃصÄ2½øÖÆÎļþ¶¼ÊÇʲô¶«Î÷?
4
5 DIRECTORY="/usr/X11R6/bin"
6 # Ò²ÊÔÊÔ "/bin", "/usr/bin", "/usr/local/bin", µÈµÈ.
7
8 for file in $DIRECTORY/*
9 do
10 whatis `basename $file` # ½«»á echo ³öÕâ¸ö2½øÖÆÎļþµÄÐÅÏ¢.
11 done
12
13 exit 0
14
15 # Äã¿ÉÄÜÏ£Íû½«Õâ¸ö½Å±¾µÄÊä³öÖض¨Ïò, ÏñÕâÑù:
16 # ./what.sh >>whatis.db
17 # »òÕßÒ»Ò³Ò»Ò³µÄÔÚ stdout Éϲ鿴,
18 # ./what.sh | less
################################End Script######################################
###
²Î¼û Example 10-3.
vdir
ÏÔʾÏêϸµÄĿ¼Áбí. Óë ls -l µÄЧ¹ûÀàËÆ.
ÕâÊÇÒ»¸ö GNU fileutils.
bash$ vdir
total 10
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo
bash ls -l
total 10
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo
locate, slocate
locate ÃüÁ»áÔÚÔ¤ÏȽ¨Á¢ºÃµÄµµ°¸Êý¾Ý¿âÖвéѯÎļþ. slocate ÃüÁîÊÇ locate µÄ°²È«
°æ±¾( locate ÃüÁî¿ÉÄÜÒѾ­±»¹ØÁªµ½ slocate ÃüÁîÉÏÁË).
$bash locate hickson
/usr/lib/xephem/catalogs/hickson.edb
readlink
ÏÔʾ·ûºÅÁ¬½ÓËùÖ¸ÏòµÄÎļþ.
bash$ readlink /usr/bin/awk
../../bin/gawk
strings
ʹÓà strings ÃüÁîÔÚ¶þ½øÖÆ»òÊý¾ÝÎļþÖÐÕÒ³ö¿É´òÓ¡×Ö·û. Ëü½«ÔÚÄ¿±êÎļþÖÐÁгöËùÓÐÕÒ
µ½µÄ¿É´òÓ¡×Ö·ûµÄÐòÁÐ. Õâ¸öÃüÁî¶ÔÓÚÏë½øÐпìËÙ²éÕÒÒ»¸ö n ¸ö×Ö·ûµÄ´òÓ¡¼ì²éÀ´ËµÊǺÜ
·½±ãµÄ,Ò²¿ÉÒÔÓÃÀ´¼ì²éÒ»¸öδ֪¸ñʽµÄͼƬÎļþ (strings image-file | more ¿ÉÄÜ»áËÑ
Ë÷³öÏñ JFIF ÕâÑùµÄ×Ö·û´®, ÄÇôÕâ¾ÍÒâζ×ÅÕâ¸öÎļþÊÇÒ»¸ö jpeg ¸ñʽµÄͼƬÎļþ).
Ôڽű¾ÖÐ, Äã¿ÉÄÜ»áʹÓà grep »ò sed ÃüÁîÀ´·ÖÎö strings ÃüÁîµÄÊä³ö. ²Î¼û
Example 10-7 ºÍ Example 10-9.
Example 12-31 Ò»¸ö"¸Ä½ø¹ý"µÄ strings ÃüÁî
################################Start Script####################################
###
1 #!/bin/bash
2 # wstrings.sh: "word-strings" (ÔöÇ¿µÄ "strings" ÃüÁî)
3 #
4 # Õâ¸ö½Å±¾½«»á¹ýÂË "strings" ÃüÁîµÄÊä³ö.
5 #+ ͨ¹ýÅųý±ê×¼µ¥´ÊÁбíµÄÐÎʽ¼ì²éÀ´¹ýÂËÊä³ö.
6 # Õ⽫ÓÐЧµÄ¹ýÂ˵ôÎÞÒâÒåµÄ×Ö·û,
7 #+ ²¢ÇÒÖ¸»ÓÊä³ö¿ÉÒÔʶ±ðµÄ×Ö·û.
8
9 # ===========================================================
10 # ½Å±¾²ÎÊýµÄ±ê×¼¼ì²é
11 ARGS=1
12 E_BADARGS=65
13 E_NOFILE=66
14
15 if [ $# -ne $ARGS ]
16 then
17 echo "Usage: `basename $0` filename"
18 exit $E_BADARGS
19 fi
20
21 if [ ! -f "$1" ] # ¼ì²éÎļþÊÇ·ñ´æÔÚ.
22 then
23 echo "File \"$1\" does not exist."
24 exit $E_NOFILE
25 fi
26 # ===========================================================
27
28
29 MINSTRLEN=3 # ×îСµÄ×Ö·û´®³¤¶È.
30 WORDFILE=/usr/share/dict/linux.words # ×ÖµäÎļþ.
31 # Ò²¿ÉÒÔÖ¸¶¨Ò»¸ö²»Í¬µÄ
32 #+ µ¥´ÊÁбíÎļþ,
33 #+ µ«ÕâÖÖÎļþ±ØÐëÊÇÒÔÿ¸öµ¥´ÊÒ»Ðеķ½Ê½½øÐб£´æ.
34
35
36 wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \
37 tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`
38
39 # ½«'strings' ÃüÁîµÄÊä³öͨ¹ý¹ÜµÀ´«µÝµ½¶à¸ö 'tr' ÃüÁîÖÐ.
40 # "tr A-Z a-z" È«²¿×ª»»ÎªÐ¡Ð´×Ö·û.
41 # "tr '[:space:]'" ת»»¿Õ°××Ö·ûΪ¶à¸ö Z.
42 # "tr -cs '[:alpha:]' Z" ½«·Ç×Öĸ±í×Ö·ûת»»Îª¶à¸ö Z,
43 #+ È»ºóÈ¥³ý¶à¸öÁ¬ÐøµÄ Z.
44 # "tr -s '\173-\377' Z" °ÑËùÓÐzºó±ßµÄ×Ö·û¶¼×ª»»Îª Z.
45 #+ ²¢ÇÒÈ¥³ý¶àÓàÖظ´µÄZ.(×¢Òâ173(123 ascii "{")ºÍ377(255 ascii ×îºóÒ»¸ö×Ö·û)¶¼ÊÇ8½øÖÆ)
46 #+ ÕâÑù´¦ÀíÖ®ºó, ÎÒÃÇËùÓÐ֮ǰÐèÒª´¦ÀíµÄÁîÎÒÃÇÍ·Í´µÄ×Ö·û
47 #+ ¾ÍÈ«¶¼×ª»»Îª×Ö·û Z ÁË.
48 # ×îºó"tr Z ' '" ½«°ÑËùÓÐµÄ Z ¶¼×ª»»Îª¿Õ¸ñ,
49 #+ ÕâÑùÎÒÃÇÔÚϱßÑ­»·ÖÐÓõ½µÄ±äÁ¿ wlist ÖеÄÄÚÈݾÍÈ«²¿ÒÔ¿Õ¸ñ·Ö¸ôÁË.
50
51 # ****************************************************************
52 # ×¢Òâ, ÎÒÃÇʹÓùܵÀÀ´½«¶à¸ö 'tr' µÄÊä³ö´«µÝµ½ÏÂÒ»¸ö 'tr' ʱ
53 #+ ÿ´Î¶¼Ê¹ÓÃÁ˲»Í¬µÄ²ÎÊý.
54 # ****************************************************************
55
56
57 for word in $wlist # ÖØÒª:
58 # $wlist ÕâÀï²»ÄÜʹÓÃË«ÒýºÅ.
59 # "$wlist" ²»ÄÜÕý³£¹¤×÷.
60 # Ϊʲô²»ÐÐ?
61 do
62
63 strlen=${#word} # ×Ö·û´®³¤¶È.
64 if [ "$strlen" -lt "$MINSTRLEN" ] # Ìø¹ý¶ÌµÄ×Ö·û´®.
65 then
66 continue
67 fi
68
69 grep -Fw $word "$WORDFILE" # ֻƥÅäÕû¸öµ¥´Ê.
70 # ^^^ # "¹Ì¶¨×Ö·û´®" ºÍ
71 #+ "Õû¸öµ¥´Ê" Ñ¡Ïî.
72
73 done
74
75
76 exit $?
################################End Script######################################
###
±È½ÏÃüÁî
diff, patch
diff: Ò»¸ö·Ç³£Áé»îµÄÎļþ±È½Ï¹¤¾ß. Õâ¸ö¹¤¾ß½«»áÒÔÒ»ÐнÓÒ»ÐеÄÐÎʽÀ´±È½ÏÄ¿±êÎļþ.
ÔÚijЩӦÓÃÖÐ, ±ÈÈç˵±È½Ïµ¥´Ê´Êµä, ÔÚͨ¹ý¹ÜµÀ½«½á¹û´«µÝ¸ø diff ÃüÁî֮ǰ, ʹÓÃÖî
Èç sort ºÍ uniq ÃüÁîÀ´¶ÔÎļþ½øÐйýÂ˽«ÊǷdz£ÓÐÓõÄ.diff file-1 file-2 ½«»áÊä³ö
2¸öÎļþ²»Í¬µÄÐÐ,²¢»áͨ¹ý·ûºÅ±êʶ³öÿ¸ö²»Í¬ÐÐËùÊôµÄÎļþ.
diff ÃüÁîµÄ --side-by-side Ñ¡Ï»á°Ñ2¸ö±È½ÏÖеÄÎļþÈ«²¿Êä³ö, °´ÕÕ×óÓÒ·Ö¸ôµÄÐÎ
ʽ, ²¢»á°Ñ²»Í¬µÄÐбê¼Ç³öÀ´. -c ºÍ -u Ñ¡ÏîÒ²»áʹµÃ diff ÃüÁîµÄÊä³ö±äµÃÈÝÒ×½âÊÍ
һЩ.
»¹ÓÐһЩ diff ÃüÁîµÄ±äÖÖ, ±ÈÈç sdiff, wdiff, xdiff, ºÍ mgdiff.
×¢Òâ: Èç¹û±È½ÏµÄÁ½¸öÎļþÊÇÍêÈ«Ò»ÑùµÄ»°, ÄÇô diff ÃüÁî»á·µ»Ø 0 ×÷ΪÍ˳öÂë, Èç¹û
²»Í¬µÄ»°¾Í·µ»Ø 1 ×÷ΪÍ˳öÂë. ÕâÑù diff ÃüÁî¾Í¿ÉÒÔÓÃÔÚ shell ½Å±¾µÄ²âÊԽṹ
ÖÐÁË. (¼ûϱß)
diff ÃüÁîµÄÒ»¸öÖØÒªÓ÷¨¾ÍÊDzúÉúÇø±ðÎļþ, Õâ¸öÎļþ½«ÓÃ×÷ patch ÃüÁîµÄ -e Ñ¡ÏîµÄ
²ÎÊý, -e Ñ¡Ïî½ÓÊÜ ed »ò ex ½Å±¾.
patch: Áé»îµÄ°æ±¾¹¤¾ß.¸ø³öÒ»¸öÓà diff ÃüÁî²úÉúµÄÇø±ðÎļþ, patch ÃüÁî¿ÉÒÔ½«Ò»¸ö
ÀÏ°æ±¾µÄ°ü¸üÐÂΪһ¸öа汾µÄ°ü. ÒòΪÄã·¢²¼Ò»¸öСµÄÇø±ðÎļþÔ¶±ÈÖØз¢²¼Ò»¸ö´óµÄ
Èí¼þ°üÀ´µÄÈÝÒ׵öà.¶ÔÓÚƵ·±¸üÐ嵀 Linux ÄÚºËÀ´Ëµ, ʹÓò¹¶¡°üµÄÐÎʽÀ´·¢²¼½«ÊÇÒ»
Öֺܺõķ½·¨.
1 patch -p1 <patch-file
2 # ÔÚ'patch-file'ÖÐÈ¡µÃËùÓеÄÐÞ¸ÄÁбí
3 # È»ºó°ÑËüÃÇÓ¦ÓÃÓÚÆäÖÐË÷Òýµ½µÄÎļþÉÏ.
4 # ÄÇôÕâ¸ö°ü¾Í±»¸üÐÂΪа汾ÁË.
¸üРkernel:
1 cd /usr/src
2 gzip -cd patchXX.gz | patch -p0
3 # ʹÓÃ'patch'À´¸üÐÂÄÚºËÔ´Îļþ.
4 # À´×ÔÓÚÄäÃû×÷Õß(Alan Cox?)µÄ
5 # Linux ÄÚºËÎĵµ "README".
×¢Òâ: diff ÃüÁîÒ²¿ÉÒԵݹéµÄ±È½ÏĿ¼ÏµÄËùÓÐÎļþ(°üº¬×ÓĿ¼).
bash$ diff -r ~/notes1 ~/notes2
Only in /home/bozo/notes1: file02
Only in /home/bozo/notes1: file03
Only in /home/bozo/notes2: file04
×¢Òâ: ʹÓà zdiff À´±È½Ï gzip Îļþ.
diff3
Ò»¸ö diff ÃüÁîµÄÀ©Õ¹°æ±¾, ¿ÉÒÔͬʱ±È½Ï3¸öÎļþ. Èç¹û³É¹¦Ö´ÐÐÄÇôÕâ¸öÃüÁî¾Í·µ»Ø0,
µ«ÊDz»ÐÒµÄÊÇÕâ¸öÃüÁî²»¸ø³ö±È½Ï½á¹ûµÄÐÅÏ¢.
bash$ diff3 file-1 file-2 file-3
====
1:1c
This is line 1 of "file-1".
2:1c
This is line 1 of "file-2".
3:1c
This is line 1 of "file-3"
sdiff
±È½Ï ºÍ/»ò ±à¼­2¸öÎļþ, ½«ËüÃǺϲ¢µ½Ò»¸öÊä³öÎļþÖÐ. ÒòΪÕâ¸öÃüÁîµÄ½»»¥ÌØÐÔ, Ëù
ÒÔÔڽű¾ÖкÜÉÙʹÓÃÕâ¸öÃüÁî.
cmp
cmp ÃüÁîÊÇÉ챧 diff ÃüÁîµÄÒ»¸ö¼òµ¥°æ±¾. diff ÃüÁî»á±¨¸æÁ½¸öÎļþµÄ²»Í¬Ö®´¦, ¶ø
cmp ÃüÁî½ö½öÖ¸³öÄÇЩλÖÃÓв»Í¬, ¶ø²»»áÏÔʾ²»Í¬µÄ¾ßÌåϸ½Ú.
×¢Òâ: Óë diff Ò»Ñù,Èç¹ûÁ½¸öÎļþÏàͬ cmp ·µ»Ø0×÷ΪÍ˳öÂë, Èç¹û²»Í¬·µ»Ø1. ÕâÑù¾Í
¿ÉÒÔÓÃÔÚ shell ½Å±¾µÄ²âÊԽṹÖÐÁË.
Example 12-32 ÔÚÒ»¸ö½Å±¾ÖÐʹÓà cmp À´±È½Ï2¸öÎļþ.
################################Start Script####################################
###
1 #!/bin/bash
2
3 ARGS=2 # ½Å±¾ÐèÒª2¸ö²ÎÊý.
4 E_BADARGS=65
5 E_UNREADABLE=66
6
7 if [ $# -ne "$ARGS" ]
8 then
9 echo "Usage: `basename $0` file1 file2"
10 exit $E_BADARGS
11 fi
12
13 if [[ ! -r "$1" || ! -r "$2" ]]
14 then
15 echo "Both files to be compared must exist and be readable."
16 exit $E_UNREADABLE
17 fi
18
19 cmp $1 $2 &> /dev/null # /dev/null ½«»á½ûÖ¹ "cmp" ÃüÁîµÄÊä³ö.
20 # cmp -s $1 $2 ÓëÉϱßÕâ¾ä½á¹ûÏàͬ ("-s" Ñ¡ÏîÊÇ°²¾²±êÖ¾)
21 # Thank you Anders Gustavsson for pointing this out.
22 #
23 # Óà 'diff' ÃüÁîÒ²¿ÉÒÔ, ±ÈÈç, diff $1 $2 &> /dev/null
24
25 if [ $? -eq 0 ] # ²âÊÔ "cmp" ÃüÁîµÄÍ˳öÂë.
26 then
27 echo "File \"$1\" is identical to file \"$2\"."
28 else
29 echo "File \"$1\" differs from file \"$2\"."
30 fi
31
32 exit 0
################################End Script######################################
###
×¢Òâ: Óà zcmp ´¦Àí gzip Îļþ.
comm
¶à¹¦ÄܵÄÎļþ±È½Ï¹¤¾ß. ʹÓÃÕâ¸öÃüÁî֮ǰ±ØÐëÏÈÅÅÐò.
comm -options first-file second-file
comm file-1 file-2 ½«»áÊä³ö3ÁÐ:
* µÚ 1 ÁÐ = Ö»ÔÚ file-1 ÖдæÔÚµÄÐÐ
* µÚ 2 ÁÐ = Ö»ÔÚ file-2 ÖдæÔÚµÄÐÐ
* µÚ 2 ÁÐ = Á½±ßÏàͬµÄÐÐ.
ÏÂÁÐÑ¡Ïî¿ÉÒÔ½ûÖ¹1Áлò¶àÁеÄÊä³ö.
* -1 ½ûÖ¹ÏÔʾµÚÒ»À¸ (ÒëÕß: ÔÚ File1 ÖеÄÐÐ)
* -2 ½ûÖ¹ÏÔʾµÚ¶þÀ¸ (ÒëÕß: ÔÚ File2 ÖеÄÐÐ)
* -3 ½ûÖ¹ÏÔʾµÚÈýÀ¸ (ÒëÕß: File1 ºÍ File2 ¹«¹²µÄÐÐ)
* -12 ½ûÖ¹µÚÒ»Áк͵ڶþÁÐ, (¾ÍÊÇ˵ѡÏî¿ÉÒÔ×éºÏ).
Ò»°ã¹¤¾ß
basename
´ÓÎļþÃûÖÐÈ¥µô·¾¶ÐÅÏ¢, Ö»´òÓ¡³öÎļþÃû. ½á¹¹ basename $0 ¿ÉÒÔÈýű¾ÖªµÀËü×Ô¼ºµÄ
Ãû×Ö, Ò²¾ÍÊÇ, Ëü±»µ÷ÓõÄÃû×Ö. ¿ÉÒÔÓÃÀ´ÏÔʾÓ÷¨ÐÅÏ¢, ±ÈÈçÈç¹ûÄãµ÷Óýű¾µÄʱºòȱ
ÉÙ²ÎÊý, ¿ÉÒÔʹÓÃÈçÏÂÓï¾ä:
1 echo "Usage: `basename $0` arg1 arg2 ... argn"
dirname
´Ó´ø·¾¶µÄÎļþÃûÖÐÈ¥µôÎļþÃû, Ö»´òÓ¡³ö·¾¶ÐÅÏ¢.
×¢Òâ: basename ºÍ dirname ¿ÉÒÔ²Ù×÷ÈÎÒâ×Ö·û´®. ²ÎÊý¿ÉÒÔ²»ÊÇÒ»¸öÕæÕý´æÔÚµÄÎļþ,
ÉõÖÁ¿ÉÒÔ²»ÊÇÒ»¸öÎļþÃû.(²Î¼û Example A-7).
Example 12-33 basename ºÍ dirname
################################Start Script####################################
###
1 #!/bin/bash
2
3 a=/home/bozo/daily-journal.txt
4
5 echo "Basename of /home/bozo/daily-journal.txt = `basename $a`"
6 echo "Dirname of /home/bozo/daily-journal.txt = `dirname $a`"
7 echo
8 echo "My own home is `basename ~/`." # `basename ~` also works.
9 echo "The home of my home is `dirname ~/`." # `dirname ~` also works.
10
11 exit 0
################################End Script######################################
###
split, csplit
½«Ò»¸öÎļþ·Ö¸îΪ¼¸¸öС¶ÎµÄ¹¤¾ß. ÕâЩÃüÁîͨ³£ÓÃÀ´½«´óµÄÎļþ·Ö¸î, ²¢±¸·Ýµ½ÈíÅÌÉÏ,
»òÕßÊÇΪÁËÇгɺÏÊʵijߴçÓà email ÉÏ´«.
csplit ¸ù¾Ý ÉÏÏÂÎÄ À´ÇиîÎļþ, ÇиîµÄλÖý«»á·¢ÉúÔÚģʽƥÅäµÄµØ·½.
sum, cksum, md5sum, sha1sum
ÕâЩ¶¼ÊÇÓÃÀ´²úÉú checksum µÄ¹¤¾ß. checksum µÄÄ¿µÄÊÇÓÃÀ´¼ìÑéÎļþµÄÍêÕûÐÔ, ÊǶÔÎÄ
¼þµÄÄÚÈݽøÐÐÊýѧ¼ÆËã¶øµÃµ½µÄ. ³öÓÚ°²È«Ä¿µÄÒ»¸ö½Å±¾¿ÉÄÜ»áÓÐÒ»¸ö checksum Áбí,
ÕâÑù¿ÉÒÔÈ·±£¹Ø¼üϵͳÎļþµÄÄÚÈݲ»»á±»Ð޸ĻòËð»µ. ¶ÔÓÚÐèÒª°²È«ÐÔµÄÓ¦ÓÃÀ´Ëµ, Ó¦¸Ã
ʹÓà md5sum (message digest 5 checksum) ÃüÁî, »òÕ߸üºÃµÄ¸üÐ嵀 sha1sum
(°²È« Hash Ëã·¨).
bash$ cksum /boot/vmlinuz
1670054224 804083 /boot/vmlinuz
bash$ echo -n "Top Secret" | cksum
3391003827 10

bash$ md5sum /boot/vmlinuz


0f43eccea8f09e0a0b2b5cf1dcf333ba /boot/vmlinuz
bash$ echo -n "Top Secret" | md5sum
8babc97a6f62a4649716f4df8d61728f -
×¢Òâ: cksum ÃüÁ»áÏÔʾĿ±êµÄ³ß´ç(×Ö½Ú), Ä¿±ê¿ÉÒÔʹÎļþ»ò stdout.
md5sum ºÍ sha1sum ÃüÁîÔÚËüÃÇÊÕµ½ stdout µÄÊäÈëʱºò, ÏÔʾһ¸ö dash .
Example 12-34 ¼ì²éÎļþÍêÕûÐÔ
################################Start Script####################################
###
1 #!/bin/bash
2 # file-integrity.sh: ¼ì²éÒ»¸ö¸ø¶¨Ä¿Â¼ÏµÄÎļþ
3 # ÊÇ·ñ±»¸Ä¶¯ÁË.
4
5 E_DIR_NOMATCH=70
6 E_BAD_DBFILE=71
7
8 dbfile=File_record.md5
9 # ´æ´¢¼Ç¼µÄÎļþÃû (Êý¾Ý¿âÎļþ).
10
11
12 set_up_database ()
13 {
14 echo ""$directory"" > "$dbfile"
15 # °ÑĿ¼Ãûдµ½ÎļþµÄµÚÒ»ÐÐ.
16 md5sum "$directory"/* >> "$dbfile"
17 # ÔÚÎļþÖи½ÉÏ md5 checksums ºÍ filenames.
18 }
19
20 check_database ()
21 {
22 local n=0
23 local filename
24 local checksum
25
26 # ------------------------------------------- #
27 # Õâ¸öÎļþ¼ì²éÆäʵÊDz»±ØÒªµÄ,
28 #+ µ«ÊÇÄÜ°²È«Ò»Ð©.
29
30 if [ ! -r "$dbfile" ]
31 then
32 echo "Unable to read checksum database file!"
33 exit $E_BAD_DBFILE
34 fi
35 # ------------------------------------------- #
36
37 while read record[n]
38 do
39
40 directory_checked="${record[0]}"
41 if [ "$directory_checked" != "$directory" ]
42 then
43 echo "Directories do not match up!"
44 # »»¸öĿ¼ÊÔÒ»ÏÂ.
45 exit $E_DIR_NOMATCH
46 fi
47
48 if [ "$n" -gt 0 ] # ²»ÊÇĿ¼Ãû.
49 then
50 filename[n]=$( echo ${record[$n]} | awk '{ print $2 }' )
51 # md5sum Ïòºóд¼Ç¼,
52 #+ ÏÈд checksum, È»ºóд filename.
53 checksum[n]=$( md5sum "${filename[n]}" )
54
55
56 if [ "${record[n]}" = "${checksum[n]}" ]
57 then
58 echo "${filename[n]} unchanged."
59
60 elif [ "`basename ${filename[n]}`" != "$dbfile" ]
61 # Ìø¹ýchecksum Êý¾Ý¿âÎļþ,
62 #+ ÒòΪÔÚÿ´Îµ÷Óýű¾Ëü¶¼»á±»ÐÞ¸Ä.
63 # ---
64 # Õâ²»ÐÒµÄÒâζ×ŵ±ÎÒÃÇÔÚ $PWDÖÐÔËÐÐÕâ¸ö½Å±¾
65 #+ ʱ, ÐÞ¸ÄÕâ¸ö checksum Êý
66 #+ ¾Ý¿âÎļþ½«²»»á±»¼ì²â³öÀ´.
67 # Á·Ï°: ÐÞ¸´Õâ¸öÎÊÌâ.
68 then
69 echo "${filename[n]} : CHECKSUM ERROR!"
70 # ÒòΪ×îºóµÄ¼ì²é, ÎļþÒѾ­±»ÐÞ¸Ä.
71 fi
72
73 fi
74
75
76
77 let "n+=1"
78 done <"$dbfile" # ´Ó checksum Êý¾Ý¿âÎļþÖжÁ.
79
80 }
81
82 # =================================================== #
83 # main ()
84
85 if [ -z "$1" ]
86 then
87 directory="$PWD" # Èç¹ûûÖƶ¨²ÎÊý,
88 else #+ ÄÇô¾ÍʹÓõ±Ç°µÄ¹¤×÷Ŀ¼.
89 directory="$1"
90 fi
91
92 clear # ÇåÆÁ.
93 echo " Running file integrity check on $directory"
94 echo
95
96 # ------------------------------------------------------------------ #
97 if [ ! -r "$dbfile" ] # ÊÇ·ñÐèÒª½¨Á¢Êý¾Ý¿âÎļþ?
98 then
99 echo "Setting up database file, \""$directory"/"$dbfile"\"."; echo
100 set_up_database
101 fi
102 # ------------------------------------------------------------------ #
103
104 check_database # µ÷ÓÃÖ÷Òª´¦Àíº¯Êý.
105
106 echo
107
108 # Äã¿ÉÄÜÏë°ÑÕâ¸ö½Å±¾µÄÊä³öÖض¨Ïòµ½ÎļþÖÐ,
109 #+ ÓÈÆäÔÚÕâ¸öĿ¼ÖÐÓкܶàÎļþµÄʱºò.
110
111 exit 0
112
113 # Èç¹ûÒª¶ÔÊýÁ¿·Ç³£¶àµÄÎļþ×öÍêÕûÐÔ¼ì²é,
114 #+ ¿ÉÒÔ¿¼ÂÇһϠ"Tripwire" °ü,
115 #+ http://sourceforge.net/projects/tripwire/.
116
################################End Script######################################
###
²Î¼û Example A-19 ºÍ Example 33-14 , ÕâÁ½¸öÀý×ÓչʾÁË md5sum ÃüÁîµÄÓ÷¨.
×¢Òâ: ÒѾ­ÓÐ 128-bit md5sum ±»ÆƽâµÄ±¨¸æÁË,ËùÒÔÏÖÔÚ¸ü°²È«µÄ 160-bit sha1sum ÊÇ
·Ç³£ÊÜ»¶Ó­µÄ, ²¢ÇÒÒѾ­±»¼ÓÈëµ½ checksum ¹¤¾ß°üÖÐ.
һЩ°²È«¹ËÎÊÈÏΪ¼´Ê¹ÊÇ sha1sum Ò²Êǻᱻй©µÄ. ËùÒÔ, ÏÂÒ»¸ö¹¤¾ßÊÇʲôÄØ?
-- 512-bit µÄ checksum ¹¤¾ß?
bash$ md5sum testfile
e181e2c8720c60522c4c4c981108e367 testfile

bash$ sha1sum testfile


5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile
shred
ÓÃËæ»ú×Ö·ûÌî³äÎļþ, ʹµÃÎļþÎÞ·¨»Ö¸´, ÕâÑù¾Í¿ÉÒÔ±£Ö¤Îļþ°²È«µÄ±»É¾³ý. Õâ¸öÃüÁî
µÄЧ¹ûÓë Example 12-55 Ò»Ñù, µ«ÊÇʹÓÃÕâ¸öÃüÁîÊÇÒ»ÖÖ¸üÓÅÑŸü³¹µ×µÄ·½·¨.
ÕâÊÇÒ»¸ö GNU fileutils.
×¢Òâ: ¼´Ê¹Ê¹ÓÃÁË shred ÃüÁî, ¸ß¼¶µÄ(forensic)±çÂÛ¼¼Êõ»¹ÊÇÄܹ»»Ö¸´ÎļþµÄÄÚÈÝ.
±àÂëºÍ½âÂë
uuencode
Õâ¸ö¹¤¾ßÓÃÀ´°Ñ¶þ½øÖÆÎļþ±àÂë³É ASCII ×Ö·û´®,Õâ¸ö¹¤¾ßÊÊÓÃÓÚ±àÂëe-mailÏûÏ¢Ìå,»òÕß
ÐÂÎÅ×éÏûÏ¢.
uudecode
Õâ¸ö¹¤¾ßÓÃÀ´°Ñ uuencode ºóµÄ ASCII ×Ö·û´®»Ö¸´Îª¶þ½øÖÆÎļþ.
Example 12-35 Uudecod ±àÂëºóµÄÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # ÔÚµ±Ç°Ä¿Â¼Ï uudecode ËùÓÐÓà uuencode ±àÂëµÄÎļþ.
3
4 lines=35 # ÔÊÐí¶ÁÍ·²¿µÄ 35 ÐÐ(·¶Î§ºÜ¿í).
5
6 for File in * # Test ËùÓÐ $PWD ϵÄÎļþ.
7 do
8 search1=`head -$lines $File | grep begin | wc -w`
9 search2=`tail -$lines $File | grep end | wc -w`
10 # Uuencode ¹ýµÄÎļþÔÚÎļþ¿ªÊ¼µÄµØ·½Óиö "begin",
11 #+ ÔÚÎļþ½áβµÄµØ·½Óиö "end".
12 if [ "$search1" -gt 0 ]
13 then
14 if [ "$search2" -gt 0 ]
15 then
16 echo "uudecoding - $File -"
17 uudecode $File
18 fi
19 fi
20 done
21
22 # СÐIJ»ÒªÈÃÕâ¸ö½Å±¾ÔËÐÐ×Ô¼º,
23 #+ ÒòΪËüÒ²»á°Ñ×ÔÉíÒ²ÈÏΪÊÇÒ»¸ö uuencoded Îļþ,
24 #+ ÕⶼÊÇÒòΪÕâ¸ö½Å±¾×ÔÉíÒ²°üº¬ "begin" ºÍ "end".
25
26 # Á·Ï°:
27 # -----
28 # ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃËü¿ÉÒÔ¼ì²éÒ»¸öÐÂÎÅ×éµÄÿ¸öÎļþ,
29 #+ ²¢ÇÒÈç¹ûÏÂÒ»¸öûÕҵĻ°¾ÍÌø¹ý.
30
31 exit 0
################################End Script######################################
###
×¢Òâ: fold -s ÃüÁîÔÚ´¦Àí´Ó Usenet ÐÂÎÅ×éÏÂÔØÏÂÀ´µÄ³¤µÄuudecode Îı¾ÏûÏ¢µÄʱºò¿É
ÄÜ»áÓÐÓÃ(¿ÉÄÜÔڹܵÀÖÐ).
mimencode, mmencode
mimencode ºÍ mmencode ÃüÁî´¦Àí¶àýÌå±àÂëµÄ email ¸½¼þ. ËäÈ» mail Óû§´úÀí
(±ÈÈç pine »ò kmail) ͨ³£Çé¿ö϶¼»á×Ô¶¯´¦Àí, µ«ÊÇÕâЩÌض¨µÄ¹¤¾ßÔÊÐí´ÓÃüÁîÐлò
shell½Å±¾ÖÐÀ´ÊÖ¶¯²Ù×÷ÕâЩ¸½¼þ.
crypt
Õâ¸ö¹¤¾ßÔø¾­ÊDZê×¼µÄ UNIX Îļþ¼ÓÃܹ¤¾ß. [3] Õþ¸®ÓÉÓÚÕþ²ßÉϵĶ¯»ú¹æ¶¨½ûÖ¹¼ÓÃÜÈí
¼þµÄÊä³ö, ÕâÑùµ¼ÖÂÁË crypt ÃüÁî´Ó UNIX ÊÀ½çÏûʧ, ²¢ÇÒÔÚ´ó¶àÊýµÄ Linux ·¢ÐаæÖÐ
ҲûÓÐÕâ¸öÃüÁî. ÐÒÔ˵ÄÊÇ, ³ÌÐòÔ±ÃÇÏë³öÁËһЩÌæ´úËüµÄ·½·¨, ÔÚÕâЩ·½·¨ÖÐÓÐ×÷Õß×Ô
¼ºµÄ cruft (²Î¼û Example A-4).
һЩÔÓÏ¾ß
mktemp
ʹÓÃÒ»¸ö"Ψһ"µÄÎļþÃûÀ´´´½¨Ò»¸ö ÁÙʱÎļþ [4] . Èç¹û²»´ø²ÎÊýµÄÔÚÃüÁîÐÐϵ÷ÓÃ
Õâ¸öÃüÁîʱ, ½«»áÔÚ /tmp Ŀ¼Ï²úÉúÒ»¸öÁ㳤¶ÈµÄÎļþ.
bash$ mktemp
/tmp/tmp.zzsvql3154
1 PREFIX=filename
2 tempfile=`mktemp $PREFIX.XXXXXX`
3 # ^^^^^^ ÔÚÕâ¸öÁÙʱµÄÎļþÃûÖÐ
4 #+ ÖÁÉÙÐèÒª6¸öռλ·û.
5 # Èç¹ûûÓÐÖ¸¶¨ÁÙʱÎļþµÄÎļþÃû,
6 #+ ÄÇôĬÈϾÍÊÇ "tmp.XXXXXXXXXX".
7
8 echo "tempfile name = $tempfile"
9 # tempfile name = filename.QA2ZpY
10 # »òÕßһЩÆäËûµÄÏàËƵÄÃû×Ö...
11
12 # ʹÓà 600 ΪÎļþȨÏÞ
13 #+ À´ÔÚµ±Ç°¹¤×÷Ŀ¼Ï´´½¨Ò»¸öÕâÑùµÄÎļþ.
14 # ÕâÑù¾Í²»ÐèÒª "umask 177" ÁË.
15 # µ«²»¹ÜÔõô˵, ÕâÒ²ÊÇÒ»¸öºÃµÄ±à³Ì·ç¸ñ.
make
build ºÍ compile ¶þ½øÖÆ°üµÄ¹¤¾ß. µ±Ô´Îļþ±»Ôö¼Ó»òÐÞ¸Äʱ¾Í»á´¥·¢Ò»Ð©²Ù×÷, Õâ¸ö¹¤
¾ßÓÃÀ´¿ØÖÆÕâЩ²Ù×÷.
make ÃüÁ»á¼ì²é Makefile, makefile ÊÇÎļþµÄÒÀÀµºÍ²Ù×÷Áбí.
install
ÌØÊâÄ¿µÄµÄÎļþ¿½±´ÃüÁî, Óë cp ÃüÁîÏàËÆ, µ«ÊǾßÓÐÉèÖÿ½±´ÎļþµÄȨÏÞºÍÊôÐÔµÄÄÜÁ¦.
Õâ¸öÃüÁî¿´ÆðÀ´ÊÇΪÁË°²×°Èí¼þ°üËù¶¨ÖƵÄ, ¶øÇÒ¾ÍÆä±¾Éí¶øÑÔ, Õâ¸öÃüÁî¾­³£³öÏÖÔÚ
Makefile ÖÐ(ÔÚ make install : ÇøÖÐ). ÔÚ°²×°½Å±¾ÖÐÒ²»á¿´µ½Õâ¸öÃüÁîµÄʹÓÃ.
dos2unix
Õâ¸ö¹¤¾ßÊÇÓÉ Benjamin Lin ºÍÆäͬʱàдµÄ, Ä¿µÄÊǽ« DOS ¸ñʽµÄÎı¾Îļþ
(ÒÔ CR-LF ΪÐнáÊø·û) ת»»Îª UNIX ¸ñʽ (ÒÔ LF ΪÐнáÊø·û), ·´¹ýÀ´Ò²Ò»Ñù.
ptx
ptx [targetfile] ÃüÁ»áÊä³öÄ¿±êÎļþµÄÐòÁиıäµÄË÷Òý(½»²æÒýÓÃÁбí). Èç¹û±ØÒªµÄ
»°, Õâ¸öÃüÁî¿ÉÒÔÔڹܵÀÖнøÐиüÉî²ã´ÎµÄ¹ýÂ˺͸ñʽ»¯.
more, less
·ÖÒ³ÏÔʾÎı¾Îļþ»ò stdout, Ò»´ÎÒ»ÆÁ.¿ÉÒÔÓÃÀ´¹ýÂË stdout µÄÊä³ö . . . »òÒ»¸ö½Å±¾
µÄÊä³ö.
more ÃüÁîµÄÒ»¸öÓÐȤµÄÓ¦ÓþÍÊDzâÊÔÒ»¸öÃüÁîÐòÁеÄÖ´ÐÐ, À´±ÜÃâ¿ÉÄÜ·¢ÉúµÄÔã¸âµÄ
½á¹û.
1 ls /home/bozo | awk '{print "rm -rf " $1}' | more
2 # ^^^^
3
4 # ¼ì²âϱß(ÔÖÄÑÐÔµÄ)ÃüÁîÐеÄЧ¹û:
5 # ls /home/bozo | awk '{print "rm -rf " $1}' | sh
6 # ÍÆÈë shell ÖÐÖ´ÐÐ . . .
×¢ÒâÊÂÏî:
[1] ÔÚÕâÀïËùÌÖÂÛµÄÒ»¸ö¹éµµÎļþ, Ö»²»¹ýÊÇ´æ´¢ÔÚÒ»¸öµ¥Ò»Î»ÖÃÉϵÄһЩÏà¹ØÎļþµÄ
¼¯ºÏ.
[2] tar czvf archive_name.tar.gz * ¿ÉÒÔ °üº¬µ±Ç°¹¤×÷Ŀ¼ÏµĵãÎļþ. ÕâÊÇÒ»¸ö
δÎĵµ»¯µÄ GNU tar µÄ"ÌØÕ÷".
[3] ÕâÊÇÒ»¸ö¶Ô³ÆµÄ¿éÃÜÂë, ¹ýÈ¥ÔøÔÚµ¥ÏµÍ³»ò±¾µØÍøÂçÖÐÓÃÀ´¼ÓÃÜÎļþ, ÓÃÀ´¶Ô¿¹
"public key" ÃÜÂëÀà, pgp ¾ÍÊÇÒ»¸öÖÚËùÖÜÖªµÄÀý×Ó.
[4] ʹÓà -d Ñ¡Ïî¿ÉÒÔ´´½¨Ò»¸öÁÙʱµÄĿ¼.

12.6 ͨѶÃüÁî
-------------
ϱßÃüÁîÖеÄij¼¸¸öÃüÁîÄã»áÔÚ "×·×ÙÀ¬»øÓʼþ" Á·Ï°ÖÐÕÒµ½ÆäÓ÷¨, ÓÃÀ´½øÐÐÍøÂçÊý
¾ÝµÄת»»ºÍ·ÖÎö.
ÐÅÏ¢Óëͳ¼Æ
host
ͨ¹ýÃû×Ö»ò IP µØÖ·À´ËÑË÷Ò»¸ö»¥ÁªÍøÖ÷»úµÄÐÅÏ¢, ʹÓà DNS.
bash$ host surfacemail.com
surfacemail.com. has address 202.92.42.236
ipcalc
ÏÔʾһ¸öÖ÷»ú IP ÐÅÏ¢. ʹÓà -h Ñ¡Ïî, ipcalc ½«»á×öÒ»¸ö DNS µÄ·´Ïò²éѯ, ͨ¹ý IP
µØÖ·ÕÒµ½Ö÷»ú(·þÎñÆ÷)Ãû.
bash$ ipcalc -h 202.92.42.236
HOSTNAME=surfacemail.com
nslookup
ͨ¹ý IP µØÖ·ÔÚÒ»¸öÖ÷»úÉÏ×öÒ»¸ö»¥ÁªÍøµÄ "Ãû×Ö·þÎñ²éѯ". ÊÂʵÉÏÕâÓë ipcalc -h »ò
dig -x µÈ¼Û. Õâ¸öÃüÁî¼È¿ÉÒÔ½»»¥ÔËÐÐÒ²¿ÉÒԷǽ»»¥ÔËÐÐ, »»¾ä»°Ëµ, ¾ÍÊÇÔڽű¾ÖÐÔË
ÐÐ.
nslookup ÃüÁî¾Ý˵ÒѾ­ÂýÂý±»"ºöÊÓ"ÁË, µ«ÊÇËü»¹ÊÇÓÐËüµÄÓô¦.
bash$ nslookup -sil 66.97.104.180
nslookup kuhleersparnis.ch
Server: 135.116.137.2
Address: 135.116.137.2#53
Non-authoritative answer:
Name: kuhleersparnis.ch
dig
ÓòÐÅÏ¢²éѯ. Óë nslookup ºÜÏàËÆ, dig ÔÚÒ»¸öÖ÷»úÉÏ×öÒ»¸ö»¥ÁªÍøµÄ "Ãû×Ö·þÎñ²éѯ".
Õâ¸öÃüÁî¼È¿ÉÒÔ½»»¥ÔËÐÐÒ²¿ÉÒԷǽ»»¥ÔËÐÐ, »»¾ä»°Ëµ, ¾ÍÊÇÔڽű¾ÖÐÔËÐÐ.
ϱßÊÇһЩ dig ÃüÁîÓÐȤµÄÑ¡Ïî, +time=N Ñ¡ÏîÓÃÀ´ÉèÖòéѯ³¬Ê±Îª N Ãë, +nofail
Ñ¡ÏîÓÃÀ´³ÖÐø²éѯ·þÎñÆ÷Ö±µ½ÊÕµ½Ò»¸öÏìÓ¦, -x Ñ¡Ïî»á×ö·´ÏòµØÖ·²éѯ.
±È½ÏϱßÕâ3¸öÃüÁîµÄÊä³ö, dig -x , ipcalc -h ºÍ nslookup.
bash$ dig -x 81.9.6.2
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 11649
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;2.6.9.81.in-addr.arpa. IN PTR
;; AUTHORITY SECTION:
6.9.81.in-addr.arpa. 3600 IN SOA ns.eltel.net. noc.eltel
.net.
2002031705 900 600 86400 3600
;; Query time: 537 msec
;; SERVER: 135.116.137.2#53(135.116.137.2)
;; WHEN: Wed Jun 26 08:35:24 2002
;; MSG SIZE rcvd: 91
Example 12-36 ²éÕÒÀÄÓõÄÁ¬½ÓÀ´±¨¸æÀ¬»øÓʼþ·¢ËÍÕß
################################Start Script####################################
###
1 #!/bin/bash
2 # spam-lookup.sh: ²éÕÒÀÄÓõÄÁ¬½ÓÀ´±¨¸æÀ¬»øÓʼþ·¢ËÍÕß.
3 # ¸Ðл Michael Zick.
4
5 # ¼ì²éÃüÁîÐвÎÊý.
6 ARGCOUNT=1
7 E_WRONGARGS=65
8 if [ $# -ne "$ARGCOUNT" ]
9 then
10 echo "Usage: `basename $0` domain-name"
11 exit $E_WRONGARGS
12 fi
13
14
15 dig +short $1.contacts.abuse.net -c in -t txt
16 # Ò²ÊÔÊÔ:
17 # dig +nssearch $1
18 # ¾¡Á¿ÕÒµ½ "¿ÉÐÅÀµµÄÃû×Ö·þÎñÆ÷" ²¢ÇÒÏÔʾ SOA ¼Ç¼.
19
20 # ϱßÕâ¾äÒ²¿ÉÒÔ:
21 # whois -h whois.abuse.net $1
22 # ^^ ^^^^^^^^^^^^^^^ Ö¸¶¨Ö÷»ú.
23 # ʹÓÃÕâ¸öÃüÁîÒ²¿ÉÒÔ²éÕÒ¶à¸öÀ¬»øÓʼþ·¢ËÍÕß, ±ÈÈç:"
24 # whois -h whois.abuse.net $spamdomain1 $spamdomain2 . . .
25
26
27 # Á·Ï°:
28 # -----
29 # À©Õ¹Õâ¸ö½Å±¾µÄ¹¦ÄÜ,
30 #+ ÈÃËü¿ÉÒÔ×Ô¶¯·¢ËÍ e-mail À´Í¨Öª
31 #+ ÐèÒª¶Ô´Ë¸ºÔðµÄ ISP µÄÁªÏµµØÖ·.
32 # °µÊ¾: ʹÓà "mail" ÃüÁî.
33
34 exit $?
35
36 # spam-lookup.sh chinatietong.com
37 # Ò»¸öÒÑÖªµÄÀ¬»øÓʼþÓò.(ÒëÕß: ÖйúÌúͨ. . .)
38
39 # "crnet_mgr@chinatietong.com"
40 # "crnet_tec@chinatietong.com"
41 # "postmaster@chinatietong.com"
42
43
44 # Èç¹ûÏëÕÒµ½Õâ¸ö½Å±¾µÄÒ»¸ö¸üÏ꾡µÄ°æ±¾,
45 #+ Çë·ÃÎÊ SpamViz µÄÖ÷Ò³, http://www.spamviz.net/index.html.
################################End Script######################################
###
Example 12-37 ·ÖÎöÒ»¸öÀ¬»øÓʼþÓò<rojy bug>
################################Start Script####################################
###
1 #! /bin/bash
2 # is-spammer.sh: ¼ø±ðÒ»¸öÀ¬»øÓʼþÓò
3
4 # $Id: is-spammer, v 1.4 2004/09/01 19:37:52 mszick Exp $
5 # ÉϱßÕâÐÐÊÇ RCS ID ÐÅÏ¢.
6 #
7 # ÕâÊǸ½¼þÖоèÏ׽ű¾ is_spammer.bash
8 #+ µÄÒ»¸ö¼òµ¥°æ±¾.
9
10 # is-spammer <domain.name>
11
12 # ʹÓÃÍⲿ³ÌÐò: 'dig'
13 # ²âÊÔ°æ±¾: 9.2.4rc5
14
15 # ʹÓú¯Êý.
16 # ʹÓà IFS À´·ÖÎö·ÖÅäÔÚÊý×éÖеÄ×Ö·û´®.
17 # ¼ì²é e-mail ºÚÃûµ¥.
18
19 # ʹÓÃÀ´×ÔÎı¾ÌåÖÐµÄ domain.name:
20 # http://www.good_stuff.spammer.biz/just_ignore_everything_else
21 # ^^^^^^^^^^^
22 # »òÕßʹÓÃÀ´×ÔÈÎÒâ e-mail µØÖ·µÄ domain.name:
23 # Really_Good_Offer@spammer.biz
24 #
25 # ²¢½«Æä×÷ΪÕâ¸ö½Å±¾µÄΨһ²ÎÊý.
26 #(Áí: ÄãµÄ Inet Á¬½ÓÓ¦¸Ã±£Ö¤Á¬½Ó)
27 #
28 # ÕâÑù, ÔÚÉϱßÁ½¸öʵÀýÖе÷ÓÃÕâ¸ö½Å±¾:
29 # is-spammer.sh spammer.biz
30
31
32 # Whitespace == :Space:Tab:Line Feed:Carriage Return:
33 WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D'
34
35 # No Whitespace == Line Feed:Carriage Return
36 No_WSP=$'\x0A'$'\x0D'
37
38 # Óò·Ö¸ô·ûΪµã·Ö10½øÖÆ ip µØÖ·
39 ADR_IFS=${No_WSP}'.'
40
41 # È¡µÃ dns Îı¾×ÊÔ´¼Ç¼.
42 # get_txt <error_code> <list_query>
43 get_txt() {
44
45 # ·ÖÎöÔÚ"."ÖзÖÅäµÄ $1.
46 local -a dns
47 IFS=$ADR_IFS
48 dns=( $1 )
49 IFS=$WSP_IFS
50 if [ "${dns[0]}" == '127' ]
51 then
52 # ²é¿´´Ë´¦ÊÇ·ñÓÐÔ­Òò.
53 echo $(dig +short $2 -t txt)
54 fi
55 }
56
57 # È¡µÃ dns µØÖ·×ÊÔ´¼Ç¼.
58 # chk_adr <rev_dns> <list_server>
59 chk_adr() {
60 local reply
61 local server
62 local reason
63
64 server=${1}${2}
65 reply=$( dig +short ${server} )
66
67 # ¼ÙÉèÓ¦´ð¿ÉÄÜÊÇÒ»¸ö´íÎóÂë . . .
68 if [ ${#reply} -gt 6 ]
69 then
70 reason=$(get_txt ${reply} ${server} )
71 reason=${reason:-${reply}}
72 fi
73 echo ${reason:-' not blacklisted.'}
74 }
75
76 # ÐèÒª´ÓÃû×ÖÖÐÈ¡µÃ IP µØÖ·.
77 echo 'Get address of: '$1
78 ip_adr=$(dig +short $1)
79 dns_reply=${ip_adr:-' no answer '}
80 echo ' Found address: '${dns_reply}
81
82 # Ò»¸ö¿ÉÓõÄÓ¦´ðÖÁÉÙÊÇ4¸öÊý×Ö¼ÓÉÏ3¸öµã.
83 if [ ${#ip_adr} -gt 6 ]
84 then
85 echo
86 declare query
87
88 # ·ÖÎöµãÖеķÖÅä.
89 declare -a dns
90 IFS=$ADR_IFS
91 dns=( ${ip_adr} )
92 IFS=$WSP_IFS
93
94 # Reorder octets into dns query order.
95 rev_dns="${dns[3]}"'.'"${dns[2]}"'.'"${dns[1]}"'.'"${dns[0]}"'.'
96
97 # ²Î¼û: http://www.spamhaus.org (Conservative, well maintained)
98 echo -n 'spamhaus.org says: '
99 echo $(chk_adr ${rev_dns} 'sbl-xbl.spamhaus.org')
100
101 # ²Î¼û: http://ordb.org (Open mail relays)
102 echo -n ' ordb.org says: '
103 echo $(chk_adr ${rev_dns} 'relays.ordb.org')
104
105 # ²Î¼û: http://www.spamcop.net/ (Äã¿ÉÒÔÔÚÕâÀﱨ¸æ spammer)
106 echo -n ' spamcop.net says: '
107 echo $(chk_adr ${rev_dns} 'bl.spamcop.net')
108
109 # # # ÆäËûµÄºÚÃûµ¥²Ù×÷ # # #
110
111 # ²Î¼û: http://cbl.abuseat.org.
112 echo -n ' abuseat.org says: '
113 echo $(chk_adr ${rev_dns} 'cbl.abuseat.org')
114
115 # ²Î¼û: http://dsbl.org/usage (Various mail relays)
116 echo
117 echo 'Distributed Server Listings'
118 echo -n ' list.dsbl.org says: '
119 echo $(chk_adr ${rev_dns} 'list.dsbl.org')
120
121 echo -n ' multihop.dsbl.org says: '
122 echo $(chk_adr ${rev_dns} 'multihop.dsbl.org')
123
124 echo -n 'unconfirmed.dsbl.org says: '
125 echo $(chk_adr ${rev_dns} 'unconfirmed.dsbl.org')
126
127 else
128 echo
129 echo 'Could not use that address.'
130 fi
131
132 exit 0
133
134 # Á·Ï°:
135 # -----
136
137 # 1) ¼ì²é½Å±¾µÄ²ÎÊý,
138 # ²¢ÇÒÈç¹û±ØÒªµÄ»°Ê¹ÓúÏÊʵĴíÎóÏûÏ¢Í˳ö.
139
140 # 2) ¼ì²éµ÷ÓÃÕâ¸ö½Å±¾µÄʱºòÊÇ·ñÔÚÏß,
141 # ²¢ÇÒÈç¹û±ØÒªµÄ»°Ê¹ÓúÏÊʵĴíÎóÏûÏ¢Í˳ö.
142
143 # 3) Substitute generic variables for "hard-coded" BHL domains.
144
145 # 4) ͨ¹ý¶Ô 'dig' ÃüÁîʹÓà "+time=" Ñ¡Ïî
146 À´¸øÕâ¸ö½Å±¾ÉèÖÃÒ»¸öÔÝÍ£.
################################End Script######################################
###
Ïë»ñµÃ±ÈÉϱßÕâ¸ö½Å±¾¸üÏêϸµÄ°æ±¾, ²Î¼û Example A-27.
traceroute
¸ú×Ù°ü·¢Ë͵½Ô¶¶ËÖ÷»ú¹ý³ÌÖеÄ·ÓÉÐÅÏ¢. Õâ¸öÃüÁîÔÚ LAN, WAN, »òÕßÔÚ Internet É϶¼
¿ÉÒÔÕý³£¹¤×÷. Ô¶¶ËÖ÷»ú¿ÉÒÔͨ¹ý IP µØÖ·À´Ö¸¶¨. Õâ¸öÃüÁîµÄÊä³öÒ²¿ÉÒÔͨ¹ý¹ÜµÀÖеÄ
grep »ò sed ÃüÁîÀ´¹ýÂË.
bash$ traceroute 81.9.6.2
traceroute to 81.9.6.2 (81.9.6.2), 30 hops max, 38 byte packets
1 tc43.xjbnnbrb.com (136.30.178.8) 191.303 ms 179.400 ms 179.767 ms
2 or0.xjbnnbrb.com (136.30.178.1) 179.536 ms 179.534 ms 169.685 ms
3 192.168.11.101 (192.168.11.101) 189.471 ms 189.556 ms *
...
ping
¹ã²¥Ò»¸ö "ICMP ECHO_REQUEST" °üµ½ÆäËûÖ÷»úÉÏ, ¼È¿ÉÒÔÊDZ¾µØÍøÂçÒ²¿ÉÒÔʹԶ¶ËÍøÂç.
ÕâÊÇÒ»¸ö²âÊÔÍøÂçÁ¬½ÓµÄÕï¶Ï¹¤¾ß, Ó¦¸ÃСÐÄʹÓÃ.
Ò»¸ö³É¹¦µÄ ping ·µ»ØµÄ Í˳öÂë Ϊ 0. ¿ÉÒÔÓÃÔڽű¾µÄ²âÊÔÓï¾äÖÐ.
bash$ ping localhost
PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of
data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 tim
e=709 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 tim
e=286 usec
--- localhost.localdomain ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/mdev = 0.286/0.497/0.709/0.212 ms
whois
Ö´ÐÐDNS (ÓòÃûϵͳ) ²éѯlookup. -h Ñ¡ÏîÔÊÐíÖ¸¶¨ÐèÒª²éѯµÄÌض¨µÄ whois ·þÎñÆ÷.
²Î¼û Example 4-6 ºÍ Example 12-36.
finger
È¡µÃÍøÂçÉϵÄÓû§ÐÅÏ¢. ÁíÍâÕâ¸öÃüÁî¿ÉÒÔÏÔʾһ¸öÓû§µÄ~/.plan, ~/.project, ºÍ
~/.forward Îļþ, Èç¹û´æÔڵĻ°.
bash$ finger
Login Name Tty Idle Login Time Office Office Pho
ne
bozo Bozo Bozeman tty1 8 Jun 25 16:59
bozo Bozo Bozeman ttyp0 Jun 25 16:59
bozo Bozo Bozeman ttyp1 Jun 25 17:07

bash$ finger bozo


Login: bozo Name: Bozo Bozeman
Directory: /home/bozo Shell: /bin/bash
Office: 2355 Clown St., 543-1234
On since Fri Aug 31 20:13 (MST) on tty1 1 hour 38 minutes idle
On since Fri Aug 31 20:13 (MST) on pts/0 12 seconds idle
On since Fri Aug 31 20:13 (MST) on pts/1
On since Fri Aug 31 20:31 (MST) on pts/2 1 hour 16 minutes idle
No mail.
No Plan.
´¦ÓÚ°²È«ÉϵĿ¼ÂÇ, Ðí¶àÍøÂ綼½ûÓÃÁË finger ÒÔ¼°ºÍËüÏà¹ØµÄÓÄÁé½ø³Ì. [1]
chfn
ÐÞ¸Ä finger ÃüÁîËùÏÔʾ³öÀ´µÄÓû§ÐÅÏ¢.
vrfy
ÑéÖ¤Ò»¸ö»¥ÁªÍøµÄ e-mail µØÖ·.
Ô¶¶ËÖ÷»ú½ÓÈë
sx, rx
sx ºÍ rx ÃüÁîʹÓà xmodem ЭÒé, ÉèÖ÷þÎñÀ´ÏòÔ¶¶ËÖ÷»ú´«ÊäÎļþºÍ½ÓÊÕÎļþ. ÕâЩ¶¼
ÊÇͨѶ°²×°°üµÄÒ»°ã²¿·Ö, ±ÈÈç minicom.
sz, rz
sz ºÍ rz ÃüÁîʹÓà zmodem ЭÒé, ÉèÖ÷þÎñÀ´ÏòÔ¶¶ËÖ÷»ú´«ÊäÎļþºÍ½ÓÊÕÎļþ. zmodem
ЭÒéÔÚijЩ·½Ãæ±È xmodemÇ¿, ±ÈÈçʹÓøü¿ìµÄµÄ´«Ê䲨ÌØÂÊ, ²¢ÇÒ¿ÉÒÔ¶ÔÖжϵÄÎļþ½ø
ÐÐÐø´«.Óë sx Ò»Ñù rx, ÕâЩ¶¼ÊÇͨѶ°²×°°üµÄÒ»°ã²¿·Ö.
ftp
ÏòÔ¶¶Ë·þÎñÆ÷ÉÏ´«»òÏÂÔصŤ¾ßºÍЭÒé. Ò»¸öftp»á»°¿ÉÒÔдµ½½Å±¾ÖÐ×Ô¶¯ÔËÐÐ. (¼û
Example 17-6, Example A-4, ºÍ Example A-13).
uucp, uux, cu
uucp: UNIX µ½ UNIX ¿½±´. ÕâÊÇÒ»¸öͨѶ°²×°°ü, Ä¿µÄÊÇΪÁËÔÚ UNIX ·þÎñÆ÷Ö®¼ä´«Êä
Îļþ. ʹÓà shell ½Å±¾À´´¦Àí uucp ÃüÁîÐòÁÐÊÇÒ»ÖÖÓÐЧµÄ·½·¨.
ÒòΪ»¥ÁªÍøºÍµç×ÓÓʼþµÄ³öÏÖ, uucp ÏÖÔÚ¿´ÆðÀ´ÒѾ­ºÜÂäÎéÁË, µ«ÊÇÕâ¸öÃüÁîÔÚ»¥ÁªÍø
Á¬½Ó²»¿ÉÓûòÕß²»ÊʺÏʹÓõĵط½, Õâ¸öÃüÁÊÇ¿ÉÒÔÍêÃÀµÄÔËÐÐ. uucp µÄÓŵã¾ÍÊÇËü
µÄÈÝ´íÐÔ, ¼´Ê¹ÓÐÒ»¸ö·þÎñ½«¿½±´²Ù×÷ÖжÏÁË, ÄÇôµ±Á¬½Ó»Ö¸´µÄʱºò, Õâ¸öÃüÁÊÇ
¿ÉÒÔÔÚÖжϵĵط½Ðø´«.
---
uux: UNIX µ½ UNIX Ö´ÐÐ. ÔÚÔ¶¶ËϵͳÉÏÖ´ÐÐÒ»¸öÃüÁî.Õâ¸öÃüÁîÊÇ uucp °üµÄÒ»²¿·Ö.
---
cu: Call Up Ò»¸öÔ¶¶Ëϵͳ²¢ÇÒ×÷Ϊһ¸ö¼òµ¥Öն˽øÐÐÁ¬½Ó. ÕâÊÇÒ»¸ö telnet µÄËõ¼õ
°æ±¾. Õâ¸öÃüÁîÊÇ uucp °üµÄÒ»²¿·Ö.
telnet
Á¬½ÓÔ¶¶ËÖ÷»úµÄ¹¤¾ßºÍЭÒé.
×¢Òâ:telnet ЭÒé±¾Éí°üº¬°²È«Â©¶´, Òò´ËÎÒÃÇÓ¦¸ÃÊʵ±µÄ±ÜÃâʹÓÃ.
wget
wget ¹¤¾ßʹÓ÷ǽ»»¥µÄÐÎʽ´Ó web »ò ftp Õ¾µãÉÏÈ¡µÃ»òÏÂÔØÎļþ. Ôڽű¾ÖÐʹÓÃÕýºÃ.
1 wget -p http://www.xyz23.com/file01.html
2 # The -p or --page-requisite Ñ¡Ï»áʹµÃ wget È¡µÃÏÔʾָ¶¨Ò³Ê±
3 #+ ËùÐèÒªµÄËùÓÐÎļþ.(ÒëÕß: ±ÈÈçÄÚǶͼƬºÍÑùʽ±íµÈ).
4
5 wget -r ftp://ftp.xyz24.net/~bozo/project_files/ -O $SAVEFILE
6 # -r Ñ¡Ï»áµÝ¹éµÄ´ÓÖ¸¶¨Õ¾µã
7 #+ ÉÏÏÂÔØËùÓÐÁ¬½Ó.
Example 12-38 »ñµÃÒ»·Ý¹ÉƱ±¨¼Û
################################Start Script####################################
###
1 #!/bin/bash
2 # quote-fetch.sh: ÏÂÔØÒ»·Ý¹ÉƱ±¨¼Û.
3
4
5 E_NOPARAMS=66
6
7 if [ -z "$1" ] # ±ØÐëÖ¸¶¨ÐèÒª»ñÈ¡µÄ¹ÉƱ(´úºÅ).
8 then echo "Usage: `basename $0` stock-symbol"
9 exit $E_NOPARAMS
10 fi
11
12 stock_symbol=$1
13
14 file_suffix=.html
15 # »ñµÃÒ»¸ö HTML Îļþ, ËùÒÔÒªÕýÈ·ÃüÃûËü.
16 URL='http://finance.yahoo.com/q?s='
17 # Yahoo ½ðÈÚ°å¿é, ºó׺ÊǹÉƱ²éѯ.
18
19 # -----------------------------------------------------------
20 wget -O ${stock_symbol}${file_suffix} "${URL}${stock_symbol}"
21 # -----------------------------------------------------------
22
23
24 # ÔÚ http://search.yahoo.com ÉϲéѯÏà¹Ø²ÄÁÏ:
25 # -----------------------------------------------------------
26 # URL="http://search.yahoo.com/search?fr=ush-news&p=${query}"
27 # wget -O "$savefilename" "${URL}"
28 # -----------------------------------------------------------
29 # ±£´æÏà¹Ø URL µÄÁбí.
30
31 exit $?
32
33 # Á·Ï°:
34 # -----
35 #
36 # 1) Ìí¼ÓÒ»¸ö²âÊÔÀ´ÑéÖ¤Óû§ÕýÔÚÏß.
37 # (°µÊ¾: ¶Ô "ppp" »ò "connect" À´·ÖÎö 'ps -ax' µÄÊä³ö.
38 #
39 # 2) ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃÕâ¸ö½Å±¾¾ßÓлñµÃ±¾µØÌìÆøÔ¤±¨µÄÄÜÁ¦,
40 #+ ½«Óû§µÄ zip code ×÷Ϊ²ÎÊý.
################################End Script######################################
###
²Î¼û Example A-29 ºÍ Example A-30.
lynx
lynx ÊÇÒ»¸öÍøÒ³ä¯ÀÀÆ÷, Ò²ÊÇÒ»¸öÎļþä¯ÀÀÆ÷. Ëü¿ÉÒÔ(ͨ¹ýʹÓà -dump Ñ¡Ïî)Ôڽű¾ÖÐ
ʹÓÃ. ËüµÄ×÷ÓÃÊÇ¿ÉÒÔ´Ó Web »ò ftp Õ¾µãÉϷǽ»»¥µÄ»ñµÃÎļþ.
1 lynx -dump http://www.xyz23.com/file01.html >$SAVEFILE
ʹÓà -traversal Ñ¡Ïî, lynx ½«´Ó²ÎÊýÖÐÖ¸¶¨µÄ HTTP URL ¿ªÊ¼, ±éÀúÖ¸¶¨·þÎñÆ÷ÉϵÄ
ËùÓÐÁ´½Ó. Èç¹ûÓë -crawl Ñ¡ÏîÒ»ÆðÓõĻ°, ½«»á°Ñÿ¸öÊä³öµÄÒ³ÃæÎı¾¶¼·Åµ½Ò»¸ö log
ÎļþÖÐ.
rlogin
Ô¶¶ËµÇ½, ÔÚÔ¶¶ËµÄÖ÷»úÉÏ¿ªÆôÒ»¸ö»á»°. Õâ¸öÃüÁî´æÔÚ°²È«Òþ»¼, ËùÒÔҪʹÓà ssh À´
´úÌæ.
rsh
Ô¶¶Ë shell, ÔÚÔ¶¶ËµÄÖ÷»úÉÏÖ´ÐÐÃüÁî. Õâ¸öÃüÁî´æÔÚ°²È«Òþ»¼, ËùÒÔҪʹÓà ssh À´´ú
Ìæ.
rcp
Ô¶¶Ë¿½±´, ÔÚÍøÂçÉϵIJ»Í¬Ö÷»ú¼ä¿½±´Îļþ.
rsync
Ô¶¶Ëͬ²½, ÔÚÍøÂçÉϵIJ»Í¬Ö÷»ú¼ä(ͬ²½)¸üÐÂÎļþ.
bash$ rsync -a ~/sourcedir/*txt /node1/subdirectory/
Example 12-39 ¸üРFedora 4 <rojy bug>
################################Start Script####################################
###
1 #!/bin/bash
2 # fc4upd.sh
3
4 # ½Å±¾×÷Õß: Frank Wang.
5 # ±¾Êé×÷Õß×÷ÁËÉÙÁ¿ÐÞ¸Ä.
6 # ÊÚȨÔÚ±¾ÊéÖÐʹÓÃ.
7
8
9 # ʹÓà rsync ÃüÁî´Ó¾µÏñÕ¾µãÉÏÏÂÔØ Fedora 4 µÄ¸üÐÂ.
10 # ΪÁ˽ÚÊ¡¿Õ¼ä, Èç¹ûÓжà¸ö°æ±¾´æÔڵĻ°,
11 #+ Ö»ÏÂÔØ×îеİü.
12
13 URL=rsync://distro.ibiblio.org/fedora-linux-core/updates/
14 # URL=rsync://ftp.kddilabs.jp/fedora/core/updates/
15 # URL=rsync://rsync.planetmirror.com/fedora-linux-core/updates/
16
17 DEST=${1:-/var/www/html/fedora/updates/}
18 LOG=/tmp/repo-update-$(/bin/date +%Y-%m-%d).txt
19 PID_FILE=/var/run/${0##*/}.pid
20
21 E_RETURN=65 # ijЩÒâÏë²»µ½µÄ´íÎó.
22
23
24 # Ò»°á rsync Ñ¡Ïî
25 # -r: µÝ¹éÏÂÔØ
26 # -t: ±£´æʱ¼ä
27 # -v: verbose
28
29 OPTS="-rtv --delete-excluded --delete-after --partial"
30
31 # rsync include ģʽ
32 # Leading slash causes absolute path name match.
33 INCLUDE=(
34 "/4/i386/kde-i18n-Chinese*"
35 # ^ ^
36 # Ë«ÒýºÅÊDZØÐëµÄ, ÓÃÀ´·ÀÖ¹file globbing.
37 )
38
39
40 # rsync exclude ģʽ
41 # ʹÓà "#" ÁÙʱעÊ͵ôһЩ²»ÐèÒªµÄ°ü.
42 EXCLUDE=(
43 /1
44 /2
45 /3
46 /testing
47 /4/SRPMS
48 /4/ppc
49 /4/x86_64
50 /4/i386/debug
51 "/4/i386/kde-i18n-*"
52 "/4/i386/openoffice.org-langpack-*"
53 "/4/i386/*i586.rpm"
54 "/4/i386/GFS-*"
55 "/4/i386/cman-*"
56 "/4/i386/dlm-*"
57 "/4/i386/gnbd-*"
58 "/4/i386/kernel-smp*"
59 # "/4/i386/kernel-xen*"
60 # "/4/i386/xen-*"
61 )
62
63
64 init () {
65 # ÈùܵÀÃüÁî·µ»Ø¿ÉÄÜµÄ rsync ´íÎó, ±ÈÈç, ÍøÂçÑÓʱ(stalled network).
66 set -o pipefail
67
68 TMP=${TMPDIR:-/tmp}/${0##*/}.$$ # ±£´æ¾«Á¶µÄÏÂÔØÁбí.
69 trap "{
70 rm -f $TMP 2>/dev/null
71 }" EXIT # ɾ³ý´æÔÚµÄÁÙʱÎļþ.
72 }
73
74
75 check_pid () {
76 # ¼ì²é½ø³ÌÊÇ·ñ´æÔÚ.
77 if [ -s "$PID_FILE" ]; then
78 echo "PID file exists. Checking ..."
79 PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE)
80 if /bin/ps --pid $PID &>/dev/null; then
81 echo "Process $PID found. ${0##*/} seems to be running!"
82 /usr/bin/logger -t ${0##*/} \
83 "Process $PID found. ${0##*/} seems to be running!"
84 exit $E_RETURN
85 fi
86 echo "Process $PID not found. Start new process . . ."
87 fi
88 }
89
90
91 # ¸ù¾ÝÉϱߵÄģʽ,
92 #+ ÉèÖÃÕû¸öÎļþµÄ¸üз¶Î§, ´Ó root »ò $URL ¿ªÊ¼.
93 set_range () {
94 include=
95 exclude=
96 for p in "${INCLUDE[@]}"; do
97 include="$include --include \"$p\""
98 done
99
100 for p in "${EXCLUDE[@]}"; do
101 exclude="$exclude --exclude \"$p\""
102 done
103 }
104
105
106 # »ñµÃ²¢ÌáÁ¶ rsync ¸üÐÂÁбí.
107 get_list () {
108 echo $$ > $PID_FILE || {
109 echo "Can't write to pid file $PID_FILE"
110 exit $E_RETURN
111 }
112
113 echo -n "Retrieving and refining update list . . ."
114
115 # »ñµÃÁбí -- ΪÁË×÷Ϊµ¥¸öÃüÁîÀ´ÔËÐÐ rsync ÐèÒª 'eval'.
116 # $3 ºÍ $4 ÊÇÎļþ´´½¨µÄÈÕÆÚºÍʱ¼ä.
117 # $5 ÊÇÍêÕûµÄ°üÃû×Ö.
118 previous=
119 pre_file=
120 pre_date=0
121 eval /bin/nice /usr/bin/rsync \
122 -r $include $exclude $URL | \
123 egrep '^dr.x|^-r' | \
124 awk '{print $3, $4, $5}' | \
125 sort -k3 | \
126 { while read line; do
127 # »ñµÃÕâ¶ÎÔËÐеÄÃëÊý, ¹ýÂ˵ô²»Óõİü.
128 cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s)
129 # echo $cur_date
130
131 # È¡µÃÎļþÃû.
132 cur_file=$(echo $line | awk '{print $3}')
133 # echo $cur_file
134
135 # Èç¹û¿ÉÄܵĻ°, ´ÓÎļþÃûÖÐÈ¡µÃ rpm µÄ°üÃû×Ö.
136 if [[ $cur_file == *rpm ]]; then
137 pkg_name=$(echo $cur_file | sed -r -e \
138 's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/')
139 else
140 pkg_name=
141 fi
142 # echo $pkg_name
143
144 if [ -z "$pkg_name" ]; then # Èç¹û²»ÊÇÒ»¸ö rpm Îļþ,
145 echo $cur_file >> $TMP #+ È»ºóÌí¼Óµ½ÏÂÔØÁбíÀï.
146 elif [ "$pkg_name" != "$previous" ]; then # ·¢ÏÖÒ»¸öаü.
147 echo $pre_file >> $TMP # Êä³ö×îеÄÎļþ.
148 previous=$pkg_name # ±£´æµ±Ç°×´Ì¬.
149 pre_date=$cur_date
150 pre_file=$cur_file
151 elif [ "$cur_date" -gt "$pre_date" ]; then # Èç¹ûÊÇÏàͬµÄ°ü, µ«ÊǸüÐÂһЩ,
152 pre_date=$cur_date #+ ÄÇô¾Í¸üÐÂ×îеÄ.
153 pre_file=$cur_file
154 fi
155 done
156 echo $pre_file >> $TMP # TMP ÏÖÔÚ°üº¬ËùÓÐ
157 #+ ÌáÁ¶¹ýµÄÁбí.
158 # echo "subshell=$BASH_SUBSHELL"
159
160 } # ÕâÀïµÄ´òÀ¨ºÅÊÇΪÁËÈÃ×îºóÕâ¾ä"echo $pre_file >> $TMP"
161 # Ò²ÄÜÓëÕû¸öÑ­»·Ò»Æð·Åµ½Í¬Ò»¸ö×Ó shell ( 1 )ÖÐ.
162
163 RET=$? # È¡µÃ¹ÜµÀÃüÁîµÄ·µ»ØÂë.
164
165 [ "$RET" -ne 0 ] && {
166 echo "List retrieving failed with code $RET"
167 exit $E_RETURN
168 }
169
170 echo "done"; echo
171 }
172
173 # ÕæÕýµÄ rsync µÄÏÂÔز¿·Ö.
174 get_file () {
175
176 echo "Downloading..."
177 /bin/nice /usr/bin/rsync \
178 $OPTS \
179 --filter "merge,+/ $TMP" \
180 --exclude '*' \
181 $URL $DEST \
182 | /usr/bin/tee $LOG
183
184 RET=$?
185
186 # --filter merge,+/ is crucial for the intention.
187 # + modifier means include and / means absolute path.
188 # Then sorted list in $TMP will contain ascending dir name and
189 #+ prevent the following --exclude '*' from "shortcutting the circui
t."
190
191 echo "Done"
192
193 rm -f $PID_FILE 2>/dev/null
194
195 return $RET
196 }
197
198 # -------
199 # Main
200 init
201 check_pid
202 set_range
203 get_list
204 get_file
205 RET=$?
206 # -------
207
208 if [ "$RET" -eq 0 ]; then
209 /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully."
210 else
211 /usr/bin/logger -t ${0##*/} "Fedora update mirrored with failure code: $
RET"
212 fi
213
214 exit $RET
################################End Script######################################
###
ʹÓà rcp, rsync, ºÍÆäËûһЩÓа²È«ÎÊÌâµÄÀàËƹ¤¾ß, ²¢½«ÕâЩ¹¤¾ßÓÃÔÚ shell ½Å±¾ÖÐ
ÊDz»Ã÷ÖǵÄ. Ó¦¸Ã¿¼ÂÇʹÓà ssh, scp, »òÕßÒ»¸ö expect ½Å±¾À´´úÌæÕâЩ²»°²È«µÄ¹¤¾ß.
ssh
°²È« shell, µÇ½Զ¶ËÖ÷»ú²¢ÔÚÆäÉÏÔËÐÐÃüÁî. Õâ¸ö¹¤¾ß¾ßÓÐÉí·ÝÈÏÖ¤ºÍ¼ÓÃܵŦÄÜ, ¿É
ÒÔ°²È«µÄÌæ»» telnet, rlogin, rcp, ºÍ rsh µÈ¹¤¾ß. ²Î¼û manÒ³ À´»ñÈ¡ÏêϸÐÅÏ¢.
Example 12-40 ʹÓà ssh
################################Start Script####################################
###
1 #!/bin/bash
2 # remote.bash: ʹÓà ssh.
3
4 # Õâ¸öÀý×ÓÊÇ Michael Zick ±àдµÄ.
5 # ÊÚȨʹÓÃ.
6
7
8 # ¼ÙÉè:
9 # -----
10 # fd-2(ÎļþÃèÊö·û2) ²¢Ã»Óб»Å×Æú ( '2>/dev/null' ).
11 # ssh/sshd ¼ÙÉè stderr ('2') ½«»á±»ÏÔʾ¸øÓû§.
12 #
13 # sshd ÕýÔËÐÐÔÚÄãµÄ»úÆ÷ÉÏ.
14 # ¶ÔÓÚ´ó¶àÊý '±ê×¼' µÄ·¢Ðаæ, ÊÇÓ¦¸ÃÓеÄ,
15 #+ ²¢ÇÒûÓÐһЩϡÆæ¹Å¹ÖµÄ ssh-keygen.
16
17 # ÔÚÄãµÄ»úÆ÷ÉÏ´ÓÃüÁîÐÐÖÐÊÔһϠssh:
18 #
19 # $ ssh $HOSTNAME
20 # ²»Í¬ÌØÊâµÄ×¼±¸, Ä㽫±»ÒªÇóÊäÈëÄãµÄÃÜÂë.
21 # ÊäÈëÃÜÂë
22 # Íê³Éºó, $ exit
23 #
24 # ºÃʹÁËô? Èç¹ûºÃʹÁË, Äã¿ÉÒÔ×öºÃ×¼±¸À´»ñÈ¡¸ü¶àµÄÀÖȤÁË.
25
26 # ÔÚÄãµÄ»úÆ÷ÉÏÓà 'root'Éí·ÝÀ´ÊÔÊÔ ssh:
27 #
28 # $ ssh -l root $HOSTNAME
29 # µ±Ñ¯ÎÊÃÜÂëʱ, ÊäÈë root µÄÃÜÂë, ±ðÊäÈëÄãµÄÃÜÂë.
30 # Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain
31 # Íê³Éºó¼üÈë 'exit'.
32
33 # ÉϱߵĶ¯×÷½«»á¸øÄãÒ»¸ö½»»¥µÄshell.
34 # ÔÚ 'single command' ģʽϽ¨Á¢ sshd ÊÇ¿ÉÄܵÄ, <rojy bug>
35 #+ ²»¹ýÕâÒѾ­³¬³ö±¾ÀýµÄ·¶Î§ÁË.
36 # ΨһÐèҪעÒâµÄÊÂÇéÊÇÏÂÃ涼¿ÉÒÔ¹¤×÷ÔÚ
37 #+ 'single command' ģʽ.
38
39
40 # Ò»¸ö»ù±¾µÄдÊä³ö(±¾µØ)ÃüÁî.
41
42 ls -l
43
44 # ÏÖÔÚÔÚÔ¶¶Ë»úÆ÷ÉÏʹÓÃͬÑùµÄ»ù±¾ÃüÁî.
45 # ʹÓÃÒ»Ìײ»Í¬µÄ 'USERNAME' ºÍ 'HOSTNAME' :
46 USER=${USERNAME:-$(whoami)}
47 HOST=${HOSTNAME:-$(hostname)}
48
49 # ÏÖÔÚÔÚÔ¶¶ËÖ÷»úÉÏÔËÐÐÉϱߵÄÃüÁîÐÐÃüÁî,
50 #+ µ±È», ËùÓеĴ«Ê䶼±»¼ÓÃÜÁË.
51
52 ssh -l ${USER} ${HOST} " ls -l "
53
54 # ÆÚÍûµÄ½á¹û¾ÍÊÇÔÚÔ¶¶ËÖ÷»úÉÏÁгöÄãµÄ
55 #+ username Ö÷Ŀ¼µÄËùÓÐÎļþ.
56 # Èç¹ûÏë¿´µã²»Ò»ÑùµÄ, ÄǾÍ
57 #+ ÔÚ±ðµÄµØ·½ÔËÐÐÕâ¸ö½Å±¾, ±ðÔÙÄãµÄÖ÷Ŀ¼ÉÏÔËÐÐÕâ¸ö½Å±¾.
58
59 # »»¾ä»°Ëµ, Bash ÃüÁîÒѾ­×÷Ϊһ¸öÒýÓÃÐÐ
60 #+ ±»´«µÝµ½Ô¶¶ËµÄshell ÖÐÁË,ÕâÑù¾Í¿ÉÒÔÔÚÔ¶¶ËµÄ»úÆ÷ÉÏÔËÐÐËüÁË.
61 # ÔÚÕâÖÖÇé¿öÏÂ, sshd ´ú±íÄãÔËÐÐÁË ' bash -c "ls -l" '.
62
63 # ¶ÔÓÚÿ¸öÃüÁîÐÐÈç¹ûÏë²»ÊäÈëÃÜÂëµÄ»°,
64 #+ ¶ÔÓÚÕâÖÖÀàËƵÄÒéÌâ, ¿ÉÒÔ²ÎÔÄ
65 #+ man ssh
66 #+ man ssh-keygen
67 #+ man sshd_config.
68
69 exit 0
################################End Script######################################
###
×¢Òâ: ÔÚÑ­»·ÖÐ, ssh ¿ÉÄÜ»áÒýÆðÒâÏë²»µ½µÄÒì³£ÐÐΪ. ¸ù¾Ýcomp.unix ÉϵÄshellÎĵµ
Usenet post , ssh ¼Ì³ÐÁËÑ­»·µÄ±ê×¼ÊäÈë.ΪÁ˽â¾öÕâ¸öÎÊÌâ, ʹÓà ssh µÄ
»òÕß -f Ñ¡Ïî.
¸Ðл Jason Bechtel, Ö¸³öÕâµã.
scp
°²È«¿½±´, ÔÚ¹¦ÄÜÉÏÓë rcp ºÜÏàËÆ, ¾ÍÊÇÔÚ2¸ö²»Í¬µÄÍøÂçÖ÷»úÖ®¼ä¿½±´Îļþ, µ«ÊÇҪͨ
¹ý¼øȨµÄ·½Ê½, ²¢ÇÒʹÓÃÓë ssh ÀàËƵݲȫ²ã.
Local Network
write
ÕâÊÇÒ»¸ö¶Ëµ½¶ËͨѶµÄ¹¤¾ß. Õâ¸ö¹¤¾ß¿ÉÒÔ´ÓÄãµÄÖÕ¶ËÉÏ(console »òÕß xterm)·¢ËÍÕûÐÐ
µ½ÁíÒ»¸öÓû§µÄÖÕ¶ËÉÏ. mesg ÃüÁȻҲ¿ÉÒÔÓÃÀ´¶ÔÓÚÒ»¸öÖն˵ÄдȨÏÞ
ÒòΪ write ÊÇÐèÒª½»»¥µÄ, ËùÒÔÕâ¸öÃüÁîͨ³£²»Ê¹ÓÃÔڽű¾ÖÐ.
netconfig
ÓÃÀ´ÅäÖÃÍøÂçÊÊÅäÆ÷(ʹÓà DHCP)µÄÃüÁîÐй¤¾ß. Õâ¸öÃüÁî¶ÔÓÚºìñ·¢ÐаæÀ´ËµÊÇÄÚÖõÄ.
Mail
mail
·¢ËÍ»ò¶ÁÈ¡ e-mail ÏûÏ¢.
Èç¹û°ÑÕâ¸öÃüÁîÐÐµÄ mail ¿Í»§¶Ëµ±³ÉÒ»¸ö½Å±¾ÖеÄÃüÁîÀ´Ê¹ÓõĻ°, Ч¹û·Ç³£ºÃ.
Example 12-41 Ò»¸ö¿ÉÒÔmail×Ô¼ºµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/sh
2 # self-mailer.sh: mail×Ô¼ºµÄ½Å±¾
3
4 adr=${1:-`whoami`} # Èç¹û²»Ö¸¶¨µÄ»°, ĬÈÏÊǵ±Ç°Óû§.
5 # ¼üÈë 'self-mailer.sh wiseguy@superdupergenius.com'
6 #+ ·¢ËÍÕâ¸ö½Å±¾µ½Õâ¸öµØÖ·.
7 # Èç¹ûÖ»¼üÈë 'self-mailer.sh' (²»¸ø²ÎÊý) µÄ»°, ÄÇôÕâ½Å±¾¾Í»á±»·¢Ë͸ø
8 #+ µ÷ÓÃÕß, ±ÈÈç bozo@localhost.localdomain.
9 #
10 # Èç¹ûÏëÁ˽â ${parameter:-default} ½á¹¹µÄ¸ü¶àϸ½Ú,
11 #+ Çë²Î¼ûµÚ9Õ ±äÁ¿ÖØÓÎÖеÄ
12 #+ µÚ3½Ú ²ÎÊýÌæ»».
13
14 # ===========================================================================
=
15 cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$adr
"
16 # ===========================================================================
=
17
18 # --------------------------------------------
19 # À´×Ô self-mailing ½Å±¾µÄÒ»·Ý×£¸£.
20 # Ò»¸öϲ»¶¶ñ¸ãµÄ¼Ò»ïÔËÐÐÁËÕâ¸ö½Å±¾,
21 #+ Õâµ¼ÖÂÁËËû×Ô¼ºÊÕµ½ÁËÕâ·Ýmail.
22 # ÏÔÈ»µÄ, ÓÐЩÈËȷʵûʲôʺÃ×ö,
23 #+ ¾ÍÖ»ÄÜÀË·ÑËûÃÇ×Ô¼ºµÄʱ¼äÍæÁË.
24 # --------------------------------------------
25
26 echo "At `date`, script \"`basename $0`\" mailed to "$adr"."
27
28 exit 0
################################End Script######################################
###
mailto
Óë mail ÃüÁîºÜÏàËÆ, mailto ÃüÁî¿ÉÒÔʹÓÃÃüÁîÐлòÔڽű¾Öз¢ËÍ e-mail ÏûÏ¢. È»¶ø,
mailto ÃüÁîÒ²ÔÊÐí·¢ËÍ MIME (¶àýÌå) ÏûÏ¢.
vacation
Õâ¸ö¹¤¾ß¿ÉÒÔ×Ô¶¯»Ø¸´ e-mail ¸ø·¢ËÍÕß, ±íʾÓʼþµÄ½ÓÊÜÕßÕýÔڶȼÙÔÝʱÎÞ·¨ÊÕµ½Óʼþ.
Õâ¸ö¹¤¾ßÓë sendmail Ò»ÆðÔËÐÐÓÚÍøÂçÉÏ, ²¢ÇÒÕâ¸ö¹¤¾ß²»Ö§³Ö²¦ºÅµÄ POPmail ÕʺÅ.
×¢ÒâÊÂÏî:
[1] Ò»¸öÓÄÁé½ø³ÌÖ¸µÄÊDz¢Î´¸½¼ÓÔÚÖն˻ỰÖеĺǫ́½ø³Ì. ÓÄÁé½ø³Ì ÔÚÖ¸¶¨µÄʱ¼äÖ´
ÐÐÖ¸¶¨µÄ·þÎñ, »òÕßÓÉÌض¨µÄʼþ³ö·¢À´Ö´ÐÐÖ¸¶¨µÄ·þÎñ.

12.7 Öն˿ØÖÆÃüÁî
-----------------
Ó°Ïì¿ØÖÆ̨»òÖն˵ÄÃüÁî
tput
³õʼ»¯Öն˻òÕß´Ó terminfo data ÖÐÈ¡µÃÖÕ¶ËÐÅÏ¢. ²»Í¬µÄÑ¡ÏîÔÊÐíÌض¨µÄÖն˲Ù×÷.
tput clear ÓëÏÂ±ßµÄ clear µÈ¼Û. tput reset ÓëÏÂ±ßµÄ reset µÈ¼Û. tput sgr0 Ò²¿É
ÒÔÖØÖÃÖÕ¶Ë, µ«ÊDz¢²»Çå³ýÆÁÄ».
bash$ tput longname
xterm terminal emulator (XFree86 4.0 Window System)
ʹÓà tput cup X Y ½«»á°Ñ¹â±êÒƶ¯µ½µ±Ç°Öն˵Ä(X,Y)×ø±êÉÏ. ʹÓÃÕâ¸öÃüÁî֮ǰһ±ß
¶¼ÒªÏÈʹÓÃһϠclear ÃüÁî, °ÑÆÁÄ»Çå³ýÒ»ÏÂ.
×¢Òâ: stty ÌṩÁËÒ»¸ö¸üÇ¿Á¦µÄÃüÁîרÃÅÓÃÀ´ÉèÖÃÈçºÎ¿ØÖÆÖÕ¶Ë.
infocmp
Õâ¸öÃüÁî»á´òÓ¡³ö´óÁ¿µÄµ±Ç°Öն˵ÄÐÅÏ¢. ÊÂʵÉÏËüÊÇÒýÓÃÁË terminfo Êý¾Ý¿â.
bash$ infocmp
# ͨ¹ýÀ´×ÔÓÚÎļþµÄ infocmp ÏÔʾ³öÀ´:
/usr/share/terminfo/r/rxvt
rxvt|rxvt terminal emulator (X Window System),
am, bce, eo, km, mir, msgr, xenl, xon,
colors#8, cols#80, it#8, lines#24, pairs#64,
acsc=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~
,
bel=^G, blink=\E[5m, bold=\E[1m,
civis=\E[?25l,
clear=\E[H\E[2J, cnorm=\E[?25h, cr=^M,
...
reset
ÖØÖÃÖն˲ÎÊý²¢ÇÒÇå³ýÆÁÄ». Óë clear ÃüÁîÒ»Ñù, ¹â±êºÍÌáʾ·û½«»áÖØгöÏÖÔÚÖն˵Ä
×óÉϽÇ.
clear
clear ÃüÁîÖ»²»¹ýÊǼòµ¥µÄÇå³ý¿ØÖÆ̨»òÕß xterm µÄÆÁÄ». ¹â±êºÍÌáʾ·û½«»áÖØгöÏÖ
ÔÚÆÁÄ»»òÕß xterm window µÄ×óÉϽÇ. Õâ¸öÃüÁî¼È¿ÉÒÔÓÃÔÚÃüÁîÐÐÖÐÒ²¿ÉÒÔÓÃÔڽű¾ÖÐ.
²Î¼û Example 10-25.
script
Õâ¸ö¹¤¾ß½«»á¼Ç¼(±£´æµ½Ò»¸öÎļþÖÐ)ËùÓеÄÓû§ÔÚ¿ØÖÆ̨ϵĻòÔÚ xterm windowϵÄ
°´¼üÐÅÏ¢. ÕâÆäʵ¾ÍÊÇ´´½¨ÁËÒ»¸ö»á»°¼Ç¼.

12.8 Êýѧ¼ÆËãÃüÁî
-----------------
"Doing the numbers"
factor
½«Ò»¸öÕýÊý·Ö½âΪ¶à¸öËØÊý.
bash$ factor 27417
27417: 3 13 19 37
bc
Bash ²»ÄÜ´¦Àí¸¡µãÔËËã, ²¢ÇÒȱ·¦Ìض¨µÄһЩ²Ù×÷,ÕâЩ²Ù×÷¶¼ÊÇһЩÖØÒªµÄ¼ÆË㹦ÄÜ.
ÐÒÔ˵ÄÊÇ, bc ¿ÉÒÔ½â¾öÕâ¸öÎÊÌâ.
bc ²»½ö½öÊǸö¶à¹¦ÄÜÁé»îµÄ¾«È·µÄ¹¤¾ß, ¶øÇÒËü»¹ÌṩÐí¶à±à³ÌÓïÑԲž߱¸µÄһЩ·½±ã
µÄ¹¦ÄÜ.
bc ±È½ÏÀàËÆÓÚ C ÓïÑÔµÄÓï·¨.
ÒòΪËüÊÇÒ»¸öÍêÕûµÄ UNIX ¹¤¾ß, ËùÒÔËü¿ÉÒÔÓÃÔڹܵÀÖÐ, bc Ôڽű¾ÖÐÒ²ÊǺܳ£ÓõÄ.
ÕâÀïÓÐÒ»¸ö¼òµ¥µÄʹÓà bc ÃüÁîµÄÄ£°æ¿ÉÒÔÓÃÀ´ÔÚ¼ÆËã½Å±¾ÖеıäÁ¿. ÓÃÔÚÃüÁîÌæ»»ÖÐ.
variable=$(echo "OPTIONS; OPERATIONS" | bc)
Example 12-42 °´Ô³¥»¹´û¿î
################################Start Script####################################
###
1 #!/bin/bash
2 # monthlypmt.sh: ¼ÆËã°´Ô³¥»¹´û¿îµÄÊýÁ¿.
3
4
5 # Õâ·Ý´úÂëÊÇÒ»·ÝÐ޸İ汾, ԭʼ°æ±¾ÔÚ "mcalc" (´û¿î¼ÆËã)°üÖÐ,
6 #+ Õâ¸ö°üµÄ×÷ÕßÊÇ Jeff Schmidt ºÍ Mendel Cooper (±¾Êé×÷Õß).
7 # http://www.ibiblio.org/pub/Linux/apps/financial/mcalc-1.6.tar.gz [15k]
8
9 echo
10 echo "Given the principal, interest rate, and term of a mortgage,"
11 echo "calculate the monthly payment."
12
13 bottom=1.0
14
15 echo
16 echo -n "Enter principal (no commas) "
17 read principal
18 echo -n "Enter interest rate (percent) " # Èç¹ûÊÇ 12%, ÄǾͼüÈë "12", ±ðÊäÈë ".12".
19 read interest_r
20 echo -n "Enter term (months) "
21 read term
22
23
24 interest_r=$(echo "scale=9; $interest_r/100.0" | bc) # ת»»³ÉСÊý.
25 # "scale" Ö¸¶¨ÁËÓÐЧÊý×ֵĸöÊý.
26
27
28 interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)
29
30
31 top=$(echo "scale=9; $principal*$interest_rate^$term" | bc)
32
33 echo; echo "Please be patient. This may take a while."
34
35 let "months = $term - 1"
36 # ====================================================================
37 for ((x=$months; x > 0; x--))
38 do
39 bot=$(echo "scale=9; $interest_rate^$x" | bc)
40 bottom=$(echo "scale=9; $bottom+$bot" | bc)
41 # bottom = $(($bottom + $bot"))
42 done
43 # ====================================================================
44
45 # --------------------------------------------------------------------
46 # Rick Boivie ¸ø³öÁËÒ»¸ö¶ÔÉϱßÑ­»·µÄÐÞ¸Ä,
47 #+ Õâ¸öÐ޸ĸü¼ÓÓÐЧÂÊ, ½«»á½ÚÊ¡´ó¸Å 2/3 µÄʱ¼ä.
48
49 # for ((x=1; x <= $months; x++))
50 # do
51 # bottom=$(echo "scale=9; $bottom * $interest_rate + 1" | bc)
52 # done
53
54
55 # È»ºóËûÓÖÏë³öÁËÒ»¸ö¸ü¼ÓÓÐЧÂʵİ汾,
56 #+ ½«»á½ÚÊ¡ 95% µÄʱ¼ä!
57
58 # bottom=`{
59 # echo "scale=9; bottom=$bottom; interest_rate=$interest_rate"
60 # for ((x=1; x <= $months; x++))
61 # do
62 # echo 'bottom = bottom * interest_rate + 1'
63 # done
64 # echo 'bottom'
65 # } | bc` # ÔÚÃüÁîÌæ»»ÖÐǶÈëÒ»¸ö 'for Ñ­»·'.
66 # --------------------------------------------------------------------------
67 # On the other hand, Frank Wang suggests:
68 # bottom=$(echo "scale=9; ($interest_rate^$term-1)/($interest_rate-1)" | bc)
69
70 # ÒòΪ . . .
71 # ÔÚÑ­»·ºó±ßµÄËã·¨
72 #+ ÊÂʵÉÏÊÇÒ»¸öµÈ±ÈÊýÁеÄÇóºÍ¹«Ê½.
73 # ÇóºÍ¹«Ê½ÊÇ e0(1-q^n)/(1-q),
74 #+ e0 ÊǵÚÒ»¸öÔªËØ ²¢ÇÒ q=e(n+1)/e(n)
75 #+ ºÍ n ÊÇÔªËصÄÊýÁ¿.
76 # --------------------------------------------------------------------------
77
78
79 # let "payment = $top/$bottom"
80 payment=$(echo "scale=2; $top/$bottom" | bc)
81 # ʹÓÃ2λÓÐЧÊý×ÖÀ´±íʾÃÀÔªºÍÃÀ·Ö.
82
83 echo
84 echo "monthly payment = \$$payment" # ÔÚ×ܺ͵ÄÇ°±ßÏÔʾÃÀÔª·ûºÅ.
85 echo
86
87
88 exit 0
89
90
91 # Á·Ï°:
92 # 1) ´¦ÀíÊäÈëÔÊÐí±¾½ð×ÜÊýÖеĶººÅ.
93 # 2) ´¦ÀíÊäÈëÔÊÐí°´ÕհٷֺźÍСÊýµãµÄÐÎʽÊäÈëÀûÂÊ.
94 # 3) Èç¹ûÄãÕæÕýÏëºÃºÃ±àдÕâ¸ö½Å±¾,
95 # ÄÇô¾ÍÀ©Õ¹Õâ¸ö½Å±¾ÈÃËüÄܹ»´òÓ¡³öÍêÕûµÄ·ÖÆÚ¸¶¿î±í.
################################End Script######################################
###
Example 12-43 ÊýÖÆת»»
################################Start Script####################################
###
1 #!/bin/bash
2 ##########################################################################
3 # ½Å±¾ : base.sh - Óò»Í¬µÄÊýÖµÀ´´òÓ¡Êý×Ö (Bourne Shell)
4 # ×÷Õß : Heiner Steven (heiner.steven@odn.de)
5 # ÈÕÆÚ : 07-03-95
6 # ÀàÐÍ : ×ÀÃæ
7 # $Id: base.sh,v 1.2 2000/02/06 19:55:35 heiner Exp $
8 # ==> ÉϱßÕâÐÐÊÇ RCS ID ÐÅÏ¢.
9 ##########################################################################
10 # ÃèÊö
11 #
12 # Changes
13 # 21-03-95 stv fixed error occuring with 0xb as input (0.2)
14 ##########################################################################
15
16 # ==> ÔÚ±¾ÊéÖÐʹÓÃÕâ¸ö½Å±¾Í¨¹ýÁË×÷ÕßµÄÊÚȨ.
17 # ==> ×¢ÊÍÊDZ¾Êé×÷ÕßÌí¼ÓµÄ.
18
19 NOARGS=65
20 PN=`basename "$0"` # ³ÌÐòÃû
21 VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2` # ==> VER=1.2
22
23 Usage () {
24 echo "$PN - print number to different bases, $VER (stv '95)
25 usage: $PN [number ...]
26
27 If no number is given, the numbers are read from standard input.
28 A number may be
29 binary (base 2) starting with 0b (i.e. 0b1100)
30 octal (base 8) starting with 0 (i.e. 014)
31 hexadecimal (base 16) starting with 0x (i.e. 0xc)
32 decimal otherwise (i.e. 12)" >&2
33 exit $NOARGS
34 } # ==> ´òÓ¡³öÓ÷¨ÐÅÏ¢µÄº¯Êý.
35
36 Msg () {
37 for i # ==> Ê¡ÂÔ [list] .
38 do echo "$PN: $i" >&2
39 done
40 }
41
42 Fatal () { Msg "$@"; exit 66; }
43
44 PrintBases () {
45 # ¾ö¶¨ÊýÖµµÄÊýÖÆ
46 for i # ==> Ê¡ÂÔ [list]...
47 do # ==> ËùÒÔÊǶÔÃüÁîÐвÎÊý½øÐвÙ×÷.
48 case "$i" in
49 0b*) ibase=2;; # 2½øÖÆ
50 0x*|[a-f]*|[A-F]*) ibase=16;; # 16½øÖÆ
51 0*) ibase=8;; # 8½øÖÆ
52 [1-9]*) ibase=10;; # 10½øÖÆ
53 *)
54 Msg "illegal number $i - ignored"
55 continue;;
56 esac
57
58 # È¥µôǰ׺, ½«16½øÖÆÊý×Öת»»Îª´óд(bcÐèÒª´óд)
59 number=`echo "$i" | sed -e 's:^0[bBxX]::' | tr '[a-f]' '[A-F]'`
60 # ==>ʹÓÃ":" ×÷Ϊsed·Ö¸ô·û, ¶ø²»Ê¹ÓÃ"/".
61
62 # ½«Êý×Öת»»Îª10½øÖÆ
63 dec=`echo "ibase=$ibase; $number" | bc` # ==> 'bc' ÊǸö¼ÆË㹤¾ß.
64 case "$dec" in
65 [0-9]*) ;; # Êý×ÖûÎÊÌâ
66 *) continue;; # ´íÎó: ºöÂÔ
67 esac
68
69 # ÔÚÒ»ÐÐÉÏ´òÓ¡ËùÓеÄת»»ºóµÄÊý×Ö.
70 # ==> 'here document' ÌṩÃüÁîÁбí¸ø'bc'.
71 echo `bc <<!
72 obase=16; "hex="; $dec
73 obase=10; "dec="; $dec
74 obase=8; "oct="; $dec
75 obase=2; "bin="; $dec
76 !
77 ` | sed -e 's: : :g'
78
79 done
80 }
81
82 while [ $# -gt 0 ]
83 # ==> ÕâÀï±ØÐëʹÓÃÒ»¸ö "while Ñ­»·",
84 # ==>+ ÒòΪËùÓÐµÄ case ¶¼¿ÉÄÜÍ˳öÑ­»·»òÕß
85 # ==>+ ½áÊø½Å±¾.
86 # ==> (¸Ðл, Paulo Marcel Coelho Aragao.)
87 do
88 case "$1" in
89 --) shift; break;;
90 -h) Usage;; # ==> °ïÖúÐÅÏ¢.
91 -*) Usage;;
92 *) break;; # µÚÒ»¸öÊý×Ö
93 esac # ==> ¶ÔÓÚ·Ç·¨ÊäÈë¸üÑϸñ¼ì²éÊǷdz£ÓÐÓõÄ.
94 shift
95 done
96
97 if [ $# -gt 0 ]
98 then
99 PrintBases "$@"
100 else # ´Ó±ê×¼ÊäÈëÖжÁÈ¡
101 while read line
102 do
103 PrintBases $line
104 done
105 fi
106
107
108 exit 0
################################End Script######################################
###
µ÷Óà bc µÄÁíÒ»ÖÖ¿ÉÑ¡µÄ·½·¨¾ÍÊÇʹÓà here document ,²¢°ÑËüǶÈëµ½ ÃüÁîÌæ»» ¿éÖÐ.
µ±Ò»¸ö½Å±¾ÐèÒª½«Ò»¸öÑ¡ÏîÁбíºÍ¶à¸öÃüÁî´«µÝµ½ bc ÖÐʱ, ÕâÖÖ·½·¨¾ÍÏԵ÷dz£ºÏÊÊ.
1 variable=`bc << LIMIT_STRING
2 options
3 statements
4 operations
5 LIMIT_STRING
6 `
7
8 ...or...
9
10
11 variable=$(bc << LIMIT_STRING
12 options
13 statements
14 operations
15 LIMIT_STRING
16 )
Example 12-44 ʹÓà "here document" À´µ÷Óà bc
################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓÃÃüÁîÌæ»»À´µ÷Óà 'bc'
3 # ²¢Óë 'here document' Ïà½áºÏ.
4
5
6 var1=`bc << EOF
7 18.33 * 19.78
8 EOF
9 `
10 echo $var1 # 362.56
11
12
13 # $( ... ) ÕâÖÖ±ê¼Ç·¨Ò²¿ÉÒÔ.
14 v1=23.53
15 v2=17.881
16 v3=83.501
17 v4=171.63
18
19 var2=$(bc << EOF
20 scale = 4
21 a = ( $v1 + $v2 )
22 b = ( $v3 * $v4 )
23 a * b + 15.35
24 EOF
25 )
26 echo $var2 # 593487.8452
27
28
29 var3=$(bc -l << EOF
30 scale = 9
31 s ( 1.7 )
32 EOF
33 )
34 # ·µ»Ø»¡¶ÈΪ1.7µÄÕýÏÒ.
35 # "-l" Ñ¡Ï»áµ÷Óà 'bc' ËãÊý¿â.
36 echo $var3 # .991664810
37
38
39 # ÏÖÔÚ, ÔÚº¯ÊýÖÐÊÔÒ»ÏÂ...
40 hyp= # ÉùÃ÷È«¾Ö±äÁ¿.
41 hypotenuse () # ¼ÆËãÖ±½ÇÈý½ÇÐεÄб±ß.
42 {
43 hyp=$(bc -l << EOF
44 scale = 9
45 sqrt ( $1 * $1 + $2 * $2 )
46 EOF
47 )
48 # ²»ÐÒµÄÊÇ, ²»ÄÜ´Óbash º¯ÊýÖзµ»Ø¸¡µãÖµ.
49 }
50
51 hypotenuse 3.68 7.31
52 echo "hypotenuse = $hyp" # 8.184039344
53
54
55 exit 0
################################End Script######################################
###
Example 12-45 ¼ÆËãÔ²ÖÜÂÊ
################################Start Script####################################
###
1 #!/bin/bash
2 # cannon.sh: ͨ¹ý¿ªÅÚÀ´È¡µÃ½üËƵÄÔ²ÖÜÂÊÖµ.
3
4 # ÕâÊÂʵÉÏÊÇÒ»¸ö"Monte Carlo"ÃÉÌØ¿¨ÂåÄ£ÄâµÄ·Ç³£¼òµ¥µÄʵÀý:
5 #+ ÃÉÌØ¿¨ÂåÄ£ÄâÊÇÒ»ÖÖÓÉÏÖʵʼþ³éÏó³öÀ´µÄÊýѧģÐÍ,
6 #+ ÓÉÓÚҪʹÓÃËæ»ú³éÑùͳ¼ÆÀ´¹ÀËãÊýѧº¯Êý, ËùÒÔʹÓÃαËæ»úÊýÀ´Ä£ÄâÕæÕýµÄËæ»ú.
7
8 # ÏëÏóÓÐÒ»¸öÍêÃÀµÄÕý·½ÐÎÍÁµØ, ±ß³¤Îª10000¸öµ¥Î».
9 # ÔÚÕâ¿éÍÁµØµÄÖмäÓÐÒ»¸öÍêÃÀµÄÔ²Ðκþ,
10 #+ Õâ¸öºþµÄÖ±¾¶ÊÇ10000¸öµ¥Î».
11 # Õâ¿éÍÁµØµÄ¾ø´ó¶àÊýÃæ»ý¶¼ÊÇË®, µ±È»Ö»ÓÐ4¸ö½ÇÉÏÓÐһЩÍÁµØ.
12 # (¿ÉÒÔ°ÑÕâ¸öºþÏëÏó³ÉΪʹÕâ¸öÕý·½ÐεÄÄÚ½ÓÔ².)
13 #
14 # ÎÒÃǽ«Ê¹ÓÃÀÏʽµÄ´óÅÚºÍÌúÅÚµ¯
15 #+ ÏòÕâ¿éÕý·½ÐεÄÍÁµØÉÏ¿ªÅÚ.
16 # ËùÓеÄÅÚµ¯¶¼»á»÷ÖÐÕâ¿éÕý·½ÐÎÍÁµØµÄij¸öµØ·½.
17 #+ »òÕßÊÇ´òµ½ºþÉÏ, »òÕßÊÇ´òµ½4¸ö½ÇµÄÍÁµØÉÏ.
18 # ÒòΪÕâ¸öºþÕ¼¾ÝÁËÕâ¸öÇøÓò´ó²¿·ÖµØ·½,
19 #+ ËùÒԴ󲿷ֵÄÅÚµ¯¶¼»á"ÆËͨ"Ò»ÉùÂ䵽ˮÀï.
20 # ¶øÖ»ÓкÜÉÙµÄÅÚµ¯»á"Åé"µÄÒ»ÉùÂäµ½4¸ö
21 #+ ½ÇµÄÍÁµØÉÏ.
22 #
23 # Èç¹ûÎÒÃÇ·¢³öµÄÅÚµ¯×ã¹»Ëæ»úµÄÂäµ½Õâ¿éÕý·½ÐÎÇøÓòÖеĻ°,
24 #+ ÄÇôÂ䵽ˮÀïµÄÅÚµ¯Óë´ò³öÅÚµ¯µÄ×ÜÊýµÄ±ÈÂÊ,
25 #+ ´ó¸Å·Ç³£½Ó½üÓÚ PI/4.
26 #
27 # Ô­ÒòÊÇËùÓеÄÅÚµ¯ÊÂʵÉ϶¼
28 #+ ´òÔÚÁËÕâ¸öÍÁµØµÄÓÒÉϽÇ,
29 #+ Ò²¾ÍÊÇ, µÑ¿¨¶û×ø±êϵµÄµÚÒ»ÏóÏÞ.
30 # (֮ǰµÄ½âÊÍÖ»ÊÇÒ»¸ö¼ò»¯.)
31 #
32 # ÀíÂÛÉÏÀ´Ëµ, Èç¹û´ò³öµÄÅÚµ¯Ô½¶à, ¾ÍÔ½½Ó½üÕâ¸öÊý×Ö.
33 # È»¶ø, ¶ÔÓÚshell ½Å±¾À´ËµÒ»¶¨»á×÷ЩÈò½µÄ,
34 #+ ÒòΪËü¿Ï¶¨²»ÄܺÍÄÇЩÄÚ½¨¾ÍÖ§³Ö¸¡µãÔËËãµÄ±àÒëÓïÑÔÏà±È.
35 # µ±È»¾Í»á½µµÍ¾«¶È.
36
37
38 DIMENSION=10000 # Õâ¿éÍÁµØµÄ±ß³¤.
39 # ÕâÒ²ÊÇËù²úÉúµÄËæ»úÕûÊýµÄÉÏÏÞ.
40
41 MAXSHOTS=1000 # ¿ªÅÚ´ÎÊý.
42 # 10000 »ò¸ü¶à´ÎµÄ»°, Ч¹ûÓ¦¸Ã¸üºÃ, µ«ÓеãÌ«ÀË·Ñʱ¼äÁË.
43 PMULTIPLIER=4.0 # ½Ó½üÓÚ PI µÄ±ÈÀýÒò×Ó.
44
45 get_random ()
46 {
47 SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
48 RANDOM=$SEED # À´×ÔÓÚ "seeding-random.sh"
49 #+ µÄÀý×ӽű¾.
50 let "rnum = $RANDOM % $DIMENSION" # ·¶Î§Ð¡ÓÚ 10000.
51 echo $rnum
52 }
53
54 distance= # ÉùÃ÷È«¾Ö±äÁ¿.
55 hypotenuse () # ´Ó "alt-bc.sh" Àý×ÓÀ´µÄ,
56 { # ¼ÆËãÖ±½ÇÈý½ÇÐεÄб±ßµÄº¯Êý.
57 distance=$(bc -l << EOF
58 scale = 0
59 sqrt ( $1 * $1 + $2 * $2 )
60 EOF
61 )
62 # ÉèÖà "scale" Ϊ 0 , ºÃÈýá¹ûËÄÉáÎåÈëΪÕûÊýÖµ,
63 #+ ÕâÊÇÕâ¸ö½Å±¾ÖбØÐëÕÛÖеÄÒ»¸öµØ·½.
64 # ²»ÐÒµÄÊÇ, Õ⽫½µµÍÄ£ÄâµÄ¾«¶È.
65 }
66
67
68 # main() {
69
70 # ³õʼ»¯±äÁ¿.
71 shots=0
72 splashes=0
73 thuds=0
74 Pi=0
75
76 while [ "$shots" -lt "$MAXSHOTS" ] # Ö÷Ñ­»·.
77 do
78
79 xCoord=$(get_random) # È¡µÃËæ»úµÄ X Óë Y ×ø±ê.
80 yCoord=$(get_random)
81 hypotenuse $xCoord $yCoord # Ö±½ÇÈý½ÇÐÎб±ß =
82 #+ distance.
83 ((shots++))
84
85 printf "#%4d " $shots
86 printf "Xc = %4d " $xCoord
87 printf "Yc = %4d " $yCoord
88 printf "Distance = %5d " $distance # µ½ºþÖÐÐĵÄ
89 #+ ¾àÀë --
90 # Æðʼ×ø±êµã --
91 #+ (0,0).
92
93 if [ "$distance" -le "$DIMENSION" ]
94 then
95 echo -n "SPLASH! "
96 ((splashes++))
97 else
98 echo -n "THUD! "
99 ((thuds++))
100 fi
101
102 Pi=$(echo "scale=9; $PMULTIPLIER*$splashes/$shots" | bc)
103 # ½«±ÈÀý³ËÒÔ 4.0.
104 echo -n "PI ~ $Pi"
105 echo
106
107 done
108
109 echo
110 echo "After $shots shots, PI looks like approximately $Pi."
111 # Èç¹û²»Ì«×¼µÄ»°, ÄÇô¾ÍÌá¸ßÒ»ÏÂÔËÐеĴÎÊý. . .
112 # ¿ÉÄÜÊÇÓÉÓÚÔËÐдíÎóºÍËæ»úÊýËæ»ú³Ì¶È²»¸ßÔì³ÉµÄ.
113 echo
114
115 # }
116
117 exit 0
118
119 # ÒªÏëÖªµÀÒ»¸öshell½Å±¾µ½µ×Êʲ»ÊʺÏ×÷Ϊ
120 #+ Ò»ÖÖÐèÒª¶Ô¸´ÔӺ;«¶È¶¼ÓÐÒªÇóµÄ¼ÆËãÓ¦ÓõÄÄ£ÄâµÄ»°.
121 #
122 # Ò»°ãÖÁÉÙÐèÒªÁ½¸öÅжÏÌõ¼þ.
123 # 1) ×÷ΪһÖÖ¸ÅÄîµÄÑéÖ¤: À´ÏÔʾËü¿ÉÒÔ×öµ½.
124 # 2) ÔÚʹÓÃÕæÕýµÄ±àÒëÓïÑÔÀ´ÊµÏÖÒ»¸öË㷨֮ǰ,
125 #+ ʹÓýű¾À´²âÊÔºÍÑéÖ¤Õâ¸öËã·¨.
################################End Script######################################
###
dc
dc (×ÀÃæ¼ÆËãÆ÷desk calculator) ¹¤¾ßÊÇÃæÏòÕ»µÄ²¢ÇÒʹÓà RPN (Ä沨À¼±í´ïʽ
"Reverse Polish Notation" ÓÖ½Ð"ºó׺±í´ïʽ"). Óë bc ÃüÁîºÜÏàÏñ , µ«ÊÇÕâ¸ö¹¤¾ß
¾ß±¸ºÃ¶àÖ»Óбà³ÌÓïÑԲž߱¸µÄÄÜÁ¦.
(ÒëÕß×¢: Õý³£±í´ïʽ Ä沨À¼±í´ïʽ
a+b a,b,+
a+(b-c) a,b,c,-,+
a+(b-c)*d a,d,b,c,-,*,+
)
¾ø´ó¶àÊýÈ˶¼±ÜÃâʹÓÃÕâ¸ö¹¤¾ß, ÒòΪËüÐèÒª·ÇÖ±¾õµÄ RPN ÊäÈë. µ«ÊÇ, ËüÈ´ÓÐÌض¨µÄ
ÓÃ;.
Example 12-46 ½«10½øÖÆÊý×Öת»»Îª16½øÖÆÊý×Ö
################################Start Script####################################
###
1 #!/bin/bash
2 # hexconvert.sh: ½«10½øÖÆÊý×Öת»»Îª16½øÖÆÊý×Ö
3
4 E_NOARGS=65 # ȱÃüÁîÐвÎÊý´íÎó.
5 BASE=16 # 16½øÖÆ.
6
7 if [ -z "$1" ]
8 then
9 echo "Usage: $0 number"
10 exit $E_NOARGS
11 # ÐèÒªÒ»¸öÃüÁîÐвÎÊý.
12 fi
13 # Á·Ï°: Ìí¼ÓÃüÁîÐвÎÊý¼ì²é.
14
15
16 hexcvt ()
17 {
18 if [ -z "$1" ]
19 then
20 echo 0
21 return # Èç¹ûûÓвÎÊý´«µÝµ½Õâ¸öº¯ÊýÖÐ¾Í "return" 0.
22 fi
23
24 echo ""$1" "$BASE" o p" | dc
25 # "o" ÉèÖÃÊä³öµÄ»ùÊý(ÊýÖÆ).
26 # "p" ´òÓ¡Õ»¶¥.
27 # ²ì¿´ dc µÄ man Ò³À´Á˽âÆäËûµÄÑ¡Ïî.
28 return
29 }
30
31 hexcvt "$1"
32
33 exit 0
################################End Script######################################
###
ͨ¹ý×Ðϸѧϰ dc ÃüÁîµÄ info Ò³, ¿ÉÒÔ¸üÉîÈëµÄÀí½âÕâ¸ö¸´ÔÓµÄÃüÁî. µ«ÊÇ, ÓÐһЩ
¾«Í¨ dcÎ×Êõ µÄС×é¾­³£»áìÅÒ«ËûÃÇʹÓÃÕâ¸öÇ¿´ó¶øÓÖ»ÞɬÄѶ®µÄ¹¤¾ßʱµÄһЩ¼¼ÇÉ,
²¢ÒÔ´ËΪÀÖ.
bash$ echo "16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D68736142snlbxq" | dc"
Bash
Example 12-47 Òò×Ó·Ö½â
################################Start Script####################################
###
1 #!/bin/bash
2 # factr.sh: ·Ö½âÔ¼Êý
3
4 MIN=2 # Èç¹û±ÈÕâ¸öÊýС¾Í²»ÐÐÁË.
5 E_NOARGS=65
6 E_TOOSMALL=66
7
8 if [ -z $1 ]
9 then
10 echo "Usage: $0 number"
11 exit $E_NOARGS
12 fi
13
14 if [ "$1" -lt "$MIN" ]
15 then
16 echo "Number to factor must be $MIN or greater."
17 exit $E_TOOSMALL
18 fi
19
20 # Á·Ï°: Ìí¼ÓÀàÐͼì²é (·ÀÖ¹·ÇÕûÐ͵IJÎÊý).
21
22 echo "Factors of $1:"
23 # ---------------------------------------------------------------------------
------
24 echo "$1[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=1lrli2+dsi!>.]ds.xd1<2
" | dc
25 # ---------------------------------------------------------------------------
------
26 # ÉϱßÕâÐдúÂëÊÇ Michel Charpentier ±àдµÄ<charpov@cs.unh.edu>.
27 # ÔÚ´ËʹÓþ­¹ýÊÚȨ (thanks).
28
29 exit 0
################################End Script######################################
###
awk
Ôڽű¾ÖÐʹÓø¡µãÔËËãµÄÁíÒ»ÖÖ·½·¨ÊÇʹÓà awk ÄÚ½¨µÄÊýѧÔËË㺯Êý, ¿ÉÒÔÓÃÔÚshell
wrapperÖÐ.
Example 12-48 ¼ÆËãÖ±½ÇÈý½ÇÐεÄб±ß
################################Start Script####################################
###
1 #!/bin/bash
2 # hypotenuse.sh: ·µ»ØÖ±½ÇÈý½ÇÐεÄб±ß.
3 # ( Ö±½Ç±ß³¤µÄƽ·½ºÍ,È»ºó¶ÔºÍȡƽ·½¸ù)
4
5 ARGS=2 # ÐèÒª½«2¸öÖ±½Ç±ß×÷Ϊ²ÎÊý´«µÝ½øÀ´.
6 E_BADARGS=65 # ´íÎóµÄ²ÎÊýÖµ.
7
8 if [ $# -ne "$ARGS" ] # ²âÊÔ´«µÝµ½½Å±¾ÖеIJÎÊýÖµ.
9 then
10 echo "Usage: `basename $0` side_1 side_2"
11 exit $E_BADARGS
12 fi
13
14
15 AWKSCRIPT=' { printf( "%3.7f\n", sqrt($1*$1 + $2*$2) ) } '
16 # ÃüÁî / ´«µÝ¸øawkµÄ²ÎÊý
17
18
19 # ÏÖÔÚ, ½«²ÎÊýͨ¹ý¹ÜµÀ´«µÝ¸øawk.
20 echo -n "Hypotenuse of $1 and $2 = "
21 echo $1 $2 | awk "$AWKSCRIPT"
22
23 exit 0
################################End Script######################################
###

12.9 »ìÔÓÃüÁî
-------------
һЩ²»ºÃ¹éÀàµÄÃüÁî
jot, seq
ÕâЩ¹¤¾ßͨ¹ýÓû§Ö¸¶¨µÄ·¶Î§ºÍÔöÁ¿À´²úÉúһϵÁеÄÕûÊý.
ÿ¸ö²úÉú³öÀ´µÄÕûÊýÒ»°ã¶¼Õ¼Ò»ÐÐ, µ«ÊÇ¿ÉÒÔʹÓà -s Ñ¡ÏîÀ´¸Ä±äÕâÖÖÉèÖÃ.
bash$ seq 5
1
2
3
4
5

bash$ seq -s : 5
1:2:3:4:5
jot ºÍ seq ÃüÁ¾­³£ÓÃÔÚ for Ñ­»·ÖÐ.
Example 12-49 ʹÓà seq À´²úÉúÑ­»·²ÎÊý
################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓà "seq"
3
4 echo
5
6 for a in `seq 80` # »òÕß for a in $( seq 80 )
7 # Óë " for a in 1 2 3 4 5 ... 80 "Ïàͬ (ÉÙÇÃÁ˺öà×Ö!).
8 # Ò²¿ÉÒÔʹÓà 'jot' (Èç¹ûϵͳÉÏÓеĻ°).
9 do
10 echo -n "$a "
11 done # 1 2 3 4 5 ... 80
12 # ÕâÒ²ÊÇÒ»¸öͨ¹ýʹÓÃÃüÁîµÄÊä³ö
13 # À´²úÉú "for"Ñ­»·ÖÐ [list] ÁбíµÄÀý×Ó.
14
15 echo; echo
16
17
18 COUNT=80 # µ±È», 'seq' Ò²¿ÉÒÔʹÓÃÒ»¸ö¿ÉÌæ»»µÄ²ÎÊý.
19
20 for a in `seq $COUNT` # »òÕß for a in $( seq $COUNT )
21 do
22 echo -n "$a "
23 done # 1 2 3 4 5 ... 80
24
25 echo; echo
26
27 BEGIN=75
28 END=80
29
30 for a in `seq $BEGIN $END`
31 # ´«¸ø "seq" Á½¸ö²ÎÊý, ´ÓµÚÒ»¸ö²ÎÊý¿ªÊ¼Ôö³¤,
32 #+ Ò»Ö±Ôö³¤µ½µÚ¶þ¸ö²ÎÊýΪֹ.
33 do
34 echo -n "$a "
35 done # 75 76 77 78 79 80
36
37 echo; echo
38
39 BEGIN=45
40 INTERVAL=5
41 END=80
42
43 for a in `seq $BEGIN $INTERVAL $END`
44 # ´«¸ø "seq" Èý¸ö²ÎÊý´ÓµÚÒ»¸ö²ÎÊý¿ªÊ¼Ôö³¤,
45 #+ ²¢ÒÔµÚ¶þ¸ö²ÎÊý×÷ΪÔöÁ¿,
46 #+ Ò»Ö±Ôö³¤µ½µÚÈý¸ö²ÎÊýΪֹ.
47 do
48 echo -n "$a "
49 done # 45 50 55 60 65 70 75 80
50
51 echo; echo
52
53 exit 0
################################End Script######################################
###
Ò»¸ö¼òµ¥Ð©µÄÀý×Ó:
1 # ²úÉú10¸öÁ¬ÐøÀ©Õ¹ÃûµÄÎļþ,
2 #+ Ãû×Ö·Ö±ðÊÇ file.1, file.2 . . . file.10.
3 COUNT=10
4 PREFIX=file
5
6 for filename in `seq $COUNT`
7 do
8 touch $PREFIX.$filename
9 # »òÕß, Äã¿ÉÒÔ×öһЩÆäËûµÄ²Ù×÷,
10 #+ ±ÈÈç rm, grep, µÈµÈ.
11 done

Example 12-50 ×Öĸͳ¼Æ


################################Start Script####################################
###
1 #!/bin/bash
2 # letter-count.sh: ͳ¼ÆÒ»¸öÎı¾ÎļþÖÐ×Öĸ³öÏֵĴÎÊý.
3 # ÓÉ Stefano Palmeri ±àд.
4 # ¾­¹ýÊÚȨʹÓÃÔÚ±¾ÊéÖÐ.
5 # ±¾Êé×÷Õß×öÁËÉÙÐíÐÞ¸Ä.
6
7 MINARGS=2 # ±¾½Å±¾ÖÁÉÙÐèÒª2¸ö²ÎÊý.
8 E_BADARGS=65
9 FILE=$1
10
11 let LETTERS=$#-1 # Öƶ¨Á˶àÉÙ¸ö×Öĸ (×÷ΪÃüÁîÐвÎÊý).
12 # (´ÓÃüÁîÐвÎÊýµÄ¸öÊýÖмõ1.)
13
14
15 show_help(){
16 echo
17 echo Usage: `basename $0` file letters
18 echo Note: `basename $0` arguments are case sensitive.
19 echo Example: `basename $0` foobar.txt G n U L i N U x.
20 echo
21 }
22
23 # ¼ì²é²ÎÊý¸öÊý.
24 if [ $# -lt $MINARGS ]; then
25 echo
26 echo "Not enough arguments."
27 echo
28 show_help
29 exit $E_BADARGS
30 fi
31
32
33 # ¼ì²éÎļþÊÇ·ñ´æÔÚ.
34 if [ ! -f $FILE ]; then
35 echo "File \"$FILE\" does not exist."
36 exit $E_BADARGS
37 fi
38
39
40
41 # ͳ¼Æ×Öĸ³öÏֵĴÎÊý.
42 for n in `seq $LETTERS`; do
43 shift
44 if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # ¼ì²é²ÎÊý.
45 echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # ͳ¼Æ.
46 else
47 echo "$1 is not a single char."
48 fi
49 done
50
51 exit $?
52
53 # Õâ¸ö½Å±¾ÔÚ¹¦ÄÜÉÏÓë letter-count2.sh ÍêÈ«Ïàͬ,
54 #+ µ«ÊÇÔËÐеøü¿ì.
55 # Ϊʲô?
################################End Script######################################
###
getopt
getopt ÃüÁ»á·ÖÎöÒÔÆÆÕۺſªÍ·µÄÃüÁîÐÐÑ¡Ïî. Õâ¸öÍⲿÃüÁîÓëBashµÄÄÚ½¨ÃüÁî
getopts ×÷ÓÃÏàͬ. ͨ¹ýʹÓà -l ±êÖ¾, getopt ¿ÉÒÔ´¦Àí³¤(¶à×Ö·û)Ñ¡Ïî, ²¢ÇÒÒ²ÔÊÐí²Î
ÊýÖØÖÃ.
Example 12-51 ʹÓÃgetoptÀ´·ÖÎöÃüÁîÐÐÑ¡Ïî
################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓà getopt.
3
4 # ³¢ÊÔʹÓÃϱߵIJ»Í¬µÄ·½·¨À´µ÷ÓÃÕâ½Å±¾:
5 # sh ex33a.sh -a
6 # sh ex33a.sh -abc
7 # sh ex33a.sh -a -b -c
8 # sh ex33a.sh -d
9 # sh ex33a.sh -dXYZ
10 # sh ex33a.sh -d XYZ
11 # sh ex33a.sh -abcd
12 # sh ex33a.sh -abcdZ
13 # sh ex33a.sh -z
14 # sh ex33a.sh a
15 # ½âÊÍÉÏÃæÿһ´Îµ÷ÓõĽá¹û.
16
17 E_OPTERR=65
18
19 if [ "$#" -eq 0 ]
20 then # ½Å±¾ÐèÒªÖÁÉÙÒ»¸öÃüÁîÐвÎÊý.
21 echo "Usage $0 -[options a,b,c]"
22 exit $E_OPTERR
23 fi
24
25 set -- `getopt "abcd:" "$@"`
26 # ΪÃüÁîÐвÎÊýÉèÖÃλÖòÎÊý.
27 # Èç¹ûʹÓà "$*" À´´úÌæ "$@" µÄ»°»á·¢Éúʲô?
28
29 while [ ! -z "$1" ]
30 do
31 case "$1" in
32 -a) echo "Option \"a\"";;
33 -b) echo "Option \"b\"";;
34 -c) echo "Option \"c\"";;
35 -d) echo "Option \"d\" $2";;
36 *) break;;
37 esac
38
39 shift
40 done
41
42 # ͨ³£À´ËµÔڽű¾ÖÐʹÓÃÄÚ½¨µÄ 'getopts' ÃüÁî,
43 #+ »á±ÈʹÓà 'getopt' ºÃһЩ.
44 # ²Î¼û "ex33.sh".
45
46 exit 0
################################End Script######################################
###
²Î¼û Example 9-12 , ÕâÊÇ¶Ô getopt ÃüÁîµÄÒ»¸ö¼òµ¥Ä£Äâ.
run-parts
run-parts ÃüÁî [1] ½«»áÖ´ÐÐÄ¿±êĿ¼ÖÐËùÓеĽű¾, ÕâЩ½«±¾»áÒÔ ASCII µÄÑ­Ðò½øÐÐ
ÅÅÁÐ. µ±È», ÕâЩ½Å±¾¶¼ÐèÒª¾ßÓпÉÖ´ÐÐȨÏÞ.
cron ÓÄÁé½ø³Ì »áµ÷Óà run-parts À´ÔËÐÐ /etc/cron.* ϵÄËùÓнű¾.
yes
yes ÃüÁîµÄĬÈÏÐÐΪÊÇÏò stdout ÖÐÁ¬Ðø²»¶ÏµÄÊä³ö×Ö·û y,ÿ¸öyÕ¼Ò»ÐÐ.ʹÓÃcontrol-c
À´½áÊøÔËÐÐ. Èç¹ûÏë»»Ò»¸öÊä³ö×Ö·ûµÄ»°, ¿ÉÒÔʹÓà yes ÆäËûµÄ×Ö·û´®, ÕâÑù¾Í»áÁ¬Ðø
²»Í¬µÄÊä³öÄãÖ¸¶¨µÄ×Ö·û´®. ÄÇôÕâÑùµÄÃüÁ¾¹ÄÜ×öʲôÄØ? ÔÚÃüÁîÐлòÕ߽ű¾ÖÐ,
yesµÄÊä³ö¿ÉÒÔͨ¹ýÖض¨Ïò»ò¹ÜµÀÀ´´«µÝ¸øһЩÐèÒªÓû§ÊäÈë½øÐн»»¥µÄÃüÁî. ÊÂʵÉÏ,
Õâ¸öÃüÁî¿ÉÒÔ˵ÊÇ expect ÃüÁî(ÒëÕß×¢: Õâ¸öÃüÁî±¾Êéδ½éÉÜ, Ò»¸ö×Ô¶¯ÊµÏÖ½»»¥µÄÃü
Áî)µÄÒ»¸ö¼ò»¯°æ±¾.
yes | fsck /dev/hda1 ½«»áÒԷǽ»»¥µÄÐÎʽÔËÐÐfsck(ÒòΪÐèÒªÓû§ÊäÈëµÄ y È«ÓÉyes
ÃüÁî¸ã¶¨ÁË)(СÐÄʹÓÃ!).
yes | rm -r dirname Óë rm -rf dirname Ч¹ûÏàͬ(СÐÄʹÓÃ!).
×¢Òâ: µ±Óà yes µÄ¹ÜµÀÐÎʽÀ´Ê¹ÓÃһЩ¿ÉÄܾßÓÐDZÔÚΣÏÕµÄϵͳÃüÁîµÄʱºòÒ»¶¨ÒªÉî˼
ÊìÂÇ, ±ÈÈç fsck »ò fdisk. ¿ÉÄÜ»á²úÉúһЩÒâÍâµÄ¸±×÷ÓÃ.
banner
½«»á°Ñ×Ö·û´®ÓÃÒ»¸ö ASCII ×Ö·û(ĬÈÏÊÇ '#')À´»­³öÀ´(¾ÍÊǽ«¶à¸ö'#'Æ´³öÒ»¸±×Ö·ûµÄ
ͼÐÎ).¿ÉÒÔ×÷ΪӲ¿½±´Öض¨Ïòµ½´òÓ¡»úÉÏ(ÒëÕß×¢: ¿ÉÒÔʹÓÃ-w Ñ¡ÏîÉèÖÿí¶È).
printenv
¶ÔÓÚij¸öÌض¨µÄÓû§, ÏÔʾ³öËùÓÐµÄ »·¾³±äÁ¿.
bash$ printenv | grep HOME
HOME=/home/bozo
lp
lp ºÍ lpr ÃüÁ»á°ÑÎļþ·¢Ë͵½´òÓ¡¶ÓÁÐÖÐ, ²¢ÇÒ×÷ΪӲ¿½±´À´´òÓ¡. [2] ÕâЩÃüÁî
»á¼Í¼ËüÃÇÃû×ÖµÄÆðʼλÖò¢´«µÝµ½ÐдòÓ¡»úµÄÁíÒ»¸öλÖÃ.<rojy bug>
bash$ lp file1.txt »òÕß bash lp <file1.txt
ͨ³£Çé¿ö϶¼Êǽ«prµÄ¸ñʽ»¯µÄÊä³ö´«µÝµ½ lp.
bash$ pr -options file1.txt | lp
¸ñʽ»¯µÄ°ü, ±ÈÈç groff ºÍ Ghostscript ¾Í¿ÉÒÔ½«ËüÃǵÄÊä³öÖ±½Ó·¢Ë͸ø lp.
bash$ groff -Tascii file.tr | lp
bash$ gs -options | lp file.ps
»¹ÓÐһЩÏà¹ØµÄÃüÁî, ±ÈÈç lpq, ¿ÉÒԲ鿴´òÓ¡¶ÓÁÐ, lprm, ¿ÉÒÔÓÃÀ´´Ó´òÓ¡¶ÓÁÐÖÐɾ
³ý×÷Òµ.
tee
[UNIX ´Ó¹ÜµÀÐÐÒµ½èÀ´µÄÖ÷Òâ.]
ÕâÊÇÒ»¸öÖض¨Ïò²Ù×÷, µ«ÊÇÓÐЩ²»Í¬. ¾ÍÏñ¹ÜµÀÖеÄ"Èýͨ"Ò»Ñù, Õâ¸öÃüÁî¿ÉÒÔ½«ÃüÁî»ò
Õ߹ܵÀÃüÁîµÄÊä³ö³é³öµ½Ò»¸öÎļþÖÐ,¶øÇÒ²¢²»Ó°Ïì½á¹û. µ±ÄãÏ뽫һ¸öÕýÔÚÔËÐеĽø³Ì
µÄÊä³ö±£´æµ½ÎļþÖÐʱ, »òÕßΪÁËdebug¶ø±£´æÊä³ö¼Ç¼µÄʱºò, Õâ¸öÃüÁî¾Í·Ç³£ÓÐÓÃÁË.
(Öض¨Ïò)
|----> to file
|
==========================|====================
command ---> command ---> |tee ---> command ---> ---> output of pipe
===============================================
1 cat listfile* | sort | tee check.file | uniq > result.file
(ÔÚ¶ÔÅÅÐòµÄ½á¹û½øÐÐ uniq (È¥µôÖظ´ÐÐ) ֮ǰ,Îļþ check.file Öб£´æÁËÅŹýÐòµÄ
"listfiles".)
mkfifo
Õâ¸ö²»´óÒýÈË×¢ÒâµÄÃüÁî¿ÉÒÔ´´½¨Ò»¸öÃüÃû¹ÜµÀ, ²¢²úÉúÒ»¸öÁÙʱµÄÏȽøÏȳöµÄbuffer
ÓÃÀ´ÔÚÁ½¸ö½ø³Ì¼ä´«ÊäÊý¾Ý. [3] µäÐ͵ÄʹÓÃÊÇÒ»¸ö½ø³ÌÏòFIFOÖÐдÊý¾Ý, ÁíÒ»¸ö½ø³Ì¶Á
³öÀ´. ²Î¼û Example A-15.
pathchk
Õâ¸öÃüÁîÓÃÀ´¼ì²éÎļþÃûµÄÓÐЧÐÔ. Èç¹ûÎļþÃû³¬¹ýÁË×î´óÔÊÐí³¤¶È(255 ¸ö×Ö·û), »òÕß
ËüËùÔÚµÄÒ»¸ö»ò¶à¸ö·¾¶ËÑË÷²»µ½, ÄÇô¾Í»á²úÉúÒ»¸ö´íÎó½á¹û.
²»ÐÒµÄÊÇ,²¢²»Äܹ»·µ»ØÒ»¸ö¿Éʶ±ðµÄ´íÎóÂë, Òò´ËËüÔڽű¾Öм¸ºõûÓÐʲôÓÃ. Ò»°ã¶¼
ʹÓÃÎļþ²âÊÔ²Ù×÷.
dd
ÕâÒ²ÊÇÒ»¸ö²»Ì«³öÃûµÄ¹¤¾ß, µ«È´ÊÇÒ»¸öÁîÈË¿Ö¾åµÄ "Êý¾Ý¸´ÖÆ" ÃüÁî. ×ʼ, Õâ¸öÃü
ÁîÊDZ»ÓÃÀ´ÔÚUNIX ΢»úºÍIBM´óÐÍ»úÖ®¼äͨ¹ý´Å´øÀ´½»»»Êý¾Ý, Õâ¸öÃüÁîÏÖÔÚÈÔÈ»ÓÐËüµÄ
ÓÃ;. dd ÃüÁîÖ»²»¹ýÊǼòµ¥µÄ¿½±´Ò»¸öÎļþ (»òÕß stdin/stdout), µ«ÊÇËü»á×öһЩת
»». ϱßÊÇһЩ¿ÉÄܵÄת»», ±ÈÈç ASCII/EBCDIC, [4] ´óд/Сд, ÔÚÊäÈëºÍÊä³öÖ®¼ä
µÄ×ֽڶԵĽ»»», »¹ÓжÔÊäÈëÎļþ×öһЩ½ØͷȥβµÄ¹¤×÷. dd --help ÁгöÁËËùÓÐת»»,
»¹ÓÐÕâ¸öÇ¿Á¦¹¤¾ßµÄһЩÆäËûÑ¡Ïî.
1 # ½«Ò»¸öÎļþת»»Îª´óд:
2
3 dd if=$filename conv=ucase > $filename.uppercase
4 # lcase # ת»»ÎªÐ¡Ð´
Example 12-52 Ò»¸ö¿½±´×ÔÉíµÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # self-copy.sh
3
4 # Õâ¸ö½Å±¾½«»á¿½±´×ÔÉí.
5
6 file_subscript=copy
7
8 dd if=$0 of=$0.$file_subscript 2>/dev/null
9 # ×èÖ¹dd²úÉúµÄÏûÏ¢: ^^^^^^^^^^^
10
11 exit $?
################################End Script######################################
###
Example 12-53 Á·Ï°dd
################################Start Script####################################
###
1 #!/bin/bash
2 # exercising-dd.sh
3
4 # ÓÉStephane Chazelas±àд.
5 # ±¾ÎÄ×÷Õß×öÁËÉÙÁ¿ÐÞ¸Ä.
6
7 input_file=$0 # ½Å±¾±¾Éí.
8 output_file=log.txt
9 n=3
10 p=5
11
12 dd if=$input_file of=$output_file bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev
/null
13 # ´Ó½Å±¾ÖаÑλÖÃnµ½pµÄ×Ö·ûÌáÈ¡³öÀ´.
14
15 # -------------------------------------------------------
16
17 echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null
18 # ´¹Ö±µÄ echo "hello world" .
19
20 exit 0
################################End Script######################################
###
ΪÁËչʾddµÄ¶àÖÖÓÃ;, ÈÃÎÒÃÇʹÓÃËüÀ´¼Ç¼°´¼ü.
Example 12-54 ¼Ç¼°´¼ü
################################Start Script####################################
###
1 #!/bin/bash
2 # dd-keypress.sh: ¼Ç¼°´¼ü, ²»ÐèÒª°´»Ø³µ.
3
4
5 keypresses=4 # ¼Ç¼°´¼üµÄ¸öÊý.
6
7
8 old_tty_setting=$(stty -g) # ±£´æÀϵÄÖÕ¶ËÉèÖÃ.
9
10 echo "Press $keypresses keys."
11 stty -icanon -echo # ½ûÓñê׼ģʽ.
12 # ½ûÓñ¾µØ echo.
13 keys=$(dd bs=1 count=$keypresses 2> /dev/null)
14 # Èç¹û²»Ö¸¶¨ÊäÈëÎļþµÄ»°, 'dd' ʹÓñê×¼ÊäÈë.
15
16 stty "$old_tty_setting" # »Ö¸´ÀϵÄÖÕ¶ËÉèÖÃ.
17
18 echo "You pressed the \"$keys\" keys."
19
20 # ¸Ðл Stephane Chazelas, ÑÝʾÁËÕâÖÖ·½·¨.
21 exit 0
################################End Script######################################
###
dd ÃüÁî¿ÉÒÔÔÚÊý¾ÝÁ÷ÉÏ×öËæ¼´´æÈ¡.
1 echo -n . | dd bs=1 seek=4 of=file conv=notrunc
2 # "conv=notrunc" Ñ¡ÏîÒâζ×ÅÊä³öÎļþ²»Äܱ»½Ø¶Ì.
3
4 # Thanks, S.C.
dd ÃüÁî¿ÉÒÔ½«Êý¾Ý»ò´ÅÅ̾µÏñ¿½±´µ½É豸ÖÐ, Ò²¿ÉÒÔ´ÓÉ豸Öп½±´Êý¾Ý»ò´ÅÅ̾µÏñ, ±È
Èç˵´ÅÅÌ»ò´Å´øÉ豸¶¼¿ÉÒÔ (Example A-5). ͨ³£ÓÃÀ´´´½¨Æô¶¯ÅÌ.
dd if=kernel-image of=/dev/fd0H1440
ͬÑùµÄ, dd ¿ÉÒÔ¿½±´ÈíÅ̵ÄÕû¸öÄÚÈÝ(ÉõÖÁÊÇÆäËû²Ù×÷ϵͳµÄ´ÅÅ̸ñʽ) µ½Ó²ÅÌÇý¶¯Æ÷ÉÏ
(ÒÔ¾µÏñÎļþµÄÐÎʽ).
dd if=/dev/fd0 of=/home/bozo/projects/floppy.img
dd ÃüÁÓÐһЩÆäËûÓÃ;, °üÀ¨¿ÉÒÔ³õʼ»¯ÁÙʱ½»»»Îļþ (Example 28-2) ºÍ ramdisks
(ÄÚ´æÐéÄâÓ²ÅÌ) (Example 28-3). ËüÉõÖÁ¿ÉÒÔ×öһЩ¶ÔÕû¸öÓ²ÅÌ·ÖÇøµÄµ×²ã¿½±´, ËäÈ»
²»½¨ÒéÕâô×ö.
һЩ(¿ÉÄÜÊDZȽÏÎÞÁĵÄ)ÈË×Ü»áÏëһЩ¹ØÓÚ dd ÃüÁîµÄÓÐȤµÄÓ¦ÓÃ.

Example 12-55 °²È«µÄɾ³ýÒ»¸öÎļþ


################################Start Script####################################
###
1 #!/bin/bash
2 # blot-out.sh: ɾ³ýÒ»¸öÎļþËùÓеļǼ.
3
4 # Õâ¸ö½Å±¾»áʹÓÃËæ¼´×Ö½Ú½»ÌæµÄ¸²¸Ç
5 #+ Ä¿±êÎļþ, ²¢ÇÒÔÚ×îÖÕɾ³ýÕâ¸öÎļþ֮ǰÇåÁã.
6 # Õâô×öÖ®ºó, ¼´Ê¹Äãͨ¹ý´«Í³ÊÖ¶ÎÀ´¼ì²é´ÅÅÌÉÈÇø
7 #+ Ò²²»ÄÜ°ÑÎļþԭʼÊý¾ÝÖØлָ´.
8
9 PASSES=7 # ÆÆ»µÎļþµÄ´ÎÊý.
10 # Ìá¸ßÕâ¸öÊý×Ö»á¼õÂý½Å±¾ÔËÐеÄËÙ¶È,
11 #+ ÓÈÆäÊǶԳߴç±È½Ï´óµÄÄ¿±êÎļþ½øÐвÙ×÷µÄʱºò.
12 BLOCKSIZE=1 # ´øÓÐ /dev/urandom µÄ I/O ÐèÒªµ¥Î»¿é³ß´ç,
13 #+ ·ñÔòÄã¿ÉÄÜ»á»ñµÃÆæ¹ÖµÄ½á¹û.
14 E_BADARGS=70 # ²»Í¬µÄ´íÎóÍ˳öÂë.
15 E_NOT_FOUND=71
16 E_CHANGED_MIND=72
17
18 if [ -z "$1" ] # ûָ¶¨ÎļþÃû.
19 then
20 echo "Usage: `basename $0` filename"
21 exit $E_BADARGS
22 fi
23
24 file=$1
25
26 if [ ! -e "$file" ]
27 then
28 echo "File \"$file\" not found."
29 exit $E_NOT_FOUND
30 fi
31
32 echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)?
"
33 read answer
34 case "$answer" in
35 [nN]) echo "Changed your mind, huh?"
36 exit $E_CHANGED_MIND
37 ;;
38 *) echo "Blotting out file \"$file\".";;
39 esac
40
41
42 flength=$(ls -l "$file" | awk '{print $5}') # 5 ÊÇÎļþ³¤¶È.
43 pass_count=1
44
45 chmod u+w "$file" # Allow overwriting/deleting the file.
46
47 echo
48
49 while [ "$pass_count" -le "$PASSES" ]
50 do
51 echo "Pass #$pass_count"
52 sync # Ë¢ÐÂbuffer.
53 dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength
54 # ʹÓÃËæ»ú×Ö½Ú½øÐÐÌî³ä.
55 sync # ÔÙË¢ÐÂbuffer.
56 dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength
57 # ÓÃ0Ìî³ä.
58 sync # ÔÙË¢ÐÂbuffer.
59 let "pass_count += 1"
60 echo
61 done
62
63
64 rm -f $file # ×îºó, ɾ³ýÕâ¸öÒѾ­±»ÆÆ»µµÃ²»³ÉÑù×ÓµÄÎļþ.
65 sync # ×îºóÒ»´ÎË¢ÐÂbuffer.
66
67 echo "File \"$file\" blotted out and deleted."; echo
68
69
70 exit 0
71
72 # ÕâÊÇÒ»ÖÖÕæÕý°²È«µÄɾ³ýÎļþµÄ°ì·¨,
73 #+ µ«ÊÇЧÂʱȽϵÍ, ÔËÐбȽÏÂý.
74 # GNU µÄÎļþ¹¤¾ß°üÖÐµÄ "shred" ÃüÁî,
75 #+ Ò²¿ÉÒÔÍê³ÉÏàͬµÄ¹¤×÷, ²»¹ý¸üÓÐЧÂÊ.
76
77 # ʹÓÃÆÕͨµÄ·½·¨ÊDz»¿ÉÄÜÖØлָ´Õâ¸öÎļþÁË.
78 # È»¶ø . . .
79 #+ Õâ¸ö¼òµ¥µÄÀý×ÓÊDz»Äܹ»µÖ¿¹
80 #+ ÄÇЩ¾­Ñé·á¸»²¢ÇÒÕý¹æµÄ·ÖÎö.
81
82 # Õâ¸ö½Å±¾¿ÉÄܲ»»áºÜºÃµÄÔËÐÐÔÚÈÕÖ¾ÎļþϵͳÉÏ.(ÒëÕß×¢: JFS)
83 # Á·Ï° (ºÜÄÑ): ÏñËü×öµÄÄÇÑùÐÞÕýÕâ¸öÎÊÌâ.
84
85
86
87 # Tom VierµÄÎļþɾ³ý°ü¿ÉÒÔ¸ü¼Ó³¹µ×
88 #+ µÄɾ³ýÎļþ, ±ÈÕâ¸ö¼òµ¥µÄÀý×ÓÀ÷º¦µÃ¶à.
89 # http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2
90
91 # Èç¹ûÏë¶Ô°²È«É¾³ýÎļþÕâÒ»ÂÛÌâ½øÐÐÉî¶ÈµÄ·ÖÎö,
92 #+ ¿ÉÒԲμûPeter GutmannµÄÒ³Ãæ,
93 #+ "Secure Deletion of Data From Magnetic and Solid-State Memory".
94 # http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
################################End Script######################################
###
od
od(octal dump)¹ýÂËÆ÷, ½«»á°ÑÊäÈë(»òÎļþ)ת»»Îª8½øÖÆ»òÕßÆäËû½øÖÆ. ÔÚÄãÐèÒª²é¿´
»ò´¦ÀíһЩ¶þ½øÖÆÊý¾ÝÎļþ»òÕßÒ»¸ö²»¿É¶ÁµÄϵͳÉ豸ÎļþµÄʱºò, Õâ¸öÃüÁî·Ç³£ÓÐÓÃ,
±ÈÈç/dev/urandom,»òÕßÊÇÒ»¸ö¶þ½øÖÆÊý¾Ý¹ýÂËÆ÷. ²Î¼û Example 9-28 ºÍ
Example 12-13.
hexdump
¶Ô¶þ½øÖÆÎļþ½øÐÐ 16½øÖÆ, 8½øÖÆ, 10½øÖÆ, »òÕß ASCII ÂëµÄ²éÔĶ¯×÷. Õâ¸öÃüÁî´óÌå
ÉÏÓëÉϱߵÄodÃüÁî×÷ÓÃÏàͬ, µ«ÊÇÔ¶²»¼° od ÃüÁîÓÐÓÃ.
objdump
ÏÔʾ±àÒëºóµÄ2½øÖÆÎļþ»ò2½øÖÆ¿ÉÖ´ÐÐÎļþµÄÐÅÏ¢, ÒÔ16½øÖƵÄÐÎʽÏÔʾ, »òÕßÏÔʾ·´»ã
±àÁбí(ʹÓÃ-dÑ¡Ïî).
bash$ objdump -d /bin/ls
/bin/ls: file format elf32-i386
Disassembly of section .init:
080490bc <.init>:
80490bc: 55 push %ebp
80490bd: 89 e5 mov %esp,%ebp
. . .
mcookie
Õâ¸öÃüÁî»á²úÉúÒ»¸ö"magic cookie", ÕâÊÇÒ»¸ö128-bit (32-×Ö·û) µÄαËæ»ú16½øÖÆÊý×Ö,
Õâ¸öÊý×ÖÒ»°ã¶¼ÓÃÀ´×÷ΪX serverµÄ¼øȨ"Ç©Ãû". Õâ¸öÃüÁ¿ÉÒÔÓÃÀ´Ôڽű¾ÖÐ×÷Ϊһ
ÖÖÉú³ÉËæ»úÊýµÄÊÖ¶Î, µ±È»ÕâÊÇÒ»ÖÖ"С³Ôµê"(ËäÈ»²»Ì«Õýͳ, µ«ÊǺܷ½±ã)µÄ·ç¸ñ.
1 random000=$(mcookie)
µ±È», Íê³ÉͬÑùµÄÄ¿µÄ»¹¿ÉÒÔʹÓà md5 ÃüÁî.
1 # ²úÉú¹ØÓڽű¾±¾ÉíµÄ md5 checksum.
2 random001=`md5sum $0 | awk '{print $1}'`
3 # ʹÓà 'awk' À´È¥µôÎļþÃû.
mcookie »¹¸ø³öÁ˲úÉú"Ψһ"ÎļþÃûµÄÁíÒ»ÖÖ·½·¨.
Example 12-56 ÎļþÃû²úÉúÆ÷
################################Start Script####################################
###
1 #!/bin/bash
2 # tempfile-name.sh: ÁÙʱÎļþÃû²úÉúÆ÷
3
4 BASE_STR=`mcookie` # 32-×Ö·ûµÄ magic cookie.
5 POS=11 # ×Ö·û´®ÖÐËæ±ãµÄÒ»¸öλÖÃ.
6 LEN=5 # È¡µÃ $LEN ³¤¶ÈÁ¬ÐøµÄ×Ö·û´®.
7
8 prefix=temp # ×îÖÕµÄÒ»¸öÁÙʱÎļþ.
9 # Èç¹ûÏëÈÃÕâ¸öÎļþ¸ü¼ÓΨһ,
10 #+ ¿ÉÒÔ¶ÔÕâ¸öǰ׺ҲʹÓÃϱߵķ½·¨À´Éú³É.
11
12 suffix=${BASE_STR:POS:LEN}
13 # ÌáÈ¡´ÓµÚ11¸ö×Ö·ûÖ®ºóµÄ³¤¶ÈΪ5µÄ×Ö·û´®.
14
15 temp_filename=$prefix.$suffix
16 # ¹¹ÔìÎļþÃû.
17
18 echo "Temp filename = "$temp_filename""
19
20 # sh tempfile-name.sh
21 # Temp filename = temp.e19ea
22
23 # ÓëʹÓà 'date' ÃüÁî(²Î¿¼ ex51.sh)À´´´½¨Î¨Ò»ÎļþÃû
24 #+ µÄ·½·¨Ïà±È½Ï.
25
26 exit 0
################################End Script######################################
###
units
Õâ¸ö¹¤¾ßÓÃÀ´ÔÚ²»Í¬µÄ¼ÆÁ¿µ¥Î»Ö®¼ä»¥Ïàת»». µ±ÄãÔÚ½»»¥Ä£Ê½ÏÂÕý³£µ÷ÓÃʱ, »á·¢ÏÖÔÚ
½Å±¾ÖÐ units Ò²ºÜÓÐÓÃ.
Example 12-57 ½«Ã×ת»»ÎªÓ¢Àï
################################Start Script####################################
###
1 #!/bin/bash
2 # unit-conversion.sh
3
4
5 convert_units () # ͨ¹ý²ÎÊýÈ¡µÃÐèҪת»»µÄµ¥Î».
6 {
7 cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
8 # ³ýÁËÕæÕýÐèҪת»»µÄ²¿·Ö±£ÁôÏÂÀ´Íâ,ÆäËûµÄ²¿·Ö¶¼È¥µô.
9 echo "$cf"
10 }
11
12 Unit1=miles
13 Unit2=meters
14 cfactor=`convert_units $Unit1 $Unit2`
15 quantity=3.73
16
17 result=$(echo $quantity*$cfactor | bc)
18
19 echo "There are $result $Unit2 in $quantity $Unit1."
20
21 # Èç¹ûÄã´«µÝÁËÁ½¸ö²»Æ¥ÅäµÄµ¥Î»»á·¢Éúʲô?
22 #+ ±ÈÈç·Ö±ð´«ÈëӢĶºÍÓ¢Àï?
23
24 exit 0
################################End Script######################################
###
m4
Ò»¸öÒþ²ØµÄ²Æ±¦, m4 ÊÇÒ»¸öÇ¿Á¦µÄºê´¦Àí¹ýÂËÆ÷, [5] ²î²»¶à¿ÉÒÔ˵ÊÇÒ»ÖÖÓïÑÔÁË. Ëä
È»×ʼÕâ¸ö¹¤¾ßÊÇÓÃÀ´×÷Ϊ RatFor µÄÔ¤´¦ÀíÆ÷¶ø±àдµÄ, µ«ÊǺóÀ´Ö¤Ã÷ m4 ×÷Ϊ¶À
Á¢µÄ¹¤¾ßÒ²ÊǷdz£ÓÐÓõÄ. ÊÂʵÉÏ, m4 ½áºÏÁËÐí¶à¹¤¾ßµÄ¹¦ÄÜ, ±ÈÈç eval, tr, ºÍ awk,
³ý´ËÖ®Íâ, Ëü»¹Ê¹µÃºêÀ©Õ¹±äµÃÈÝÒ×.
ÔÚ 2004Äê4Ô嵀 Linux Journal µÄÎÊÌâÁбíÖÐÓÐһƪ¹ØÓÚ m4 ÃüÁîÓ÷¨µÃ·Ç³£ºÃµÄÎÄÕÂ.
Example 12-58 ʹÓà m4
################################Start Script####################################
###
1 #!/bin/bash
2 # m4.sh: ʹÓà m4 ºê´¦ÀíÆ÷
3
4 # ×Ö·û²Ù×÷
5 string=abcdA01
6 echo "len($string)" | m4 # 7
7 echo "substr($string,4)" | m4 # A01
8 echo "regexp($string,[0-1][0-1],\&Z)" | m4 # 01Z
9
10 # ËãÊõ²Ù×÷
11 echo "incr(22)" | m4 # 23
12 echo "eval(99 / 3)" | m4 # 33
13
14 exit 0
################################End Script######################################
###
doexec
doexec ÃüÁîÔÊÐí½«Ò»¸öËæ±ãµÄ²ÎÊýÁÐ±í´«µÝµ½Ò»¸ö¶þ½øÖÆ¿ÉÖ´ÐÐÎļþÖÐ. ÌرðµÄ, ÉõÖÁ
¿ÉÒÔ´«µÝ arg[0] (Ï൱Óڽű¾ÖÐµÄ $0 ), ÕâÑù¿ÉÒÔʹÓò»Í¬µÄÃû×ÖÀ´µ÷ÓÃÕâ¸ö¿ÉÖ´ÐÐ
Îļþ, ²¢ÇÒͨ¹ý²»Í¬µÄµ÷ÓõÄÃû×Ö, ¿ÉÒÔÈÃÕâ¸ö¿ÉÖ´ÐÐÎļþÖ´Ðв»Í¬µÄ¶¯×÷. ÕâÒ²¿ÉÒÔ
˵ÊÇÒ»ÖÖ½«²ÎÊý´«µÝµ½¿ÉÖ´ÐÐÎļþÖеıȽÏÈÆȦ×ÓµÄ×ö·¨.
±ÈÈç, /usr/local/bin Ŀ¼¿ÉÄÜ°üº¬Ò»¸ö "aaa" µÄ¶þ½øÖÆÎļþ. ʹÓÃ
doexec /usr/local/bin/aaa list ¿ÉÒÔ Áгö µ±Ç°¹¤×÷Ŀ¼ÏÂËùÓÐÒÔ "a" ¿ªÍ·µÄµÄÎÄ
¼þ, ¶øʹÓà doexec /usr/local/bin/aaa delete ½«»áɾ³ýÕâЩÎļþ.
×¢Òâ: ¿ÉÖ´ÐÐÎļþµÄ²»Í¬ÐÐΪ±ØÐ붨ÒåÔÚ¿ÉÖ´ÐÐÎļþ×ÔÉíµÄ´úÂëÖÐ, ¿ÉÒÔʹÓÃÈçϵÄ
shell½Å±¾×÷Àà±È:
1 case `basename $0` in
2 "name1" ) do_something;;
3 "name2" ) do_something_else;;
4 "name3" ) do_yet_another_thing;;
5 * ) bail_out;;
6 esac
dialog
dialog ¹¤¾ß¼¯ÌṩÁËÒ»Öִӽű¾Öе÷Óý»»¥¶Ô»°¿òµÄ·½·¨. dialog µÄ¸üºÃµÄ±äÖÖ°æ±¾ÊÇ
-- gdialog, Xdialog, ºÍ kdialog -- ÊÂʵÉÏÊǵ÷ÓÃµÄ X-Windows µÄ½çÃ湤¾ß¼¯. ²Î
¼û Example 33-19.
sox
sox ÃüÁî, "sound exchange" (ÉùÒôת»»)ÃüÁî, ¿ÉÒÔ½øÐÐÉùÒôÎļþµÄת»». ÊÂʵÉÏ,¿ÉÖ´
ÐÐÎļþ /usr/bin/play (ÏÖÔÚ²»½¨ÒéʹÓÃ) Ö»²»¹ýÊÇ sox µÄÒ»¸ö shell °ü×°Æ÷¶øÒÑ.
¾Ù¸öÀý×Ó, sox soundfile.wav soundfile.au ½«»á°ÑÒ»¸ö WAV ÉùÒôÎļþת»»³ÉÒ»¸ö
(Sun ÒôƵ¸ñʽ) AU ÉùÒôÎļþ.
Shell ½Å±¾·Ç³£ÊʺÏÓÚʹÓà sox µÄÉùÒô²Ù×÷À´Åú´¦ÀíÉùÒôÎļþ. ±ÈÈç, ²Î¼û
Linux Radio Timeshift HOWTO ºÍ MP3do Project.
×¢ÒâÊÂÏî:
[1] Õâ¸ö¹¤¾ßÊÂʵÉÏÊÇ´Ó Debian Linux ·¢ÐаæÖеÄÒ»¸ö½Å±¾½è¼ø¹ýÀ´µÄ.
[2] ´òÓ¡¶ÓÁÐ ¾ÍÊÇ"ÔÚÏߵȴý"´òÓ¡µÄ×÷Òµ×é.
[3] ¶ÔÓÚ±¾»°ÌâµÄÒ»¸öÍêÃÀµÄ½éÉÜ, Çë²Î¼û Andy Vaught µÄÎÄÕÂ, ÃüÃû¹ÜµÀµÄ½éÉÜ,
(http://www2.linuxjournal.com/lj-issues/issue41/2156.html), ÕâÊÇ
Linux Journal (http://www.linuxjournal.com/)1997Äê9ÔµÄÒ»¸öÎÊÌâ.
[4] EBCDIC (·¢ÒôÊÇ "ebb-sid-ick") Êǵ¥´Ê (Extended Binary Coded Decimal
Interchange Code) µÄÊ××ÖĸËõд. ÕâÊÇ IBM µÄÊý¾Ý¸ñʽ, ÏÖÔÚÒѾ­²»³£¼ûÁË.
dd ÃüÁîµÄ conv=ebcdic Ñ¡ÏîµÄÒ»¸ö±È½ÏÆæÒìµÄʹÓ÷½·¨ÊǶÔÒ»¸öÎļþ½øÐпìËÙ¶ø
ÇÒÈÝÒ×µ«²»Ì«°²È«µÄ±àÂë.
1 cat $file | dd conv=swab,ebcdic > $file_encrypted
2 # ±àÂë (¿´ÆðÀ´ºÃÏñûʲôÓÃ).
3 # Ó¦¸Ã½»»»×Ö½Ú(swab), Óеã»Þɬ.
4
5 cat $file_encrypted | dd conv=swab,ascii > $file_plaintext
6 # ½âÂë.
[5] ºê ÊÇÒ»¸ö·ûºÅ³£Á¿, ½«»á±»À©Õ¹³ÉÒ»¸öÃüÁî×Ö·û´®»òÕßһϵÁеIJÎÊý²Ù×÷.

µÚ13Õ ϵͳÓë¹ÜÀíÃüÁî
======================
ÔÚ/etc/rc.dĿ¼ÖеÄÆô¶¯ºÍ¹Ø»ú½Å±¾Öаüº¬Á˺öàÓÐÓõÄ(ºÍûÓõÄ)ÕâЩϵͳ¹ÜÀíÃüÁî. ÕâЩ
ÃüÁîͨ³£×ÜÊDZ»rootÓû§Ê¹ÓÃ, ÓÃÓëϵͳά»¤»òÕßÊǽô¼±ÎļþϵͳÐÞ¸´.Ò»¶¨ÒªÐ¡ÐÄʹÓÃÕâЩ¹¤
¾ß, ÒòΪÈç¹ûÀÄÓõĻ°, ËüÃÇ»áËð»µÄãµÄϵͳ.
Users ºÍ Groups ÀàÃüÁî
users
ÏÔʾËùÓеĵǼµÄÓû§. Õâ¸öÃüÁîÓë who -q »ù±¾Ò»ÖÂ.
groups
Áгöµ±Ç°Óû§ºÍËûËùÊôÓÚµÄ×é. ÕâÏ൱ÓÚ $GROUPS ÄÚ²¿±äÁ¿, µ«ÊÇÕâ¸öÃüÁ»á¸ø³ö×éÃû
×Ö, ¶ø²»ÊÇÊý×Ö.
bash$ groups
bozita cdrom cdwriter audio xgrp
bash$ echo $GROUPS
501
chown, chgrp
chown ÃüÁ»áÐÞ¸ÄÒ»¸ö»ò¶à¸öÎļþµÄËùÓÐȨ. ¶ÔÓÚrootÀ´ËµÕâÊÇÒ»Öַdz£ºÃµÄ½«ÎļþµÄ
ËùÓÐȨ´ÓÒ»¸öÓû§»»µ½ÁíÒ»¸öÓû§µÄ·½·¨. Ò»¸öÆÕͨÓû§²»ÄÜÐÞ¸ÄÎļþµÄËùÓÐȨ, ¼´Ê¹Ëû
ÊÇÎļþµÄËÞÖ÷Ò²²»ÐÐ. [1]
root# chown bozo *.txt
chgrp ½«»áÐÞ¸ÄÒ»¸ö»ò¸öÎļþµ³×éËùÓÐȨ. Äã±ØÐëÊÇÕâЩÎļþµÄËÞÖ÷, ²¢ÇÒÊÇÄ¿µÄ×éµÄ³É
Ô±(»òÕßroot), ÕâÑù²ÅÄÜʹÓÃÕâ¸ö²Ù×÷.
1 chgrp --recursive dunderheads *.data
2 # "dunderheads"(ÒëÕß: ÔÎ,´À²Å...) ×éÏÖÔÚÓµÓÐÁËËùÓеÄ"*.data"Îļþ.
3 #+ °üÀ¨ËùÓÐ$PWDĿ¼ÏµÄ×ÓĿ¼ÖеÄÎļþ(--recursiveµÄ×÷ÓþÍÊÇ°üº¬×ÓĿ¼).
useradd, userdel
useradd ¹ÜÀíÃüÁ»áÔÚϵͳÉÏÌí¼ÓÒ»¸öÓû§ÕʺÅ, ²¢ÇÒÈç¹ûÖ¸¶¨µÄ»°, »¹»áΪÌض¨µÄÓÃ
»§´´½¨homeĿ¼. ÏàÓ¦µÄuserdel ÃüÁ»á´ÓϵͳÉÏɾ³ýÒ»¸öÓû§ÕʺÅ, [2] ²¢ÇÒɾ³ýÏà
Ó¦µÄÎļþ.
×¢Òâ: adduserÃüÁîÓëuseraddÊÇÏàͬµÄ, adduserͨ³£¶¼ÊÇÒ»¸ö·ûºÅÁ´½Ó.
usermod
ÐÞ¸ÄÓû§ÕʺÅ. ¿ÉÒÔÐÞ¸ÄÃÜÂë, ×éÉí·Ý, ½ØÖ¹ÈÕÆÚ, »òÕ߸ø¶¨Óû§ÕʺŵÄÆäËûµÄÊôÐÔ. ʹ
ÓÃÕâ¸öÃüÁî, Óû§µÄÃÜÂë¿ÉÄܻᱻËø¶¨, ÒòΪÃÜÂë»áÓ°Ïìµ½ÕʺŵÄÓÐЧÐÔ.
groupmod
ÐÞ¸ÄÖ¸¶¨×é. ×éÃû×Ö»òÕßIDºÅ¶¼¿ÉÒÔʹÓÃÕâ¸öÃüÁîÀ´ÐÞ¸Ä.
id
id ½«»áÁгöµ±Ç°½ø³ÌµÄÕæʵºÍÓÐЧÓû§ID, »¹ÓÐÓû§µÄ×éID. ÕâÓëBashµÄÄÚ²¿±äÁ¿
$UID, $EUID, ºÍ $GROUPS ºÜÏàÏñ.
bash$ id
uid=501(bozo) gid=501(bozo) groups=501(bozo),22(cdrom),80(cdwriter),81(
audio)
bash$ echo $UID
501
×¢Òâ: id ÃüÁîÖ»ÓÐÔÚÓÐЧIDÓëÕæʵID²»·ûʱ²Å»áÏÔʾÓÐЧid.
²Î¼û Example 9-5.
who
ÏÔʾϵͳÉÏËùÓÐÒѾ­µÇ¼µÄÓû§.
bash$ who
bozo tty1 Apr 27 17:45
bozo pts/0 Apr 27 17:46
bozo pts/1 Apr 27 17:47
bozo pts/2 Apr 27 17:49
-m Ñ¡Ï»áÖ»¸ø³öµ±Ç°Óû§µÄÏêϸÐÅÏ¢. ½«ÈÎÒâÁ½¸ö²ÎÊý´«µÝµ½whoÖÐ ¶¼µÈ¼ÛÓÚwho -m,
¾ÍÏñ who am i »òÕß who The Man.
bash$ who -m
localhost.localdomain!bozo pts/2 Apr 27 17:49
whoami Óëwho -m ºÜÏàËÆ, µ«ÊÇÖ»ÁгöÓû§Ãû.
bash$ whoami
bozo
w
ÏÔʾËùÓеĵǼµÄÓû§ºÍÊôÓÚËüÃǵĽø³Ì. ÕâÊÇÒ»¸öwhoµÄÀ©Õ¹°æ±¾. wµÄÊä³ö¿ÉÒÔͨ¹ý¹Ü
µÀ´«µÝµ½grepÖÐ, ÕâÑù¾Í¿ÉÒÔ²éÕÒÖ¸¶¨µÄÓû§»ò½ø³Ì.
bash$ w | grep startx
bozo tty1 - 4:22pm 6:41 4.47s 0.45s startx
logname
ÏÔʾµ±Ç°Óû§µÄµÇ¼Ãû(¿ÉÒÔÔÚ/var/run/utmpÖÐÕÒµ½). ÕâÓëÉϱߵÄwhoamiºÜÏà½ü.
bash$ logname
bozo
bash$ whoami
bozo
È»¶ø...
bash$ su
Password: ......
bash# whoami
root
bash# logname
bozo
×¢Òâ: lognameÖ»»á´òÓ¡³öµÇ¼µÄÓû§Ãû, ¶øwhoami ½«»á¸ø³ö¸½×ŵ½µ±Ç°½ø³ÌµÄÓû§Ãû.
¾ÍÏñÎÒÃÇÉϱ߿´µ½µÄÄÇÑù, ÕâÁ½¸öÃû×ÖÓÐʱ»á²»Í¬.
su
ʹÓÃÒ»¸ö´úÌæµÄÓû§À´ÔËÐÐÒ»¸ö³ÌÐò»ò½Å±¾. su rjones ½«»áÒÔ rjones À´Æô¶¯Ò»¸ö
shell. Ò»¸ö²»¼Ó²ÎÊýµÄsuĬÈϾÍÊÇroot. ²Î¼û Example A-15.
sudo
ÒÔroot(»òÆäËûÓû§)µÄÉí·ÝÀ´ÔËÐÐÒ»¸öÃüÁî. Õâ¸öÃüÁî¿ÉÒÔÔËÐÐÔڽű¾ÖÐ, ÕâÑù¾ÍÔÊÐíÒÔ
Õý¹æµÄÓû§Éí·ÝÀ´ÔËÐнű¾.
1 #!/bin/bash
2
3 # һЩÃüÁî.
4 sudo cp /root/secretfile /home/bozo/secret
5 # һЩÃüÁî.
Îļþ /etc/sudoers ³ÖÓÐÔÊÐíµ÷ÓÃsudoµÄÓû§Ãû.
passwd
ÉèÖÃ, ÐÞ¸Ä, »òÕß¹ÜÀíÓû§µÄÃÜÂë.
passwd ÃüÁî¿ÉÒÔÓÃÔڽű¾ÖÐ, µ«¿ÉÄÜÄã²»ÏëÕâôÓÃ.
Example 13-1 ÉèÖÃÒ»¸öÐÂÃÜÂë
################################Start Script####################################
###
1 #!/bin/bash
2 # setnew-password.sh: Ö»ÓÃÓÚ˵Ã÷Ä¿µÄ.
3 # Èç¹ûÕæÕýÔËÐÐÕâ¸ö½Å±¾²¢²»ÊÇÒ»¸öºÃÖ÷Òâ.
4 # Õâ¸ö½Å±¾±ØÐëÒÔrootÉí·ÝÔËÐÐ.
5
6 ROOT_UID=0 # Root µÄ $UID 0.
7 E_WRONG_USER=65 # ²»ÊÇ root?
8
9 E_NOSUCHUSER=70
10 SUCCESS=0
11
12
13 if [ "$UID" -ne "$ROOT_UID" ]
14 then
15 echo; echo "Only root can run this script."; echo
16 exit $E_WRONG_USER
17 else
18 echo
19 echo "You should know better than to run this script, root."
20 echo "Even root users get the blues... "
21 echo
22 fi
23
24
25 username=bozo
26 NEWPASSWORD=security_violation
27
28 # ¼ì²ébozoÊÇ·ñÔÚÕâÀï.
29 grep -q "$username" /etc/passwd
30 if [ $? -ne $SUCCESS ]
31 then
32 echo "User $username does not exist."
33 echo "No password changed."
34 exit $E_NOSUCHUSER
35 fi
36
37 echo "$NEWPASSWORD" | passwd --stdin "$username"
38 # 'passwd'ÃüÁî '--stdin' Ñ¡ÏîÔÊÐí
39 #+ ´Óstdin(»òÕ߹ܵÀ)ÖлñµÃÒ»¸öеÄÃÜÂë.
40
41 echo; echo "User $username's password changed!"
42
43 # Ôڽű¾ÖÐʹÓÃ'passwd'ÃüÁîÊǺÜΣÏÕµÄ.
44
45 exit 0
################################End Script######################################
###
passwd ÃüÁîµÄ -l, -u, ºÍ -d Ñ¡ÏîÔÊÐíËø¶¨, ½âËø,ºÍɾ³ýÒ»¸öÓû§µÄÃÜÂë. Ö»ÓÐroot
Óû§¿ÉÒÔʹÓÃÕâЩѡÏî.
ac
ÏÔʾÓû§µÇ¼µÄÁ¬½Óʱ¼ä, ¾ÍÏñ´Ó /var/log/wtmp ÖжÁÈ¡Ò»Ñù. ÕâÊÇGNUµÄÒ»¸öͳ¼Æ¹¤¾ß.
bash$ ac
total 68.08
last
Óû§×îºóµÇ¼µÄÐÅÏ¢, ¾ÍÏñ´Ó/var/log/wtmpÖжÁ³öÀ´Ò»Ñù. Õâ¸öÃüÁîÒ²¿ÉÒÔÓÃÀ´ÏÔʾԶ
¶ËµÇ¼.
±ÈÈç, ÏÔʾ×îºó¼¸´ÎϵͳµÄÖØÆôÐÅÏ¢:
bash$ last reboot
reboot system boot 2.6.9-1.667 Fri Feb 4 18:18 (00:02
)
reboot system boot 2.6.9-1.667 Fri Feb 4 15:20 (01:27
)
reboot system boot 2.6.9-1.667 Fri Feb 4 12:56 (00:49
)
reboot system boot 2.6.9-1.667 Thu Feb 3 21:08 (02:17
)
. . .
wtmp begins Tue Feb 1 12:50:09 2005
newgrp
²»Óõdzö¾Í¿ÉÒÔÐÞ¸ÄÓû§µÄ×éID. ²¢ÇÒÔÊÐí´æÈ¡ÐÂ×éµÄÎļþ. ÒòΪÓû§¿ÉÄÜͬʱÊôÓÚ¶à¸ö
×é, Õâ¸öÃüÁîºÜÉÙ±»Ê¹ÓÃ.

ÖÕ¶ËÀàÃüÁî
tty
ÏÔʾµ±Ç°Óû§Öն˵ÄÃû×Ö. ×¢Òâÿһ¸öµ¥¶ÀµÄxterm´°¿Ú¶¼±»Ëã×÷Ò»¸ö²»Í¬µÄÖÕ¶Ë.
bash$ tty
/dev/pts/1
stty
ÏÔʾ²¢(»ò)ÐÞ¸ÄÖÕ¶ËÉèÖÃ. Õâ¸ö¸´ÔÓÃüÁî¿ÉÒÔÓÃÔڽű¾ÖÐ, ²¢¿ÉÒÔÓÃÀ´¿ØÖÆÖն˵ÄÐÐΪºÍ
ÆäÏÔʾÊä³öµÄ·½·¨. ²Î¼ûÕâ¸öÃüÁîµÄinfoÒ³, ²¢×ÐϸѧϰËü.
Example 13-2 ÉèÖÃÒ»¸ö²Á³ý×Ö·û
################################Start Script####################################
###
1 #!/bin/bash
2 # erase.sh: ÔÚ¶ÁÈ¡ÊäÈëʱʹÓÃ"stty"À´ÉèÖÃÒ»¸ö²Á³ý×Ö·û.
3
4 echo -n "What is your name? "
5 read name # ÊÔÊÔÍ˸ñ¼ü
6 #+ À´É¾³ýÊäÈëµÄ×Ö·û.
7 # ÓÐʲôÎÊÌâ?
8 echo "Your name is $name."
9
10 stty erase '#' # ½« "hashmark" (#) ÉèÖÃΪÍ˸ñ×Ö·û.
11 echo -n "What is your name? "
12 read name # ʹÓÃ#À´É¾³ý×îºó¼üÈëµÄ×Ö·û.
13 echo "Your name is $name."
14
15 # ¾¯¸æ: ¼´Ê¹Ôڽű¾Í˳öºó, еļüÖµ»¹ÊDZ£³ÖÉèÖÃ.(ÒëÕß: ʹÓÃstty erase '^?' »Ö¸´)
16
17 exit 0
################################End Script######################################
###
Example 13-3 ¹ØµôÖն˶ÔÓÚÃÜÂëµÄecho
################################Start Script####################################
###
1 #!/bin/bash
2 # secret-pw.sh: ±£»¤ÃÜÂë²»±»ÏÔʾ
3
4 echo
5 echo -n "Enter password "
6 read passwd
7 echo "password is $passwd"
8 echo -n "If someone had been looking over your shoulder, "
9 echo "your password would have been compromised."
10
11 echo && echo # ÔÚÒ»¸ö"ÓëÁбí"ÖвúÉú2¸ö»»ÐÐ.
12
13
14 stty -echo # ¹Ø±ÕÆÁÄ»µÄecho.
15
16 echo -n "Enter password again "
17 read passwd
18 echo
19 echo "password is $passwd"
20 echo
21
22 stty echo # »Ö¸´ÆÁÄ»µÄecho.
23
24 exit 0
25
26 # ÏêϸµÄÔĶÁsttyÃüÁîµÄinfoÒ³, ÒÔ±ãÓÚ¸üºÃµÄÕÆÎÕÕâ¸öÓÐÓò¢Çҽƻ«µÄ¹¤¾ß.
################################End Script######################################
###
Ò»¸ö¾ßÓд´ÔìÐÔµÄsttyÃüÁîµÄÓ÷¨, ¼ì²âÓû§Ëù°´µÄ¼ü(²»ÓÃÇûسµ).
Example 13-4 °´¼ü¼ì²â
################################Start Script####################################
###
1 #!/bin/bash
2 # keypress.sh: ¼ì²âÓû§°´¼ü ("hot keys").
3
4 echo
5
6 old_tty_settings=$(stty -g) # ±£´æÀϵÄÉèÖÃ(Ϊʲô?).
7 stty -icanon
8 Keypress=$(head -c1) # »òÕß $(dd bs=1 count=1 2> /dev/null)
9 # ÔÚ·ÇGNUµÄϵͳÉÏ
10
11 echo
12 echo "Key pressed was \""$Keypress"\"."
13 echo
14
15 stty "$old_tty_settings" # »Ö¸´ÀϵÄÉèÖÃ.
16
17 # ¸Ðл, Stephane Chazelas.
18
19 exit 0
################################End Script######################################
###
²Î¼û Example 9-3.
×¢Òâ: ÖÕ¶ËÓëģʽterminals and modes
Ò»°ãÇé¿öÏÂ, Ò»¸öÖն˶¼Êǹ¤×÷ÔÚcanonical(±ê×¼)ģʽÏÂ. µ±Óû§°´¼üºó, ÊÂʵÉÏËù
²úÉúµÄ×Ö·û²¢Ã»ÓÐÂíÉÏ´«µÝµ½ÔËÐÐÔÚµ±Ç°ÖÕ¶ËÉϵijÌÐò. ÖÕ¶ËÉϵÄÒ»¸ö±¾µØµÄ»º´æ±£
´æÁËÕâЩ°´¼ü. µ±Óð´ÏÂENTER¼üµÄʱºò, ²Å»á½«ËùÓб£´æµÄ°´¼üÐÅÏ¢´«µÝµ½ÔËÐеijÌ
ÐòÖÐ. Õâ¾ÍÒâζ×ÅÔÚÖÕ¶ËÄÚ²¿´æÔÚÒ»¸ö»ù±¾µÄÐб༭Æ÷.

bash$ stty -a
speed 9600 baud; rows 36; columns 96; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <u
ndef>; eol2 = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnex
t = ^V; flush = ^O;
...
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tos
top -echoprt
ÔÚʹÓÃcanonicalģʽµÄʱºò, ¿ÉÒÔ¶Ô±¾µØÖÕ¶ËÐб༭Æ÷Ëù¶¨ÒåµÄÌØÊâ°´¼ü½øÐÐÖØж¨
Òå.
bash$ cat > filexxx
wha<ctl-W>I<ctl-H>foo bar<ctl-U>hello world<ENTER>
<ctl-D>
bash$ cat filexxx
hello world
bash$ wc -c < filexxx
12
¿ØÖÆÖն˵Ľø³ÌÖ»±£´æÁË12¸ö×Ö·û(11¸ö×Öĸ¼ÓÉÏÒ»¸ö»»ÐÐ), ËäÈ»Óû§ÇÃÁË26¸ö°´¼ü.
ÔÚ non-canonical ("raw") ģʽ, ÿ´Î°´¼ü(°üÀ¨ÌØÊⶨÒåµÄ°´¼ü, ±ÈÈç ctl-H)½«»á
Á¢¼´·¢ËÍÒ»¸ö×Ö·ûµ½¿ØÖƽø³Ì.
BashÌáʾ·û½ûÓÃÁËicanonºÍecho, ÒòΪËüÓÃ×Ô¼ºµÄ¸üºÃµÄÐб༭Æ÷´úÌæÁËÖն˵Ļù±¾
Ðб༭Æ÷. ±ÈÈç, µ±ÄãÔÚBashÌáʾ·ûÏÂÇÃctl-AµÄʱºò, Öն˽«²»»áÏÔʾ ^A, µ«ÊÇ
Bash½«»á»ñµÃ\1×Ö·û, È»ºó½âÊÍÕâ¸ö×Ö·û, ÕâÑù¹â±ê¾ÍÒƶ¯µ½ÐÐÊ×ÁË.
St¨¦phane Chazelas
setterm
ÉèÖÃÌض¨µÄÖÕ¶ËÊôÐÔ. Õâ¸öÃüÁÏòËüµÄÖն˵Ästdoutдһ¸ö×Ö·û´®, Õâ¸ö×Ö·û´®½«ÐÞ¸Ä
Öն˵ÄÐÐΪ.
bash$ setterm -cursor off
bash$
setterm ÃüÁî¿ÉÒÔ±»ÓÃÔڽű¾ÖÐÀ´ÐÞ¸Äдµ½stdoutµÄÎı¾µÄÍâ¹Û, ËäÈ»Èç¹ûÄã½ö½öÖ»ÏëÍê
³ÉÕâ¸öÄ¿µÄ, »¹ÓÐÌض¨µÄ¸üºÃµÄ¹¤¾ß¿ÉÒÔÓÃ.

1 setterm -bold on
2 echo bold hello
3
4 setterm -bold off
5 echo normal hello
tset
ÏÔʾ»ò³õʼ»¯ÖÕ¶ËÉèÖÃ. ¿ÉÒÔ˵ÕâÊÇsttyµÄ¹¦ÄܱȽÏÈõµÄ°æ±¾.
bash$ tset -r
Terminal type is xterm-xfree86.
Kill is control-U (^U).
Interrupt is control-C (^C).
setserial
ÉèÖûòÕßÏÔʾ´®¿Ú²ÎÊý. Õâ¸ö½Å±¾Ö»Äܱ»rootÓû§À´ÔËÐÐ, ²¢ÇÒͨ³£¶¼ÔÚϵͳ°²×°½Å±¾
ÖÐʹÓÃ.
1 # À´×ÔÓÚ /etc/pcmcia/serial ½Å±¾:
2
3 IRQ=`setserial /dev/$DEVICE | sed -e 's/.*IRQ: //'`
4 setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ
getty, agetty
Ò»¸öÖն˵ijõʼ»¯¹ý³Ìͨ³£¶¼ÊÇʹÓÃgetty»òagettyÀ´½¨Á¢, ÕâÑù²ÅÄÜÈÃÓû§µÇ¼. ÕâЩ
ÃüÁî²¢²»ÓÃÔÚÓû§µÄshell½Å±¾ÖÐ. ËüÃǵÄÐÐΪÓësttyºÜÏàËÆ.
mesg
ʹÄÜ»ò½ûÓõ±Ç°Óû§Öն˵ĴæȡȨÏÞ. ½ûÓôæȡȨÏÞ½«»á×èÖ¹ÍøÂçÉϵÄÁíÒ»Óû§ÏòÕâ¸öÖÕ
¶ËдÏûÏ¢.
×¢Òâ: µ±ÄãÕýÔÚ±àдÎı¾ÎļþµÄʱºò, ÔÚÎı¾ÖмäͻȻÀ´ÁËÒ»¸öĪÃûÆäÃîµÄÏûÏ¢, Õâ¶ÔÄã
À´ËµÊǷdz£·³È˵Ä. ÔÚ¶àÓû§µÄÍøÂç»·¾³ÏÂ, µ±Äã²»Ïë±»´ò¶ÏµÄʱºò, Äã¿ÉÄÜÒò´ËÏ£
Íû½ûÓöÔÄãÖն˵ÄдȨÏÞ.
wall
ÕâÊÇÒ»¸öËõдµ¥´Ê "write all", Ò²¾ÍÊÇ, ÏòµÇ¼µ½ÍøÂçÉϵÄÈκÎÖն˵ÄËùÓÐÓû§¶¼·¢ËÍ
Ò»¸öÏûÏ¢. ×îÔçÕâÊÇÒ»¸ö¹ÜÀíÔ±µÄ¹¤¾ß, ºÜÓÐÓÃ, ±ÈÈç, µ±ÏµÍ³ÓÐÎÊÌâµÄʱºò, ¹ÜÀí¿ÉÒÔ
¾¯¸æϵͳÉϵÄËùÓÐÈËÔÝʱÀ뿪 (²Î¼û Example 17-1).
bash$ wall System going down for maintenance in 5 minutes!
Broadcast message from bozo (pts/1) Sun Jul 8 13:53:27 2001...
System going down for maintenance in 5 minutes!
×¢Òâ: Èç¹ûij¸öÌض¨ÖÕ¶ËʹÓÃmesgÀ´½ûÖ¹ÁËдȨÏÞ, ÄÇôwall½«²»»á¸øËü·¢ÏûÏ¢.

ÐÅÏ¢Óëͳ¼ÆÀà
uname
Êä³öϵͳµÄ˵Ã÷(OS, Äں˰汾, µÈµÈ.)µ½stdout. ʹÓà -a Ñ¡Ïî, ½«»á¸ø³öÏêϸµÄÐÅÏ¢
(²Î¼û Example 12-5). ʹÓÃ-sÑ¡ÏîÖ»»áÊä³öOSÀàÐÍ.
bash$ uname -a
Linux localhost.localdomain 2.2.15-2.5.0 #1 Sat Feb 5 00:13:43 EST 2000
i686 unknown
bash$ uname -s
Linux
arch
ÏÔʾϵͳµÄÓ²¼þÌåϵ½á¹¹. µÈ¼ÛÓÚ uname -m. ²Î¼û Example 10-26.
bash$ arch
i686

bash$ uname -m
i686
lastcomm
¸ø³öÇ°Ò»¸öÃüÁîµÄÐÅÏ¢, ´æ´¢ÔÚ/var/account/pacctÎļþÖÐ. ÃüÁîÃû×ÖÓëÓû§Ãû×Ö¶¼¿ÉÒÔ
ʹÓÃÑ¡ÏîÀ´Ö¸¶¨. ÕâÊÇGNUµÄÒ»¸öͳ¼Æ¹¤¾ß.
lastlog
ÁгöϵͳÉÏËùÓÐÓû§×îºóµÇ¼µÄʱ¼ä. ´æÔÚ/var/log/lastlogÎļþÖÐ.
bash$ lastlog
root tty1 Fri Dec 7 18:43:21 -0700 2001
bin **Never logged in**
daemon **Never logged in**
...
bozo tty1 Sat Dec 8 21:14:29 -0700 2001

bash$ lastlog | grep root


root tty1 Fri Dec 7 18:43:21 -0700 2001
×¢Òâ: Èç¹ûÓû§¶ÔÓÚ/var/log/lastlogÎļþûÓжÁȨÏ޵Ļ°, ÄÇôµ÷ÓÃÕâ¸öÃüÁî¾Í»áʧ°Ü.
lsof
Áгö´ò¿ªµÄÎļþ. Õâ¸öÃüÁ»á°ÑËùÓе±Ç°´ò¿ªµÄÎļþÁгöÒ»·ÝÏêϸµÄ±í¸ñ, °üÀ¨ÎļþµÄ
ËùÓÐÕßÐÅÏ¢, ³ß´ç, ÓëËüÃÇÏà¹ØµÄÐÅÏ¢µÈµÈ. µ±È», lsofÒ²¿ÉÒԹܵÀÊä³öµ½ grep ºÍ(»ò)
awkÀ´·ÖÎöËüµÄ½á¹û.
bash$ lsof
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
init 1 root mem REG 3,5 30748 30303 /sbin/in
it
init 1 root mem REG 3,5 73120 8069 /lib/ld-
2.1.3.so
init 1 root mem REG 3,5 931668 8075 /lib/lib
c-2.1.3.so
cardmgr 213 root mem REG 3,5 36956 30357 /sbin/ca
rdmgr
...
strace
ΪÁ˸ú×ÙϵͳºÍÐźŵÄÕï¶ÏºÍµ÷ÊÔ¹¤¾ß. µ÷ÓÃËü×î¼òµ¥µÄ·½·¨¾ÍÊÇstrace COMMAND.
bash$ strace df
execve("/bin/df", ["df"], [/* 45 vars */]) = 0
uname({sys="Linux", node="bozo.localdomain", ...}) = 0
brk(0) = 0x804f5e4
...
ÕâÊÇ Solaris trussÃüÁîµÄLinuxµÄµÈ¼Û¹¤¾ß.
nmap
ÍøÂç¶Ë¿ÚɨÃèÆ÷. Õâ¸öÃüÁ»áɨÃèÒ»¸ö·þÎñÆ÷À´¶¨Î»´ò¿ªµÄ¶Ë¿Ú, ²¢ÇÒ¶¨Î»ÕâЩ¶Ë¿ÚÏà
¹ØµÄ·þÎñ. ÕâÊÇÒ»¸ö·ÀÖ¹ÍøÂç±»ºÚ¿ÍÈëÇÖµÄÒ»¸öÖØÒªµÄ°²È«¹¤¾ß.

1 #!/bin/bash
2
3 SERVER=$HOST # localhost.localdomain (127
.0.0.1).
4 PORT_NUMBER=25 # SMTP ¶Ë¿Ú.
5
6 nmap $SERVER | grep -w "$PORT_NUMBER" # Õâ¸öÖ¸¶¨¶Ë¿Ú´ò¿ªÁËô?
7 # grep -w Æ¥ÅäÕû¸öµ¥´Ê,
8 #+ ÕâÑù¾Í²»»áÆ¥ÅäÀàËÆÓÚ1025ÕâÖÖº¬ÓÐ25µÄ¶Ë¿ÚÁË.
9
10 exit 0
11
12 # 25/tcp open smtp
nc
nc(netcat)¹¤¾ßÊÇÒ»¸öÍêÕûµÄ¹¤¾ß°ü, ¿ÉÒÔʹÓÃËüÀ´Á¬½ÓºÍ¼àÌýTCPºÍUDP¶Ë¿Ú. Ëü¿ÉÒÔÓÃ
À´×÷ΪÕï¶ÏºÍ²âÊÔ¹¤¾ß, Ò²¿ÉÒÔÓÃÀ´×÷Ϊ»ùÓڽű¾µÄHTTP¿Í»§¶ËºÍ·þÎñÆ÷µÄ×é¼þ.
bash$ nc localhost.localdomain 25
220 localhost.localdomain ESMTP Sendmail 8.13.1/8.13.1; Thu, 31 Mar 200
5 15:41:35 -0700
Example 13-5 Checking a remote server for identd<rojy bug>
################################Start Script####################################
###
1 #! /bin/sh
2 ## Duplicate DaveG's ident-scan thingie using netcat. Oooh, he'll be p*ssed.
3 ## Args: target port [port port port ...]
4 ## Hose stdout _and_ stderr together.
5 ##
6 ## Óŵã: runs slower than ident-scan, giving remote inetd less cause
7 ##+ for alarm, and only hits the few known daemon ports you specify.
8 ## ȱµã: requires numeric-only port args, the output sleazitude,
9 ##+ and won't work for r-services when coming from high source ports.
10 # ½Å±¾×÷Õß: Hobbit <hobbit@avian.org>
11 # ÊÚȨʹÓÃÔÚ±¾ÊéÖÐ.
12
13 # ---------------------------------------------------
14 E_BADARGS=65 # ÖÁÉÙÐèÒªÁ½¸ö²ÎÊý.
15 TWO_WINKS=2 # ÐèҪ˯¶à³¤Ê±¼ä.
16 THREE_WINKS=3
17 IDPORT=113 # Authentication "tap ident" port.
18 RAND1=999
19 RAND2=31337
20 TIMEOUT0=9
21 TIMEOUT1=8
22 TIMEOUT2=4
23 # ---------------------------------------------------
24
25 case "${2}" in
26 "" ) echo "Need HOST and at least one PORT." ; exit $E_BADARGS ;;
27 esac
28
29 # Ping 'em once and see if they *are* running identd.
30 nc -z -w $TIMEOUT0 "$1" $IDPORT || { echo "Oops, $1 isn't running identd." ;
exit 0 ; }
31 # -z scans for listening daemons.
32 # -w $TIMEOUT = How long to try to connect.
33
34 # Generate a randomish base port.
35 RP=`expr $$ % $RAND1 + $RAND2`
36
37 TRG="$1"
38 shift
39
40 while test "$1" ; do
41 nc -v -w $TIMEOUT1 -p ${RP} "$TRG" ${1} < /dev/null > /dev/null &
42 PROC=$!
43 sleep $THREE_WINKS
44 echo "${1},${RP}" | nc -w $TIMEOUT2 -r "$TRG" $IDPORT 2>&1
45 sleep $TWO_WINKS
46
47 # Õâ¸ö½Å±¾¿´ÆðÀ´ÊDz»ÊÇÒ»¸öȳÍȽű¾, »òÕßÆäËü¸ü²îµÄʲô¶«Î÷?
48 # ABS Guide ×÷Õß×¢ÊÍ: "²¢²»ÊÇÕæµÄÄÇô²î,
49 #+ ÊÂʵÉÏÏ൱Çå³þ."
50
51 kill -HUP $PROC
52 RP=`expr ${RP} + 1`
53 shift
54 done
55
56 exit $?
57
58 # ×¢ÒâÊÂÏî:
59 # ---------
60
61 # ³¢ÊÔ×¢ÊÍһϵÚ30ÐеijÌÐò, ²¢ÇÒʹÓÃ"localhost.localdomain 25"
62 #+ ×÷Ϊ²ÎÊýÀ´ÔËÐÐÕâ¸ö½Å±¾.
63
64 # For more of Hobbit's 'nc' example scripts,
65 #+ look in the documentation:
66 #+ the /usr/share/doc/nc-X.XX/scripts directory.
################################End Script######################################
###
²¢ÇÒ, µ±È», ÕâÀﻹÓÐDr. Andrew TridgellÔÚBistKeeperʼþÖгôÃû׿ÖøµÄÒ»Ðнű¾:
1 echo clone | nc thunk.org 5000 > e2fsprogs.dat
free
ʹÓñí¸ñÐÎʽÀ´ÏÔʾÄÚ´æºÍ»º´æµÄʹÓÃÇé¿ö. Õâ¸öÃüÁîµÄÊä³ö·Ç³£ÊʺÏÓÚʹÓà grep, awk
»òÕßPerlÀ´·ÖÎö. procinfoÃüÁ»áÏÔʾfreeÃüÁîËùÄÜÏÔʾµÄËùÓÐÐÅÏ¢, ¶øÇÒ¸ü¶à.
bash$ free
total used free shared
buffers cached
Mem: 30504 28624 1880 15820 1608
16376
-/+ buffers/cache: 10640 19864
Swap: 68540 3128 65412
ÏÔʾδʹÓõÄRAMÄÚ´æ:
bash$ free | grep Mem | awk '{ print $4 }'
1880
procinfo
´Ó/proc pseudo-filesystemÖÐÌáÈ¡ºÍÏÔʾËùÓÐÐÅÏ¢ºÍͳ¼Æ×ÊÁÏ. Õâ¸öÃüÁ¸ø³ö¸üÏêϸ
µÄÐÅÏ¢.
bash$ procinfo | grep Bootup
Bootup: Wed Mar 21 15:15:50 2001 Load average: 0.04 0.21 0.34 3/47 6
829
lsdev
ÏÔʾÉ豸, Ò²¾ÍÊÇÏÔʾ°²×°µÄÓ²¼þ.
bash$ lsdev
Device DMA IRQ I/O Ports
------------------------------------------------
cascade 4 2
dma 0080-008f
dma1 0000-001f
dma2 00c0-00df
fpu 00f0-00ff
ide0 14 01f0-01f7 03f6-03f6
...
du
µÝ¹éµÄÏÔʾ(´ÅÅÌ)ÎļþµÄʹÓÃ×´¿ö. ³ý·ÇÖ¸¶¨, ĬÈÏÊǵ±Ç°¹¤×÷Ŀ¼.
bash$ du -ach
1.0k ./wi.sh
1.0k ./tst.sh
1.0k ./random.file
6.0k .
6.0k total
df
ʹÓÃÁбíµÄÐÎʽÏÔʾÎļþϵͳµÄʹÓÃ×´¿ö.
bash$ df
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda5 273262 92607 166547 36% /
/dev/hda8 222525 123951 87085 59% /home
/dev/hda7 1408796 1075744 261488 80% /usr
dmesg
½«ËùÓеÄϵͳÆô¶¯ÏûÏ¢Êä³öµ½stdoutÉÏ. ·½±ã³ö´í,²¢ÇÒ¿ÉÒÔ²é³ö°²×°ÁËÄÄЩÉ豸Çý¶¯ºÍ
²ì¿´Ê¹ÓÃÁËÄÄЩϵͳÖжÏ. dmesgÃüÁîµÄÊä³öµ±È»Ò²¿ÉÒÔÔڽű¾ÖÐʹÓà grep, sed, »ò
awk À´½øÐзÖÎö.
bash$ dmesg | grep hda
Kernel command line: ro root=/dev/hda2
hda: IBM-DLGA-23080, ATA DISK drive
hda: 6015744 sectors (3080 MB) w/96KiB Cache, CHS=746/128/63
hda: hda1 hda2 hda3 < hda5 hda6 hda7 > hda4
stat
ÏÔʾһ¸ö»ò¶à¸ö¸ø¶¨Îļþ(Ò²¿ÉÒÔÊÇĿ¼Îļþ»òÉ豸Îļþ)µÄÏêϸµÄͳ¼ÆÐÅÏ¢.
bash$ stat test.cru
File: "test.cru"
Size: 49970 Allocated Blocks: 100 Filetype: Regular F
ile
Mode: (0664/-rw-rw-r--) Uid: ( 501/ bozo) Gid: ( 501/ bozo
)
Device: 3,8 Inode: 18185 Links: 1
Access: Sat Jun 2 16:40:24 2001
Modify: Sat Jun 2 16:40:24 2001
Change: Sat Jun 2 16:40:24 2001
Èç¹ûÄ¿±êÎļþ²»´æÔÚ, stat ½«»á·µ»ØÒ»¸ö´íÎóÐÅÏ¢.
bash$ stat nonexistent-file
nonexistent-file: No such file or directory
vmstat
ÏÔʾÐéÄâÄÚ´æµÄͳ¼ÆÐÅÏ¢.
bash$ vmstat
procs memory swap io system
cpu
r b w swpd free buff cache si so bi bo in cs us
sy id
0 0 0 0 11040 2636 38952 0 0 33 7 271 88 8
3 89
netstat
ÏÔʾµ±Ç°ÍøÂçµÄͳ¼ÆºÍÐÅÏ¢, ±ÈÈç·ÓɱíºÍ¼¤»îµÄÁ¬½Ó. Õâ¸ö¹¤¾ß´æÈ¡/proc/net(µÚ27ÕÂ)
ÖеÄÐÅÏ¢. ²Î¼û Example 27-3.
netstat -r µÈ¼ÛÓÚ route ÃüÁî.
bash$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address Sta
te
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 11 [ ] DGRAM 906 /dev/log
unix 3 [ ] STREAM CONNECTED 4514 /tmp/.X11-unix
/X0
unix 3 [ ] STREAM CONNECTED 4513
. . .
uptime
ÏÔʾϵͳÔËÐеÄʱ¼ä, »¹ÓÐÆäËûһЩͳ¼ÆÐÅÏ¢.
bash$ uptime
10:28pm up 1:57, 3 users, load average: 0.17, 0.34, 0.27
×¢Òâ: load average Èç¹ûСÓÚ»òµÈÓÚ1, ÄÇô¾ÍÒâζ×Åϵͳ»áÂíÉÏ´¦Àí. Èç¹û
load average´óÓÚ1, ÄÇô¾ÍÒâζ׎ø³ÌÐèÒªÅŶÓ. Èç¹ûload average´óÓÚ3,
ÄÇô¾ÍÒâζ×Å, ϵͳÐÔÄÜÒѾ­ÏÔÖøϽµÁË.
hostname
ÏÔʾϵͳµÄÖ÷»úÃû×Ö. Õâ¸öÃüÁîÔÚ /etc/rc.d °²×°½Å±¾(/etc/rc.d/rc.sysinit
»òÀàËƵÄ)ÖÐÉèÖÃÖ÷»úÃû. µÈ¼ÛÓÚuname -n, ²¢ÇÒÓë$HOSTNAMEÄÚ²¿±äÁ¿ºÜÏàÏñ.
bash$ hostname
localhost.localdomain
bash$ echo $HOSTNAME
localhost.localdomain
Óë hostname ÃüÁîºÜÏàÏñµÄÃüÁÓÐ domainname, dnsdomainname, nisdomainname, ºÍ
ypdomainname ÃüÁî. ʹÓÃÕâЩÀ´ÏÔʾ»òÉèÖÃϵͳDNS »òÕß NIS/YP ÓòÃû. ¶ÔÓÚhostname
ÃüÁîÀ´ËµÊ¹Óò»Í¬µÄÑ¡ÏîÒ»Ñù¿ÉÒÔ´ïµ½ÉϱßÕâЩÃüÁîµÄÄ¿µÄ.
hostid
ÏÔʾÖ÷»úµÄ32λµÄ16½øÖÆID.
bash$ hostid
7f0100
×¢Òâ: Õâ¸öÃüÁî¾Ý˵¶ÔÓÚÌض¨ÏµÍ³¿ÉÒÔ»ñµÃÒ»¸ö"Ψһ"µÄÐòºÅ. ijЩ²úÆ·µÄ×¢²á¹ý³Ì¿ÉÄÜ
»áÐèÒªÕâ¸öÐòºÅÀ´×÷ΪÓû§µÄÐí¿ÉÖ¤. ²»ÐÒµÄÊÇ, hostid Ö»»áʹÓÃ×Ö½Úת»»µÄ·½·¨
À´ÓÃ16½øÖÆÏÔʾ»úÆ÷µÄÍøÂçµØÖ·.
Ò»¸öûÓÐÍøÂçµÄLinux»úÆ÷µÄµäÐ͵ÄÍøÂçµØÖ·ÉèÖÃÔÚ/ect/hostsÖÐ.
bash$ cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
ÅöÇÉ, ͨ¹ý¶Ô127.0.0.1½øÐÐ×Ö½Úת»», ÎÒÃÇ»ñµÃÁË 0.127.1.0, ÓÃ16½øÖƱíʾ¾ÍÊÇ
007f0100, Õâ¾ÍÊÇÉϱßhostid·µ»ØµÄ½á¹û. ÕâÑù¼¸ºõËùÓеÄÎÞÍøÂçµÄLinux»úÆ÷¶¼»á
µÃµ½Õâ¸öhostid.
sar
sar (System Activity Reporterϵͳ»î¶¯±¨¸æ) ÃüÁ»á¸ø³öϵͳͳ¼ÆµÄÒ»¸ö·Ç³£ÏêϸµÄ
¸ÅÒª. Santa Cruz Operation("ÀÏ" SCO)¹«Ë¾ÔÚ1999Äê4Ô·ÝÒÔ¿ªÔ´Èí¼þµÄÐÎʽ·¢²¼ÁËsar.
Õâ¸öÃüÁî²¢²»ÊÇ»ù±¾Linux·¢ÐаæµÄÒ»²¿·Ö, µ«ÊÇÄã¿ÉÒÔ´ÓSebastien Godard дµÄ
sysstat utilities °üÖлñµÃÕâ¸ö¹¤¾ß.
bash$ sar
Linux 2.4.9 (brooks.seringas.fr) 09/26/03
10:30:00 CPU %user %nice %system %iowait %idle
10:40:00 all 2.21 10.90 65.48 0.00 21.41
10:50:00 all 3.36 0.00 72.36 0.00 24.28
11:00:00 all 1.12 0.00 80.77 0.00 18.11
Average: all 2.23 3.63 72.87 0.00 21.27
14:32:30 LINUX RESTART
15:00:00 CPU %user %nice %system %iowait %idle
15:10:00 all 8.59 2.40 17.47 0.00 71.54
15:20:00 all 4.07 1.00 11.95 0.00 82.98
15:30:00 all 0.79 2.94 7.56 0.00 88.71
Average: all 6.33 1.70 14.71 0.00 77.26
readelf
ÏÔʾָ¶¨µÄ elf ¸ñʽµÄ2½øÖÆÎļþµÄͳ¼ÆÐÅÏ¢. Õâ¸ö¹¤¾ßÊÇbinutils¹¤¾ß°üµÄÒ»²¿·Ö.
bash$ readelf -h /bin/bash
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
. . .
size
size [/path/to/binary] ÃüÁî¿ÉÒÔÏÔʾ2½øÖÆ¿ÉÖ´ÐÐÎļþ»ò¹éµµÎļþÿ²¿·ÖµÄ³ß´ç. Õâ¸ö
¹¤¾ßÖ÷ÒªÊdzÌÐòԱʹÓÃ.
bash$ size /bin/bash
text data bss dec hex filename
495971 22496 17392 535859 82d33 /bin/bash
ϵͳÈÕÖ¾Àà
logger
¸½¼ÓÒ»¸öÓû§²úÉúµÄÏûÏ¢µ½ÏµÍ³ÈÕÖ®ÖÐ (/var/log/messages). ²»ÊÇrootÓû§Ò²¿ÉÒÔµ÷ÓÃ
logger.
1 logger Experiencing instability in network connection at 23:10, 05/
21.
2 # ÏÖÔÚ, ÔËÐÐ 'tail /var/log/messages'.
ͨ¹ýÔڽű¾Öе÷ÓÃÒ»¸öloggerÃüÁî, ¾Í¿ÉÒÔ½«µ÷ÊÔÐÅϢдµ½/var/log/messagesÖÐ.
1 logger -t $0 -i Logging at line "$LINENO".
2 # "-t" Ñ¡Ïî¿ÉÒÔΪ³¤µÄÈë¿ÚÖ¸¶¨±êÇ©.
3 # "-i" Ñ¡Ïî¼Ç¼½ø³ÌID.
4
5 # tail /var/log/message
6 # ...
7 # Jul 7 20:48:58 localhost ./test.sh[1712]: Logging at line 3.
logrotate
Õâ¸ö¹¤¾ßÓÃÀ´¹ÜÀíϵͳµÄlogÎļþ, ¿ÉÒÔÔÚºÏÊʵÄʱºòÂÖ»», ѹËõ, ɾ³ý, ºÍ(»ò)e-mail
ËüÃÇ. Õâ¸ö¹¤¾ß½«´ÓÀϵÄlogÎļþÖÐÈ¡µÃһЩÔÓÂҵļǼ±£´æÔÚ/var/logÖÐ. ͨ³£Ê¹ÓÃ
cron À´Ã¿ÌìÔËÐÐlogrotate.
ÔÚ/etc/logrotate.confÖÐÌí¼ÓºÏÊʵÄÈë¿Ú¾Í¿ÉÒÔ¹ÜÀí×Ô¼ºµÄlogÎļþÁË, ¾ÍÏñ¹ÜÀíϵͳ
logÎļþÒ»Ñù.
×¢Òâ: Stefano Falsetto ´´ÔìÁËrottlog, ËûÈÏΪÕâÊÇlogrotateµÄ¸Ä½ø°æ±¾.
×÷Òµ¿ØÖÆ
ps
½ø³Ìͳ¼Æ: ͨ¹ý½ø³ÌËùÓÐÕߺÍPID(½ø³ÌID)À´Áгöµ±Ç°Ö´ÐеĽø³Ì. ͨ³£¶¼ÊÇʹÓÃaxÑ¡Ïî
À´µ÷ÓÃÕâ¸öÃüÁî, ²¢ÇÒ½á¹û¿ÉÒÔͨ¹ý¹ÜµÀ´«µÝµ½ grep »ò sed ÖÐÀ´ËÑË÷Ìض¨µÄ½ø³Ì
(²Î¼û Example 11-12 ºÍ Example 27-2).
bash$ ps ax | grep sendmail
295 ? S 0:00 sendmail: accepting connections on port 25
Èç¹ûÏëʹÓÃ"Ê÷"µÄÐÎʽÀ´ÏÔʾϵͳ½ø³Ì: ps afjx »òÕß ps ax --forest.
pgrep, pkill
ps ÃüÁîÓëgrep»òkill½áºÏʹÓÃ.
bash$ ps a | grep mingetty
2212 tty2 Ss+ 0:00 /sbin/mingetty tty2
2213 tty3 Ss+ 0:00 /sbin/mingetty tty3
2214 tty4 Ss+ 0:00 /sbin/mingetty tty4
2215 tty5 Ss+ 0:00 /sbin/mingetty tty5
2216 tty6 Ss+ 0:00 /sbin/mingetty tty6
4849 pts/2 S+ 0:00 grep mingetty

bash$ pgrep mingetty


2212 mingetty
2213 mingetty
2214 mingetty
2215 mingetty
2216 mingetty
pstree
ʹÓÃ"Ê÷"ÐÎʽÁгöµ±Ç°Ö´ÐеĽø³Ì. -pÑ¡ÏîÏÔʾPID,ºÍ½ø³ÌÃû×Ö.
top
Á¬Ðø²»¶ÏµÄÏÔʾcpuʹÓÃÂÊ×î¸ßµÄ½ø³Ì. -b Ñ¡Ï»áÒÔÎı¾·½Ê½ÏÔʾ, ÒÔ±ãÓÚ¿ÉÒÔÔڽű¾
ÖзÖÎö»ò´æÈ¡.
bash$ top -b
8:30pm up 3 min, 3 users, load average: 0.49, 0.32, 0.13
45 processes: 44 sleeping, 1 running, 0 zombie, 0 stopped
CPU states: 13.6% user, 7.3% system, 0.0% nice, 78.9% idle
Mem: 78396K av, 65468K used, 12928K free, 0K shrd, 2352
K buff
Swap: 157208K av, 0K used, 157208K free 37244
K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND
848 bozo 17 0 996 996 800 R 5.6 1.2 0:00 top
1 root 8 0 512 512 444 S 0.0 0.6 0:04 init
2 root 9 0 0 0 0 SW 0.0 0.0 0:00 keven
td
...
nice
ʹÓÃÐ޸ĺóµÄÓÅÏȼ¶À´ÔËÐÐÒ»¸öºǫ́×÷Òµ. ÓÅÏȼ¶´Ó19(×îµÍ)µ½-20(×î¸ß). Ö»ÓÐrootÓÃ
»§¿ÉÒÔÉèÖøºµÄ(±È½Ï¸ßµÄ)ÓÅÏȼ¶. Ïà¹ØµÄÃüÁîÊÇrenice, snice, ºÍskill.
nohup
±£³ÖÒ»¸öÃüÁîµÄÔËÐÐ, ¼´Ê¹Óû§µÇ³öϵͳ. Õâ¸öÃüÁî×öΪǰ̨½ø³ÌÀ´ÔËÐÐ, ³ý·ÇÇ°±ß¼Ó &.
Èç¹ûÄãÔڽű¾ÖÐʹÓÃnohupÃüÁî, ×îºÃºÍwait ÃüÁîÒ»ÆðʹÓÃ, ÕâÑù¿ÉÒÔ±ÜÃâ´´½¨Ò»¸ö
¹Â¶ù½ø³Ì»ò½©Ê¬½ø³Ì.
pidof
È¡µÃÒ»¸öÕýÔÚÔËÐеÄ×÷ÒµµÄ½ø³ÌID(PID). ÒòΪһЩ×÷Òµ¿ØÖÆÃüÁî, ±ÈÈçkillºÍreniceÖ»
ÄÜʹÓýø³ÌµÄPID(¶ø²»ÊÇËüµÄÃû×Ö), ËùÒÔÓÐʱºò±ØÐëµÄÈ¡µÃPID. pidofÃüÁîÓë$PPIDÄÚ²¿
±äÁ¿·Ç³£ÏàËÆ.
bash$ pidof xclock
880
Example 13-6 pidof °ïÖúɱµôÒ»¸ö½ø³Ì
################################Start Script####################################
###
1 #!/bin/bash
2 # kill-process.sh
3
4 NOPROCESS=2
5
6 process=xxxyyyzzz # ʹÓò»´æÔڵĽø³Ì.
7 # Ö»²»¹ýÊÇΪÁËÑÝʾ...
8 # ... ²¢²»ÏëÔÚÕâ¸ö½Å±¾ÖÐɱµôÈκÎÕæÕýµÄ½ø³Ì.
9 #
10 # Èç¹û, ¾Ù¸öÀý×Ó, ÄãÏëʹÓÃÕâ¸ö½Å±¾À´¶ÏÏßInternet,
11 # process=pppd
12
13 t=`pidof $process` # È¡µÃ$processµÄpid(½ø³Ìid).
14 # 'kill'±ØÐëʹÓÃpid(²»ÄÜÓóÌÐòÃû).
15
16 if [ -z "$t" ] # Èç¹ûûÕâ¸ö½ø³Ì, 'pidof' ·µ»Ø¿Õ.
17 then
18 echo "Process $process was not running."
19 echo "Nothing killed."
20 exit $NOPROCESS
21 fi
22
23 kill $t # ¶ÔÓÚÍç¹ÌµÄ½ø³Ì¿ÉÄÜÐèÒª'kill -9'.
24
25 # ÕâÀïÐèÒª×öÒ»¸ö¼ì²é, ¿´¿´½ø³ÌÊÇ·ñÔÊÐí×ÔÉí±»kill.
26 # »òÐíÁíÒ»¸ö " t=`pidof $process` " »òÕß ...
27
28
29 # Õû¸ö½Å±¾¶¼¿ÉÒÔʹÓÃϱßÕâ¾äÀ´Ìæ»»:
30 # kill $(pidof -x process_name)
31 # µ«ÊÇÕâ¾ÍûÓнÌÓýÒâÒåÁË.
32
33 exit 0
################################End Script######################################
###
fuser
È¡µÃÒ»¸öÕýÔÚ´æȡij¸ö»òijЩÎļþ(»òĿ¼)µÄ½ø³ÌID. ʹÓÃ-kÑ¡Ï»áɱµôÕâЩ½ø³Ì. ¶Ô
ÓÚϵͳ°²È«À´Ëµ, ÓÈÆäÊÇÔڽű¾ÖÐÏë×èֹδ±»ÊÚȨµÄÓû§´æȡϵͳ·þÎñµÄʱºò, Õâ¸öÃüÁî
¾ÍÏԵúÜÓÐÓÃÁË.
bash$ fuser -u /usr/bin/vim
/usr/bin/vim: 3207e(bozo)
bash$ fuser -u /dev/null
/dev/null: 3009(bozo) 3010(bozo) 3197(bozo) 3199(bozo)
µ±Õý³£µÄ²åÈë»òɾ³ý±£´æµÄýÌå, ±ÈÈçCD ROM»òÕßUSBÉÁ´æÉ豸µÄʱºò, fuserµÄÓ¦ÓÃÒ²ÏÔ
µÃÌرðÖØÒª. ÓÐʱºòµ±ÄãÏëumountÒ»¸öÉ豸ʧ°ÜµÄʱºò(³öÏÖÉ豸æµÄ´íÎóÏûÏ¢), ÕâÒâζ
×ÅijЩÓû§»ò½ø³ÌÕýÔÚ´æÈ¡Õâ¸öÉ豸. ʹÓÃfuser -um /dev/device_name¿ÉÒԸ㶨ÕâЩ,
ÕâÑùÄã¾Í¿ÉÒÔɱµôËùÓÐÏà¹ØµÄ½ø³Ì.
bash$ umount /mnt/usbdrive
umount: /mnt/usbdrive: device is busy
bash$ fuser -um /dev/usbdrive
/mnt/usbdrive: 1772c(bozo)
bash$ kill -9 1772
bash$ umount /mnt/usbdrive
fuser µÄ-nÑ¡Ïî¿ÉÒÔ»ñµÃÕýÔÚ´æȡijһ¶Ë¿ÚµÄ½ø³Ì. µ±ºÍnmapÃüÁî×éºÏʹÓõÄʱºòÓÈÆä
ÓÐÓÃ.
root# nmap localhost.localdomain
PORT STATE SERVICE
25/tcp open smtp
root# fuser -un tcp 25
25/tcp: 2095(root)
root# ps ax | grep 2095 | grep -v grep
2095 ? Ss 0:00 sendmail: accepting connections
cron
¹ÜÀí³ÌÐòµ÷¶ÈÆ÷, Ö´ÐÐһЩÈÕ³£ÈÎÎñ, ±ÈÈçÇå³ýºÍɾ³ýϵͳlogÎļþ, »òÕ߸üÐÂslocateÃü
ÁîµÄÊý¾Ý¿â. ÕâÊÇatÃüÁîµÄ³¬¼¶Óû§°æ±¾(ËäȻÿ¸öÓû§¶¼¿ÉÒÔÓÐ×Ô¼ºµÄcrontabÎļþ, ²¢
ÇÒÕâ¸öÎļþ¿ÉÒÔʹÓÃcrontabÃüÁîÀ´ÐÞ¸Ä). ËüÒÔÓÄÁé½ø³ÌTµÄÉí·ÝÀ´ÔËÐÐ, ²¢ÇÒ´Ó
/ect/crontabÖлñµÃÖ´Ðеĵ÷¶ÈÈë¿Ú.
×¢Òâ: һЩLinuxµÄ·ç¸ñ¶¼Ê¹ÓÃcrond, Matthew DillonµÄcron.
½ø³Ì¿ØÖƺÍÆô¶¯Àà
init
init ÃüÁîÊÇËùÓнø³ÌµÄ¸¸½ø³Ì. ÔÚϵͳÆô¶¯µÄ×îºóÒ»²½µ÷ÓÃ, init ½«»áÒÀ¾Ý
/etc/inittabÀ´¾ö¶¨ÏµÍ³µÄÔËÐ춱ð. Ö»ÄÜʹÓÃrootÉí·ÝÀ´ÔËÐÐËüµÄ±ðÃûtelinit.
telinit
initÃüÁîµÄ·ûºÅÁ´½Ó, ÕâÊÇÒ»ÖÖÐÞ¸ÄϵͳÔËÐ춱ðµÄÒ»¸öÊÖ¶Î, ͨ³£ÔÚϵͳά»¤»òÕß½ô¼±
µÄÎļþϵͳÐÞ¸´µÄʱºò²ÅÓÃ. Ö»ÄÜʹÓÃrootÉí·Ýµ÷ÓÃ. µ÷ÓÃÕâ¸öÃüÁîÊǷdz£Î£ÏÕµÄ - ÔÚ
ÄãʹÓÃ֮ǰȷ¶¨ÄãÒѾ­ºÜºÃµØÁ˽âËü.
runlevel
ÏÔʾµ±Ç°ºÍ×îºóµÄÔËÐ춱ð, Ò²¾ÍÊÇ, È·¶¨ÄãµÄϵͳÊÇ·ñÖÕÖ¹(runlevel Ϊ0), »¹ÊÇÔËÐÐ
ÔÚµ¥Óû§Ä£Ê½(1), ¶àÓû§Ä£Ê½(2), »òÕßÊÇÔËÐÐÔÚX Windows(5), »¹ÊÇÕýÔÚÖØÆô(6). Õâ
¸öÃüÁ»á´æÈ¡/var/run/utmpÎļþ.
halt, shutdown, reboot
ÉèÖÃϵͳ¹Ø»úµÄÃüÁî, ͨ³£±ÈµçÔ´¹Ø»úµÄÓÅÏȼ¶¸ß.
service
¿ªÆô»òÍ£Ö¹Ò»¸öϵͳ·þÎñ. Æô¶¯½Å±¾ÔÚ/etc/init.dÖÐ, ²¢ÇÒ/etc/rc.dÔÚϵͳÆô¶¯µÄʱºò
ʹÓÃÕâ¸öÃüÁîÀ´Æô¶¯·þÎñ.
root# /sbin/service iptables stop
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
ÍøÂçÀà
ifconfig
ÍøÂçµÄ½Ó¿ÚÅäÖú͵÷ÊÔ¹¤¾ß.
bash$ ifconfig -a
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:10 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:700 (700.0 b) TX bytes:700 (700.0 b)
ifconfig ÃüÁî¾ø´ó¶àÊýÇé¿ö¶¼ÊÇÔÚÆô¶¯Ê±ºòÉèÖýӿÚ, »òÕßÔÚÖØÆôµÄʱºò¹Ø±ÕËüÃÇ.
1 # À´×ÔÓÚ /etc/rc.d/init.d/network µÄ´úÂëƬ¶Î
2
3 # ...
4
5 # ¼ì²éÍøÂçÊÇ·ñÆô¶¯.
6 [ ${NETWORKING} = "no" ] && exit 0
7
8 [ -x /sbin/ifconfig ] || exit 0
9
10 # ...
11
12 for i in $interfaces ; do
13 if ifconfig $i 2>/dev/null | grep -q "UP" >/dev/null 2>&1 ; then
14 action "Shutting down interface $i: " ./ifdown $i boot
15 fi
16 # grepÃüÁîµÄGNUÖ¸¶¨µÄ "-q" µÄÒâ˼ÊÇ"°²¾²", Ò²¾ÍÊDz»²úÉúÊä³ö.
17 # ÕâÑù, ºó±ßÖض¨Ïòµ½/dev/nullµÄ²Ù×÷¾ÍÓеãÖظ´ÁË.
18
19 # ...
20
21 echo "Currently active devices:"
22 echo `/sbin/ifconfig | grep ^[a-z] | awk '{print $1}'`
23 # ^^^^^ Ó¦¸Ã±»ÒýÓ÷ÀÖ¹globbing.
24 # ϱßÕâ¶ÎÒ²Äܹ¤×÷.
25 # echo $(/sbin/ifconfig | awk '/^[a-z]/ { print $1 })'
26 # echo $(/sbin/ifconfig | sed -e 's/ .*//')
27 # Thanks, S.C.×öÁ˶îÍâµÄ×¢ÊÍ.
²Î¼û Example 29-6.
iwconfig
ÕâÊÇΪÁËÅäÖÃÎÞÏßÍøÂçµÄÃüÁºÏ. ¿ÉÒÔ˵ÊÇÉϱߵÄifconfigµÄÎÞÏß°æ±¾.
route
ÏÔʾÄں˷ÓɱíÐÅÏ¢, »òÕ߲鿴Äں˷ÓɱíµÄÐÞ¸Ä.
bash$ route
Destination Gateway Genmask Flags MSS Window irt
t Iface
pm3-67.bozosisp * 255.255.255.255 UH 40 0
0 ppp0
127.0.0.0 * 255.0.0.0 U 40 0
0 lo
default pm3-67.bozosisp 0.0.0.0 UG 40 0
0 ppp0
chkconfig
¼ì²éÍøÂçÅäÖÃ. Õâ¸öÃüÁÔðÏÔʾºÍ¹ÜÀíÔÚÆô¶¯¹ý³ÌÖÐËù¿ªÆôµÄÍøÂç·þÎñ(ÕâЩ·þÎñ¶¼ÊÇ
´Ó/etc/rc?.dĿ¼ÖпªÆôµÄ).
×ʼÊÇ´ÓIRIXµ½Red Hat LinuxµÄÒ»¸ö½Ó¿Ú, chkconfigÔÚijЩLinux·¢ÐаæÖв¢²»ÊǺË
ÐÄ°²×°µÄÒ»²¿·Ö.
bash$ chkconfig --list
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
rwhod 0:off 1:off 2:off 3:off 4:off 5:off 6:off
...
tcpdump
ÍøÂç°üµÄ"Ðá̽Æ÷". ÕâÊÇÒ»¸öÓÃÀ´·ÖÎöºÍµ÷ÊÔÍøÂçÉÏ´«ÊäÇé¿öµÄ¹¤¾ß, ËüËùʹÓõÄÊÖ¶ÎÊÇ
°ÑÆ¥ÅäÖ¸¶¨¹æÔòµÄ°üÍ·¶¼ÏÔʾ³öÀ´.
ÏÔʾÖ÷»úbozovilleºÍÖ÷»úcaduceusÖ®¼äËùÓд«ÊäµÄip°ü.
bash$ tcpdump ip host bozoville and caduceus
µ±È»,tcpdumpµÄÊä³ö¿ÉÒÔ±»·ÖÎö, ¿ÉÒÔÓÃÎÒÃÇ֮ǰÌÖÂÛµÄÎı¾´¦Àí¹¤¾ßÀ´·ÖÎö½á¹û.
ÎļþϵͳÀà
mount
¼ÓÔØÒ»¸öÎļþϵͳ, ͨ³£¶¼ÓÃÀ´°²×°ÍⲿÉ豸, ±ÈÈçÈíÅÌ»òCDROM. Îļþ/etc/fstab ½«»á
Ìṩһ¸ö·½±ãµÄÁбí, Õâ¸öÁбíÁгöÁËËùÓпÉÓõÄÎļþϵͳ, ·ÖÇøºÍÉ豸, ÁíÍ⻹°üÀ¨Ä³
ЩѡÏî, ±ÈÈçÊÇ·ñ¿ÉÒÔ×Ô¶¯»òÕßÊÖ¶¯µÄmount. Îļþ/etc/mtab ÏÔʾÁ˵±Ç°ÒѾ­mountµÄÎÄ
¼þϵͳºÍ·ÖÇø(°üÀ¨ÐéÄâµÄ, ±ÈÈç/proc).
mount -a ½«»ámountËùÓÐÁÐÔÚ/ect/fstabÖеÄÎļþϵͳºÍ·ÖÇø, ³ýÁËÄÇЩ±ê¼ÇÓзÇ×Ô¶¯Ñ¡
ÏîµÄ. ÔÚÆô¶¯µÄʱºò, ÔÚ/etc/rc.dÖеÄÒ»¸öÆô¶¯½Å±¾(rc.sysinit»òÕßһЩÏàËƵĽű¾)
½«»áÕâôµ÷ÓÃ, mountËùÓпÉÓõÄÎļþϵͳºÍ·ÖÇø.
1 mount -t iso9660 /dev/cdrom /mnt/cdrom
2 # ¼ÓÔØ CDROM
3 mount /mnt/cdrom
4 # ·½±ãµÄ·½·¨, Èç¹û /mnt/cdrom °üº¬ÔÚ /etc/fstab ÖÐ
Õâ¸ö¶à¹¦ÄܵÄÃüÁîÉõÖÁ¿ÉÒÔ½«Ò»¸öÆÕͨÎļþmountµ½¿éÉ豸ÖÐ, ²¢ÇÒÕâ¸öÎļþ¾ÍºÃÏñÒ»¸ö
ÎļþϵͳһÑù. mount¿ÉÒÔ½«ÎļþÓëÒ»¸öloopbackÉ豸Ïà¹ØÁªÀ´´ïµ½Õâ¸öÄ¿µÄ.
ccomplishes that by associating the file with a loopback device. ÕâÖÖÓ¦ÓÃͨ³£
¶¼ÊÇÓÃÀ´mountºÍ¼ì²éÒ»¸öISO9660¾µÏñ,ÔÚÕâ¸ö¾µÏñ±»ÉÕ¼µ½CDR֮ǰ. [3]
Example 13-7 ¼ì²éÒ»¸öCD¾µÏñ
################################Start Script####################################
###
1 # ÒÔrootÉí·Ý...
2
3 mkdir /mnt/cdtest # Èç¹ûûÓеĻ°,×¼±¸Ò»¸ömountµã.
4
5 mount -r -t iso9660 -o loop cd-image.iso /mnt/cdtest # mountÕâ¸ö¾µÏñ.
6 # "-o loop" option equivalent to "losetup /dev/loop0"
7 cd /mnt/cdtest # ÏÖÔÚ¼ì²éÕâ¸ö¾µÏñ.
8 ls -alR # ÁгöĿ¼Ê÷ÖеÄÎļþ.
9 # µÈµÈ.
################################End Script######################################
###
umount
ж³ýÒ»¸öµ±Ç°ÒѾ­mountµÄÎļþϵͳ. ÔÚÕý³£É¾³ý֮ǰÒѾ­mountµÄÈíÅ̺ÍCDROM֮ǰ, Õâ
¸öÉ豸±ØÐë±»unmount, ·ñÔòÎļþϵͳ½«»áËð»µ.
1 umount /mnt/cdrom
2 # ÏÖÔÚÄã¿ÉÒÔ°´ÏÂÍ˳ö°´Å¥(Ö¸µÄÊÇcdrom»òÈíÅÌÇý¶¯Æ÷ÉϵÄÍ˳öÅ¥), ²¢°²È«µÄÍ˳ö¹âÅÌ.
sync
Ç¿ÖÆдÈëËùÓÐÐèÒª¸üеÄbufferÉϵÄÊý¾Ýµ½Ó²ÅÌÉÏ(ͬ²½´øÓÐbufferµÄÇý¶¯Æ÷). Èç¹û²»ÊÇ
Ñϸñ±ØÒªµÄ»°,Ò»¸ösync¾Í¿ÉÒÔ±£Ö¤ÏµÍ³¹ÜÀíÔ±»òÕßÓû§¸Õ¸ÕÐ޸ĵÄÊý¾Ý»á°²È«µÄÔÚͻȻ
µÄ¶ÏµãÖÐÐÒ´æÏÂÀ´. ÔڱȽÏÔçÒÔÇ°, ÔÚϵͳÖØÆôÇ°¶¼ÊÇʹÓà sync; sync (Á½´Î, ÕâÑù±£
Ö¤¾ø¶Ô¿É¿¿), ÕâÊÇÒ»ÖÖºÜÓÐÓõÄСÐĵķ½·¨.
ÓÐʱºò, ±ÈÈçµ±ÄãÏ밲ȫɾ³ýÒ»¸öÎļþµÄʱºò(²Î¼û Example 12-55), »òÕßµ±´ÅÅ̵ƿªÊ¼
ÉÁ˸µÄʱºò, Äã¿ÉÄÜÐèҪǿÖÆÂíÉϽøÐÐbufferË¢ÐÂ.
losetup
½¨Á¢ºÍÅäÖÃloopbackÉ豸.
Example 13-8 ÔÚÒ»¸öÎļþÖд´½¨Îļþϵͳ
################################Start Script####################################
###
1 SIZE=1000000 # 1M
2
3 head -c $SIZE < /dev/zero > file # ½¨Á¢Ö¸¶¨³ß´çµÄÎļþ.
4 losetup /dev/loop0 file # ×÷ΪloopbackÉ豸À´½¨Á¢.
5 mke2fs /dev/loop0 # ´´½¨Îļþϵͳ.
6 mount -o loop /dev/loop0 /mnt # MountËü.
7
8 # Thanks, S.C.
################################End Script######################################
###
mkswap
´´½¨Ò»¸ö½»»»·ÖÇø»òÎļþ. ½»»»ÇøÓòËæºó±ØÐëÂíÉÏʹÓÃswaponÀ´Ê¹ÄÜ.
swapon, swapoff
ʹÄÜ/½ûÓà ½»»»·ÖÇø»òÎļþ. ÕâÁ½¸öÃüÁîͨ³£ÔÚÆô¶¯ºÍ¹Ø»úµÄʱºò²ÅÓÐЧ.
mke2fs
´´½¨Linux ext2 Îļþϵͳ. Õâ¸öÃüÁî±ØÐëÒÔrootÉí·Ýµ÷ÓÃ.
Example 13-9 Ìí¼ÓÒ»¸öеÄÓ²ÅÌÇý¶¯Æ÷
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ÔÚϵͳÉÏÌí¼ÓµÚ¶þ¿éÓ²ÅÌÇý¶¯Æ÷.
4 # Èí¼þÅäÖÃ. ¼ÙÉèÓ²¼þÒѾ­°²×°ÁË.
5 # À´×ÔÓÚ±¾Êé×÷ÕßµÄһƪÎÄÕÂ.
6 # ÔÚ"Linux Gazette"µÄÎÊÌâ#38ÉÏ, http://www.linuxgazette.com.
7
8 ROOT_UID=0 # Õâ¸ö½Å±¾±ØÐëÒÔrootÉí·ÝÔËÐÐ.
9 E_NOTROOT=67 # ·ÇrootÓû§½«»á²úÉúÕâ¸ö´íÎó.
10
11 if [ "$UID" -ne "$ROOT_UID" ]
12 then
13 echo "Must be root to run this script."
14 exit $E_NOTROOT
15 fi
16
17 # Òª·Ç³£½÷É÷µÄСÐÄʹÓÃ!
18 # Èç¹ûij²½´íÁË, ¿ÉÄܻ᳹µ×´Ý»ÙÄ㵱ǰµÄÎļþϵͳ.
19
20
21 NEWDISK=/dev/hdb # ¼ÙÉè/dev/hdb¿Õ°×. ¼ì²éÒ»ÏÂ!
22 MOUNTPOINT=/mnt/newdisk # »òÕßÑ¡ÔñÁíÍâµÄmountµã.
23
24
25 fdisk $NEWDISK
26 mke2fs -cv $NEWDISK1 # ¼ì²é»µ¿é, ÏêϸÊä³ö.
27 # ×¢Òâ: /dev/hdb1, *²»ÊÇ* /dev/hdb!
28 mkdir $MOUNTPOINT
29 chmod 777 $MOUNTPOINT # ÈÃËùÓÐÓû§¶¼¾ßÓÐÈ«²¿È¨ÏÞ.
30
31
32 # ÏÖÔÚ, ²âÊÔÒ»ÏÂ...
33 # mount -t ext2 /dev/hdb1 /mnt/newdisk
34 # ³¢ÊÔ´´½¨Ò»¸öĿ¼.
35 # Èç¹û¹¤×÷ÆðÀ´ÁË, umountËü, È»ºó¼ÌÐø.
36
37 # ×îºóÒ»²½:
38 # ½«Ï±ßÕâÐÐÌí¼Óµ½/etc/fstab.
39 # /dev/hdb1 /mnt/newdisk ext2 defaults 1 1
40
41 exit 0
################################End Script######################################
###
²Î¼û Example 13-8 ºÍ Example 28-3.
tune2fs
µ÷Õûext2Îļþϵͳ. ¿ÉÒÔÓÃÀ´ÐÞ¸ÄÎļþϵͳ²ÎÊý, ±ÈÈçmountµÄ×î´óÊýÁ¿. ±ØÐëÒÔrootÉí
·Ýµ÷ÓÃ.
×¢Òâ: ÕâÊÇÒ»¸ö·Ç³£Î£ÏÕµÄÃüÁî. Èç¹û»µÁË, ÄãÐèÒª×Ô¼º¸ºÔð, ÒòΪËü¿ÉÄÜ»áÆÆ»µÄãµÄÎÄ
¼þϵͳ.
dumpe2fs
´òÓ¡(Êä³öµ½stdoutÉÏ)·Ç³£ÏêϸµÄÎļþϵͳÐÅÏ¢. ±ØÐëÒÔrootÉí·Ýµ÷ÓÃ.
root# dumpe2fs /dev/hda7 | grep 'ount count'
dumpe2fs 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09
Mount count: 6
Maximum mount count: 20
hdparm
Áгö»òÐÞ¸ÄÓ²Å̲ÎÊý. Õâ¸öÃüÁî±ØÐëÒÔrootÉí·Ýµ÷ÓÃ, Èç¹ûÀÄÓõĻ°»áÓÐΣÏÕ.
fdisk
ÔÚ´æ´¢É豸ÉÏ(ͨ³£¶¼ÊÇÓ²ÅÌ)´´½¨ºÍÐÞ¸ÄÒ»¸ö·ÖÇø±í. ±ØÐëÒÔrootÉí·ÝʹÓÃ.
×¢Òâ: ½÷É÷ʹÓÃÕâ¸öÃüÁî. Èç¹û³ö´í, »áÆÆ»µÄãÏÖ´æµÄÎļþϵͳ.
fsck, e2fsck, debugfs
ÎļþϵͳµÄ¼ì²é, ÐÞ¸´, ºÍ³ý´íÃüÁºÏ.
fsck: ¼ì²éUNIXÎļþϵͳµÄÇ°¶Ë¹¤¾ß(Ò²¿ÉÒÔµ÷ÓÃÆäËüµÄ¹¤¾ß). ÎļþϵͳµÄÀàÐÍÒ»°ã¶¼ÊÇ
ĬÈϵÄext2.
e2fsck: ext2Îļþϵͳ¼ì²éÆ÷.
debugfs: ext2Îļþϵͳ³ý´íÆ÷. Õâ¸ö¶à¹¦Äܵ«ÊÇΣÏյŤ¾ßµÄÓô¦Ö®Ò»¾ÍÊÇ(³¢ÊÔ)»Ö¸´
ɾ³ýµÄÎļþ. Ö»Óи߼¶Óû§²ÅÄÜÓÃ.
ÉϱߵÄÕ⼸¸öÃüÁ±ØÐëÒÔrootÉí·Ýµ÷ÓÃ, ÕâЩÃüÁºÜΣÏÕ, Èç¹ûÀÄÓõĻ°»áÆÆ»µÎļþ
ϵͳ.
badblocks
¼ì²é´æ´¢É豸µÄ»µ¿é(ÎïÀíËð»µ). Õâ¸öÃüÁîÔÚ¸ñʽ»¯Ð°²×°µÄÓ²ÅÌʱ»òÕß²âÊÔ±¸·ÝµÄÍêÕû
ÐÔµÄʱºò»á±»Óõ½. [4] ¾Ù¸öÀý×Ó, badblocks /dev/fd0 ²âÊÔÒ»¸öÈíÅÌ.
badblocks¿ÉÄÜ»áÒýÆð±È½ÏÔã¸âµÄ½á¹û(¸²¸ÇËùÓÐÊý¾Ý), ÔÚÖ»¶ÁģʽϾͲ»»á·¢ÉúÕâÖÖÇé
¿ö.Èç¹ûrootÓû§ÓµÓÐÐèÒª²âÊÔµÄÉ豸(ͨ³£¶¼ÊÇÕâÖÖÇé¿ö), ÄÇôrootÓû§±ØÐëµ÷ÓÃÕâ¸ö
ÃüÁî.
lsusb, usbmodules
lsusb ÃüÁî»áÁгöËùÓÐUSB(Universal Serial BusͨÓô®ÐÐ×ÜÏß)×ÜÏߺÍʹÓÃUSBµÄÉ豸.
usbmodules ÃüÁî»áÊä³öÁ¬½ÓUSBÉ豸µÄÇý¶¯Ä£¿éµÄÐÅÏ¢.
root# lsusb
Bus 001 Device 001: ID 0000:0000
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.00
bDeviceClass 9 Hub
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x0000
idProduct 0x0000
. . .
mkbootdisk
´´½¨Æô¶¯ÈíÅÌ, Æô¶¯ÅÌ¿ÉÒÔ»½ÐÑϵͳ, ±ÈÈçµ±MBR(master boot recordÖ÷Æô¶¯¼Ç¼)»µµô
µÄʱºò. mkbootdisk ÃüÁîÆäʵÊÇÒ»¸öBash½Å±¾, ÓÉErik TroanËù±àд, ·ÅÔÚ/sbinĿ¼ÖÐ.
chroot
ÐÞ¸ÄROOTĿ¼. Ò»°ãµÄÃüÁÊÇ´Ó$PATHÖлñµÃµÄ, Ïà¶ÔµÄĬÈϵĸùĿ¼ÊÇ /. Õâ¸öÃüÁî
½«»á°Ñ¸ùĿ¼ÐÞ¸ÄΪÁíÒ»¸öĿ¼(²¢ÇÒÒ²½«°Ñ¹¤×÷Ŀ¼Ð޸ĵ½ÄÇ). ³öÓÚ°²È«Ä¿µÄ, Õâ¸öÃü
Áîʱ·Ç³£ÓÐÓõÄ, ¾Ù¸öÀý×Ó, µ±ÏµÍ³¹ÜÀíԱϣÍûÏÞÖÆһЩÌض¨µÄÓû§, ±ÈÈçtelnetÉÏÀ´µÄ
Óû§, ½«ËûÃÇÏÞ¶¨µ½ÎļþϵͳÉÏÒ»¸ö°²È«µÄµØ·½(ÕâÓÐʱºò±»³ÆΪ½«Ò»¸öguestÓû§ÏÞÖÆÔÚ
"chroot ¼àÀÎ"ÖÐ). ×¢Òâ, ÔÚʹÓÃchrootÖ®ºó, ϵͳµÄ¶þ½øÖÆ¿ÉÖ´ÐÐÎļþµÄĿ¼½«²»ÔÙ
¿ÉÓÃÁË.
chroot /opt ½«»áʹµÃÔ­À´µÄ/usr/binĿ¼±äΪ/opt/usr/bin. ͬÑù,
chroot /aaa/bbb /bin/ls ½«»áʹµÃlsÃüÁîÒÔ/aaa/bbb×÷Ϊ¸ùĿ¼, ¶ø²»ÊÇÒÔÇ°µÄ/.
Èç¹ûʹÓÃalias XX 'chroot /aaa/bbb ls', ²¢°ÑÕâ¾ä·Åµ½Óû§µÄ~/.bashrcÎļþÖеĻ°,
Õ⽫¿ÉÒÔÓÐЧµØÏÞÖÆÔËÐÐÃüÁî"XX"ʱ, ÃüÁî"XX"¿ÉÒÔʹÓÃÎļþϵͳµÄ·¶Î§.
µ±´ÓÆô¶¯Åָ̻´µÄʱºò(chroot µ½ /dev/fd0), »òÕßµ±ÏµÍ³´ÓËÀ»ú״̬»Ö¸´¹ýÀ´²¢×÷Ϊ½ø
ÈëliloµÄÑ¡ÔñÊֶεÄʱºò, chrootÃüÁÊǷdz£·½±ãµÄ. ÆäËüµÄÓ¦Óû¹°üÀ¨´Ó²»Í¬µÄÎļþ
ϵͳ½øÐа²×°(Ò»¸örpmÑ¡Ïî)»òÕß´ÓCDROMÉÏÔËÐÐÒ»¸öÖ»¶ÁÎļþϵͳ. Ö»ÄÜÒÔrootÉí·Ýµ÷ÓÃ,
СÐÄʹÓÃ.
×¢Òâ: ÓÉÓÚÕý³£µÄ$PATH½«²»ÔÙ±»¹ØÁªÁË, ËùÒÔ¿ÉÄÜÐèÒª½«Ò»Ð©Ìض¨µÄϵͳÎļþ¿½±´µ½
chrootedĿ¼ÖÐ.
lockfile
Õâ¸ö¹¤¾ßÊÇprocmail°üµÄÒ»²¿·Ö(www.procmail.org). Ëü¿ÉÒÔ´´½¨Ò»¸öËø¶¨Îļþ, Ëø¶¨ÎÄ
¼þÊÇÒ»ÖÖÓÃÀ´¿ØÖÆ´æÈ¡Îļþ, É豸»ò×ÊÔ´µÄ±ê¼ÇÎļþ. Ëø¶¨Îļþ¾ÍÏñÒ»¸ö±ê¼ÇÒ»Ñù±»Ê¹ÓÃ,
Èç¹ûÌض¨µÄÎļþ, É豸, »ò×ÊÔ´ÕýÔÚ±»Ò»¸öÌض¨µÄ½ø³ÌËùʹÓÃ("busy"), ÄÇô¶ÔÓÚÆäËü½ø
³ÌÀ´Ëµ, ¾ÍÖ»ÄÜÊÜÏÞ½øÐдæÈ¡(»òÕß²»ÄÜ´æÈ¡).
1 lockfile /home/bozo/lockfiles/$0.lock
2 # ´´½¨Ò»¸öÒԽű¾Ãû×ÖΪǰ׺µÄд±£»¤Ëø¶¨Îļþ.
Ëø¶¨ÎļþÓÃÔÚһЩÌض¨µÄ³¡ºÏ, ±ÈÈç˵±£»¤ÏµÍ³µÄmailĿ¼ÒÔ·ÀÖ¹¶à¸öÓû§Í¬Ê±ÐÞ¸Ä, »ò
ÕßÌáʾһ¸ömodem¶Ë¿ÚÕýÔÚ±»´æÈ¡, »òÕßÏÔʾNetscapeµÄÒ»¸öʵÀýÕýÔÚʹÓÃËüµÄ»º´æ. ½Å±¾
¿ÉÒÔ×öһЩ¼ì²é¹¤×÷, ±ÈÈç˵һ¸öÌض¨µÄ½ø³Ì¿ÉÒÔ´´½¨Ò»¸öËø¶¨Îļþ, ÄÇôֻҪ¼ì²éÕâ¸ö
Ìض¨µÄ½ø³ÌÊÇ·ñÔÚÔËÐÐ, ¾Í¿ÉÒÔÅжϳöËø¶¨ÎļþÊÇ·ñ´æÔÚÁË. ×¢ÒâÈç¹û½Å±¾³¢ÊÔ´´½¨Ò»¸ö
ÒѾ­´æÔÚµÄËø¶¨ÎļþµÄ»°, ÄÇô½Å±¾ºÜ¿ÉÄܱ»¹ÒÆð.
Ò»°ãÇé¿öÏÂ, Ó¦Óô´½¨»ò¼ì²éËø¶¨Îļþ¶¼·ÅÔÚ/var/lockĿ¼ÖÐ. [5] ½Å±¾¿ÉÒÔʹÓÃÏÂÃæ
µÄ·½·¨À´¼ì²âËø¶¨ÎļþÊÇ·ñ´æÔÚ.
1 appname=xyzip
2 # Ó¦Óà "xyzip" ´´½¨Ëø¶¨Îļþ "/var/lock/xyzip.lock".
3
4 if [ -e "/var/lock/$appname.lock" ]
5 then
6 ...
flock<rojy bug>
flockÃüÁî±ÈlockfileÃüÁîÓõÃÉٵöà.Much less useful than the lockfile command
is flock. It sets an "advisory" lock on a file and then executes a comma
nd
while the lock is on. This is to prevent any other process from setting
a lock
on that file until completion of the specified command.
1 flock $0 cat $0 > lockfile__$0
2 # Set a lock on the script the above line appears in,
3 #+ while listing the script to stdout.
×¢Òâ: Óëlockfile²»Í¬, flock²»»á×Ô¶¯´´½¨Ò»¸öËø¶¨Îļþ.
mknod
´´½¨¿é»òÕß×Ö·ûÉ豸Îļþ(µ±ÔÚϵͳÉÏ°²×°ÐÂÓ²ÅÌʱ¿ÉÄÜÊDZØÒªµÄ). MAKEDEV¹¤¾ßÊÂʵÉÏ
¾ßÓÐnknodµÄÈ«²¿¹¦ÄÜ, ¶øÇÒ¸üÈÝÒ×ʹÓÃ.
MAKEDEV
´´½¨É豸ÎļþµÄ¹¤¾ß. ±ØÐëÔÚ/devĿ¼ÏÂ, ²¢ÇÒÒÔrootÉí·ÝʹÓÃ.
root# ./MAKEDEV
ÕâÊÇmknodµÄ¸ß¼¶°æ±¾.
tmpwatch
×Ô¶¯É¾³ýÔÚÖ¸¶¨Ê±¼äÄÚδ±»´æÈ¡¹ýµÄÎļþ. ͨ³£¶¼ÊDZ»cronµ÷ÓÃ, ÓÃÀ´É¾µôÀϵÄlogÎļþ.
±¸·ÝÀà
dump, restore
dump ÃüÁîÊÇÒ»¸ö¾«ÇɵÄÎļþϵͳ±¸·Ý¹¤¾ß, ͨ³£¶¼ÓÃÔڱȽϴóµÄ°²×°ºÍÍøÂçÉÏ. [6] Ëü
¶ÁȡԭʼµÄ´ÅÅÌ·ÖÇø²¢ÇÒÒÔ¶þ½øÖÆÐÎʽÀ´Ð´±¸·ÝÎļþ. ÐèÒª±¸·ÝµÄÎļþ¿ÉÒÔ±£´æµ½¸÷ÖÖ¸÷
ÑùµÄ´æ´¢É豸ÉÏ, °üÀ¨´ÅÅ̺ʹŴø. restoreÃüÁîÓÃÀ´»Ö¸´dumpËù²úÉúµÄ±¸·Ý.
fdformat
¶ÔÈíÅ̽øÐеͼ¶¸ñʽ»¯.

ϵͳ×ÊÔ´Àà
ulimit
ÉèÖÃʹÓÃϵͳ×ÊÔ´µÄÉÏÏÞ. ͨ³£Çé¿ö϶¼ÊÇʹÓÃ-fÑ¡ÏîÀ´µ÷ÓÃ, -fÓÃÀ´ÉèÖÃÎļþ³ß´çµÄÏÞ
ÖÆ(ulimit -f 1000¾ÍÊǽ«Îļþ´óСÏÞÖÆΪ1M). -c(ÒëÕß×¢: ÕâÀïÓ¦¸ÃÊÇ×÷Õß±ÊÎó, ×÷Õß
дµÄÊÇ-t)Ñ¡ÏîÀ´ÏÞÖÆcoredump(ÒëÕß×¢: ºËÐÄת´¢, ³ÌÐò±ÀÀ£Ê±µÄÄÚ´æ״̬дÈëÎļþ)
³ß´ç(ulimit -c 0 ¾ÍÊDz»Òªcoredumps). Ò»°ãÇé¿öÏÂ, ulimitµÄÖµÓ¦¸ÃÉèÖÃÔÚ
/etc/profile ºÍ(»ò)~/.bash_profileÖÐ(²Î¼û Appendix G).
×¢Òâ: Judicious ʹÓÃulimit ¿ÉÒÔ±£»¤ÏµÍ³ÃâÊÜ¿ÉŵÄforkÕ¨µ¯µÄÆȺ¦.
1 #!/bin/bash
2 # Õâ¸ö½Å±¾Ö»ÊÇΪÁËչʾÓÃ.
3 # ÄãÒª×Ô¼ºÎªÔËÐÐÕâ¸ö½Å±¾µÄºó¹û¸ºÔð -- Ëü*½«*Äý¹ÌÄãµÄϵͳ.
4
5 while true # ËÀÑ­»·.
6 do
7 $0 & # Õâ¸ö½Å±¾µ÷ÓÃ×ÔÉí . . .
8 #+ forkÎÞÏÞ´Î . . .
9 #+ Ö±µÀϵͳÍêÈ«²»¶¯, ÒòΪËùÓеÄ×ÊÔ´¶¼ºÄ¾¡ÁË.
10 done # Õâ¾ÍÊdzôÃû׿ÖøµÄ "sorcerer's appentice" ¾çÇé.<rojy bug>(Ò
11
12 exit 0 # ÕâÀï²»»áÕæÕýµÄÍƳö, ÒòΪÕâ¸ö½Å±¾²»»áÖÕÖ¹.
µ±Õâ¸ö½Å±¾³¬¹ýÔ¤ÏÈÉèÖõÄÏÞÖÆʱ, ÔÚ/etc/profileÖÐµÄ ulimit -Hu XX (XX ¾ÍÊÇÐè
ÒªÏÞÖƵÄÓû§½ø³Ì) ¿ÉÒÔÖÕÖ¹Õâ¸ö½Å±¾µÄÔËÐÐ.
quota
ÏÔʾÓû§»ò×éµÄ´ÅÅÌÅä¶î.
setquota
´ÓÃüÁîÐÐÖÐÉèÖÃÓû§»ò×éµÄ´ÅÅÌÅä¶î.
umask
É趨Óû§´´½¨ÎļþʱȨÏÞµÄȱʡmask(ÑÚÂë). Ò²¿ÉÒÔÓÃÀ´ÏÞÖÆÌض¨Óû§µÄĬÈÏÎļþÊôÐÔ.
ËùÓÐÓû§´´½¨µÄÎļþÊôÐÔ¶¼ÊÇÓÉumaskËùÖ¸¶¨µÄ. The (octal) ´«µÝ¸øumaskµÄ8½øÖƵÄÖµ¶¨
ÒåÁËÎļþµÄȨÏÞ. ±ÈÈç, umask 022½«»áʹµÃÐÂÎļþµÄȨÏÞ×î¶àΪ755(777 Óë·Ç 022) [7]
µ±È», Óû§¿ÉÒÔËæºóʹÓÃchmodÀ´ÐÞ¸ÄÖ¸¶¨ÎļþµÄÊôÐÔ. Óû§Ò»°ã¶¼Êǽ«umaskÉèÖÃÖµµÄµØ
·½·ÅÔÚ/etc/profile ºÍ(»ò) ~/.bash_profileÖÐ (²Î¼û Appendix G).
Example 13-10 ʹÓÃumaskÀ´½«Êä³öÎļþÒþ²ØÆðÀ´
################################Start Script####################################
###
1 #!/bin/bash
2 # rot13a.sh: Óë"rot13.sh"½Å±¾Ïàͬ, µ«ÊǻὫÊä³öдµÀ"°²È«"ÎļþÖÐ.
3
4 # Ó÷¨: ./rot13a.sh filename
5 # »ò ./rot13a.sh <filename
6 # »ò ./rot13a.sh ͬʱÌṩ¼üÅÌÊäÈë(stdin)
7
8 umask 177 # Îļþ´´½¨ÑÚÂë.
9 # ±»Õâ¸ö½Å±¾Ëù´´½¨µÄÎļþ
10 #+ ½«¾ßÓÐ600ȨÏÞ.
11
12 OUTFILE=decrypted.txt # ½á¹û±£´æÔÚ"decrypted.txt"ÖÐ
13 #+ Õâ¸öÎļþÖ»Äܹ»±»
14 # Õâ¸ö½Å±¾µÄµ÷ÓÃÕß(or root)Ëù¶Áд.
15
16 cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' > $OUTFILE
17 # ^^ ´Óstdin »òÎļþÖÐÊäÈë. ^^^^^^^^^^ Êä³öÖض¨Ïòµ½ÎļþÖÐ.
18
19 exit 0
################################End Script######################################
###
rdev
È¡µÃroot device, swap space, »ò video modeµÄÏà¹ØÐÅÏ¢, »òÕ߶ÔËüÃǽøÐÐÐÞ¸Ä. ͨ³£
˵À´rdev¶¼ÊDZ»liloËùʹÓÃ, µ«ÊÇÔÚ½¨Á¢Ò»¸öram diskµÄʱºò, Õâ¸öÃüÁîÒ²ºÜÓÐÓÃ. СÐÄ
ʹÓÃ, ÕâÊÇÒ»¸öΣÏÕµÄÃüÁî.
Ä£¿éÀà
lsmod
ÁгöËùÓа²×°µÄÄÚºËÄ£¿é.
bash$ lsmod
Module Size Used by
autofs 9456 2 (autoclean)
opl3 11376 0
serial_cs 5456 0 (unused)
sb 34752 0
uart401 6384 0 [sb]
sound 58368 0 [opl3 sb uart401]
soundlow 464 0 [sound]
soundcore 2800 6 [sb sound]
ds 6448 2 [serial_cs]
i82365 22928 2
pcmcia_core 45984 0 [serial_cs ds i82365]
×¢Òâ: ʹÓÃcat /proc/modules¿ÉÒԵõ½Í¬ÑùµÄ½á¹û.
insmod
Ç¿ÖÆÒ»¸öÄÚºËÄ£¿éµÄ°²×°(Èç¹û¿ÉÄܵĻ°, ʹÓÃmodprobeÀ´´úÌæ) ±ØÐëÒÔrootÉí·Ýµ÷ÓÃ.
rmmod
Ç¿ÖÆжÔØÒ»¸öÄÚºËÄ£¿é. ±ØÐëÒÔrootÉí·Ýµ÷ÓÃ.
modprobe
Ä£¿é×°ÔØÆ÷, Ò»°ãÇé¿ö϶¼ÊÇÔÚÆô¶¯½Å±¾ÖÐ×Ô¶¯µ÷ÓÃ. ±ØÐëÒÔrootÉí·Ýµ÷ÓÃ.
depmod
´´½¨Ä£¿éÒÀÀµÎļþ, Ò»°ã¶¼ÊÇÔÚÆô¶¯½Å±¾Öе÷ÓÃ.
modinfo
Êä³öÒ»¸ö¿É×°ÔØÄ£¿éµÄÐÅÏ¢.
bash$ modinfo hid
filename: /lib/modules/2.4.20-6/kernel/drivers/usb/hid.o
description: "USB HID support drivers"
author: "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
license: "GPL"
ÔÓÏîÀà
env
ʹÓÃÉèÖùýµÄ»òÐ޸Ĺý(²¢²»ÊÇÐÞ¸ÄÕû¸öϵͳ»·¾³)µÄ»·¾³±äÁ¿À´ÔËÐÐÒ»¸ö³ÌÐò»ò½Å±¾. ʹ
Óà [varname=xxx] ÐÎʽ¿ÉÒÔÔڽű¾ÖÐÐ޸Ļ·¾³±äÁ¿. Èç¹ûûÓÐÖ¸¶¨²ÎÊý, ÄÇôÕâ¸öÃüÁî
½«»áÁгöËùÓÐÉèÖõĻ·¾³±äÁ¿.
×¢Òâ: ÔÚBashºÍÆäËüµÄBourne shell ÑÜÉúÎïÖÐ, ÊÇ¿ÉÒÔÔÚµ¥Ò»ÃüÁîÐÐÉÏÉèÖöà¸ö±äÁ¿µÄ.
1 var1=value1 var2=value2 commandXXX
2 # $var1 ºÍ $var2 Ö»ÉèÖÃÔÚ'commandXXX'µÄ»·¾³ÖÐ.
×¢Òâ: µ±²»ÖªµÀshell»ò½âÊÍÆ÷µÄ·¾¶µÄʱºò, ½Å±¾µÄµÚÒ»ÐÐ(#!ÐÐ)¿ÉÒÔʹÓÃenv.
1 #! /usr/bin/env perl
2
3 print "This Perl script will run,\n";
4 print "even when I don't know where to find Perl.\n";
5
6 # ±ãÓÚ¿çƽ̨ÒÆÖ²,
7 # Perl³ÌÐò¿ÉÄÜûÔÚÆÚÍûµÄµØ·½.
8 # Thanks, S.C.
ldd
ÏÔʾһ¸ö¿ÉÖ´ÐÐÎļþµÄ¹²Ïí¿âµÄÒÀÀµ¹Øϵ.
bash$ ldd /bin/ls
libc.so.6 => /lib/libc.so.6 (0x4000c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
watch
ÒÔÖ¸¶¨µÄʱ¼ä¼ä¸ôÀ´Öظ´ÔËÐÐÒ»¸öÃüÁî.
ĬÈϵÄʱ¼ä¼ä¸ôÊÇ2Ãë, µ«Ê±¿ÌÒÔʹÓÃ-nÑ¡ÏîÀ´ÐÞ¸Ä.
1 watch -n 5 tail /var/log/messages
2 # ÿ¸ô5ÃëÖÓÏÔʾϵͳlogÎļþµÄ½áβ, /var/log/messages.
strip
´Ó¿ÉÖ´ÐÐÎļþÖÐÈ¥µôµ÷ÊÔ·ûºÅÒýÓÃ. ÕâÑù×ö¿ÉÒÔ¼õС³ß´ç, µ«ÊǾͲ»Äܵ÷ÊÔÁË.
Õâ¸öÃüÁîÒ»°ã¶¼ÓÃÔÚMakefileÖÐ, µ«ÊǺÜÉÙÓÃÔÚshell½Å±¾ÖÐ.
nm
Áгöδstrip¹ýµÄ±àÒëºóµÄ2½øÖÆÎļþµÄ·ûºÅ.
rdist
Ô¶³ÌÎļþ·Ö²¼¿Í»§»ú³ÌÐò: ÔÚÔ¶¶Ë·þÎñÆ÷ÉÏͬ²½, ¿Ë¡, »òÕß±¸·ÝÒ»¸öÎļþϵͳ.

13.1 ·ÖÎöÒ»¸öϵͳ½Å±¾
---------------------
ÀûÓÃÎÒÃÇËùѧµ½µÄ¹ØÓÚ¹ÜÀíÃüÁîµÄ֪ʶ, ÈÃÎÒÃÇÒ»ÆðÀ´Á·Ï°·ÖÎöÒ»¸öϵͳ½Å±¾. ×î¼òµ¥²¢
ÇÒ×î¶ÌµÄϵͳ½Å±¾Ö®Ò»ÊÇkillall, Õâ¸ö½Å±¾±»ÓÃÀ´ÔÚϵͳ¹Ø»úʱ¹ÒÆðÔËÐеĽű¾.
Example 13-11 killall, À´×ÔÓÚ /etc/rc.d/init.d
################################Start Script####################################
###
1 #!/bin/sh
2
3 # --> ±¾Êé×÷ÕßËù×÷µÄ×¢ÊÍÈ«²¿ÒÔ"# -->"¿ªÍ·.
4
5 # --> ÕâÊÇÓÉMiquel van SmoorenburgËù±àдµÄ
6 # --> 'rc'½Å±¾°üµÄÒ»²¿·Ö, <miquels@drinkel.nl.mugnet.org>.
7
8 # --> Õâ¸öÌØÊâµÄ½Å±¾¿´ÆðÀ´ÊÇÊÇΪRed Hat / FCËùÌض¨µÄ,
9 # --> (ÔÚÆäËüµÄ·¢ÐаæÖпÉÄܲ»»á³öÏÖ).
10
11 # Í£Ö¹ËùÓÐÕýÔÚÔËÐеIJ»±ØÒªµÄ·þÎñ
12 #+ (there shouldn't be any, so this is just a sanity check)
13
14 for i in /var/lock/subsys/*; do
15 # --> ±ê×¼µÄfor/inÑ­»·, µ«ÊÇÓÉÓÚ"do"ÔÚͬһÐÐÉÏ,
16 # --> ËùÒÔ±ØÐëÌí¼Ó";".
17 # ¼ì²é½Å±¾ÊÇ·ñÔÚÄÇ.
18 [ ! -f $i ] && continue
19 # --> ÕâÊÇÒ»ÖÖʹÓÃ"ÓëÁбí"µÄ´ÏÃ÷µÄ·½·¨, µÈ¼ÛÓÚ:
20 # --> if [ ! -f "$i" ]; then continue
21
22 # È¡µÃ×ÓϵͳµÄÃû×Ö.
23 subsys=${i#/var/lock/subsys/}
24 # --> Æ¥Åä±äÁ¿Ãû, ÔÚÕâÀï¾ÍÊÇÎļþÃû.
25 # --> Óësubsys=`basename $i`ÍêÈ«µÈ¼Û.
26
27 # --> ´ÓËø¶¨ÎļþÃûÖлñµÃ
28 # -->+ (Èç¹ûÄÇÀïÓÐËø¶¨ÎļþµÄ»°,
29 # -->+ ÄǾÍÖ¤Ã÷½ø³ÌÕýÔÚÔËÐÐ).
30 # --> ²Î¿¼Ò»ÏÂÉϱßËù½²µÄ"Ëø¶¨Îļþ"µÄÄÚÈÝ.
31
32
33 # ÖÕÖ¹×Óϵͳ.
34 if [ -f /etc/rc.d/init.d/$subsys.init ]; then
35 /etc/rc.d/init.d/$subsys.init stop
36 else
37 /etc/rc.d/init.d/$subsys stop
38 # --> ¹ÒÆðÔËÐеÄ×÷ÒµºÍÓÄÁé½ø³Ì.
39 # --> ×¢Òâ"stop"Ö»ÊÇÒ»¸öλÖòÎÊý,
40 # -->+ ²¢²»ÊÇshellÄÚ½¨ÃüÁî.
41 fi
42 done
################################End Script######################################
###
Õâ¸öûÓÐÄÇôÔã. ³ýÁËÔÚ±äÁ¿Æ¥ÅäµÄµØ·½ÍæÁËÒ»µã»¨Ñù, ÆäËüҲûÓбðµÄ²ÄÁÏÁË.
Á·Ï° 1. ÔÚ/etc/rc.d/init.dÖÐ, ·ÖÎöhalt½Å±¾. ±È½Å±¾killall³¤Ò»Ð©, µ«ÊǸÅÄîÉϺÜÏà½ü.
¶ÔÕâ¸ö½Å±¾×öÒ»¸ö¿½±´, ·Åµ½ÄãµÄhomeĿ¼Ï²¢ÇÒÓÃËüÁ·Ï°Ò»ÏÂ(²»ÒªÒÔrootÉí·ÝÔË
ÐÐËü). ʹÓÃ-vn±êÖ¾À´Ä£ÄâÔËÐÐÒ»ÏÂ(sh -vn scriptname). Ìí¼ÓÏêϸµÄ×¢ÊÍ. ½«
"action"ÃüÁîÐÞ¸ÄΪ"echos".
Á·Ï° 2. ²ì¿´/etc/rc.d/init.dϵĸü¶à¸ü¸´ÔӵĽű¾. ¿´¿´ÄãÊDz»ÊÇÄܹ»Àí½âÆäÖеÄһЩ½Å
±¾. ʹÓÃÉϱߵĹý³ÌÀ´·ÖÎöÕâЩ½Å±¾. ΪÁ˸üÏêϸµÄÀí½â, Äã¿ÉÄÜÒ²ÐèÒª·ÖÎöÔÚ
usr/share/doc/initscripts-?.??Ŀ¼ÏµÄÎļþsysvinitfile, ÕâЩ¶¼ÊÇ
"initscript"ÎļþµÄÒ»²¿·Ö.
×¢ÒâÊÂÏî:
[1] ÕâÊÇÔÚLinux»úÆ÷ÉÏ»òÕßÔÚ´øÓдÅÅÌÅä¶îµÄUNIXϵͳÉϵÄÕæʵÇé¿ö.
[2] Èç¹ûÕýÔÚ±»É¾³ýµÄÌض¨µÄÓû§ÒѾ­µÇ¼ÁËÖ÷»ú, ÄÇô userdel ÃüÁ»áʧ°Ü.
[3] ¶ÔÓÚÉÕ¼CDRµÄ¸ü¶àµÄϸ½Ú, ¿ÉÒԲμûAlex WithersµÄÎÄÕÂ, ´´½¨CD, ÔÚ
Linux Journal µÄ1999ÄêµÄ10ÔÂÎÄÕÂÁбíÖÐ.
[4] mke2fsµÄ-cÑ¡ÏîÒ²»á½øÐлµ¿é¼ì²é.
[5] ÒòΪֻÓÐrootÓû§²Å¾ßÓжÔ/var/lockĿ¼µÄдȨÏÞ, Ò»°ãµÄÓû§½Å±¾ÊDz»ÄÜÔÚÄÇÀï
ÉèÖÃÒ»¸öËø¶¨ÎļþµÄ.
[6] µ¥Óû§µÄLinuxϵͳµÄ²Ù×÷¸üÇãÏòÓÚʹÓüòµ¥µÄ±¸·Ý¹¤¾ß, ±ÈÈçtar.
[7] NAND(Óë·Ç)ÊÇÒ»ÖÖÂß¼­²Ù×÷. ÕâÖÖ²Ù×÷µÄЧ¹ûºÍ¼õ·¨ºÜÏàÏñ.

µÚ14Õ ÃüÁîÌæ»»
================
ÃüÁîÌæ»»½«»áÖØзÖÅäÒ»¸öÃüÁî[1]ÉõÖÁÊǶà¸öÃüÁîµÄÊä³ö; Ëü»á½«ÃüÁîµÄÊä³öÈçʵµØÌí¼Óµ½
ÁíÒ»¸öÉÏÏÂÎÄÖÐ. [2]
ʹÓÃÃüÁîÌæ»»µÄµäÐÍÐÎʽÊÇʹÓúóÖÃÒýÓÃ(`...`). ºóÖÃÒýÓÃÐÎʽµÄÃüÁî(¾ÍÊDZ»·´ÒýºÅÀ¨ÆðÀ´)
½«»á²úÉúÃüÁîÐÐÎı¾.
1 script_name=`basename $0`
2 echo "The name of this script is $script_name."
ÕâÑùµÄ»°, ÃüÁîµÄÊä³ö¿ÉÒÔ±»µ±³É´«µÝµ½ÁíÒ»¸öÃüÁîµÄ²ÎÊý, »òÕß±£´æµ½±äÁ¿ÖÐ, ÉõÖÁ¿ÉÒÔÓÃ
À´²úÉúforÑ­»·µÄ²ÎÊýÁбí.
1 rm `cat filename` # "filename" °üº¬ÁËÐèÒª±»É¾³ýµÄÎļþÁбí.
2 #
3 # S. C. Ö¸³öʹÓÃÕâÖÖÐÎʽ, ¿ÉÄÜ»á²úÉú"²ÎÊýÁбíÌ«³¤"µÄ´íÎó.
4 # ¸üºÃµÄ·½·¨ÊÇ xargs rm -- < filename
5 # ( -- ͬʱ¸²¸ÇÁËÄÇЩÒÔ"-"¿ªÍ·µÄÎļþËù²úÉúµÄÌØÊâÇé¿ö )
6
7 textfile_listing=`ls *.txt`
8 # ±äÁ¿Öаüº¬Á˵±Ç°¹¤×÷Ŀ¼ÏÂËùÓеÄ*.txtÎļþ.
9 echo $textfile_listing
10
11 textfile_listing2=$(ls *.txt) # ÕâÊÇÃüÁîÌæ»»µÄÁíÒ»ÖÖÐÎʽ.
12 echo $textfile_listing2
13 # ͬÑùµÄ½á¹û.
14
15 # ½«ÎļþÁбí·ÅÈëµ½Ò»¸ö×Ö·û´®ÖеÄÒ»¸ö¿ÉÄܵÄÎÊÌâ¾ÍÊÇ
16 # ¿ÉÄÜ»á»ì½øÒ»¸öÐÂÐÐ.
17 #
18 # Ò»¸ö°²È«µÄ½«ÎļþÁÐ±í´«µÝµ½²ÎÊýÖеķ½·¨¾ÍÊÇʹÓÃÊý×é.
19 # shopt -s nullglob # Èç¹û²»Æ¥Åä, ÄǾͲ»½øÐÐÎļþÃûÀ©Õ¹.
20 # textfile_listing=( *.txt )
21 #
22 # Thanks, S.C.
×¢Òâ: ÃüÁîÌæ»»½«»áµ÷ÓÃÒ»¸ösubshell.
×¢Òâ: ÃüÁîÌæ»»¿ÉÄÜ»áÒýÆðword splitting.
1 COMMAND `echo a b` # 2¸ö²ÎÊý: a and b
2
3 COMMAND "`echo a b`" # 1¸ö²ÎÊý: "a b"
4
5 COMMAND `echo` # ÎÞ²ÎÊý
6
7 COMMAND "`echo`" # Ò»¸ö¿ÕµÄ²ÎÊý
8
9
10 # Thanks, S.C.
¼´Ê¹Ã»ÓÐÒýÆðword splitting, ÃüÁîÌæ»»Ò²»áÈ¥µô¶àÓàµÄÐÂÐÐ.
1 # cd "`pwd`" # Õâ¾ä×Ü»áÕý³£µÄ¹¤×÷.
2 # È»¶ø...
3
4 mkdir 'dir with trailing newline
5 '
6
7 cd 'dir with trailing newline
8 '
9
10 cd "`pwd`" # ´íÎóÏûÏ¢:
11 # bash: cd: /tmp/file with trailing newline: No such file or directory
12
13 cd "$PWD" # ÔËÐÐÁ¼ºÃ.
14
15
16
17
18
19 old_tty_setting=$(stty -g) # ±£´æÀϵÄÖÕ¶ËÉèÖÃ.
20 echo "Hit a key "
21 stty -icanon -echo # ¶ÔÖն˽ûÓÃ"canonical"ģʽ.
22 # ÕâÑùµÄ»°, Ò²»á½ûÓÃÁË*±¾µØ*µÄecho.
23 key=$(dd bs=1 count=1 2> /dev/null) # ʹÓÃ'dd'ÃüÁîÀ´È¡µÃÒ»¸ö°´¼ü.
24 stty "$old_tty_setting" # ±£´æÀϵÄÉèÖÃ.
25 echo "You hit ${#key} key." # ${#variable} = number of characters in $vari
able
26 #
27 # °´¼üÈκμü³ýÁ˻سµ, ÄÇôÊä³ö¾ÍÊÇ"You hit 1 key."
28 # °´Ï»سµ, ÄÇôÊä³ö¾ÍÊÇ"You hit 0 key."
29 # ÐÂÐÐÒѾ­±»ÃüÁîÌæ»»³ÔµôÁË.
30
31 Thanks, S.C.
×¢Òâ: µ±Ò»¸ö±äÁ¿ÊÇʹÓÃÃüÁîÌæ»»µÄ½á¹û×öΪֵµÄʱºò, È»ºóʹÓÃechoÃüÁîÀ´Êä³öÕâ¸ö±äÁ¿
(²¢ÇÒ²»ÒýÓÃÕâ¸ö±äÁ¿, ¾ÍÊDz»ÓÃÒýºÅÀ¨ÆðÀ´), ÄÇôÃüÁîÌæ»»½«»á´Ó×îÖÕµÄÊä³öÖÐɾµô»»
Ðзû. Õâ¿ÉÄÜ»áÒýÆðһЩÒì³£Çé¿ö.
1 dir_listing=`ls -l`
2 echo $dir_listing # δÒýÓÃ, ¾ÍÊÇûÓÃÒýºÅÀ¨ÆðÀ´
3
4 # Ïë´ò³öÀ´Ò»¸öÓÐÐòµÄĿ¼Áбí.Expecting a nicely ordered directory listing.
5
6 # ¿Éϧ, ϱ߽«ÊÇÎÒÃÇËù»ñµÃµÄ:
7 # total 3 -rw-rw-r-- 1 bozo bozo 30 May 13 17:15 1.txt -rw-rw-r-- 1 bozo
8 # bozo 51 May 15 20:57 t2.sh -rwxr-xr-x 1 bozo bozo 217 Mar 5 21:13 wi.sh
9
10 # ÐÂÐÐÏûʧÁË.
11
12
13 echo "$dir_listing" # ÓÃÒýºÅÀ¨ÆðÀ´
14 # -rw-rw-r-- 1 bozo 30 May 13 17:15 1.txt
15 # -rw-rw-r-- 1 bozo 51 May 15 20:57 t2.sh
16 # -rwxr-xr-x 1 bozo 217 Mar 5 21:13 wi.sh
ÃüÁîÌæ»»ÉõÖÁÔÊÐí½«Õû¸öÎļþµÄÄÚÈݷŵ½±äÁ¿ÖÐ, ¿ÉÒÔʹÓÃÖض¨Ïò»òÕßcatÃüÁî.
1 variable1=`<file1` # ½«"file1"µÄÄÚÈݷŵ½"variable1"ÖÐ.
2 variable2=`cat file2` # ½«"file2"µÄÄÚÈݷŵ½"variable2"ÖÐ.
3 # µ«ÊÇÕâÐн«»áforkÒ»¸öнø³Ì, This, however, forks a new proces
4 #+ ËùÒÔÕâÐдúÂ뽫»á±ÈµÚÒ»ÐдúÂëÖ´ÐеÃÂý.
5
6 # ×¢Òâ:
7 # ±äÁ¿ÖÐÊÇ¿ÉÒÔ°üº¬¿Õ°×µÄ,
8 #+ ÉõÖÁÊÇ (Ñá¶ñÖÁ¼«µÄ), ¿ØÖÆ×Ö·û.
1 # ժ¼×ÔϵͳÎļþ, /etc/rc.d/rc.sysinit
2 #+ (ÕâÊǺìñ°²×°ÖÐʹÓõÄ)
3
4
5 if [ -f /fsckoptions ]; then
6 fsckoptions=`cat /fsckoptions`
7 ...
8 fi
9 #
10 #
11 if [ -e "/proc/ide/${disk[$device]}/media" ] ; then
12 hdmedia=`cat /proc/ide/${disk[$device]}/media`
13 ...
14 fi
15 #
16 #
17 if [ ! -n "`uname -r | grep -- "-"`" ]; then
18 ktag="`cat /proc/version`"
19 ...
20 fi
21 #
22 #
23 if [ $usb = "1" ]; then
24 sleep 5
25 mouseoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.
*Prot=02"`
26 kbdoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.*P
rot=01"`
27 ...
28 fi
×¢Òâ: ²»Òª½«Ò»¸ö·Ç³£³¤µÄÎı¾ÎļþµÄÄÚÈÝÉèÖõ½Ò»¸ö±äÁ¿ÖÐ, ³ý·ÇÄãÓÐÒ»¸ö·Ç³£ºÃµÄÔ­Òò·Ç
ÒªÕâô×ö²»¿É. ²»Òª½«2½øÖÆÎļþµÄÄÚÈݱ£´æµ½±äÁ¿ÖÐ.
Example 14-1 ÓÞ´ÀµÄ½Å±¾²ßÂÔ
################################Start Script####################################
###
1 #!/bin/bash
2 # stupid-script-tricks.sh: ÅóÓÑ, ±ðÔÚ¼ÒÕâô×ö.
3 # À´×ÔÓÚ"Stupid Script Tricks," ¾íI.
4
5
6 dangerous_variable=`cat /boot/vmlinuz` # ÕâÊÇѹËõ¹ýµÄLinuxÄں˱¾Éí.
7
8 echo "string-length of \$dangerous_variable = ${#dangerous_variable}"
9 # Õâ¸ö×Ö·û´®±äÁ¿µÄ³¤¶ÈÊÇ $dangerous_variable = 794151
10 # (²»ÒªÊ¹ÓÃ'wc -c /boot/vmlinuz'À´¼ÆË㳤¶È.)
11
12 # echo "$dangerous_variable"
13 # ǧÍò±ð³¢ÊÔÕâô×ö! ÕâÑù½«¹ÒÆðÕâ¸ö½Å±¾.
14
15
16 # Îĵµ×÷ÕßÒѾ­Òâʶµ½½«¶þ½øÖÆÎļþÉèÖõ½
17 #+ ±äÁ¿ÖÐÊÇÒ»¸öûÓõÄÓ¦ÓÃ.
18
19 exit 0
################################End Script######################################
###
×¢Òâ, ÔÚÕâÀïÊDz»»á·¢Éú»º³åÇøÒç³ö´íÎó. ÒòΪÕâÊÇÒ»¸ö½âÊÍÐÍÓïÑÔµÄʵÀý, Bash¾ÍÊÇÒ»
ÖÖ½âÊÍÐÍÓïÑÔ, ½âÊÍÐÍÓïÑÔ»á±È±àÒëÐÍÓïÑÔÌṩ¸ü¶àµÄ¶Ô³ÌÐò´íÎóµÄ±£»¤´ëÊ©.
±äÁ¿Ìæ»»ÔÊÐí½«Ò»¸öÑ­»·µÄÊä³ö·ÅÈëµ½Ò»¸ö±äÁ¿ÖÐ.Õâô×öµÄ¹Ø¼ü¾ÍÊǽ«Ñ­»·ÖÐechoÃüÁîµÄÊä
³öÈ«²¿½ØÈ¡.
Example 14-2 ´ÓÑ­»·µÄÊä³öÖвúÉúÒ»¸ö±äÁ¿
################################Start Script####################################
###
1 #!/bin/bash
2 # csubloop.sh: ´ÓÑ­»·µÄÊä³öÖвúÉúÒ»¸ö±äÁ¿.
3
4 variable1=`for i in 1 2 3 4 5
5 do
6 echo -n "$i" # ¶ÔÓÚÕâÀïµÄÃüÁîÌæ»»À´Ëµ
7 done` #+ Õâ¸ö'echo'ÃüÁîÊǷdz£¹Ø¼üµÄ.
8
9 echo "variable1 = $variable1" # variable1 = 12345
10
11
12 i=0
13 variable2=`while [ "$i" -lt 10 ]
14 do
15 echo -n "$i" # ÔÙÀ´Ò»¸ö, 'echo'ÊDZØÐëµÄ.
16 let "i += 1" # µÝÔö.
17 done`
18
19 echo "variable2 = $variable2" # variable2 = 0123456789
20
21 # Õâ¾ÍÖ¤Ã÷ÁËÔÚÒ»¸ö±äÁ¿ÉùÃ÷ÖÐ
22 #+ ǶÈëÒ»¸öÑ­»·ÊÇ¿ÉÐеÄ.
23
24 exit 0
################################End Script######################################
###
×¢Òâ: ÃüÁîÌ滻ʹµÃÀ©Õ¹ÓÐЧµÄBash¹¤¾ß¼¯±äΪ¿ÉÄÜ. ÕâÑù, дһ¶ÎС³ÌÐò»òÕßÒ»¶Î½Å±¾¾Í¿É
ÒԴﵽĿµÄ, ÒòΪ³ÌÐò»ò½Å±¾µÄÊä³ö»á´«µ½stdoutÉÏ(¾ÍÏñÒ»¸ö±ê×¼µÄ¹¤¾ßËù×öµÄÄÇÑù),
È»ºóÖØн«ÕâЩÊä³ö±£´æµ½±äÁ¿ÖÐ.(ÒëÕß: ×÷ÕßµÄÒâ˼¾ÍÊÇÔÚÕâÖÖÇé¿öÏÂд½Å±¾ºÍд³ÌÐò
×÷ÓÃÊÇÒ»ÑùµÄ.)
1 #include <stdio.h>
2
3 /* "Hello, world." C program */
4
5 int main()
6 {
7 printf( "Hello, world." );
8 return (0);
9 }
bash$ gcc -o hello hello.c
1 #!/bin/bash
2 # hello.sh
3
4 greeting=`./hello`
5 echo $greeting
bash$ sh hello.sh
Hello, world.
×¢Òâ: ¶ÔÓÚÃüÁîÌæ»»À´Ëµ,$(COMMAND) ÐÎʽÒѾ­È¡´úÁË·´ÒýºÅ"`".
1 output=$(sed -n /"$1"/p $file) # À´×ÔÓÚ "grp.sh"Àý×Ó.
2
3 # ½«Ò»¸öÎı¾µÄÄÚÈݱ£´æµ½±äÁ¿ÖÐ.
4 File_contents1=$(cat $file1)
5 File_contents2=$(<$file2) # Bash Ò²ÔÊÐíÕâô×ö.
$(...) ÐÎʽµÄÃüÁîÌæ»»ÔÚ´¦ÀíË«·´Ð±Ïß(\\)ʱÓë`...`ÐÎʽ²»Í¬.
bash$ echo `echo \\`
bash$ echo $(echo \\)
\
$(...) ÐÎʽµÄÃüÁîÌæ»»ÊÇÔÊÐíǶÌ×µÄ. [3]
1 word_count=$( wc -w $(ls -l | awk '{print $9}') )
»òÕß, ¿ÉÒÔ¸ü¼ÓÁé»î. . .
Example 14-3 ÕÒanagram(»ØÎĹ¹´Ê·¨, ¿ÉÒÔ½«Ò»¸öÓÐÒâÒåµÄµ¥´Ê, ±ä»»Îª1¸ö»ò¶à¸öÓÐÒâÒåµÄµ¥´Ê, µ«ÊÇ»
################################Start Script####################################
###
1 #!/bin/bash
2 # agram2.sh
3 # ¹ØÓÚÃüÁîÌ滻ǶÌ×µÄÀý×Ó.
4
5 # ʹÓÃ"anagram"¹¤¾ß
6 #+ ÕâÊÇ×÷ÕßµÄ"yawl"ÎÄ×Ö±í°üÖеÄÒ»²¿·Ö.
7 # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz
8 # http://personal.riverusers.com/~thegrendel/yawl-0.3.2.tar.gz
9
10 E_NOARGS=66
11 E_BADARG=67
12 MINLEN=7
13
14 if [ -z "$1" ]
15 then
16 echo "Usage $0 LETTERSET"
17 exit $E_NOARGS # ½Å±¾ÐèÒªÒ»¸öÃüÁîÐвÎÊý.
18 elif [ ${#1} -lt $MINLEN ]
19 then
20 echo "Argument must have at least $MINLEN letters."
21 exit $E_BADARG
22 fi
23
24
25
26 FILTER='.......' # ±ØÐëÖÁÉÙÓÐ7¸ö×Ö·û.
27 # 1234567
28 Anagrams=( $(echo $(anagram $1 | grep $FILTER) ) )
29 # | | ǶÌ×µÄÃüÁîÌæ»» | |
30 # ( Êý×é·ÖÅä )
31
32 echo
33 echo "${#Anagrams[*]} 7+ letter anagrams found"
34 echo
35 echo ${Anagrams[0]} # µÚÒ»¸öanagram.
36 echo ${Anagrams[1]} # µÚ¶þ¸öanagram.
37 # µÈµÈ.
38
39 # echo "${Anagrams[*]}" # ÔÚÒ»ÐÐÉÏÁгöËùÓеÄanagram . . .
40
41 # ¿¼Âǵ½ºó±ß»¹ÓÐ"Êý×é"×÷Ϊµ¥¶ÀµÄÒ»Õ½øÐн²½â,
42 #+ ÕâÀï¾Í²»ÉîÈëÁË.
43
44 # ¿ÉÒÔ²ÎÔÄagram.sh½Å±¾, ÕâÒ²ÊÇÒ»¸öÕÒ³öanagramµÄÀý×Ó.
45
46 exit $?
################################End Script######################################
###
ÃüÁîÌæ»»Ôڽű¾ÖÐʹÓõÄÀý×Ó:
1. Example 10-7
2. Example 10-26
3. Example 9-28
4. Example 12-3
5. Example 12-19
6. Example 12-15
7. Example 12-49
8. Example 10-13
9. Example 10-10
10. Example 12-29
11. Example 16-8
12. Example A-17
13. Example 27-2
14. Example 12-42
15. Example 12-43
16. Example 12-44
×¢ÒâÊÂÏî:
[1] ¶ÔÓÚÃüÁîÌæ»»À´Ëµ, Õâ¸öÃüÁî¿ÉÒÔÊÇÍⲿµÄϵͳÃüÁî, Ò²¿ÉÒÔÊÇÄÚ²¿½Å±¾µÄÄÚ½¨
ÃüÁî, ÉõÖÁÊÇÒ»¸ö½Å±¾º¯Êý.
[2] ´Ó¼¼ÊõµÄ½Ç¶ÈÀ´½², ÃüÁîÌæ»»½«»á³éÈ¡³öÒ»¸öÃüÁîµÄÊä³ö, È»ºóʹÓÃ=²Ù×÷¸³Öµµ½
Ò»¸ö±äÁ¿ÖÐ.
[3] ÊÂʵÉÏ, ¶ÔÓÚ·´ÒýºÅµÄǶÌ×ÊÇ¿ÉÐеÄ, µ«ÊÇÖ»Äܽ«ÄÚ²¿µÄ·´ÒýºÅתÒå²ÅÐÐ, ¾ÍÏñ
JohnĬÈÏÖ¸³öµÄÄÇÑù.
1 word_count=` wc -w \`ls -l | awk '{print $9}'\` `

µÚ15Õ ËãÊõÀ©Õ¹
================
ËãÊõÀ©Õ¹ÌṩÁËÒ»ÖÖÇ¿Á¦µÄ¹¤¾ß, ¿ÉÒÔÔڽű¾ÖÐÖ´ÐÐ(ÕûÐÍ)Ëã·¨²Ù×÷. ¿ÉÒÔʹÓÃbackticks,
double parentheses, »ò letÀ´½«×Ö·û´®×ª»»ÎªÊý×Ö±í´ïʽ.
һЩ±ä»¯
ʹÓ÷´ÒýºÅµÄËãÊõÀ©Õ¹(ͨ³£¶¼ÊǺÍexprÒ»ÆðʹÓÃ)
1 z=`expr $z + 3` # 'expr'ÃüÁ»áÖ´ÐÐÕâ¸öÀ©Õ¹.
ʹÓÃË«À¨ºÅ, ºÍletÐÎʽµÄËãÊõÀ©Õ¹
·´ÒýºÅÐÎʽµÄËãÊõÀ©Õ¹ÒѾ­±»Ë«À¨ºÅÐÎʽËùÌæ´úÁË -- ((...)) ºÍ $((...)) -- µ±È»Ò²¿ÉÒÔ
ʹÓ÷dz£·½±ãµÄletÐÎʽ.
1 z=$(($z+3))
2 z=$((z+3)) # Ò²ÕýÈ·.
3 # ʹÓÃË«À¨ºÅµÄÐÎʽ,
4 #+ ²ÎÊý½âÒýÓÃ
5 #+ ÊÇ¿ÉÑ¡µÄ.
6
7 # $((EXPRESSION)) is arithmetic expansion. # ²»ÒªÓëÃüÁî
8 #+ Ìæ»»Ïà»ìÏý.
9
10
11
12 # ʹÓÃË«À¨ºÅµÄÐÎʽҲ¿ÉÒÔ²»Óøø±äÁ¿¸³Öµ.
13
14 n=0
15 echo "n = $n" # n = 0
16
17 (( n += 1 )) # µÝÔö.
18 # (( $n += 1 )) is incorrect!
19 echo "n = $n" # n = 1
20
21
22 let z=z+3
23 let "z += 3" # ʹÓÃÒýÓõÄÐÎʽ, ÔÊÐíÔÚ±äÁ¿¸³ÖµµÄʱºò´æÔÚ¿Õ¸ñ.
24 # 'let'²Ù×÷ÊÂʵÉÏÖ´ÐеõÄÊÇËãÊõ¸³Öµ,
25 #+ ¶ø²»ÊÇËãÊõÀ©Õ¹.
ϱßÊÇһЩÔڽű¾ÖÐʹÓÃËãÊõÀ©Õ¹µÄÀý×Ó:
1. Example 12-9
2. Example 10-14
3. Example 26-1
4. Example 26-11
5. Example A-17

µÚ16Õ I/O Öض¨Ïò


==================
ĬÈÏÇé¿öÏÂʼÖÕÓÐ3¸ö"Îļþ"´¦ÓÚ´ò¿ª×´Ì¬, stdin (¼üÅÌ), stdout (ÆÁÄ»), and stderr
(´íÎóÏûÏ¢Êä³öµ½ÆÁÄ»ÉÏ). Õâ3¸öÎļþºÍÆäËû´ò¿ªµÄÎļþ¶¼¿ÉÒÔ±»Öض¨Ïò. ¶ÔÓÚÖض¨Ïò¼òµ¥µÄ
½âÊ;ÍÊDz¶×½Ò»¸öÎļþ, ÃüÁî, ³ÌÐò, ½Å±¾, »òÕßÉõÖÁÊǽű¾ÖеĴúÂë¿é(²Î¼û Example 3-1
ºÍ Example 3-2)µÄÊä³ö, È»ºó½«ÕâЩÊä³ö×÷ΪÊäÈë·¢Ë͵½ÁíÒ»¸öÎļþ, ÃüÁî, ³ÌÐò, »ò½Å±¾
ÖÐ.
ÿ¸ö´ò¿ªµÄÎļþ¶¼»á±»·ÖÅäÒ»¸öÎļþÃèÊö·û.[1]stdin, stdout, ºÍstderrµÄÎļþÃèÊö·û·Ö±ð
ÊÇ0, 1, ºÍ 2. ¶ÔÓÚÕýÔÚ´ò¿ªµÄ¶îÍâÎļþ, ±£ÁôÁËÃèÊö·û3µ½9. ÔÚijЩʱºò½«ÕâЩ¸ñÍâµÄÎļþ
ÃèÊö·û·ÖÅä¸østdin, stdout, »òÕßÊÇstderr×÷ΪÁÙʱµÄ¸±±¾Á´½ÓÊǷdz£ÓÐÓõÄ. [2] ÔÚ¾­¹ý
¸´ÔÓµÄÖض¨ÏòºÍË¢ÐÂÖ®ºóÐèÒª°ÑËüÃǻָ´³ÉÕý³£µÄÑù×Ó (²Î¼û Example 16-1).
1 COMMAND_OUTPUT >
2 # Öض¨Ïòstdoutµ½Ò»¸öÎļþ.
3 # Èç¹ûûÓÐÕâ¸öÎļþ¾Í´´½¨, ·ñÔò¾Í¸²¸Ç.
4
5 ls -lR > dir-tree.list
6 # ´´½¨Ò»¸ö°üº¬Ä¿Â¼Ê÷ÁбíµÄÎļþ.
7
8 : > filename
9 # > »á°ÑÎļþ"filename"½Ø¶ÏΪ0³¤¶È.
10 # Èç¹ûÎļþ²»´æÔÚ, ÄÇô¾Í´´½¨Ò»¸ö0³¤¶ÈµÄÎļþ(Óë'touch'µÄЧ¹ûÏàͬ).
11 # : ÊÇÒ»¸öռλ·û, ²»²úÉúÈκÎÊä³ö.
12
13 > filename
14 # > »á°ÑÎļþ"filename"½Ø¶ÏΪ0³¤¶È.
15 # Èç¹ûÎļþ²»´æÔÚ, ÄÇô¾Í´´½¨Ò»¸ö0³¤¶ÈµÄÎļþ(Óë'touch'µÄЧ¹ûÏàͬ).
16 # (ÓëÉϱߵÄ": >"Ч¹ûÏàͬ, µ«ÊÇÔÚijЩshellÏ¿ÉÄܲ»Äܹ¤×÷.)
17
18 COMMAND_OUTPUT >>
19 # Öض¨Ïòstdoutµ½Ò»¸öÎļþ.
20 # Èç¹ûÎļþ²»´æÔÚ, ÄÇô¾Í´´½¨Ëü, Èç¹û´æÔÚ, ÄÇô¾Í×·¼Óµ½Îļþºó±ß.
21
22
23 # µ¥ÐÐÖض¨ÏòÃüÁî(Ö»»áÓ°ÏìËüÃÇËùÔÚµÄÐÐ):
24 # -------------------------------------------------------------------
-
25
26 1>filename
27 # Öض¨Ïòstdoutµ½Îļþ"filename".
28 1>>filename
29 # Öض¨Ïò²¢×·¼Óstdoutµ½Îļþ"filename".
30 2>filename
31 # Öض¨Ïòstderrµ½Îļþ"filename".
32 2>>filename
33 # Öض¨Ïò²¢×·¼Óstderrµ½Îļþ"filename".
34 &>filename
35 # ½«stdoutºÍstderr¶¼Öض¨Ïòµ½Îļþ"filename".
36
37 #====================================================================
==========
38 # Öض¨Ïòstdout, Ò»´ÎÒ»ÐÐ.
39 LOGFILE=script.log
40
41 echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFI
LE
42 echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE
43 echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE
44 echo "This statement is echoed to stdout, and will not appear in \"$L
OGFILE\"."
45 # ÿÐйýºó, ÕâЩÖض¨ÏòÃüÁî»á×Ô¶¯"reset".
46
47
48
49 # Öض¨Ïòstderr, Ò»´ÎÒ»ÐÐ.
50 ERRORFILE=script.errors
51
52 bad_command1 2>$ERRORFILE # ´íÎóÏûÏ¢·¢µ½$ERRORFILEÖÐ.
53 bad_command2 2>>$ERRORFILE # ´íÎóÏûÏ¢Ìí¼Óµ½$ERRORFILEÖÐ.
54 bad_command3 # ´íÎóÏûÏ¢echoµ½stderr,
55 #+ ²¢ÇÒ²»³öÏÖÔÚ$ERRORFILEÖÐ.
56 # ÿÐйýºó, ÕâЩÖض¨ÏòÃüÁîÒ²»á×Ô¶¯"reset".
57 #====================================================================
==========
58
59
60
61 2>&1
62 # Öض¨Ïòstderrµ½stdout.
63 # µÃµ½µÄ´íÎóÏûÏ¢ÓëstdoutÒ»Ñù, ·¢Ë͵½Ò»¸öµØ·½.
64
65 i>&j
66 # Öض¨ÏòÎļþÃèÊö·ûi µ½ j.
67 # Ö¸ÏòiÎļþµÄËùÓÐÊä³ö¶¼·¢Ë͵½jÖÐÈ¥.
68
69 >&j
70 # ĬÈϵÄ, Öض¨ÏòÎļþÃèÊö·û1(stdout)µ½ j.
71 # ËùÓд«µÝµ½stdoutµÄÊä³ö¶¼Ë͵½jÖÐÈ¥.
72
73 0< FILENAME
74 < FILENAME
75 # ´ÓÎļþÖнÓÊÜÊäÈë.
76 # Óë">"ÊdzɶÔÃüÁî, ²¢ÇÒͨ³£¶¼ÊǽáºÏʹÓÃ.
77 #
78 # grep search-word <filename
79
80
81 [j]<>filename
82 # ΪÁ˶Áд"filename", °ÑÎļþ"filename"´ò¿ª, ²¢ÇÒ·ÖÅäÎļþÃèÊö·û"j"¸øËü.
83 # Èç¹ûÎļþ"filename"²»´æÔÚ, ÄÇô¾Í´´½¨Ëü.
84 # Èç¹ûÎļþÃèÊö·û"j"ûָ¶¨, ÄÇĬÈÏÊÇfd 0, stdin.
85 #
86 # ÕâÖÖÓ¦ÓÃͨ³£ÊÇΪÁËдµ½Ò»¸öÎļþÖÐÖ¸¶¨µÄµØ·½.
87 echo 1234567890 > File # д×Ö·û´®µ½"File".
88 exec 3<> File # ´ò¿ª"File"²¢ÇÒ¸øËü·ÖÅäfd 3.
89 read -n 4 <&3 # Ö»¶Á4¸ö×Ö·û.
90 echo -n . >&3 # дһ¸öСÊýµã.
91 exec 3>&- # ¹Ø±Õfd 3.
92 cat File # ==> 1234.67890
93 # Ëæ»ú´æ´¢.
94
95
96
97 |
98 # ¹ÜµÀ.
99 # ͨÓÃÄ¿µÄµÄ´¦ÀíºÍÃüÁîÁ´¹¤¾ß.
100 # Óë">"ºÜÏàËÆ, µ«ÊÇʵ¼ÊÉϸüͨÓÃ.
101 # ¶ÔÓÚÏ뽫ÃüÁî, ½Å±¾, ÎļþºÍ³ÌÐò´®Á¬ÆðÀ´µÄʱºòºÜÓÐÓÃ.
102 cat *.txt | sort | uniq > result-file
103 # ¶ÔËùÓеÄ.txtÎļþµÄÊä³ö½øÐÐÅÅÐò, ²¢ÇÒɾ³ýÖظ´ÐÐ,
104 # ×îºó½«½á¹û±£´æµ½"result-file"ÖÐ.

¿ÉÒÔ½«ÊäÈëÊä³öÖض¨ÏòºÍ(»ò)¹ÜµÀµÄ¶à¸öʵÀý½áºÏµ½Ò»ÆðдÔÚÒ»ÐÐÉÏ.
1 command < input-file > output-file
2
3 command1 | command2 | command3 > output-file
²Î¼û Example 12-28 ºÍ Example A-15.
¿ÉÒÔ½«¶à¸öÊä³öÁ÷Öض¨Ïòµ½Ò»¸öÎļþÉÏ.
1 ls -yz >> command.log 2>&1
2 # ½«´íÎóÑ¡Ïî"yz"µÄ½á¹û·Åµ½Îļþ"command.log"ÖÐ.
3 # ÒòΪstderr±»Öض¨Ïòµ½Õâ¸öÎļþÖÐ,
4 #+ ËùÓеĴíÎóÏûÏ¢Ò²¾Í¶¼Ö¸ÏòÄÇÀïÁË.
5
6 # ×¢Òâ, ϱßÕâ¸öÀý×ӾͲ»»á¸ø³öÏàͬµÄ½á¹û.
7 ls -yz 2>&1 >> command.log
8 # Êä³öÒ»¸ö´íÎóÏûÏ¢, µ«ÊDz¢²»Ð´µ½ÎļþÖÐ.
9
10 # Èç¹û½«stdoutºÍstderr¶¼Öض¨Ïò,
11 #+ ÃüÁîµÄ˳Ðò»áÓÐЩ²»Í¬.
¹Ø±ÕÎļþÃèÊö·û
n<&- ¹Ø±ÕÊäÈëÎļþÃèÊö·ûn.
0<&-, <&- ¹Ø±Õstdin.
n>&- ¹Ø±ÕÊä³öÎļþÃèÊö·ûn.
1>&-, >&- ¹Ø±Õstdout.
×Ó½ø³Ì¼Ì³ÐÁË´ò¿ªµÄÎļþÃèÊö·û. Õâ¾ÍÊÇΪʲô¹ÜµÀ¿ÉÒÔ¹¤×÷. Èç¹ûÏë×èÖ¹fd±»¼Ì³Ð, ÄÇô¿É
ÒԹصôËü.
1 # Ö»Öض¨Ïòstderrµ½Ò»¸ö¹ÜµÀ.
2
3 exec 3>&1 # ±£´æµ±Ç°stdoutµÄ"Öµ".
4 ls -l 2>&1 >&3 3>&- | grep bad 3>&- # ¶Ô'grep'¹Ø±Õfd 3(µ«²»¹Ø±Õ'ls').
5 # ^^^^ ^^^^
6 exec 3>&- # ÏÖÔÚ¶ÔÓÚÊ£ÓàµÄ½Å±¾¹Ø±ÕËü.
7
8 # Thanks, S.C.
Èç¹ûÏëÁ˽â¹ØÓÚI/OÖض¨Ïò¸ü¶àµÄϸ½Ú²Î¼û ¸½Â¼ E.
16.1. ʹÓÃexec
--------------
exec <filename ÃüÁî»á½«stdinÖض¨Ïòµ½ÎļþÖÐ. ´ÓÕâ¾ä¿ªÊ¼, ºó±ßµÄÊäÈë¾Í¶¼À´×ÔÓÚÕâ¸öÎÄ
¼þÁË, ¶ø²»ÊDZê×¼ÊäÈëÁË(ͨ³£¶¼ÊǼüÅÌÊäÈë). ÕâÑù¾ÍÌṩÁËÒ»ÖÖ°´ÐжÁÈ¡ÎļþµÄ·½·¨, ²¢ÇÒ
¿ÉÒÔʹÓÃsed ºÍ/»ò awkÀ´¶ÔÿһÐнøÐзÖÎö.
Example 16-1 ʹÓÃexecÖض¨Ïò±ê×¼ÊäÈë
################################Start Script####################################
###
1 #!/bin/bash
2 # ʹÓÃ'exec'Öض¨Ïò±ê×¼ÊäÈë.
3
4
5 exec 6<&0 # ½«ÎļþÃèÊö·û#6ÓëstdinÁ´½ÓÆðÀ´.
6 # ±£´æÁËstdin.
7
8 exec < data-file # stdin±»Îļþ"data-file"Ëù´úÌæ.
9
10 read a1 # ¶ÁÈ¡Îļþ"data-file"µÄµÚÒ»ÐÐ.
11 read a2 # ¶ÁÈ¡Îļþ"data-file"µÄµÚ¶þÐÐ.
12
13 echo
14 echo "Following lines read from file."
15 echo "-------------------------------"
16 echo $a1
17 echo $a2
18
19 echo; echo; echo
20
21 exec 0<&6 6<&-
22 # ÏÖÔÚ½«stdin´Ófd #6Öлָ´, ÒòΪ¸Õ²ÅÎÒÃÇ°ÑstdinÖض¨Ïòµ½#6ÁË,
23 #+ È»ºó¹Ø±Õfd #6 ( 6<&- ), ºÃÈÃÕâ¸öÃèÊö·û¼ÌÐø±»ÆäËû½ø³ÌËùʹÓÃ.
24 #
25 # <&6 6<&- Õâô×öÒ²¿ÉÒÔ.
26
27 echo -n "Enter data "
28 read b1 # ÏÖÔÚ"read"ÒѾ­»Ö¸´Õý³£ÁË, ¾ÍÊÇ´ÓstdinÖжÁÈ¡.
29 echo "Input read from stdin."
30 echo "----------------------"
31 echo "b1 = $b1"
32
33 echo
34
35 exit 0
################################End Script######################################
###
ͬÑùµÄ, exec >filename ÃüÁ»á°ÑstdoutÖض¨Ïòµ½Ò»¸öÖ¸¶¨µÄÎļþÖÐ. ÕâÑùËùÓеÄÃüÁîÊä
³ö¾Í¶¼»á·¢ÏòÄǸöÖ¸¶¨µÄÎļþ, ¶ø²»ÊÇstdout.
Example 16-2 ʹÓÃexecÀ´Öض¨Ïòstdout
################################Start Script####################################
###
1 #!/bin/bash
2 # reassign-stdout.sh
3
4 LOGFILE=logfile.txt
5
6 exec 6>&1 # ½«fd #6ÓëstdoutÏàÁ¬½Ó.
7 # ±£´æstdout.
8
9 exec > $LOGFILE # stdout¾Í±»Îļþ"logfile.txt"Ëù´úÌæÁË.
10
11 # ----------------------------------------------------------- #
12 # ÔÚÕâ¿éÖÐËùÓÐÃüÁîµÄÊä³ö¾Í¶¼·¢ÏòÎļþ $LOGFILE.
13
14 echo -n "Logfile: "
15 date
16 echo "-------------------------------------"
17 echo
18
19 echo "Output of \"ls -al\" command"
20 echo
21 ls -al
22 echo; echo
23 echo "Output of \"df\" command"
24 echo
25 df
26
27 # ----------------------------------------------------------- #
28
29 exec 1>&6 6>&- # »Ö¸´stdout, È»ºó¹Ø±ÕÎļþÃèÊö·û#6.
30
31 echo
32 echo "== stdout now restored to default == "
33 echo
34 ls -al
35 echo
36
37 exit 0
################################End Script######################################
###
Example 16-3 ʹÓÃexecÔÚͬһ½Å±¾ÖÐÖض¨ÏòstdinºÍstdout
################################Start Script####################################
###
1 #!/bin/bash
2 # upperconv.sh
3 # ½«Ò»¸öÖ¸¶¨µÄÊäÈëÎļþת»»Îª´óд.
4
5 E_FILE_ACCESS=70
6 E_WRONG_ARGS=71
7
8 if [ ! -r "$1" ] # ÅжÏÖ¸¶¨µÄÊäÈëÎļþÊÇ·ñ¿É¶Á?
9 then
10 echo "Can't read from input file!"
11 echo "Usage: $0 input-file output-file"
12 exit $E_FILE_ACCESS
13 fi # ¼´Ê¹ÊäÈëÎļþ($1)û±»Ö¸¶¨
14 #+ Ò²»¹ÊÇ»áÒÔÏàͬµÄ´íÎóÍ˳ö(Ϊʲô?).
15
16 if [ -z "$2" ]
17 then
18 echo "Need to specify output file."
19 echo "Usage: $0 input-file output-file"
20 exit $E_WRONG_ARGS
21 fi
22
23
24 exec 4<&0
25 exec < $1 # ½«»á´ÓÊäÈëÎļþÖжÁÈ¡.
26
27 exec 7>&1
28 exec > $2 # ½«Ð´µ½Êä³öÎļþÖÐ.
29 # ¼ÙÉèÊä³öÎļþÊÇ¿ÉдµÄ(Ìí¼Ó¼ì²é?).
30
31 # -----------------------------------------------
32 cat - | tr a-z A-Z # ת»»Îª´óд.
33 # ^^^^^ # ´ÓstdinÖжÁÈ¡.Reads from stdin.
34 # ^^^^^^^^^^ # дµ½stdoutÉÏ.
35 # È»¶ø, stdinºÍstdout¶¼±»Öض¨ÏòÁË.
36 # -----------------------------------------------
37
38 exec 1>&7 7>&- # »Ö¸´ stout.
39 exec 0<&4 4<&- # »Ö¸´ stdin.
40
41 # »Ö¸´Ö®ºó, ϱßÕâÐдúÂ뽫»áÈçÆÚÍûµÄÒ»Ñù´òÓ¡µ½stdoutÉÏ.
42 echo "File \"$1\" written to \"$2\" as uppercase conversion."
43
44 exit 0
################################End Script######################################
###
I/OÖض¨ÏòÊÇÒ»ÖÖ±ÜÃâ¿ÉŵÄ×ÓshellÖв»¿É´æÈ¡±äÁ¿ÎÊÌâµÄ·½·¨.
Example 16-4 ±ÜÃâ×Óshell
################################Start Script####################################
###
1 #!/bin/bash
2 # avoid-subshell.sh
3 # Matthew WalkerÌá³öµÄ½¨Òé.
4
5 Lines=0
6
7 echo
8
9 cat myfile.txt | while read line; # (ÒëÕß×¢: ¹ÜµÀ»á²úÉú×Óshell)
10 do {
11 echo $line
12 (( Lines++ )); # Ôö¼ÓÕâ¸ö±äÁ¿µÄÖµ
13 #+ µ«ÊÇÍⲿѭ»·È´²»ÄÜ´æÈ¡.
14 # ×ÓshellÎÊÌâ.
15 }
16 done
17
18 echo "Number of lines read = $Lines" # 0
19 # ´íÎó!
20
21 echo "------------------------"
22
23
24 exec 3<> myfile.txt
25 while read line <&3
26 do {
27 echo "$line"
28 (( Lines++ )); # Ôö¼ÓÕâ¸ö±äÁ¿µÄÖµ
29 #+ ÏÖÔÚÍⲿѭ»·¾Í¿ÉÒÔ´æÈ¡ÁË.
30 # ûÓÐ×Óshell, ÏÖÔÚ¾ÍûÎÊÌâÁË.
31 }
32 done
33 exec 3>&-
34
35 echo "Number of lines read = $Lines" # 8
36
37 echo
38
39 exit 0
40
41 # ϱßÕâЩÐÐÊǽű¾µÄ½á¹û, ½Å±¾ÊDz»»á×ßµ½ÕâÀïµÄ.
42
43 $ cat myfile.txt
44
45 Line 1.
46 Line 2.
47 Line 3.
48 Line 4.
49 Line 5.
50 Line 6.
51 Line 7.
52 Line 8.
################################End Script######################################
###
×¢ÒâÊÂÏî:
[1] Ò»¸öÎļþÃèÊö·û˵°×Á˾ÍÊÇÎļþϵͳΪÁ˸ú×ÙÕâ¸ö´ò¿ªµÄÎļþ¶ø·ÖÅä¸øËüµÄÒ»¸öÊý×Ö.
Ò²¿ÉÒԵĽ«ÆäÀí½âΪÎļþÖ¸ÕëµÄÒ»¸ö¼òµ¥°æ±¾. ÓëCÖеÄÎļþ¾ä±úµÄ¸ÅÄîÏàËÆ.
[2] ʹÓÃÎļþÃèÊö·û5¿ÉÄÜ»áÒýÆðÎÊÌâ. µ±BashʹÓÃexec´´½¨Ò»¸ö×Ó½ø³ÌµÄʱºò, ×Ó½ø³Ì
»á¼Ì³Ðfd5(²Î¼ûChet RameyµÄ¹éµµe-mail, SUBJECT: RE: File descriptor 5 is
held open). ×îºÃ»¹ÊDz»ÒªÈ¥ÕÐÈÇÕâ¸öÌض¨µÄfd.

µÚ17ÕÂ Here Documents


======================
here document ¾ÍÊÇÒ»¶ÎÌØÊâÄ¿µÄµÄ´úÂë¿é. ËûʹÓÃI/O Öض¨ÏòµÄÐÎʽÀ´½«Ò»¸öÃüÁîÐòÁд«µÝ
µ½Ò»¸ö½»»¥³ÌÐò»òÕßÃüÁîÖÐ, ±ÈÈçftp, cat, »òÕßexÎı¾±à¼­Æ÷.
1 COMMAND <<InputComesFromHERE
2 ...
3 InputComesFromHERE
limit string ÓÃÀ´»®¶¨ÃüÁîÐòÁеķ¶Î§(ÒëÕß×¢: Á½¸öÏàͬµÄlimit stringÖ®¼ä¾ÍÊÇÃüÁîÐòÁÐ)
. ÌØÊâ·ûºÅ << ÓÃÀ´±íʶlimit string. Õâ¸ö·ûºÅ¾ßÓÐÖض¨ÏòÎļþµÄÊä³öµ½³ÌÐò»òÃüÁîµÄÊäÈë
µÄ×÷ÓÃ. Óë interactive-program < command-file ºÜÏàÏó, command-file°üº¬:
1 command #1
2 command #2
3 ...
¶øhere document µÄÐÎʽ¿´ÉÏÈ¥ÊÇÈçϵÄÑù×Ó:
1 #!/bin/bash
2 interactive-program <<LimitString
3 command #1
4 command #2
5 ...
6 LimitString
Ñ¡ÔñÒ»¸öÃû×ַdz£¹îÒìµÄlimit string½«»á±ÜÃâÃüÁîÁбíºÍlimit stringÖØÃûµÄÎÊÌâ.
×¢Òâ,ijЩʱºòhere document ÓÃÔڷǽ»»¥¹¤¾ßºÍÃüÁîÉϵÄʱºòÒ²»áÓкõÄЧ¹û, ±ÈÈç, wall.
Example 17-1 ¹ã²¥: ·¢ËÍÏûÏ¢¸øÿ¸öµÇ¼ÉϵÄÓû§
################################Start Script####################################
###
1 #!/bin/bash
2
3 wall <<zzz23EndOfMessagezzz23
4 E-mail your noontime orders for pizza to the system administrator.
5 (Add an extra dollar for anchovy or mushroom topping.)
6 # ¶îÍâµÄÏûÏ¢Îı¾Ð´ÔÚÕâÀï.
7 # ×¢Òâ: 'wall' »á´òÓ¡×¢ÊÍÐÐ.
8 zzz23EndOfMessagezzz23
9
10 # ¿ÉÒÔʹÓøüÓÐЧÂʵÄ×ö·¨
11 # wall <message-file
12 # È»¶ø½«ÏûϢģ°æǶÈëµ½½Å±¾ÖÐ
13 #+ ÊÇÒ»ÖÖ"С³Ôµê"(¿ìËÙµ«ÊDZȽÏÔà)µÄÖ»ÄÜʹÓÃÒ»´ÎµÄ½â¾ö°ì·¨.
14
15 exit 0
################################End Script######################################
###
¼´Ê¹ÊÇijЩ²»´ó¿ÉÄܵŤ¾ß, ÈçviÒ²¿ÉÒÔʹÓÃhere document.
Example 17-2 ·ÂÔìÎļþ: ´´½¨Ò»¸öÁ½ÐеķÂÔìÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2
3 # Ó÷ǽ»»¥µÄ·½Ê½À´Ê¹ÓÃ'vi'±à¼­Ò»¸öÎļþ.
4 # Ä£·Â'sed'.
5
6 E_BADARGS=65
7
8 if [ -z "$1" ]
9 then
10 echo "Usage: `basename $0` filename"
11 exit $E_BADARGS
12 fi
13
14 TARGETFILE=$1
15
16 # ÔÚÎļþÖвåÈëÁ½ÐÐ, È»ºó±£´æ.
17 #--------Begin here document-----------#
18 vi $TARGETFILE <<x23LimitStringx23
19 i
20 This is line 1 of the example file.
21 This is line 2 of the example file.
22 ^[
23 ZZ
24 x23LimitStringx23
25 #----------End here document-----------#
26
27 # ×¢ÒâÉϱß^[ÊÇÒ»¸öתÒå·û,¼üÈëCtrl+v <Esc>¾ÍÐÐ,
28 #+ ÊÂʵÉÏËüÊÇ<Esc>¼ü.
29
30 # Bram MoolenaarÖ¸³öÕâÖÖ·½·¨²»ÄÜÕý³£µØÓÃÔÚ'vim'ÉÏ, (ÒëÕß×¢: Bram MoolenaarÊÇvim×÷Õß)
31 #+ ÒòΪ¿ÉÄÜ»áÓÐÖն˵ÄÏ໥ӰÏìÎÊÌâ.
32
33 exit 0
################################End Script######################################
###
ÉϱߵĽű¾Ò²¿ÉÒÔ²»ÓÃvi¶øÓÃexÀ´ÊµÏÖ. Here document °üº¬exÃüÁîÁбíµÄ×ö·¨×ã¹»ÐγÉ×Ô¼º
µÄÀà±ðÁË, ½Ðex scripts.
1 #!/bin/bash
2 # °ÑËùÓкó׺Ϊ".txt"Îļþ
3 #+ ÖеÄ"Smith"¶¼Ìæ»»³É"Jones".
4
5 ORIGINAL=Smith
6 REPLACEMENT=Jones
7
8 for word in $(fgrep -l $ORIGINAL *.txt)
9 do
10 # -------------------------------------
11 ex $word <<EOF
12 :%s/$ORIGINAL/$REPLACEMENT/g
13 :wq
14 EOF
15 # :%s ÊÇ"ex"µÄÌæ»»ÃüÁî.
16 # :wq ÊDZ£´æ²¢Í˳öµÄÒâ˼.
17 # -------------------------------------
18 done
Óë"ex scripts"ÏàËƵÄÊÇcat scripts.
Example 17-3 ʹÓÃcatµÄ¶àÐÐÏûÏ¢
################################Start Script####################################
###
1 #!/bin/bash
2
3 # 'echo' ¶ÔÓÚ´òÓ¡µ¥ÐÐÏûÏ¢ÊǷdz£ºÃµÄ,
4 #+ µ«ÊÇÔÚ´òÓ¡ÏûÏ¢¿éʱ¿ÉÄܾÍÓеãÎÊÌâÁË.
5 # 'cat' here document¿ÉÒÔ½â¾öÕâ¸öÏÞÖÆ.
6
7 cat <<End-of-message
8 -------------------------------------
9 This is line 1 of the message.
10 This is line 2 of the message.
11 This is line 3 of the message.
12 This is line 4 of the message.
13 This is the last line of the message.
14 -------------------------------------
15 End-of-message
16
17 # ÓÃϱßÕâÐдúÌæÉϱߵĵÚ7ÐÐ
18 #+ cat > $Newfile <<End-of-message
19 #+ ^^^^^^^^^^
20 #+ ÄÇô¾Í»á°ÑÊä³öдµ½Îļþ$NewfileÖÐ, ¶ø²»ÊÇstdout.
21
22 exit 0
23
24
25 #--------------------------------------------
26 # ϱߵĴúÂë²»»áÔËÐÐ, ÒòΪÉϱߵÄ"exit 0".
27
28 # S.C. Ö¸³öϱߴúÂëÒ²¿ÉÒÔÔËÐÐ.
29 echo "-------------------------------------
30 This is line 1 of the message.
31 This is line 2 of the message.
32 This is line 3 of the message.
33 This is line 4 of the message.
34 This is the last line of the message.
35 -------------------------------------"
36 # È»¶ø, Îı¾¿ÉÄܲ»°üº¬Ë«ÒýºÅ, ³ý·ÇËüÃDZ»×ªÒå.
################################End Script######################################
###
- Ñ¡ÏîÓÃÀ´±ê¼Çhere documentµÄlimit string (<<-LimitString), ¿ÉÒÔÒÖÖÆÊä³öʱǰ±ßµÄtab
(²»ÊÇ¿Õ¸ñ). Õâ¿ÉÒÔÔö¼ÓÒ»¸ö½Å±¾µÄ¿É¶ÁÐÔ.
Example 17-4 ´øÓÐÒÖÖÆtab¹¦ÄܵĶàÐÐÏûÏ¢
################################Start Script####################################
###
1 #!/bin/bash
2 # Óë֮ǰµÄÀý×ÓÏàͬ, µ«ÊÇ...
3
4 # - Ñ¡Ïî¶ÔÓÚhere docutmentÀ´Ëµ,<<-
5 #+ ¿ÉÒÔÒÖÖÆÎĵµÌåÇ°±ßµÄtab,
6 #+ ¶ø*²»*ÊÇ¿Õ¸ñ *not* spaces.
7
8 cat <<-ENDOFMESSAGE
9 This is line 1 of the message.
10 This is line 2 of the message.
11 This is line 3 of the message.
12 This is line 4 of the message.
13 This is the last line of the message.
14 ENDOFMESSAGE
15 # ½Å±¾ÔÚÊä³öµÄʱºò×ó±ß½«±»Ë¢µô.
16 # ¾ÍÊÇ˵ÿÐÐÇ°±ßµÄtab½«²»»áÏÔʾ.
17
18 # Éϱß5ÐÐ"ÏûÏ¢"µÄÇ°±ß¶¼ÊÇtab, ²»ÊÇ¿Õ¸ñ.
19 # ¿Õ¸ñÊDz»ÊÜ<<-Ó°ÏìµÄ.
20
21 # ×¢Òâ, Õâ¸öÑ¡Ïî¶ÔÓÚ*ǶÔÚ*ÖмäµÄtabû×÷ÓÃ.
22
23 exit 0
################################End Script######################################
###
here document Ö§³Ö²ÎÊýºÍÃüÁîÌæ»». ËùÒÔÒ²¿ÉÒÔ¸øhere documentµÄÏûÏ¢Ìå´«µÝ²»Í¬µÄ²ÎÊý,
ÕâÑùÏàÓ¦µÄÒ²»áÐÞ¸ÄÊä³ö.
Example 17-5 ʹÓòÎÊýÌæ»»µÄhere document
################################Start Script####################################
###
1 #!/bin/bash
2 # Ò»¸öʹÓÃ'cat'ÃüÁîµÄhere document, ʹÓÃÁ˲ÎÊýÌæ»»
3
4 # ²»´«ÃüÁîÐвÎÊý¸øËü, ./scriptname
5 # ´«Ò»¸öÃüÁîÐвÎÊý¸øËü, ./scriptname Mortimer
6 # ´«Ò»¸ö2¸öµ¥´Ê(ÓÃÒýºÅÀ¨ÆðÀ´)µÄÃüÁîÐвÎÊý¸øËü,
7 # ./scriptname "Mortimer Jones"
8
9 CMDLINEPARAM=1 # ËùÆÚÍûµÄ×îÉÙµÄÃüÁîÐвÎÊýµÄ¸öÊý.
10
11 if [ $# -ge $CMDLINEPARAM ]
12 then
13 NAME=$1 # Èç¹ûÃüÁîÐвÎÊý³¬¹ý1¸ö,
14 #+ ÄÇô¾ÍֻȡµÚÒ»¸ö²ÎÊý.
15 else
16 NAME="John Doe" # ĬÈÏÇé¿öÏÂ, Èç¹ûûÓÐÃüÁîÐвÎÊýµÄ»°.
17 fi
18
19 RESPONDENT="the author of this fine script"
20
21
22 cat <<Endofmessage
23
24 Hello, there, $NAME.
25 Greetings to you, $NAME, from $RESPONDENT.
26
27 # This comment shows up in the output (why?).
28
29 Endofmessage
30
31 # ×¢ÒâÉϱߵĿÕÐÐÒ²´òÓ¡µ½Êä³ö,
32 # ¶øÉϱßÄÇÐÐ"×¢ÊÍ"µ±È»Ò²»á´òÓ¡µ½Êä³ö.
33 # (ÒëÕß×¢: Õâ¾ÍÊÇΪʲô²»·­ÒëÄÇÐÐ×¢Ê͵ÄÔ­Òò, ¾¡Á¿±£³ÖÔ­´úÂëµÄÔ­Ñù)
34 exit 0
################################End Script######################################
###
ÕâÊÇÒ»¸ö°üº¬²ÎÊýÌæ»»µÄhere documentµÄÓÐÓõĽű¾.
Example 17-6 ÉÏ´«Ò»¸öÎļþ¶Ôµ½"Sunsite"µÄincomingĿ¼
################################Start Script####################################
###
1 #!/bin/bash
2 # upload.sh
3
4 # ÉÏ´«Îļþ¶Ô(Filename.lsm, Filename.tar.gz)
5 #+ µ½Sunsite/UNC (ibiblio.org)µÄincomingĿ¼.
6 # Filename.tar.gzÊÇ×ÔÉíµÄtar°ü.
7 # Filename.lsmÊÇÃèÊöÎļþ.
8 # SunsiteÐèÒª"lsm"Îļþ, ·ñÔò¾Í¾Ü¾ø¹±Ï×.
9
10
11 E_ARGERROR=65
12
13 if [ -z "$1" ]
14 then
15 echo "Usage: `basename $0` Filename-to-upload"
16 exit $E_ARGERROR
17 fi
18
19
20 Filename=`basename $1` # ´ÓÎļþÃûÖÐÈ¥µôĿ¼×Ö·û´®.
21
22 Server="ibiblio.org"
23 Directory="/incoming/Linux"
24 # ÔÚÕâÀïÒ²²»Ò»¶¨·ÇµÃ½«ÉϱߵIJÎÊýдËÀÔÚÕâ¸ö½Å±¾ÖÐ,
25 #+ ¿ÉÒÔʹÓÃÃüÁîÐвÎÊýµÄ·½·¨À´Ìæ»».
26
27 Password="your.e-mail.address" # ¿ÉÒÔÐ޸ijÉÏàÆ¥ÅäµÄÃÜÂë.
28
29 ftp -n $Server <<End-Of-Session
30 # -n Ñ¡Ïî½ûÓÃ×Ô¶¯µÇ¼.
31
32 user anonymous "$Password"
33 binary
34 bell # ÔÚÿ¸öÎļþ´«Êäºó, ÏìÁå.
35 cd $Directory
36 put "$Filename.lsm"
37 put "$Filename.tar.gz"
38 bye
39 End-Of-Session
40
41 exit 0
################################End Script######################################
###
ÔÚhere documentµÄ¿ªÍ·ÒýÓûòתÒå"limit string"»áʹµÃhere documentµÄÏûÏ¢ÌåÖеIJÎÊýÌæ
»»±»½ûÓÃ.
Example 17-7 ¹Ø±Õ²ÎÊýÌæ»»
################################Start Script####################################
###
1 #!/bin/bash
2 # Ò»¸öʹÓÃ'cat'µÄhere document, µ«ÊǽûÓÃÁ˲ÎÊýÌæ»».
3
4 NAME="John Doe"
5 RESPONDENT="the author of this fine script"
6
7 cat <<'Endofmessage'
8
9 Hello, there, $NAME.
10 Greetings to you, $NAME, from $RESPONDENT.
11
12 Endofmessage
13
14 # µ±"limit string"±»ÒýÓûòתÒåÄÇô¾Í½ûÓÃÁ˲ÎÊýÌæ»».
15 # ϱߵÄÁ½ÖÖ·½Ê½¾ßÓÐÏàͬµÄЧ¹û.
16 # cat <<"Endofmessage"
17 # cat <<\Endofmessage
18
19 exit 0
################################End Script######################################
###
½ûÓÃÁ˲ÎÊýÌæ»»ºó, ½«ÔÊÐíÊä³öÎı¾±¾Éí(ÒëÕß×¢: ¾ÍÊÇδתÒåµÄÔ­ÎÄ). ²úÉú½Å±¾ÉõÖÁÊdzÌÐò
´úÂë¾ÍÊÇÕâÖÖÓ÷¨µÄÓÃ;֮һ.
Example 17-8 Ò»¸ö²úÉúÁíÍâÒ»¸ö½Å±¾µÄ½Å±¾
################################Start Script####################################
###
1 #!/bin/bash
2 # generate-script.sh
3 # »ùÓÚAlbert ReinerµÄÒ»¸öÖ÷Òâ.
4
5 OUTFILE=generated.sh # Ëù²úÉúÎļþµÄÃû×Ö.
6
7
8 # -----------------------------------------------------------
9 # 'Here document°üº¬ÁËÐèÒª²úÉúµÄ½Å±¾µÄ´úÂë.
10 (
11 cat <<'EOF'
12 #!/bin/bash
13
14 echo "This is a generated shell script."
15 # Note that since we are inside a subshell,
16 #+ we can't access variables in the "outside" script.
17
18 echo "Generated file will be named: $OUTFILE"
19 # Above line will not work as normally expected
20 #+ because parameter expansion has been disabled.
21 # Instead, the result is literal output.
22
23 a=7
24 b=3
25
26 let "c = $a * $b"
27 echo "c = $c"
28
29 exit 0
30 EOF
31 ) > $OUTFILE
32 # -----------------------------------------------------------
33
34 # ½«'limit string'ÒýÓÃÆðÀ´½«»á×èÖ¹Éϱß
35 #+ here documentµÄÏûÏ¢ÌåÖеıäÁ¿À©Õ¹.
36 # Õâ»áʹµÃÊä³öÎļþÖеÄÄÚÈݱ£³Öhere documentÏûÏ¢ÌåÖеÄÔ­ÎÄ.
37
38 if [ -f "$OUTFILE" ]
39 then
40 chmod 755 $OUTFILE
41 # ÈÃËù²úÉúµÄÎļþ¾ßÓпÉÖ´ÐÐȨÏÞ.
42 else
43 echo "Problem in creating file: \"$OUTFILE\""
44 fi
45
46 # Õâ¸ö·½·¨Ò²ÓÃÀ´²úÉú
47 #+ C³ÌÐò´úÂë, Perl³ÌÐò´úÂë, Python³ÌÐò´úÂë, makefile,
48 #+ ºÍÆäËûµÄһЩÀàËƵĴúÂë.
49 # (ÒëÕß×¢: ÖмäÒ»¶ÎûÒëµÄ×¢Êͽ«»á±»here document´òÓ¡³öÀ´)
50 exit 0
################################End Script######################################
###
Ò²¿ÉÒÔ½«here documentµÄÊä³ö±£´æµ½±äÁ¿ÖÐ.
1 variable=$(cat <<SETVAR
2 This variable
3 runs over multiple lines.
4 SETVAR)
5
6 echo "$variable"
ͬһ½Å±¾Öеĺ¯ÊýÒ²¿ÉÒÔ½ÓÊÜhere documentµÄÊä³ö×÷Ϊ×ÔÉíµÄ²ÎÊý.
Example 17-9 Here documentsÓ뺯Êý
################################Start Script####################################
###
1 #!/bin/bash
2 # here-function.sh
3
4 GetPersonalData ()
5 {
6 read firstname
7 read lastname
8 read address
9 read city
10 read state
11 read zipcode
12 } # Õâ¸öº¯ÊýÎÞÒɵĿ´ÆðÀ´¾ÍÒ»¸ö½»»¥º¯Êý, µ«ÊÇ...
13
14
15 # ¸øÉϱߵĺ¯ÊýÌṩÊäÈë.
16 GetPersonalData <<RECORD001
17 Bozo
18 Bozeman
19 2726 Nondescript Dr.
20 Baltimore
21 MD
22 21226
23 RECORD001
24
25
26 echo
27 echo "$firstname $lastname"
28 echo "$address"
29 echo "$city, $state $zipcode"
30 echo
31
32 exit 0
################################End Script######################################
###
Ò²¿ÉÒÔÕâôʹÓÃ: ×öÒ»¸ö¼ÙÃüÁîÀ´´ÓÒ»¸öhere documentÖнÓÊÕÊä³ö. Õâô×öÊÂʵÉϾÍÊÇ´´½¨ÁË
Ò»¸ö"ÄäÃû"µÄhere document.
Example 17-10 "ÄäÃû" here Document
################################Start Script####################################
###
1 #!/bin/bash
2
3 : <<TESTVARIABLES
4 ${HOSTNAME?}${USER?}${MAIL?} # Èç¹ûÆäÖÐÒ»¸ö±äÁ¿Ã»±»ÉèÖÃ, ÄÇô¾Í´òÓ¡´íÎóÐÅÏ¢.
5 TESTVARIABLES
6
7 exit 0
################################End Script######################################
###
×¢Òâ: ÉϱßËùʾ¼¼ÊõµÄÒ»Öֱ仯¿ÉÒÔÓÃÀ´"×¢ÊÍ"µô´úÂë¿é.
Example 17-11 ×¢Ê͵ôÒ»¶Î´úÂë¿é
################################Start Script####################################
###
1 #!/bin/bash
2 # commentblock.sh
3
4 : <<COMMENTBLOCK
5 echo "This line will not echo."
6 This is a comment line missing the "#" prefix.
7 This is another comment line missing the "#" prefix.
8
9 &*@!!++=
10 The above line will cause no error message,
11 because the Bash interpreter will ignore it.
12 COMMENTBLOCK
13
14 echo "Exit value of above \"COMMENTBLOCK\" is $?." # 0
15 # ÕâÀォ²»»áÏÔʾÈκδíÎó.
16
17
18 # ÉϱߵÄÕâÖÖ¼¼Êõµ±È»Ò²¿ÉÒÔÓÃÀ´×¢Ê͵ô
19 #+ Ò»¶ÎÕýÔÚʹÓõĴúÂë, Èç¹ûÄãÓÐijЩÌض¨µ÷ÊÔÒªÇóµÄ»°.
20 # Õ⽫±È¶ÔÿÐж¼ÇÃÈë"#"À´µÃ·½±ãµÄ¶à,
21 #+ ¶øÇÒÈç¹ûÄãÏë»Ö¸´µÄ»°, »¹µÃ½«Ìí¼ÓÉϵÄ"#"ɾ³ýµô.
22
23 : <<DEBUGXXX
24 for file in *
25 do
26 cat "$file"
27 done
28 DEBUGXXX
29
30 exit 0
################################End Script######################################
###
×¢Òâ: ¹ØÓÚÕâÖÖС¼¼ÇɵÄÁíÒ»¸öÓ¦ÓþÍÊÇÄܹ»²úÉú×ÔÎĵµ»¯(self-documenting)µÄ½Å±¾.

Example 17-12 Ò»¸ö×ÔÎĵµ»¯(self-documenting)µÄ½Å±¾


################################Start Script####################################
###
1 #!/bin/bash
2 # self-document.sh: ×ÔÎĵµ»¯(self-documenting)µÄ½Å±¾
3 # Modification of "colm.sh".
4
5 DOC_REQUEST=70
6
7 if [ "$1" = "-h" -o "$1" = "--help" ] # ÇëÇó°ïÖú.
8 then
9 echo; echo "Usage: $0 [directory-name]"; echo
10 sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |
11 sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi
12
13
14 : <<DOCUMENTATIONXX
15 List the statistics of a specified directory in tabular format.
16 ---------------------------------------------------------------
17 The command line parameter gives the directory to be listed.
18 If no directory specified or directory specified cannot be read,
19 then list the current working directory.
20
21 DOCUMENTATIONXX
22
23 if [ -z "$1" -o ! -r "$1" ]
24 then
25 directory=.
26 else
27 directory="$1"
28 fi
29
30 echo "Listing of "$directory":"; echo
31 (printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
32 ; ls -l "$directory" | sed 1d) | column -t
33
34 exit 0
################################End Script######################################
###
ʹÓÃcat ½Å±¾ Ò²Äܹ»Íê³ÉÏàͬµÄÄ¿µÄ.
1 DOC_REQUEST=70
2
3 if [ "$1" = "-h" -o "$1" = "--help" ] # ÇëÇó°ïÖú.
4 then # ʹÓÃ"cat ½Å±¾" . . .
5 cat <<DOCUMENTATIONXX
6 List the statistics of a specified directory in tabular format.
7 ---------------------------------------------------------------
8 The command line parameter gives the directory to be listed.
9 If no directory specified or directory specified cannot be read,
10 then list the current working directory.
11
12 DOCUMENTATIONXX
13 exit $DOC_REQUEST
14 fi
²Î¼û Example A-27 ¿ÉÒÔÁ˽â¸ü¶à¹ØÓÚ×ÔÎĵµ»¯½Å±¾µÄºÃÀý×Ó.
×¢Òâ: Here document´´½¨ÁÙʱÎļþ, µ«ÊÇÕâЩÎļþ½«ÔÚ´ò¿ªºó±»É¾³ý, ²¢ÇÒ²»Äܹ»±»ÈκÎÆä
Ëû½ø³ÌËù´æÈ¡.
bash$ bash -c 'lsof -a -p $$ -d0' << EOF
> EOF
lsof 1213 bozo 0r REG 3,5 0 30386 /tmp/t1213-0-sh (delete
d)
×¢Òâ: ijЩ¹¤¾ßÊDz»Äܹ¤×÷ÔÚhere documentÖеÄ.
¾¯¸æ: ½áÊøµÄlimit string, ¾ÍÊÇhere document×îºóÒ»ÐеÄlimit string, ±ØÐ뿪ʼÓÚµÚÒ»
¸ö×Ö·ûλÖÃ. ËüµÄÇ°Ãæ²»Äܹ»ÓÐÈκÎÇ°ÖõĿհ×. ¶øÔÚÕâ¸ölimit stringºó±ßµÄ¿Õ°×Ò²»á
ÒýÆðÒì³£ÎÊÌâ. ¿Õ°×½«»á×èÖ¹limit stringµÄʶ±ð.(ÒëÕß×¢: ϱßÕâ¸ö½Å±¾ÓÉÓÚ½áÊø
limit stringµÄÎÊÌâ, Ôì³É½Å±¾ÎÞ·¨½áÊø, ËùÓÐÄÚÈÝÈ«²¿±»´òÓ¡³öÀ´, ËùÒÔ×¢Ê;Ͳ»ÒëÁË,
±£³ÖÀý×ӽű¾µÄÔ­Ñù.)
1 #!/bin/bash
2
3 echo "---------------------------------------------------------------------
-"
4
5 cat <<LimitString
6 echo "This is line 1 of the message inside the here document."
7 echo "This is line 2 of the message inside the here document."
8 echo "This is the final line of the message inside the here document."
9 LimitString
10 #^^^^Indented limit string. Error! This script will not behave as expected.
11
12 echo "---------------------------------------------------------------------
-"
13
14 # These comments are outside the 'here document',
15 #+ and should not echo.
16
17 echo "Outside the here document."
18
19 exit 0
20
21 echo "This line had better not echo." # Follows an 'exit' command.
¶ÔÓÚÄÇЩʹÓÃ"here document"µÃ·Ç³£¸´ÔÓµÄÈÎÎñ, ×îºÃ¿¼ÂÇʹÓÃexpect½Å±¾ÓïÑÔ, ÕâÖÖÓïÑÔ
¾ÍÊÇΪÁË´ïµ½Ïò½»»¥³ÌÐòÌí¼ÓÊäÈëµÄÄ¿µÄ¶øÁ¿Éí¶¨×öµÄ.

17.1. Here Strings


------------------
here string ¿ÉÒÔ±»ÈÏΪÊÇhere documentµÄÒ»ÖÖ¶¨ÖÆÐÎʽ. ³ýÁËCOMMAND <<<$WORD ¾Íʲô¶¼
ûÓÐÁË, $WORD½«±»À©Õ¹²¢ÇÒ±»ËÍÈëCOMMANDµÄstdinÖÐ.
Example 17-13 ÔÚÒ»¸öÎļþµÄ¿ªÍ·Ìí¼ÓÎı¾
################################Start Script####################################
###
1 #!/bin/bash
2 # prepend.sh: ÔÚÎļþµÄ¿ªÍ·Ìí¼ÓÎı¾.
3 #
4 # Kenny StaufferËù¾èÖúµÄ½Å±¾Àý×Ó,
5 #+ ±»±¾ÎÄ×÷Õß×÷ÁËÉÙÁ¿µÄÐÞ¸Ä.
6
7
8 E_NOSUCHFILE=65
9
10 read -p "File: " file # 'read'ÃüÁîµÄ -p ²ÎÊýÏÔʾÌáʾ·û.
11 if [ ! -e "$file" ]
12 then # Èç¹ûûÓÐÕâ¸öÎļþÄǾͽøÀ´.
13 echo "File $file not found."
14 exit $E_NOSUCHFILE
15 fi
16
17 read -p "Title: " title
18 cat - $file <<<$title > $file.new
19
20 echo "Modified file is $file.new"
21
22 exit 0
23
24 # ϱßÊÇ'man bash'ÖеÄÒ»¶Î:
25 # Here Strings
26 # here documentµÄÒ»ÖÖ±äÐÎ,ÐÎʽÈçÏÂ:
27 #
28 # <<<word
29 #
30 # word±»À©Õ¹²¢ÇÒÌṩµ½commandµÄ±ê×¼ÊäÈëÖÐ.
################################End Script######################################
###
Á·Ï°: ÕÒ³öhere stringµÄÆäËûÓ÷¨.

µÚ18Õ ÐÝϢʱ¼ä
================
Õâ¸öÉñÆæµÄÔÝÍ£¿ÉÒÔ¸ø¶ÁÕßÒ»¸öÐÝÏ¢µÄ»ú»á, ¿ÉÄܶÁÕßµ½ÁËÕâÀïÒ²»á»áÐÄһЦ°É.
Linuxͬ־ÃÇ, ÏòÄãÃÇÖ¾´! ÄãÕýÔÚÔĶÁµÄÕâЩ¶«Î÷, ½«»á¸øÄãÃÇ´øÀ´ºÃÔË. °ÑÕâ·ÝÎĵµ·¢¸øÄã
µÄ10¸öÅóÓÑ. ÔÚ¿½±´Õâ·ÝÎĵµÖ®Ç°, ÔÚÐŵĽáβдÉÏÒ»¸ö100ÐеÄBash½Å±¾·¢Ë͸øÁбíÉϵĵÚÒ»
¸öÈË. È»ºóÔÚÐŵĵײ¿É¾³ýËüÃǵÄÃû×Ö²¢Ìí¼ÓÄã×Ô¼ºµÄÃû×Ö.
²»Òª´ò¶ÏÕâ¸öÁ´Ìõ! ²¢ÇÒÔÚ48Сʱ֮ÄÚÍê³ÉËü.
BrooklynµÄWilfred?P.ûÓгɹ¦µÄ·¢ËÍËûµÄ10¸ö¿½±´, µ±ËûµÚ2ÌìÔçÉÏÐÑÀ´·¢ÏÖËûµÄ¹¤×÷±ä³ÉÁË
"COBOL ³ÌÐòÔ±". Newport?NewsµÄHoward?L.ÔÚÒ»¸öÔÂÄڲŷ¢³öÁËËûµÄ10¸ö¿½±´, Õâ¸öʱ¼ä×ã¹»
½¨Á¢Ò»¸ö100¸ö½ÚµãµÄBeowulf clusterÀ´ÍæTuxracerÁË. ChicagoµÄAmelia?V.¶ÔÕâ·âП¶Ö®Ò»
Ц²¢ÇÒ´ò¶ÏÁËÕâ¸öÁ´Ìõ, ²»¾ÃÖ®ºó, ËýµÄÖն˱¬Õ¨ÁË, ËýÏÖÔÚ»¨Á˺öàÌìʱ¼äΪMS Windowsд
Îĵµ.
ǧÍò²»Òª´ò¶ÏÕâ¸öÁ´Ìõ! ½ñÌì¾Í°Ñ10¸ö¿½±´·¢³öÈ¥!

µÚËIJ¿·Ö ¸ß¼¶
++++++++++++++++
µ½ÁËÕâ¶ù,ÎÒÃǽ«Òª×¼±¸ÉîÈë½Å±¾±à³ÌÖÐһЩÄѵÄ,²»Ñ°³£µÄ»°Ìâ.Ëæ×Å»°ÌâµÄÕ¹¿ª,ÎÒÃÇ»á
ÒÔ¶àÖÖ·½·¨ºÍ¼ì²â±ß½çÌõ¼þµÄ·½Ê½À´¡°´ò¿ªÐŷ⡱,¿´¸öÃ÷°×.(µ±ÎÒÃÇÉæ×ãδ֪ÁìÓòʱ»á·¢
Éúʲô?).

Ŀ¼
19. Regular ExpressionsÕýÔò±í´ïʽ
20. ×Óshell(Subshells)
21. ÊÜÏÞshell(Restricted Shells)
22. ½ø³ÌÌæ»»
23. º¯Êý
24. ±ðÃû(Aliases)
25. Áбí½á¹¹
26. Êý×é
27. /devºÍ/proc
28. ¹ØÓÚZerosºÍNulls
29. µ÷ÊÔ
30. Ñ¡Ïî
31. ¼ì²éÒÅ©(Gotchas)
32. ½Å±¾±à³Ì·ç¸ñ
33. ÔÓÏî
34. Bash,°æ±¾2ºÍ3

µÚ19Õ ÕýÔò±í´ïʽ
==================
ΪÁ˳ä·Ö·¢»Óshell±à³ÌµÄÍþÁ¦, ÄãÐèÒª¾«Í¨ÕýÔò±í´ïʽ. һЩÃüÁîºÍÈí¼þ°üÆÕ±éÔڽű¾±à³ÌÖÐ
ʹÓÃÕýÔò±í´ïʽ,ÀýÈçgrep, expr, sedºÍawk.

19.1 Ò»¸ö¼òÒªµÄÕýÔò±í´ïʽ½éÉÜ
--------------------------------
Ò»¸öÕýʽ±í´ïʽÊÇÒ»¸ö×Ö·û´®.×Ö·û´®ÀïµÄ×Ö·û±»³ÆΪԪ×Ö·û,ËüÃÇ¿ÉÄܱíʾÁ˱ÈËüÃÇ×ÖÃæÉÏ¿´
ÆðÀ´µÄÒâ˼¸ü·á¸»µÄº¬Òå.ÀýÈç,Ò»¸öÒýÓ÷ûºÅ¿ÉÄܱíʾÒýÓÃÒ»¸öÈËÑݽ²ÖеĻ°,»òÕß±íʾÏÂ
Ã潫Ҫ½²µ½µÄÒýÉê±íʾµÄÒâ˼.ÕýÔò±í´ïʽÊÇÒ»¸ö×Ö·û»ò/ºÍÔª×Ö·û×éºÏ³ÉµÄ×Ö·û¼¯,ËüÃÇÆ¥Åä
(»òÖ¸¶¨)Ò»¸öģʽ.
Ò»¸öÕýÔò±í´ïʽ°üº¬ÏÂÃæÒ»¸ö»ò¶à¸öÏî:
1. Ò»¸ö×Ö·û¼¯.
ÕâÀïµÄ×Ö·û¼¯ÀïµÄ×Ö·û±íʾµÄ¾ÍÊÇËüÃÇ×ÖÃæÉϵÄÒâ˼.ÕýÔò±í´ïʽ×î¼òµ¥µÄÇé¿ö¾ÍÊǽö
½öÓÉ×Ö·û¼¯×é³É,¶øûÓÐÆäËûµÄÔª×Ö·û.
2. ê.
Ò»¸öêָÃ÷ÁËÕýÔò±í´ïʽÔÚÒ»ÐÐÎı¾ÖÐҪƥÅäµÄλÖÃ,ÀýÈç^ºÍ$¾ÍÊÇê.
3. ÐÞÊηû
ËüÃÇÓÃÓÚÕ¹¿ª»òËõС(¼´ÊÇÐÞ¸ÄÁË)ÕýÔò±í´ïʽƥÅäÎı¾Ðеķ¶Î§.ÐÞÊηû°üÀ¨ÁËÐǺÅ.
À¨ºÅºÍ·´Ð±¸Ü·ûºÅ.
ÕýÔò±í´ïÊǵÄÖ÷Òª×÷ÓÃÊÇÓÃÀ´Îı¾ËÑË÷ºÍ×Ö´®²Ù×÷.Ò»¸öÕýÔò±í´ïʽƥÅäÒ»¸ö×Ö·û»òÊÇÒ»´®×Ö
·û--ÍêÕûµÄÒ»´®×Ö·û»òÊÇÁíÍâÒ»¸ö×Ö·û´®µÄ×Ó´®.
ÐǺŠ-- * -- Æ¥ÅäÇ°Ò»¸ö×Ö·ûµÄÈÎÒâ¶à´Î(°üÀ¨Áã´Î).
"1133*"Æ¥Åä11 + Ò»¸ö»ò¸ü¶àµÄ3 + ¿ÉÄܵÄÆäËû×Ö·û: 113, 1133, 111312, µÈ
µã -- . -- Æ¥Åä³ýÁËÐÂÐзûÖ®ÍâµÄÈÎÒâÒ»¸ö×Ö·û. [1]
"13." Æ¥Åä13 + ÖÁÉÙÒ»¸öÈÎÒâ×Ö·û(°üÀ¨¿Õ¸ñ): 1133, 11333, µ«²»Æ¥Åä 13
(ÒòΪÉÙÁ˸½¼ÓµÄÖÁÉÙÒ»¸öÈÎÒâ×Ö·û).
ÍÑ×Ö·û -- ^ -- Æ¥ÅäÒ»ÐеĿªÍ·,µ«ÒÀÀµÓÚÉÏÏÂÎÄ»·¾³,¿ÉÄÜÔÚÕýÔò±í´ïʽÖбíʾ·ñ¶¨
Ò»¸ö×Ö·û¼¯µÄÒâ˼.
ÃÀÔª·û -- $ -- ÔÚÕýÔò±í´ïʽÖÐÆ¥ÅäÐÐβ.
"^$" Æ¥Åä¿ÕÐÐ.
·½À¨ºÅ -- [...] -- ÔÚÕýÔò±í´ïʽÖбíʾƥÅäÀ¨ºÅÖеÄÒ»¸ö×Ö·û.
"[xyz]" Æ¥Åä×Ö·ûx, y, »òz.
"[c-n]" Æ¥Åä´Ó×Ö·ûcµ½nÖ®¼äµÄÈÎÒâÒ»¸ö×Ö·û.
"[B-Pk-y]" Æ¥Åä´ÓBµ½P »ò´Ókµ½yµÄÈÎÒâÒ»¸ö×Ö·û.
"[a-z0-9]" Æ¥ÅäÈÎÒâСд×Öĸ»òÊý×Ö.
"[^b-d]" Æ¥Åä³ýÁË´Óbµ½d·¶Î§ÄÚËùÓеÄ×Ö·û. ÕâÊÇÕýÔò±í´ïʽÖз´×ªÒâ˼»òÈ¡
µÄÒ» ¸öÀý×Ó.(¾ÍºÃÏñÔÚ±ðµÄÇéÐÎÖÐ!×Ö·ûËù°çÑݵĽÇÉ«).
¶à¸ö·½À¨ºÅ×Ö·û¼¯×éºÏʹÓÿÉÒÔÆ¥ÅäÒ»°ãµÄµ¥´ÊºÍÊý×Öģʽ."[Yy][Ee][Ss]" Æ
Åäyes, Yes, YES, yEs, µÈµÈ.
"[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]"Æ¥ÅäÉç»á°²È«Âë
(Social Security number).
·´Ð±¸Ü×Ö·û -- \ -- תÒå(escapes) Ò»¸öÌØÊâµÄ×Ö·û,ʹÕâ¸ö×Ö·û±íʾԭÀ´×ÖÃæÉϵÄÒâ˼.
"\$"±íʾÁËÔ­À´µÄ×ÖÃæÒâ˼"$",¶ø²»ÊÇÔÚÕýÔò±í´ïʽÖбí´ïµÄÆ¥ÅäÐÐβµÄÒâ˼.
ͬÑù,"\\"Ò²±»½âÊͳÉÁË×ÖÃæÉϵÄÒâ˼"\".
תÒå(escape)"¼â½ÇºÅ" -- \<...\> -- ÓÃÓÚ±íʾµ¥´ÊµÄ±ß½ç.
¼â½ÇºÅ±ØÐ뱻תÒå,ÒòΪ²»ÕâÑù×öµÄ»°ËüÃǾͱíʾµ¥
¶øÒÑ.
"\<the\>" Æ¥Åäµ¥´Ê"the",µ«²»Æ¥Åä"them", "ther
µÈµÈ.
bash$ cat textfile
This is line 1, of which there is only one instance.
This is the only instance of line 2.
This is line 3, another line.
This is line 4.

bash$ grep 'the' textfile


This is line 1, of which there is only one instance.
This is the only instance of line 2.
This is line 3, another line.

bash$ grep '\<the\>' textfile


This is the only instance of line 2.
È·¶¨ÕýÔò±í´ïʽÄÜ·ñ¹¤×÷µÄΨһ°ì·¨ÊDzâÊÔËü.
1 TEST FILE: tstfile # ²»Æ¥Åä.
2 # ²»Æ¥Åä.
3 Run grep "1133*" on this file. # Æ¥Åä.
4 # ²»Æ¥Åä.
5 # ²»Æ¥Åä.
6 This line contains the number 113. # Æ¥Åä.
7 This line contains the number 13. # ²»Æ¥Åä.
8 This line contains the number 133. # ²»Æ¥Åä.
9 This line contains the number 1133. # Æ¥Åä.
10 This line contains the number 113312. # Æ¥Åä.
11 This line contains the number 1112. # ²»Æ¥Åä.
12 This line contains the number 113312312. # Æ¥Åä.
13 This line contains no numbers at all. # ²»Æ¥Åä.
bash$ grep "1133*" tstfile
Run grep "1133*" on this file. # Æ¥Åä.
This line contains the number 113. # Æ¥Åä.
This line contains the number 1133. # Æ¥Åä.
This line contains the number 113312. # Æ¥Åä.
This line contains the number 113312312. # Æ¥Åä.
À©Õ¹µÄÕýÔò±í´ïʽ. Ôö¼ÓÁËһЩԪ×Ö·ûµ½ÉÏÃæÌáµ½µÄ»ù±¾µÄÔª×Ö·û¼¯ºÏÀï. ËüÃÇÔÚegrep,
awk,ºÍPerlÖÐʹÓÃ.
ÎʺŠ-- ? -- Æ¥ÅäÁã»òÒ»¸öÇ°ÃæµÄ×Ö·û. ËüÒ»°ãÓÃÓÚÆ¥Åäµ¥¸ö×Ö·û.
¼ÓºÅ -- + -- Æ¥ÅäÒ»¸ö»ò¶à¸öÇ°ÃæµÄ×Ö·û.ËüµÄ×÷ÓúÍ*ºÜÏàËÆ,µ«Î¨Ò»µÄÇø±ðÊÇËü²»
Æ¥ÅäÁã¸ö×Ö·ûµÄÇé¿ö.
1 # GNU °æ±¾µÄ sed ºÍ awk ¿ÉÒÔʹÓÃ"+",
2 # µ«ËüÓ¦¸ÃתÒåÒ»ÏÂ.
3
4 echo a111b | sed -ne '/a1\+b/p'
5 echo a111b | grep 'a1\+b'
6 echo a111b | gawk '/a1+b/'
7 # ÉÏÃæÈý¾ä¶¼Êǵȼ۵ÄЧ¹û.
8
9 # ¶àл, S.C.
תÒå"´óÀ¨ºÅ" -- \{ \} -- ָʾǰÃæÕýÔò±í´ïʽƥÅäµÄ´ÎÊý.
ҪתÒåÊÇÒòΪ²»×ªÒåµÄ»°´óÀ¨ºÅÖ»ÊDZíʾËûÃÇ×ÖÃæÉϵÄÒâ˼.Õâ¸öÓ÷¨
¼¼ÇÉÉϵĶø²»ÊÇ»ù±¾ÕýÔò±í´ïʽµÄÄÚÈÝ.
"[0-9]\{5\}" ¾«È·Æ¥Åä5¸öÊý×Ö (´Ó 0 µ½ 9µÄÊý×Ö).
×¢Òâ: ´óÀ¨ºÅ²»ÄÜÔÚ¡°¾­µä¡±(²»ÊÇPOSIX¼æÈÝ)µÄÕýÔò±í´ïʽ°æ±¾µÄ
ʹÓÃ. È»¶ø, gawk ÓÐÒ»¸öÑ¡Ïî--re-intervalÀ´ÔÊÐ
(²»±ØתÒå).
bash$ echo 2222 | gawk --re-int
erval '/2{3}/'
2222
PerlºÍһЩegrep°æ±¾²»ÒªÇóתÒå´óÀ¨ºÅ.
Ô²À¨ºÅ -- ( ) -- À¨ÆðÒ»×éÕýÔò±í´ïʽ. ËüºÍÏÂÃæÒª½²µÄ"|"²Ù×÷·û»òÔÚÓÃexpr½øÐÐ×Ó×Ö
·û´®ÌáÈ¡(substring extraction)Ò»ÆðʹÓúÜÓÐÓÃ.
ÊúÏß -- | -- "»ò"ÕýÔò²Ù×÷·ûÓÃÓÚÆ¥ÅäÒ»×é¿ÉÑ¡µÄ×Ö·û.
bash$ egrep 're(a|e)d' misc.txt
People who read seem to be better informed than those w
ho do not.
The clarinet produces sound by the vibration of its ree
d.
×¢Òâ: һЩsed, ed, ºÍexµÄ°æ±¾ÏñGNUµÄÈí¼þ°æ±¾Ò»ÑùÖ§³ÖÉÏÃæÃèÊöµÄÀ©Õ¹Õ
Ôò±í´ïʽµÄ°æ±¾.
POSIX×Ö·ûÀà. [:class:]
ÕâÊÇÁíÍâÒ»¸ö¿ÉÑ¡µÄÓÃÓÚÖ¸¶¨Æ¥Åä×Ö·û·¶Î§µÄ·½·¨.
[:alnum:] Æ¥Åä×ÖĸºÍÊý×Ö.µÈͬÓÚA-Za-z0-9.
[:alpha:] Æ¥Åä×Öĸ. µÈͬÓÚA-Za-z.
[:blank:] Æ¥ÅäÒ»¸ö¿Õ¸ñ»òÊÇÒ»¸öÖƱí·û(tab).
[:cntrl:] Æ¥Åä¿ØÖÆ×Ö·û.
[:digit:] Æ¥Åä(Ê®½øÖÆ)Êý×Ö. µÈͬÓÚ0-9.
[:graph:] (¿É´òÓ¡µÄͼÐÎ×Ö·û). Æ¥Åä ASCII ÂëÖµµÄ33 - 126Ö®¼äµÄ×Ö·û. ÕâºÍÏÂÃæÌáµ½µÄ
[:print:]Ò»Ñù,µ«ÊDz»°üÀ¨¿Õ¸ñ×Ö·û.
[:lower:] Æ¥ÅäСд×Öĸ. µÈͬÓÚa-z.
[:print:] (¿É´òÓ¡×Ö·û). Æ¥Åä ASCIIÂëÖµ 32 - 126Ö®¼äµÄ×Ö·û. ÕâºÍÉÏÃæÌáµ½µÄÒ»Ñù
[:graph:],µ«ÊÇÔö¶àÒ»¸ö¿Õ¸ñ×Ö·û.
[:space:] Æ¥Åä¿Õ°××Ö·û (¿Õ¸ñ·ûºÍˮƽÖƱí·û).
[:upper:] Æ¥Åä´óд×Öĸ. µÈͬÓÚA-Z.
[:xdigit:] Æ¥ÅäÊ®Áù½øÖÆÊý×Ö. µÈͬÓÚ0-9A-Fa-f.
×¢Òâ: POSIX×Ö·ûÀàÒ»°ã¶¼ÒªÇóÓÃÒýºÅ»òÊÇË«·½À¨ºÅdouble brackets ([[ ]])ÒýÆðÀ´.
bash$ grep [[:digit:]] test.file
abc=723
ÕâЩ×Ö·ûÀàÔÚÒ»¸öÊÜÏ޵ķ¶Î§ÄÚÉõÖÁ¿ÉÄÜÓÃÔÚÄÜÓÃÔÚͨÅä(globbing)ÖÐ.
bash$ ls -l ?[[:digit:]][[:digit:]]?
-rw-rw-r-- 1 bozo bozo 0 Aug 21 14:47 a33b
ΪÁËÀí½âPOSIX×Ö·ûÀàÔڽű¾ÖеÄʹÓÃ,Çë²Î¿¼Àý×Ó 12-18 ºÍ Àý×Ó 12-19.
Sed, awk, ºÍPerlÔڽű¾Öб»ÓÃ×÷¹ýÂËÆ÷, "¹ýÂË"»òת»»Îļþ/IOÁ÷µÄʱºòÒÔÕýÔò±í´ïʽ×÷Ϊ²Î
Êý.²Î¿¼Àý×Ó A-12ºÍÀý×Ó A-17 À´Àí½âÕâÖÖÓ÷¨.
ÔÚÕýÔò±í´ïʽÕâ¸ö¸´ÔÓÖ÷ÌâµÄ±ê×¼²Î¿¼ÊÇFriedlµÄMastering Regular Expressions.ÓÉ
DoughertyºÍRobbinsдµÄ Sed & AwkÒ²¸ø³öÁËÒ»¸öÇåÎúµÄÕýÔò±í´ïʽÂÛÊö. ²é¿´²Î¿¼ÊéÄ¿ÕÒ
µ½Õâ¸öÖ÷Ìâ¸ü¶àµÄÐÅÏ¢.
×¢ÒâÊÂÏî:
[1] ÒòΪsed, awk, ºÍ grep ͨ³£´¦Àíµ¥ÐÐ,¶ø²»ÄÜÆ¥ÅäÒ»¸öÐÂÐзû. ÔÚÒª´¦Àí¶àÐеÄÒ»
¸öÊäÈëʱ,¿ÉÒÔʹÓõã²Ù×÷·û,Ëü¿ÉÒÔÆ¥ÅäÐÂÐзû.
1 #!/bin/bash
2
3 sed -e 'N;s/.*/[&]/' << EOF # Here Document
4 line1
5 line2
6 EOF
7 # Êä³ö:
8 # [line1
9 # line2]
10
11
12
13 echo
14
15 awk '{ $0=$1 "\n" $2; if (/line.1/) {print}}' << EOF
16 line 1
17 line 2
18 EOF
19 # Êä³ö:
20 # line
21 # 1
22
23
24 # ¶àл, S.C.
25
26 exit 0
19.1 ͨÅä
------------
Bash±¾ÉíûÓÐÕýÔò±í´ïʽµÄ¹¦ÄÜ.Ôڽű¾Àï,ʹÓÃÕýÔò±í´ïʽµÄÊÇÃüÁîºÍÈí¼þ°ü -- ÀýÈçsedºÍ
awk -- ËüÃÇ¿ÉÒÔ½âÊÍÕýÔò±í´ïʽ.
BashËù×öµÄÊÇÕ¹¿ªÎļþÃûÀ©Õ¹ [1] -- Õâ¾ÍÊÇËùνµÄͨÅä(globbing) -- µ«Ëü²»ÊÇʹÓñê×¼µÄ
ÕýÔò±í´ïʽ. ¶øÊÇʹÓÃͨÅä·û. ͨÅä½âÊͱê×¼µÄͨÅä·û£º*ºÍ?, ·½À¨ºÅÀ¨ÆðÀ´µÄ×Ö·û,»¹ÓÐÆäËû
µÄһЩÌØÊâµÄ×Ö·û(±ÈÈç˵^ÓÃÀ´±íʾȡ·´Æ¥Åä).È»¶øͨÅä»úÖƵÄͨÅä·ûÓкܴóµÄ¾ÖÏÞÐÔ. °üº¬
ÓÐ*ºÅµÄ×Ö·û´®½«²»»áÆ¥ÅäÒԵ㿪ͷµÄÎļþ,ÀýÈç.bashrc. [2] ÁíÍâ,ͨÅä»úÖƵÄ? ×Ö·ûºÍÕýÔò
±í´ïʽÖбíʾµÄÒâ˼²»Ò»Ñù.
bash$ ls -l
total 2
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1
-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt
bash$ ls -l t?.sh
-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh
bash$ ls -l [ab]*
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1
bash$ ls -l [a-c]*
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1
bash$ ls -l [^ab]*
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1
-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt
bash$ ls -l {b*,c*,*est*}
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt
Bash»á¶ÔÃüÁîÐÐÖÐûÓÐÒýºÅÒýÆðÀ´µÄ×Ö·û³¢ÊÔÎļþÃûÀ©Õ¹. echo ÃüÁî¿ÉÒÔÓ¡Ö¤ÕâÒ»µã.
bash$ echo *
a.1 b.1 c.1 t2.sh test1.txt
bash$ echo t*
t2.sh test1.txt
×¢Òâ: ¿ÉÒԸıäBash¶ÔͨÅä×Ö·û½øÐнâÊ͵ÄÐÐΪ. set -f ÃüÁî¿ÉÒÔ½ûֹͨÅä»úÖÆ, ²¢ÇÒ
shoptµÄÑ¡ÏînocaseglobºÍnullglob ÄܸıäͨÅäµÄÐÐΪ.
²Î¿¼Àý×Ó 10-4.
×¢ÒâÊÂÏî:
[1] ÎļþÃûÀ©Õ¹Òâ˼ÊÇÀ©Õ¹°üº¬ÓÐÌØÊâ×Ö·ûµÄÎļþÃûģʽºÍÄ£°å. ÀýÈç,example.???¿ÉÄÜ
À©Õ¹³Éexample.001ºÍ/»òexample.txt.
[2] ÎļþÃûÀ©Õ¹ÄÜÆ¥Åäµã¿ªÍ·µÄÎļþ,µ«½öÔÚģʽ×Ö´®Ã÷È·µØ°üº¬×ÖÃæÒâ˼µÄµã(.)ʱ²Å
À©Õ¹.
1 ~/[.]bashrc # ²»»áÀ©Õ¹³É ~/.bashrc
2 ~/?bashrc # Ò²²»»áÀ©Õ¹.
3 # ͨÅä»úÖÆÖеÄͨÅä·ûºÍÔª×Ö·û²»»áÀ©Õ¹µãÎļþ
4 #
5
6 ~/.[b]ashrc # »áÀ©Õ¹³É ~/.bashrc
7 ~/.ba?hrc # Ò²»á.
8 ~/.bashr* # Ò²»á.
9
10 # ¿ÉÒÔʹÓÃ"dotglob"Ñ¡Ïî°ÑÕâ¸öÌØÐÔ½ûÓÃ.
11
12 # ¶àл, S.C.
µÚ20ÕÂ ×Óshell(Subshells)
==========================
ÔËÐÐÒ»¸öshell½Å±¾Ê±»áÆô¶¯ÁíÒ»¸öÃüÁî½âÊÍÆ÷. ¾ÍºÃÏñÄãµÄÃüÁîÊÇÔÚÃüÁîÐÐÌáʾϱ»½âÊ͵ÄÒ»
Ñù, ÀàËÆÓÚÅú´¦ÀíÎļþÀïµÄһϵÁÐÃüÁî.ÿ¸öshell½Å±¾ÓÐЧµØÔËÐÐÔÚ¸¸shell(parent shell)µÄ
Ò»¸ö×Ó½ø³ÌÀï.Õâ¸ö¸¸shellÊÇÖ¸ÔÚÒ»¸ö¿ØÖÆÖն˻òÔÚÒ»¸öxterm´°¿ÚÖиøÄãÃüÁîָʾ·ûµÄ½ø³Ì.
shell½Å±¾Ò²ÄÜÆô¶¯Ëû×ÔÒѵÄ×Ó½ø³Ì. ÕâЩ×Óshell(¼´×Ó½ø³Ì)ʹ½Å±¾ÒòΪЧÂʶøͬʱ½øÐжà¸ö
×ÓÈÎÎñÖ´ÐÐʱÄÜ×ö´®Ðд¦Àí.
Ò»°ãÀ´Ëµ,½Å±¾ÀïµÄÒ»¸öÍⲿÃüÁî(external command)ÄÜÉú³É(forks)³öÒ»¸ö×Ó½ø³Ì,È»¶ø
BashÄÚ½¨(builtin)µÄÃüÁîÈ´²»ÕâÑù×ö,Òò´Ë,ÄÚ½¨ÃüÁî±ÈÆðÍⲿµÄµÈ¼ÛÃüÁîÖ´ÐÐÆðÀ´¸ü¿ì.
Ô²À¨ºÅÀïµÄÃüÁîÁбí
( ÃüÁî1; ÃüÁî2; ÃüÁî3; ... )
ǶÔÚÔ²À¨ºÅÀïµÄÒ»ÁÐÃüÁîÔÚÒ»¸ö×ÓshellÀïÔËÐÐ.
×¢Òâ: ÔÚ×ÓshellÀïµÄ±äÁ¿²»Äܱ»Õâ¶Î×Óshell´úÂë¿éÖ®ÍâÍâÃæµÄ½Å±¾·ÃÎÊ.ÕâЩ±äÁ¿ÊDz»Äܱ»
²úÉúÕâ¸ö×ÓshellµÄ¸¸½ø³Ì(parent process)´æÈ¡µÄ,ʵ¼ÊÉÏËüÃÇÊǾֲ¿±äÁ¿
(local variables).
Example 20-1 ×ÓshellÖеıäÁ¿×÷ÓÃÓò
################################Start Script####################################
###
1 #!/bin/bash
2 # subshell.sh
3
4 echo
5
6 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
7 # Bash, °æ±¾ 3, Ôö¼ÓÁËÐ嵀 $BASH_SUBSHELL ±äÁ¿.
8 echo
9
10 outer_variable=Outer
11
12 (
13 echo "Subshell level INSIDE subshell = $BASH_SUBSHELL"
14 inner_variable=Inner
15
16 echo "From subshell, \"inner_variable\" = $inner_variable"
17 echo "From subshell, \"outer\" = $outer_variable"
18 )
19
20 echo
21 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
22 echo
23
24 if [ -z "$inner_variable" ]
25 then
26 echo "inner_variable undefined in main body of shell"
27 else
28 echo "inner_variable defined in main body of shell"
29 fi
30
31 echo "From main body of shell, \"inner_variable\" = $inner_variable"
32 # $inner_variable »áÒÔûÓгõʼ»¯µÄ±äÁ¿À´´òÓ¡
33 #+ ÒòΪ±äÁ¿ÊÇÔÚ×ÓshellÀﶨÒåµÄ"¾Ö²¿±äÁ¿".
34 # Õâ¸öÓа취²¹¾ÈµÄÂð?
35
36 echo
37
38 exit 0
################################End Script######################################
###
²Î¿¼Àý×Ó 31-2.
+
ÔÚ×ÓshellÖеÄĿ¼¸ü¸Ä²»»áÓ°Ïìµ½¸¸shell.
Example 20-2 ÁгöÓû§µÄÅäÖÃÎļþ
################################Start Script####################################
###
1 #!/bin/bash
2 # allprofs.sh: ´òÓ¡ËùÓÐÓû§µÄÅäÖÃÎļþ
3
4 # ÓÉ Heiner Steven±àд, ²¢Óɱ¾Êé×÷ÕßÐÞ¸Ä.
5
6 FILE=.bashrc # ÔÚÒ»°ãµÄ½Å±¾Àï,°üº¬Óû§ÅäÖõÄÎļþÊÇ".profile".
7 #
8
9 for home in `awk -F: '{print $6}' /etc/passwd`
10 do
11 [ -d "$home" ] || continue # Èç¹ûûÓмÒĿ¼,Ìø¹ý´Ë´ÎÑ­»·.
12 [ -r "$home" ] || continue # Èç¹ûĿ¼ûÓжÁȨÏÞ,Ìø¹ý´Ë´ÎÑ­»·.
13 (cd $home; [ -e $FILE ] && less $FILE)
14 done
15
16 # µ±½Å±¾ÖÕֹʱ,²»±ØÓÃ'cd'ÃüÁî·µ»ØÔ­À´µÄĿ¼,
17 #+ ÒòΪ'cd $home'ÊÇÔÚ×ÓshellÖз¢ÉúµÄ,²»Ó°Ï츸shell.
18
19 exit 0
################################End Script######################################
###
×Óshell¿ÉÓÃÓÚΪһ×éÃüÁîÉ趨ÁÙʱµÄ»·¾³±äÁ¿.
1 COMMAND1
2 COMMAND2
3 COMMAND3
4 (
5 IFS=:
6 PATH=/bin
7 unset TERMINFO
8 set -C
9 shift 5
10 COMMAND4
11 COMMAND5
12 exit 3 # Ö»ÊÇ´Ó×ÓshellÍ˳ö.
13 )
14 # ¸¸shell²»ÊÜÓ°Ïì,±äÁ¿ÖµÃ»Óиü¸Ä.
15 COMMAND6
16 COMMAND7
ËüµÄÒ»¸öÓ¦ÓÃÊDzâÊÔÊÇ·ñÒ»¸ö±äÁ¿±»¶¨ÒåÁË.
1 if (set -u; : $variable) 2> /dev/null
2 then
3 echo "Variable is set."
4 fi # ±äÁ¿ÒѾ­ÔÚµ±Ç°½Å±¾Öб»ÉèÖÃ,
5 #+ »òÊÇBashµÄÒ»¸öÄÚ²¿±äÁ¿,
6 #+ »òÊǿɼû»·¾³±äÁ¿(Ö¸ÒѾ­±»µ¼³öµÄ»·¾³±äÁ¿).
7
8 # Ò²¿ÉÒÔд³É [[ ${variable-x} != x || ${variable-y} != y ]]
9 # »ò [[ ${variable-x} != x$variable ]]
10 # »ò [[ ${variable+x} = x ]]
11 # »ò [[ ${variable-x} != x ]]
ÁíÒ»¸öÓ¦ÓÃÊǼì²éÒ»¸ö¼ÓËøµÄÎļþ:
1 if (set -C; : > lock_file) 2> /dev/null
2 then
3 : # lock_file ²»´æÔÚ,»¹Ã»ÓÐÓû§ÔËÐÐÕâ¸ö½Å±¾
4 else
5 echo "Another user is already running that script."
6 exit 65
7 fi
8
9 # ÓÉStéphane Chazelas±à³Ì
10 #+ ÓÉPaulo Marcel Coelho AragaoÐÞ¸Ä.
½ø³ÌÔÚ²»Í¬µÄ×ÓshellÖпÉÒÔ´®ÐеØÖ´ÐÐ.ÕâÑù¾ÍÔÊÐí°ÑÒ»¸ö¸´ÔÓµÄÈÎÎñ·Ö³É¼¸¸öСµÄ×ÓÎÊÌâÀ´
ͬʱµØ´¦Àí.
Example 20-3 ÔÚ×ÓshellÀï½øÐд®Ðд¦Àí
################################Start Script####################################
###
1 (cat list1 list2 list3 | sort | uniq > list123) &
2 (cat list4 list5 list6 | sort | uniq > list456) &
3 #ÁбíµÄºÏ²¢ºÍÅÅÐòͬʱ½ø.
4 #·Åµ½ºǫ́ÔËÐпÉÒÔÈ·±£Äܹ»´®ÐÐÖ´ÐÐ.
5 #
6 #ºÍÏÂÃæµÄÓÐÏàͬµÄ×÷Óãº
7 # cat list1 list2 list3 | sort | uniq > list123 &
8 # cat list4 list5 list6 | sort | uniq > list456 &
9
10 wait #ÔÚËùÓеÄ×ÓshellÖ´ÐÐÍê³ÉÇ°²»ÔÙÖ´ÐкóÃæµÄÃüÁî.
11
12 diff list123 list456
################################End Script######################################
###
ÓÃ"|"¹ÜµÀ²Ù×÷°ÑI/OÁ÷Öض¨Ïòµ½×Óshell,ÀýÈçls -al | (command).
×¢Òâ: ÔÚÒ»¸ö»¨À¨ºÅÄڵĴúÂë¿é²»»áÔËÐÐÒ»¸ö×Óshell.
{ command1; command2; command3; ... }

µÚ21ÕÂ ÊÜÏÞshell(Restricted Shells)


====================================
ÔÚÊÜÏÞshellÖнûÓõÄÃüÁî
ÔÚÊÜÏÞshellÖÐÔËÐеĽű¾»ò½Å±¾µÄ¸ö´úÂë¶Ï»á½ûÓÃһЩÕý³£shellÖпÉÒÔÖ´ÐеÄÃüÁî.ÕâÊÇ
ÏÞÖƽű¾Óû§µÄȨÏÞºÍ×îС»¯ÔËÐнű¾µ¼ÖµÄÆÆ»µµÄ°²È«´ëÊ©.
ʹÓÃcd ÃüÁî¸ü¸Ä¹¤×÷Ŀ¼.
¸ü¸Ä»·¾³±äÁ¿$PATH, $SHELL, $BASH_ENV,»ò$ENV µÄÖµ.
¶Á»ò¸ü¸Äshell»·¾³Ñ¡Ïî±äÁ¿$SHELLOPTSµÄÖµ.
Êä³öÖض¨Ïò.
µ÷ÓõÄÃüÁî·¾¶ÖаüÀ¨ÓÐÒ»¸ö»ò¸ü¶à¸ö/×Ö·û.
µ÷ÓÃexecÀ´°Ñµ±Ç°µÄÊÜÏÞshellÌæ»»³ÉÁíÍâÒ»¸ö²»Í¬µÄ½ø³Ì.
½Å±¾ÖÐÐí¶àÆäËûÎÞÒâÖÐÄÜÆÆ»µ»òµ·ÂÒµÄÃüÁî.
Ôڽű¾ÖÐÆóͼÍÑÀëÊÜÏÞshellģʽµÄ²Ù×÷.
Example 21-1 ÔÚÊÜÏÞµÄÇé¿öÏÂÔËÐнű¾
################################Start Script####################################
###
1 #!/bin/bash
2
3 # ½Å±¾¿ªÍ·ÒÔ"#!/bin/bash -r"À´µ÷ÓÃ
4 #+ »áʹÕû¸ö½Å±¾ÔÚÊÜÏÞģʽÏÂÔËÐÐ.
5
6 echo
7
8 echo "Changing directory."
9 cd /usr/local
10 echo "Now in `pwd`"
11 echo "Coming back home."
12 cd
13 echo "Now in `pwd`"
14 ech