外文翻译 - Shell的介绍_第1页
外文翻译 - Shell的介绍_第2页
外文翻译 - Shell的介绍_第3页
外文翻译 - Shell的介绍_第4页
外文翻译 - Shell的介绍_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

0外文原文IntroductiontoshellInthischapter,youwilllearn:1)AboutshellsinUnixhistory2)NottobeafraidtousetheCommandLineInterface3)Howtofindhelpanddiscoverautocompletion4)Thatusingtheshellisgreat,sogreat!1BasicdefinitionAshellisasoftwareprovidingatext-basedinterfacetousers.Thereby,theycanusetheircomputers.Itisalsocalledcommandinterpreter.Indeed,theshellexecutescommandsthattheusertypes.Basically,thereistwowaytoreallyuseacomputer:withashell,bytypingcommands,orbyaGraphicalUserInterface(GUI),includingbuttons,menu,ormorerecentlymousesupport,ect.Thesetwointerfacesareoftenopposed,butyoubettermayconsiderthemascomplementary.2History2.1IntroductiontohistoryofuserinterfacesThefirstcomputerswereverybasicandofferedlowperformances.Moreover,atthebeginningofcomputingscience,therewasquiteafewnumberofsoftwaresandfeatures.Asyoucanimagine,onlytext-basedinterfaceswhereavailable,becauseofthelackofcomputingperformances,thelackofgraphicalinterfaceAPI(ApplicationProgrammingInterface).Furthermore,therewasnorealneedofgraphicalinterfacesforalongtime:whileapplicationsremainedlimitedandeaseofuse,orthatcomputerswhereonlyusedasserversorspecificbusinessapplications,nobodycomplainedabouttheuserexperience.GraphicalInterfacesbegantoappear,butmanywerestilltext-based.GraphicalUserinterfacesasweknownowadaysexpandedalotthankstothefightbetweenAppleandMicrosoftfortheend-user,familialcomputingmarket.2.2HistoryofUnixshellsUnixwasoneofthefirstOperatingSystemthatdidntintegratetheshelltoodeeply.InUnix,theshellisastandaloneprogramwithveryfewpermissions.Ithasledtoasuccessionofalternativeshelldevelopmentsandimprovements.Wecandivideshellsintofourgenerations:-Firstgenerationshells:ThomsonshellandMasheyshell.ThomsonistheveryfirstUnixshell.Somethingnew,comparedtoMulticssshell,wastheinput/outputredirectionmethod(withstill-used,|symbols).Itwasverybasic.(firstUnix)-Secondgenerationshells:C-shellandBourneshell(sh).Moreflexible,closertoscriptinglanguages,andmoreinteractive.InspiredfromawkandCprogramminglanguages.(Unix7,BSD,commercialUnix)-Thirdgenerationshells:tcshandkornshell(ksh)addnewgreatfeatures:commandhistory,filenamecompletion,andcommandlineediting.1-Fourthgenerationshells:Zshell(zsh)andBourneAgainshell(bash).Zshelltriedtoreunificatecsh/tcshsinteractivityandkshsfeatures.Itcameswithglobbingandimprovedcommandlineeditingandauocompletion.Bash,theGNUshellwhichwasfirstjustanalternativethatdidntbringanynewfeature,graduallyclosedthegapwiththeothershellsandbecameamajorshell.AsitisaGNUproject,andthankstoitsgreatfeatures,itisthebaseshellonGNU/Linuxsincemanyyears.3.Shells,terminals,consolesAswesaid,ashellisacommandinterpreter.Butyoualsomayhearaboutconsolesorterminals.Originally,aterminal,alsocalledaconsole,couldrefertotwothings:-anall-textmodeuserinterfacethatoccupiestheentirescreenofthedisplaydeviceandwhichdoesnotsitontopofaGraphicalUserInterface.-ahardwarewithinput/oupoutcapabilities,thatisusedtocontroltheserver.Hardwareterminalsweregenerallybasicandusedtheseriallink.Thankstomultiplexers,asysadminwasabletoconnecttoanyofmanyserverswithoneserialconsoleonly.Unixisamulti-useroperatingsystem.Thatimpliesthatmultipleterminalscanbeusedinthesametime.Nowadays,thoseterminalsarestillusedonmidrangecomputers,butvideoscreensandkeyboardhasreplacedserialconsolesonmostcomputers.Thetermterminal(orsometimesconsole)hastakenanothersense:ATerminalwindoworTerminalemulatorisatext-onlywindowinagraphicaluserinterface(GUI)thatemulatesaconsole(Text-only,inwhichashellcanbeattached).ThesearepicturesofmainTerminalwindowsonGNU/Linux,nowadays:(picxterm,gnome-terminal,konsole)4IntroductiontoCommandLineInterface4.1TheshellpromptNowthatweknowwhataterminalandashellare,letshavealookonhowtousetheCommandLineInterface:HereisaLinuxconsole:(consolelogin:)AndaTerminalwindow:(terminalwindow)Onaconsole,wefirstneedtologinin.Runningaterminalwindowimpliesthatweareloggedinalready.Lookatthefirstline:CODE:bashuserhostname:$Thatstheshellprompt.Ashellpromptisacharacterorsetofcharactersatthestartofthecommandlinethatindicatesthattheshellisreadytoreceivecommands.Withfirstshells,asinglecharacteronlywasused:usuallyadollar($)orahashchar(#)tospecifywhenevertheuserisasimpleuserorroot.Theshellprompthavebeenimproved,andwithcurrentshellslikezshorbash,manyinformationscanbedisplayed:username,hostname,timeanddate,uptime.Thisisthedefaultbashprompt:CODE:bashuserhostname:$user:Nameofthecurrentuserhostname:Nameofthecomputer(outputofthehostnamecommand)or/some/path:Currentworkingdirectory(outputofthepwdcommand).representstheuserhomedirectory.$or#:indicateswhenevertheuserisasimpleuser($)orroot(#)Thepromptcanbepersonalized,coloursandotherinformationscanbeadded.(seeShelltipsandtrickschapter)4.2Changinguserinthecurrentshell2WesometimesmayneedtochangeofuserwhenworkingwiththeCLI.Mostofthetime,becauseweneedmorerightsonthesystem,toinstallorconfiguresoftwares.Thecommandsu,forswitchuser,permittochangetheuser.Withnoargument,itswitchesbydefaulttoroot.CODE:bash#swithtorootuserlocalhost:$su#switchtousertotorootlocalhost:#sutoto4.3VariablesAvariableisaanidentifierthatreliesonavalue.Torespectthebashnaming-convention,variablesnamesshouldbeuppercase.EveryGNU/Linuxprocessrunsinaspecificenvironment.Anenvironmentconsistsofmanyvariables.Theshellmaintainsasetofinternalvariablesknownasshellvariables.Thesevariablescausetheshelltoworkinaparticularway.Shellvariablesarelocaltotheshellinwhichtheyaredefined;theyarenotavailabletotheparentorchildshells.Anenvironmentvariableisavariablethatisaccessibletoeveryshells,whereasashellvariableiskeptlocal.Inbash,ashellvariablehavetobeexportedtobecomeanenvironmentvariable:CODE:bashuserlocalhost:$LOCAL_VAR=test#keptlocal(notaccessiblefromothersuserlocalhost:$exportLOCAL_VAR#nowaccessiblefromothershellsforexample,theEDITORvariableisanenvironmentvariablethatdefinethedefaulttexteditorofthesystem,thatisusedinothersoftwareslikecrontab.CODE:bashuserlocalhost:$echo$EDITOR/usr/bin/vi4.4Standardinput/ouputstreamsredirectionThreestandardstreamsexistonUNIX:STDIN(0):standardinputstream,whichcorrespondbydefaulttothekeyboardsTDOUT(1):standardoutputstream,whichcorrespondtothescreenbydefault.STDERR(2):standarderrorstream,whichcorrespondbydefaulttothescreentoo.Apowerfulfeatureisthatthesestreamscanberedirected:forexample,insteadofprintingtheoutputonthescreen,itcanberedirectedintoafile.Orafilecanberead,insteadofthekeyboard.Theseredirectionscanbedonewithintheshell:outputredirectionoperator:redirecttheoutputtothespecifiedfile.Ifthefileexists,itisoverwriten.STOUTisthedefaultstream.ToredirectSTDERR,thestreamnumbermustbespecified:2TospecifytheSTDOUTstream,youalsocanspecifythenumber:CODE:bashcommandfile#commandouputgoestofilecommand2file#errorswhenexecutingcommandgotofileTodontoverwritethedestinationfile,thesymbolmustbeused.Itappendstheoutputstreamat3thefollowingofthefilescontent.CODE:bashcommandfile#commandoutputgoestotheendofthefile(dontoverwriteitscontent)inputredirectionoperator,|),这是极其基础的。(第一代unix)第二代shell:C-shell和Bourneshell(sh)。它更加详细、更加接近脚本语言、更具交互性。它从awk和c语言中获得灵感。(第七代unix,bsd,商用版unix)第三代shell:tcsh和kornshell(ksh)添加了更新更强的功能:命令历史、文件名完成和命令行编辑。第四代shell:Zshell(zsh)和BourneAgainshell(bash)。Zshell试图重组csh/tcsh的可交互性和ksh的功能。它提供通配符和先进的命令行编辑自动完成,GNUshell一开始只是一个代替品,并没有提供更新的功能,很大程度上缩小了与其他shell的差距从而成为了一款主流的shell。由于它是一个GNU工程项目,并且得益于其强大的功能,从此以后多年称为linux系统的标准shell。3shell,中断,控制台正如我们说的,shell是一个命令解释器。但是你也可能听说过关于控制台或终端的言论。最初终端也被称为控制台,可以被当做是两种东西的组合:-一个全文本模式用户界面,它占据这整个显示器的界面但是并没有时刻出现在图形界面的前端。-一个提供输入输出功能的硬件,通过这个硬件可以控制服务器。硬件终端通常很基本而且使用了串行链接。有了多路复用器的帮助,系统管理员能够只用一个串行控制台链接到很多服务器。Unix是一个多用户操作系统,这使得多个终端可以同时运行。如今,这些终端仍然在中型机上使用。但是在很多电脑上,视频显示器和键盘已经取代了串行服务台。终端家族(有时也叫做控制台)有了另一个意义:一个终端窗口或终端模拟器,也就是在图形用户界面下得一个纯文本窗口,以此来模拟一个终端(纯文本模式,以便可以运行shell)。这是如今linux主终端窗口的图片。4命令行界面简介4.1shell提示符6现在我们知道终端和shell是什么了,让我们来看一下如何使用命令行界面:这是一个linux控制台(控制台登陆):并且有一个终端窗口(terminalwindow),在控制台上,我们首先要登录。运行一个终端窗口,意味着我们已经登录了,看第一行:CODE:bashuserhostname:$这就是shell提示符。Shell提示符是一个字符或者一个字符串,它出现在命令行的起始位置,提示用户shell已经准备好开始接收指令了。在第一代shell中,我们只使用一个字符来区分普通用户或者root用户,通常是美元符号($)或者井号(#)。Shell提示符已经得到了很大的发展,比如当今的zsh和bash,可以提示出很多的信息:例如用户名、主机名、时间日期、更新时间等等。这是默认的bash提示符:CODE:bashuserhostname:$User:当前用户名Hostname:当前主机名(输出主机名命令)或者/some/path:当前工作目录(pwd命令即可输出)表示用户家目录$或#:当用户为普通用户时使用$,当用户为root用户时使用#,提示符可以个性化,颜色可以修改,内容也可以添加(详见shell的提示与技巧)4.2在当前shell中切换用户有时候当我们在命令行界面上操作的时候需要改变用户。这是因为在绝大部分情况下,我们需要更多的系统权限来安装或者配置软件。命令行中的su命令,意味着“switchuser”,即允许改变用户。没有其余参数的情况下,默认切换到root用户。例如:CODE:bash#swithtorootuserlocalhost:$su#switchtousertotorootlocalhost:#sutoto4.3变量一个变量代表着一个含有数值的标示符。为了遵守bash命名规范,变量名应该大写。每一个GNU/linux进程都运行在特殊的环境中。一个进程环境包含着很多的变量。Shell保持了一系列的内部变量,它们被称为shell变量。这些变量让shell以一个特殊的方式运行。Shell变量在本地声明,子shell是无法获得父shell的变量。环境变量是每一个shell都可以访问的变量,而不像shell变量那样只是一个本地变量。在bash中,一个shell变量加上exported命令就可成为一个环境变量:CODE:bashuserlocalhost:$LOCAL_VAR=test#keptlocal(notaccessiblefromothersuserlocalhost:$exportLOCAL_VAR#nowaccessiblefromothershellsforexample,theEDITORvariableisanenvironmentvariablethatdefinethedefaulttexteditorofthesystem,thatisusedinothersoftwareslikecrontab.CODE:bashuserlocalhost:$echo$EDITOR/usr/bin/vi4.4标准输入输出流重定向在unix中有三个标准流:STDIN(0):标准输入流,默认为键盘输入。STDOUT(1):标准输出流,默认为显示器。STDERR(2):标准错误流,也默认为是显示器。一个强大的功能就是,这些流可以被重定向:例如,可以将打印输出重定向到一个文件里来取代输出到屏幕上。或者将输入重定向从一个文件中读取内容来取代键盘。这些重定向可以绑定到shell里面,比如:*输出重定向操作符:将输出重定向到一个特定文件中。如果文件存在,它将被重写。STDOUT是默认的流,为了重定向STDERR,流的序号必须被指定出来:2。若想重定向STDOUT流,也可以指定其序号:CODE:bashcommandfile7#commandouputgoestofilecommand2file#errorswhenexecutingcommandgotofile如果不想重新改写目标文件,那么可以使用符号。它将输出流添加倒了文件内容的尾端。例如:CODE:bashcommandfile#commandoutputgoestotheendofthefile(dontoverwriteitscontent)*输入重定向符号:输入流可以从一个文件中读取内容来取代从键盘上获取输入。许多UNIX命令将会获取一个文件名作为命令行的参数,如果文件名为空,则继续从标准输入中获取输入。CODE:bashcommandfile在这里,文件内容将作为指令的输入。*将输出转到标准输入的重定向:|管道机制管道符号可以将第一条指令的标准输出重定向到第二条指令的标准输入中:CODE:bashcommand1|command2output1becomesinput24.5别名一些unix命令十分难记,尤其是需要一些选项的时候。此时,别名可以被用来建立简单的命令。CODE:bashuserlocalhost:$aliaseasycmd=hard_to_remember_command-with-o-ption#createyourali

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论