外文翻译--高级Bash脚本编程指南一个对脚本编程技术的深入讨论.doc
附录外文资料翻译原文部分AdvancedBash-ScriptingGuideAnin-depthexplorationoftheartofshellscripting1.IntroductionTheshellisacommandinterpreter.Morethanjusttheinsulatinglayerbetweentheoperatingsystemkernelandtheuser,itsalsoafairlypowerfulprogramminglanguage.Ashellprogram,calledascript,isaneasy-to-usetoolforbuildingapplicationsby"gluing"togethersystemcalls,tools,utilities,andcompiledbinaries.VirtuallytheentirerepertoireofUNIXcommands,utilities,andtoolsisavailableforinvocationbyashellscript.Ifthatwerenotenough,internalshellcommands,suchastestingandloopconstructs,giveadditionalpowerandflexibilitytoscripts.Shellscriptslendthemselvesexceptionallywelltoadministrativesystemtasksandotherroutinerepetitivejobsnotrequiringthebellsandwhistlesofafull-blowntightlystructuredprogramminglanguage.2.WhyShellProgramming?Aworkingknowledgeofshellscriptingisessentialtoanyonewishingtobecomereasonablyproficientatsystemadministration,eveniftheydonotanticipateeverhavingtoactuallywriteascript.ConsiderthatasaLinuxmachinebootsup,itexecutestheshellscriptsin/etc/rc.dtorestorethesystemconfigurationandsetupservices.Adetailedunderstandingofthesestartupscriptsisimportantforanalyzingthebehaviorofasystem,andpossiblymodifyingit.Writingshellscriptsisnothardtolearn,sincethescriptscanbebuiltinbite-sizedsectionsandthereisonlyafairlysmallsetofshell-specificoperatorsandoptionstolearn.Thesyntaxissimpleandstraightforward,similartothatofinvokingandchainingtogetherutilitiesatthecommandline,andthereareonlyafew"rules"tolearn.Mostshortscriptsworkrightthefirsttime,anddebuggingeventhelongeronesisstraightforward.Ashellscriptisa"quickanddirty"methodofprototypingacomplexapplication.Gettingevenalimitedsubsetofthefunctionalitytoworkinashellscriptisoftenausefulfirststageinprojectdevelopment.Thisway,thestructureoftheapplicationcanbetestedandplayedwith,andthemajorpitfallsfoundbeforeproceedingtothefinalcodinginC,C+,Java,orPerl.ShellscriptinghearkensbacktotheclassicUNIXphilosophyofbreakingcomplexprojectsintosimplersubtasks,ofchainingtogethercomponentsandutilities.Manyconsiderthisabetter,oratleastmoreestheticallypleasingapproachtoproblemsolvingthanusingoneofthenewgenerationofhighpoweredall-in-onelanguages,suchasPerl,whichattempttobeallthingstoallpeople,butatthecostofforcingyoutoalteryourthinkingprocessestofitthetool.WhennottouseshellscriptsResource-intensivetasks,especiallywherespeedisafactor(sorting,hashing,etc.)Proceduresinvolvingheavy-dutymathoperations,especiallyfloatingpointarithmetic,arbitraryprecisioncalculations,orcomplexnumbers(useC+orFORTRANinstead)Cross-platformportabilityrequired(useCorJavainstead)Complexapplications,wherestructuredprogrammingisanecessity(needtype-checkingofvariables,functionprototypes,etc.)Mission-criticalapplicationsuponwhichyouarebettingtheranch,orthefutureofthecompanySituationswheresecurityisimportant,whereyouneedtoguaranteetheintegrityofyoursystemandprotectagainstintrusion,cracking,andvandalismProjectconsistsofsubcomponentswithinterlockingdependenciesExtensivefileoperationsrequired(Bashislimitedtoserialfileaccess,andthatonlyinaparticularlyclumsyandinefficientline-by-linefashion)Neednativesupportformulti-dimensionalarraysNeeddatastructures,suchaslinkedlistsortreesNeedtogenerateormanipulategraphicsorGUIsNeeddirectaccesstosystemhardwareNeedportorsocketI/ONeedtouselibrariesorinterfacewithlegacycodeProprietary,closed-sourceapplications(shellscriptsputthesourcecoderightoutintheopenforalltheworldtosee)Ifanyoftheaboveapplies,consideramorepowerfulscriptinglanguage-perhapsPerl,Tcl,Python,Ruby-orpossiblyahigh-levelcompiledlanguagesuchasC,C+,orJava.Eventhen,prototypingtheapplicationasashellscriptmightstillbeausefuldevelopmentstep.WewillbeusingBash,anacronymfor"Bourne-Againshell"andapunonStephenBournesnowclassicBourneshell.BashhasbecomeadefactostandardforshellscriptingonallflavorsofUNIX.Mostoftheprinciplesthisbookcoversapplyequallywelltoscriptingwithothershells,suchastheKornShell,fromwhichBashderivessomeofitsfeatures,andtheCShellanditsvariants.(NotethatCShellprogrammingisnotrecommendedduetocertaininherentproblems,aspointedoutinanOctober,1993UsenetpostbyTomChristiansen.)Whatfollowsisatutorialonshellscripting.Itreliesheavilyonexamplestoillustratevariousfeaturesoftheshell.Theexamplescriptswork-theyvebeentested,insofaraswaspossible-andsomeofthemareevenusefulinreallife.Thereadercanplaywiththeactualworkingcodeoftheexamplesinthesourcearchive(scriptname.shorscriptname.bash),givethemexecutepermission(chmodu+rxscriptname),thenrunthemtoseewhathappens.Shouldthesourcearchivenotbeavailable,thencut-and-pastefromtheHTML,pdf,ortextrenderedversions.Beawarethatsomeofthescriptspresentedhereintroducefeaturesbeforetheyareexplained,andthismayrequirethereadertotemporarilyskipaheadforenlightenment.外文资料翻译译文部分高级Bash脚本编程指南一个对脚本编程技术的深入讨论1:简介Shell是一个命令解释器。它是操作系统内核和用户之间的绝缘层,也是一种功能强大的编程语言。一个Shell程序,通常被称作为脚本,它是由系统调用,命令工具,软件包和已编译的二进制包"结合"起来的容易使用的工具。事实上,shell脚本可以调用整个UNIX系统命令,软件包和工具。如果这还不太够的话,Shell的内部命令,如测试和循环结构,都使得Shell脚本更强大和更有弹性。Shell脚本在管理系统任务中表现得非常出色,在对日常反复性的处理工作中避免使用那些结构复杂的程序语言。2:为什么要用Shell编程?脚本应用知识对希望精通系统管理的任何个人说是必需的,他实际上不想写一个脚本程序。一般说一个Linux机器启动后,它会执行在/etc/rc.d目录下的Shell脚本重建系统环境并且启动各种服务。理解这些启动脚本的细节对分析系统运作并修改它是有很大意义的。编写shell脚本并不难学,因为脚本内建的功能集(check?)和他们只要学相当少的shell的操作符和选项。语法简单易懂,像在命令行上调用和连接软件包那样容易,它只有少量的"规则"需要掌握。大多数短小的脚本第一次就工作的很好,较长的脚本调试也相当的容易。shell脚本是个复杂应用原型的"quickanddirty"方法。在项目开发中用shell编程实现个有限的功能性子集是有用的开始。用这种方法去测试应用程序的结构和模块组合,可以在实际地用C,C+,Java或者Perl进行编程之前发现主要的设计缺陷。Shell编程遵从经典UNIX哲学:把复杂的问题分成简单的小问题,然后再把各部分功能组合起来解决复杂问题。这和用新一代高级的多用途的语言,例如Perl,试图成为所有人处理所有事情的语言但是所付出的代价是强迫改变你的思维方法来适应这种工具,大多数人认为这是一个更好的或者至少感觉上更令人能