C shell编程初步.pdf

上传人:qwe****56 文档编号:70007581 上传时间:2023-01-14 格式:PDF 页数:44 大小:116.23KB
返回 下载 相关 举报
C shell编程初步.pdf_第1页
第1页 / 共44页
C shell编程初步.pdf_第2页
第2页 / 共44页
点击查看更多>>
资源描述

《C shell编程初步.pdf》由会员分享,可在线阅读,更多相关《C shell编程初步.pdf(44页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、An Introduction to the C shellWilliam Joy(revised for 4.3BSD by Mark Seiden)Computer Science DivisionDepartment of Electrical Engineering and Computer ScienceUniversity of California,BerkeleyBerkeley,California 94720ABSTRACTCsh is a new command language interpreter forUNIX systems.It incorporatesgoo

2、d features of other shells and a history mechanism similar to the redo ofINTERLISP.While incorporating many features of other shells which make writing shell programs(shell scripts)easier,most of the features unique to csh are designed more for the interac-tiveUNIXuser.UNIXusers who have read a gene

3、ral introduction to the system will find a valuablebasic explanation of the shell here.Simple terminal interaction with csh is possible afterreading just the first section of this document.The second section describes the shellscapabilities which you can explore after you have begun to become acquai

4、nted with theshell.Later sections introduce features which are useful,but not necessary for all users ofthe shell.Additional information includes an appendix listing special characters of the shelland a glossary of terms and commands introduced in this manual.IntroductionA shell is a command languag

5、e interpreter.Csh is the name of one particular command interpreteronUNIX.The primary purpose of csh is to translate command lines typed at a terminal into system actions,such as invocation of other programs.Csh is a user program just like any you might write.Hopefully,cshwill be a very useful progr

6、am for you in interacting with theUNIXsystem.In addition to this document,you will want to refer to a copy of theUNIXUser Reference Manual.The csh documentation in section 1 of the manual provides a full description of all features of the shell andis the definitive reference for questions about the

7、shell.Many words in this document are shown in italics.These are important words;names of commands,and words which have special meaning in discussing the shell andUNIX.Many of the words are defined ina glossary at the end of this document.If you dont know what is meant by a word,you should look for

8、itin the glossary.AcknowledgementsNumerous people have provided good input about previous versions of csh and aided in its debug-ging and in the debugging of its documentation.I would especially like to thank Michael Ubell who madethe crucial observation that history commands could be done well over

9、 the word structure of input text,andimplemented a prototype history mechanism in an older version of the shell.Eric Allman has also provideda large number of useful comments on the shell,helping to unify those concepts which are present and toUNIXis a trademark of Bell Laboratories.USD:4-2 An Intro

10、duction to the C shellidentify and eliminate useless and marginally useful features.Mike OBrien suggested the pathname hash-ing mechanism which speeds command execution.Jim Kulp added the job control and directory stackprimitives and added their documentation to this introduction.An Introduction to

11、the C shellUSD:4-31.Terminal usage of the shell1.1.The basic notion of commandsA shell inUNIXacts mostly as a medium through which other programs are invoked.While it has aset of builtin functions which it performs directly,most commands cause execution of programs that are,infact,external to the sh

12、ell.The shell is thus distinguished from the command interpreters of other systemsboth by the fact that it is just a user program,and by the fact that it is used almost exclusively as a mecha-nism for invoking other programs.Commands in theUNIXsystem consist of a list of strings or words interpreted

13、 as a command namefollowed by arguments.Thus the commandmail billconsists of two words.The first word mail names the command to be executed,in this case the mail pro-gram which sends messages to other users.The shell uses the name of the command in attempting toexecute it for you.It will look in a n

14、umber of directories for a file with the name mail which is expected tocontain the mail program.The rest of the words of the command are given as arguments to the command itself when it isexecuted.In this case we specified also the argument bill which is interpreted by the mail program to bethe name

15、 of a user to whom mail is to be sent.In normal terminal usage we might use the mail commandas follows.%mail billI hav e a question about the csh documentation.My document seems to be missing page 5.Does a page five exist?BillEOT%Here we typed a message to send to bill and ended this message with a

16、D which sent an end-of-fileto the mail program.(Here and throughout this document,the notation x is to be read control-x andrepresents the striking of the x key while the control key is held down.)The mail program then echoed thecharacters EOT and transmitted our message.The characters%were printed

17、before and after the mailcommand by the shell to indicate that input was needed.After typing the%prompt the shell was reading command input from our terminal.We typed acomplete command mail bill.The shell then executed the mail program with argument bill and wentdormant waiting for it to complete.Th

18、e mail program then read input from our terminal until we signalledan end-of-file via typing a D after which the shell noticed that mail had completed and signaled us that itwas ready to read from the terminal again by printing another%prompt.This is the essential pattern of all interaction withUNIX

19、through the shell.A complete command istyped at the terminal,the shell executes the command and when this execution completes,it prompts for anew command.If you run the editor for an hour,the shell will patiently wait for you to finish editing andobediently prompt you again whenever you finish editi

20、ng.An example of a useful command you can execute now is the tset command,which sets the defaulterase and kill characters on your terminal the erase character erases the last character you typed and thekill character erases the entire line you have entered so far.By default,the erase character is th

21、e delete key(equivalent to?)and the kill character is U.Some people prefer to make the erase character thebackspace key(equivalent to H).You can make this be true by typingtset ewhich tells the program tset to set the erase character to tsets default setting for this character(abackspace).USD:4-4 An

22、 Introduction to the C shell1.2.Flag argumentsA useful notion inUNIXis that of a flag argument.While many arguments to commands specify filenames or user names,some arguments rather specify an optional capability of the command which youwish to invoke.By convention,such arguments begin with the char

23、acter (hyphen).Thus the commandlswill produce a list of the files in the current working directory.The option s is the size option,andls scauses ls to also give,for each file the size of the file in blocks of 512 characters.The manual section foreach command in theUNIXreference manual gives the avai

24、lable options for each command.The ls com-mand has a large number of useful and interesting options.Most other commands have either no options oronly one or two options.It is hard to remember options of commands which are not used very frequently,so mostUNIXutilities perform only one or two function

25、s rather than having a large number of hard toremember options.1.3.Output to filesCommands that normally read input or write output on the terminal can also be executed with thisinput and/or output done to a file.Thus suppose we wish to save the current date in a file called now.The commanddatewill

26、print the current date on our terminal.This is because our terminal is the default standard output forthe date command and the date command prints the date on its standard output.The shell lets us redirectthe standard output of a command through a notation using the metacharacter and the name of the

27、 filewhere output is to be placed.Thus the commanddate nowruns the date command such that its standard output is the file now rather than the terminal.Thus thiscommand places the current date and time into the file now.It is important to know that the date com-mand was unaware that its output was go

28、ing to a file rather than to the terminal.The shell performed thisredirection before the command began executing.One other thing to note here is that the file now need not have existed before the date commandwas executed;the shell would have created the file if it did not exist.And if the file did e

29、xist?If it hadexisted previously these previous contents would have been discarded!A shell option noclobber exists toprevent this from happening accidentally;it is discussed in section 2.2.The system normally keeps files which you create with and all other files.Thus the default is forfiles to be pe

30、rmanent.If you wish to create a file which will be removed automatically,you can begin itsname with a#character,this scratch character denotes the fact that the file will be a scratch file.*Thesystem will remove such files after a couple of days,or sooner if file space becomes very tight.Thus,inrunn

31、ing the date command above,we dont really want to save the output forever,so we would more likelydodate#now*Note that if your erase character is a#,you will have to precede the#with a.The fact that the#character is theold(pre-CRT)standard erase character means that it seldom appears in a file name,a

32、nd allows this convention to be used forscratch files.If you are using aCRT,your erase character should be a H,as we demonstrated in section 1.1 how this couldbe set up.An Introduction to the C shellUSD:4-51.4.Metacharacters in the shellThe shell has a large number of special characters(like)which i

33、ndicate special functions.We saythat these notations have syntactic and semantic meaning to the shell.In general,most characters whichare neither letters nor digits have special meaning to the shell.We shall shortly learn a means of quotationwhich allows us to use metacharacters without the shell tr

34、eating them in any special way.Metacharacters normally have effect only when the shell is reading our input.We need not worryabout placing shell metacharacters in a letter we are sending via mail,or when we are typing in text or datato some other program.Note that the shell is only reading input whe

35、n it has prompted with%(althoughwe can type our input even before it prompts).1.5.Input from files;pipelinesWe learned above how to redirect the standard output of a command to a file.It is also possible toredirect the standard input of a command from a file.This is not often necessary since most co

36、mmandswill read from a file whose name is given as an argument.We can give the commandsort datato run the sort command with standard input,where the command normally reads its input,from the filedata.We would more likely saysort dataletting the sort command open the file data for input itself since

37、this is less to type.We should note that if we just typedsortthen the sort program would sort lines from its standard input.Since we did not redirect the standardinput,it would sort lines as we typed them on the terminal until we typed a D to indicate an end-of-file.A most useful capability is the a

38、bility to combine the standard output of one command with the stan-dard input of another,i.e.to run the commands in a sequence known as a pipeline.For instance the com-mandls snormally produces a list of the files in our directory with the size of each in blocks of 512 characters.If weare interested

39、 in learning which of our files is largest we may wish to have this sorted by size rather than byname,which is the default way in which ls sorts.We could look at the many options of ls to see if therewas an option to do this but would eventually discover that there is not.Instead we can use a couple

40、 of sim-ple options of the sort command,combining it with ls to get what we want.The n option of sort specifies a numeric sort rather than an alphabetic sort.Thusls s|sort nspecifies that the output of the ls command run with the option s is to be piped to the command sort runwith the numeric sort o

41、ption.This would give us a sorted list of our files by size,but with the smallest first.We could then use the r reverse sort option and the head command in combination with the previouscommand doingls s|sort n r|head 5Here we have taken a list of our files sorted alphabetically,each with the size in

42、 blocks.We hav e run this tothe standard input of the sort command asking it to sort numerically in reverse order(largest first).Thisoutput has then been run into the command head which gives us the first few lines.In this case we haveasked head for the first 5 lines.Thus this command gives us the n

43、ames and sizes of our 5 largest files.The notation introduced above is called the pipe mechanism.Commands separated by|charactersare connected together by the shell and the standard output of each is run into the standard input of the next.The leftmost command in a pipeline will normally take its st

44、andard input from the terminal and theUSD:4-6 An Introduction to the C shellrightmost will place its standard output on the terminal.Other examples of pipelines will be given laterwhen we discuss the history mechanism;one important use of pipes which is illustrated there is in the rout-ing of inform

45、ation to the line printer.1.6.FilenamesMany commands to be executed will need the names of files as arguments.UNIXpathnames consistof a number of components separated by/.Each component except the last names a directory in whichthe next component resides,in effect specifying the path of directories

46、to follow to reach the file.Thus thepathname/etc/motdspecifies a file in the directory etc which is a subdirectory of the root directory/.Within this directorythe file named is motd which stands for message of the day.A pathname that begins with a slash is saidto be an absolute pathname since it is

47、specified from the absolute top of the entire directory hierarchy ofthe system(the root).Pathnames which do not begin with/are interpreted as starting in the currentworking directory,which is,by default,your home directory and can be changed dynamically by the cdchange directory command.Such pathnam

48、es are said to be relative to the working directory since they arefound by starting in the working directory and descending to lower levels of directories for each componentof the pathname.If the pathname contains no slashes at all then the file is contained in the working direc-tory itself and the

49、pathname is merely the name of the file in this directory.Absolute pathnames have norelation to the working directory.Most filenames consist of a number of alphanumeric characters and.s(periods).In fact,all printingcharacters except/(slash)may appear in filenames.It is inconvenient to have most non-

50、alphabetic char-acters in filenames because many of these have special meaning to the shell.The character.(period)isnot a shell-metacharacter and is often used to separate the extension of a file name from the base of thename.Thusprog.c prog.o prog.errs prog.outputare four related files.They share a

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁