📄 csh.2
字号:
.\" Copyright (c) 1980, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)csh.2 8.1 (Berkeley) 6/8/93.\".nr H1 1.NHDetails on the shell for terminal users.NH 2Shell startup and termination.PPWhen you login, the shell is started by the system in your.I homedirectory and begins by reading commands from a file.I \&.cshrcin this directory.All shells which you may start during your terminal session willread from this file.We will later see what kinds of commands are usefully placed there.For now we need not have this file and the shell does not complain aboutits absence..PPA .I "login shell" ,executed after you login to the system,will, after it reads commands from.I \&.cshrc,read commands from a file.I \&.loginalso in your home directory.This file contains commands which you wish to do each time you loginto the \s-2UNIX\s0 system.My.I \&.loginfile looks something like:.DSset ignoreeofset mail=(/usr/spool/mail/bill)echo "${prompt}users" ; usersalias ts \e \'set noglob ; eval \`tset \-s \-m dialup:c100rv4pna \-m plugboard:?hp2621nl \!*\`\';ts; stty intr ^C kill ^U crtset time=15 history=10msgs \-fif (\-e $mail) then echo "${prompt}mail" mailendif.DE.PPThis file contains several commands to be executed by \s-2UNIX\s0each time I login.The first is a.I setcommand which is interpreted directly by the shell. It sets the shellvariable.I ignoreeofwhich causes the shell to not log me off if I hit ^D. Rather,I use the.I logoutcommand to log off of the system.By setting the.I mailvariable, I ask the shell to watch for incoming mail to me. Every 5 minutesthe shell looks for this file and tells me if more mail has arrived there.An alternative to this is to put the command.DSbiff y.DEin place of this.I set;this will cause me to be notified immediately when mail arrives, and tobe shown the first few lines of the new message..PPNext I set the shell variable `time' to `15' causing the shell to automaticallyprint out statistics lines for commands which execute for at least 15 secondsof \s-2CPU\s+2 time. The variable `history' is set to 10 indicating thatI want the shell to remember the last 10 commands I type in its.I "history list" ,(described later)..PPI create an.I alias``ts'' which executes a\fItset\fR\|(1) command setting up the modes of the terminal.The parameters to.I tsetindicate the kinds of terminal which I usually use when not on a hardwiredport. I then execute ``ts'' and also use the.I sttycommand to change the interrupt character to ^C and the line killcharacter to ^U..PPI then run the `msgs' program, which provides me with anysystem messages which I have not seen before; the `\-f' option here preventsit from telling me anything if there are no new messages.Finally, if my mailbox file exists, then I run the `mail' program toprocess my mail..PPWhen the `mail' and `msgs' programs finish, the shell will finishprocessing my.I \&.loginfile and begin reading commands from the terminal, prompting for each with`% '.When I log off (by giving the .I logoutcommand) the shellwill print `logout' and execute commands from the file `.logout'if it exists in my home directory.After that the shell will terminate and \s-2UNIX\s0 will logme off the system.If the system is not going down, I will receive a new login message.In any case, after the `logout' message the shell is committed to terminatingand will take no further input from my terminal..NH 2Shell variables.PPThe shell maintains a set of.I variables.We saw above the variables.I historyand.I timewhich had values `10' and `15'.In fact, each shell variable has as value an array ofzero or more.I strings.Shell variables may be assigned values by the set command. It hasseveral forms, the most useful of which was given above and is.DSset name=value.DE.PPShell variables may be used to store values which are tobe used in commands later through a substitution mechanism.The shell variables most commonly referenced are, however, those which theshell itself refers to.By changing the values of these variables one can directly affect thebehavior of the shell..PPOne of the most important variables is the variable.I path.This variable contains a sequence of directory names where the shellsearches for commands.The.I setcommand with no argumentsshows the value of all variables currently defined (we usually say.I set)in the shell.The default value for path will be shown by.I setto be.DS% set.ta .75iargv ()cwd /usr/billhome /usr/billpath (. /usr/ucb /bin /usr/bin)prompt %shell /bin/cshstatus 0term c100rv4pnauser bill%.so tabs.DEThis output indicates that the variable path points to the currentdirectory `.' and then `/usr/ucb', `/bin' and `/usr/bin'.Commands which you may write might be in `.' (usually one ofyour directories).Commands developed at Berkeley, live in `/usr/ucb'while commands developed at Bell Laboratories live in `/bin' and `/usr/bin'..PPA number of locally developed programs on the system live in the directory`/usr/local'.If we wish that all shells which we invoke to haveaccess to these new programs we can place the command.DSset path=(. /usr/ucb /bin /usr/bin /usr/local).DEin our file.I \&.cshrcin our home directory.Try doing this and then logging out and back in and do.DSset.DEagain to see that the value assigned to.I pathhas changed..FS \(dgAnother directory that might interest you is /usr/new, which containsmany useful user-contributed programs provided with Berkeley Unix..FE.PPOne thing you should be aware of is that the shell examines each directorywhich you insert into your path and determines which commands are containedthere. Except for the current directory `.', which the shell treats specially,this means that if commands are added to a directory in your search path afteryou have started the shell, they will not necessarily be found by the shell.If you wish to use a command which has been added in this way, you shouldgive the command.DSrehash.DEto the shell, which will cause it to recompute its internal table of commandlocations, so that it will find the newly added command.Since the shell has to look in the current directory `.' on each command,placing it at the end of the path specification usually works equivalentlyand reduces overhead..PPOther useful built in variables are the variable.I homewhich shows your home directory,.I cwdwhich contains your current working directory,the variable.I ignoreeofwhich can be set in your.I \&.loginfile to tell the shell not to exit when it receives an end-of-file froma terminal (as described above).The variable `ignoreeof'is one of several variables which the shell does not care about thevalue of, only whether they are.I setor.I unset.Thus to set this variable you simply do.DSset ignoreeof.DEand to unset it do.DSunset ignoreeof.DEThese give the variable `ignoreeof' no value, but none is desired or required..PPFinally, some other built-in shell variables of use are thevariables.I noclobberand.I mail.The metasyntax.DS> filename.DEwhich redirects the standard output of a commandwill overwrite and destroy the previous contents of the named file.In this way you may accidentally overwrite a file which is valuable.If you would prefer that the shell not overwrite files in thisway you can.DSset noclobber.DEin your.I \&.loginfile.Then trying to do.DSdate > now.DEwould cause a diagnostic if `now' existed already.You could type.DSdate >! now.DEif you really wanted to overwrite the contents of `now'.The `>!' is a special metasyntax indicating that clobbering thefile is ok.\(dg.FS\(dgThe space between the `!' and the word `now' is critical here, as `!now'would be an invocation of the.I historymechanism, and have a totally different effect..FE.NH 2The shell's history list.PPThe shell can maintain a.I "history list"into which it places the wordsof previous commands.It is possible to use a notation to reuse commands or wordsfrom commands in forming new commands.This mechanism can be used to repeat previous commands or tocorrect minor typing mistakes in commands..PPThe following figure gives a sample session involving typical usage of thehistory mechanism of the shell..KF.DS% cat bug.cmain(){ printf("hello);}% cc !$cc bug.c "bug.c", line 4: newline in string or char constant"bug.c", line 5: syntax error% ed !$ed bug.c 294s/);/"&/p printf("hello");w30q% !ccc bug.c % a.outhello% !eed bug.c 304s/lo/lo\e\en/p printf("hello\en");w32q% !c \-o bugcc bug.c \-o bug% size a.out buga.out: 2784+364+1028 = 4176b = 0x1050bbug: 2784+364+1028 = 4176b = 0x1050b% ls \-l !*ls \-l a.out bug \(mirwxr\(mixr\(mix 1 bill 3932 Dec 19 09:41 a.out\(mirwxr\(mixr\(mix 1 bill 3932 Dec 19 09:42 bug% bughello% num bug.c | sppspp: Command not found.% ^spp^sspnum bug.c | ssp 1 main() 3 { 4 printf("hello\en"); 5 }% !! | lprnum bug.c | ssp | lpr% .DE.KEIn this example we have a very simple C program which has a bug (or two)in it in the file `bug.c', which we `cat' out on our terminal. We thentry to run the C compiler on it, referring to the file again as `!$',meaning the last argument to the previous command. Here the `!' is thehistory mechanism invocation metacharacter, and the `$' stands for the lastargument, by analogy to `$' in the editor which stands for the end of the line.The shell echoed the command, as it would have been typed without use ofthe history mechanism, and then executed it.The compilation yielded error diagnostics so we now run the editor on thefile we were trying to compile, fix the bug, and run the C compiler again,this time referring to this command simply as `!c', which repeats the lastcommand which started with the letter `c'. If there were othercommands starting with `c' done recently we could have said `!cc' or even`!cc:p' which would have printed the last command starting with `cc'without executing it..PPAfter this recompilation, we ran the resulting `a.out' file, and thennoting that there still was a bug, ran the editor again. After fixingthe program we ran the C compiler again, but tacked onto the commandan extra `\-o bug' telling the compiler to place the resultant binary inthe file `bug' rather than `a.out'. In general, the history mechanismsmay be used anywhere in the formation of new commands and other charactersmay be placed before and after the substituted commands..PPWe then ran the `size' command to see how large the binary program imageswe have created were, and then an `ls \-l' command with the same argumentlist, denoting the argument list `\!*'.Finally we ran the program `bug' to see that its output is indeed correct..PPTo make a numbered listing of the program we ran the `num' command on the file `bug.c'.In order to compress out blank lines in the output of `num' we ran theoutput through the filter `ssp', but misspelled it as spp. To correct thiswe used a shell substitute, placing the old text and new text between `^'characters. This is similar to the substitute command in the editor.Finally, we repeated the same command with `!!', but sent its output to theline printer..PPThere are other mechanisms available for repeating commands. The.I historycommand prints out a number of previous commands with numbers by whichthey can be referenced. There is a way to refer to a previous commandby searching for a string which appeared in it, and there are other,less useful, ways to select arguments to include in a new command.A complete description of all these mechanismsis given in the C shell manual pages in the \s-2UNIX\s0 Programmer's Manual..NH 2Aliases.PPThe shell has an.I aliasmechanism which can be used to make transformations on input commands.This mechanism can be used to simplify the commands you type,to supply default arguments to commands,or to perform transformations on commands and their arguments.The alias facility is similar to a macro facility.Some of the features obtained by aliasing can be obtained alsousing shell command files, but these take place in another instanceof the shell and cannot directly affect the current shells environmentor involve commands such as.I cdwhich must be done in the current shell.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -