前言在你使用UNIX之前,你必须在workstation上有account(帐号),你才能进去使用。
在login:出现後,打入帐号名称,然後输入密码,现在你已经完成login的动作,进入UNIX了。
在进入之後,系统会启动"shell",这个程式会接受你打的指令,去执行它,shell有好几种,有csh tcsh sh bash ksh...,我主要指的是csh。
基本上,我们待会谈的指令都是在shell下输入的。
当你输入set指令,你会看到一些变数,来看几个重要的:home:家。
Home是你拥有的地方,你可以在这个目录下放置任何东东。
你一进入系统,一定是先到这里的。
path:路径。
通常执行档是放在不同的地方,系统会顺著path中的目录一个个去找执行档。
term:终端机。
表示现在的终端机形态,常用vt100、vt102、ansi。
set<name>=xxxx就可以设定变数的值,$<name>代表这个变数。
<eg>%set term=vt100%set path=($home/bin$path)%echo$termvt100%□档案系统介绍UNIX的档案系统呈树状结构,包含了目录(Directory)和档案(file)。
如果说你以前有接触过DOS,那应该不陌生;如果没有,那你应该仔细瞧瞧。
一、目录(Directory)基本上,目录是用来分类档案的。
一个目录下面可以包含许多档案,或是许多目录,而这些下层的目录就叫″子目录″(subdirectory),它们也都具有相同性质。
由这些目录所构成的一层层分枝,看起来就像一棵树,所以才叫树状结构,而最上面的目录称作″根目录″。
一个目录在建立的时候,具有两个基本项目:″.”和″..”。
”.”指的是目录本身,而″..”指的是上一层的目录,这两个是一直跟著目录共存亡的。
这是档案系统简图,很像一棵树吧!┌Joe.....│┌Mail┬joe││├Mary┌—users┼Tom┤└Eric││└dead.letter│└Mary┬Mail.....│└..../┼—bin┬ls│├mail│├rm│└cp└—tmp<eg>%cd/%pwd/%cd bin%pwd/bin%cd..%pwd/%二、档案(file)档案是储存资料的地方,这个资料可能是一份文字档,也可能是一个可执行的程式,不同功能的档案,有著不同的属性,现在来看看。
%ls-ltotal1059drwx------4jong512Sep614:54Maildrwxr-xr-x2jong512Oct2219:02Newslrwxrwxrwx1jong5Oct2916:05bin->gamesdrwxr-xr-x2jong512Oct2916:05games-rw-------1jong9915Sep614:55mboxdrwxr-xr-x2jong512Oct110:06ok-rw-r--r--1jong107171Apr141993soccer.zipdrwxr-xr-x4jong1024Oct2720:54xtype-r-xr-xr-x3jong925696Aug2814:48xv在每个档案前有十个字,第一个字元是档案形式,d指目录,-指档案,l指连结(link)档案。
从二到十是属性,三字一组,一共三组。
一组三字代表<读、写、执行>,即r w x,-则表无此属性。
第一组是owner,第二组是group,第三组是others。
<eg>rw-------只有owner可读写r-xr-xr-x可读、执行,不可写rw-r--r--owner可读写,其他只可读档案的名字除了"/"之外,都可以用,随便多少都可以。
档案开头是"."的档案是隐藏档,平常看不到,要用ls-a或ls-al来看。
□输出入的重置(redirection)与管道(pipe)输出和输入虽然是硬体设备,但UNIX却把它们视为档案,既然是档案,就不需要知道到底是什麽,可能是keyboard、monitor,也可能是磁碟上的一个档。
一般说来,标准输出(standard input)是monitor,标准输入(standard output)是keyboard,但我们能够把它们转向到别的档案,也就是从档案输入资料,或是输出资料到某个档。
">"用来转向output,"<"用来转向input,">>"则是将结果附加在档案後面。
<eg>%echo TEST>test.1%echo test>>test.1%cat test.1TESTtest%另外还有一种是标准错误输出(standard error),用来显示错误讯息,如果output被转向,我们还是可以立刻在萤幕上看到错误讯息。
">&"可以把它转向,">>&"则是附加在档案後面。
再介绍管道的观念。
"|"是管道的符号,它的性质就像一根管子,用来连接左右两边的输出和输入。
左边指令的output,会当作右边指令的input,如此一来,我们就可以对output做筛选的工作,只留下想要的资料。
<eq>%ps-aux|grep<name>就可以看到<name>所有正在执行的程式。
□行程(process)一个正在执行中的程式,我们称做process。
UNIX是多工的系统(multitasking),可以同时执行多个程式,让好几个人同时使用系统,执行程式。
执行程式可分为两种:前景执行(foreground)和背景执行(background)。
一般我们在shell下执行程式,一直要等到程式结束才能继续输入指令,这就是前景执行。
如果说我们不想等待程式结束,或是执行时间太长,就可以将程式放到背景去执行,系统会自动执行它,而你就可以去做别的事了。
怎样才能把程式放入背景?只要在指令後面加上"&"就可以了。
一但你在背景执行程式,系统会传回"[n]xxxx",n是job id,xxxx是process id。
每一个process都有process id,而且不会有重覆。
要想停止前景执行的程式,可以按<Ctrl-C>中止,若是想暂停一下,可以按<Ctrl-Z>暂时停止执行,要恢复执行可以用fg或bg来决定前景或背景执行。
<eq>%xtetris&[1]5678%□万用字元(wildcard)在UNIX中,万用字元对输入档名非常有帮助。
现在想看一下有哪些指令可用,你先打入%cd/bin%ls-al你会发现好多执行档,如果你只想找"m"开头的档案,可以键入%ls m*就可以做到。
万用字元有三种:*代表任意长度的字串?代表任何的单一字元[...]代表区间内定义的任一字元<eq>c*代表cat,cp,cal...任意长度且开头为c。
ca?代表cat,cal...三个字且前二字为ca。
ca[a-z]代表caa,cab,caz...三个字且头二字为ca第三字为a到z。
附录A指令介绍yppasswd change your passwordman<name>see the manual page of<name>who who is logged in on the systemwhoami display the effective current usernamew who is logged in,and what are they doinggroups<username>show the user's group namequota-v<username>show the quota limited on userdu disply disk usagepwd print working directory namecd<dirname>change directory to another directorycd change directory to your home directoryls list the contents of a directory options:-a list all entries-l list in long format-g show the group ownership-R recursively list subdirectories encoutered-F mark directories with'/',executable fileswith'*'<eg>ls-alls-lFcat<filename>catenate and display a filemore<filename>display a file page by pageecho display a string<eg>%echo Hello WorldHello World%cp<file1><file2>copy filesrm<filename>remove a filerm-r<dirname>recursively remove a directorymv<filename><newname>rename a file(directory)mv<filename><dirname>move a file to the directorymkdir<dirname>creat a new directoryrmdir<dirname>remove an empty directoryln<filename>link a fileoption:-s symbolic linkchmod<Mode><filename>change the permission mode of file absolute:400read by owner200write by owner100execute by owner040read by group020write by group010execute by group004read by others002write by others001execute by otherssymbolic:u user(owner)g groupo othersa allr read accessw write accessx execute access<eg>chmod777test.1chmod a+r,a+x,a+w test.1sort<filename>sort a filetalk<username>[@<hostname>]talk with othermesg[y/n]allow others to talk to you history list your used commands jobs list of background jobskill terminate process <eg>%kill-9-1//kill all processes%kill-91234//kill process#1234ps listing your process <eg>%psPID TTY TIME COMMAND3211p00:00-(csh)1568p00:00ps3456p00:01vi/tmp/snd.34763455p00:01elm%kill-93456%psPID TTY TIME COMMAND3211p00:00-(csh)1570p00:00ps3455p00:01elmusing floppy:mdir list msdos diskmcopy a:dosfile unixfile transfer files from/to floppymcopy unixfile a:dosfilemdel a:dosfile delete floppy fileseject eject floppy files disk附录BA very brief look at Unix historyUnix history goes back to1969and the famous"little-used PDP-7in a corner"on which Ken Thompson,Dennis Ritchie(the R in K&R) and others started work on what was to become Unix.The name"Unix"was intended as a pun on Multics(and was written"Unics"at first--UNiplexed Information and Computing System).For the first10years,Unix development was essentially confinedto Bell Labs.These initial versions were labeled"Version n"or"Nth Edition"(of the manuals),and were for DEC's PDP-11(16bits)and later VAXen(32bits).Some significant versionsinclude:V1(1971):1st Unix version,in assembler on a PDP-11/20.Included file system,fork(),roff,ed.Was used as a textprocessing tool for preparation of patents.Pipe()appearedfirst in V2!V4(1973):Rewritten in C,which is probably the mostsignificant event in this OS's history:it means Unix can beported to a new hardware in months,and changes are easy.TheC language was originally designed for the Unix operatingsystem,and hence there is a strong synergy between C and Unix.V6(1975):First version of Unix widely available outsideBell Labs(esp.in universities).This was also the start ofUnix diversity and popularity. 1.xBSD(PDP-11)was derivedfrom this version.J.Lions published"A commentary on theUnix Operating System"based on V6.V7(1979):For many,this is the"last true Unix",an"improvement over all preceding and following Unices"Bourne.It included full K&R C,uucp,Bourne shell.V7wasported to the VAX as32V.The V7kernel was a mere40Kbytes!These Vn versions were developed by the Computer Research Group(CRG)of Bell Labs.Another group,the Unix System Group(USG),was responsible for support.A third group at Bell Labs was alsoinvolved in Unix development,the Programmer's WorkBench(PWB),to which we owe,for example,sccs,named pipes and otherimportant ideas.Both groups were merged into Unix SystemDevelopment Lab in1983.Work on Unix continued at Bell Labs in the1980s.The V serieswas further developed by the CRG(Stroustrup mentions V10in the2nd edition of his book on C++),but we don't seem to hear muchabout this otherwise.The company now responsible for Unix(System V)is called Unix System Laboratories(USL)and ismajority-owned by AT&T.Novell has bought USL(early93)?!But much happened to Unix outside AT&T,especially at Berkeley(where the other major flavor comes from).Vendors(esp.ofworkstations)also contributed much(e.g.Sun's NFS).The book"Life with Unix"by Don Libes and Sandy Ressler isfascinating reading for anyone interested in Unix,and covers alot of the history,interactions,etc..Much in the presentsection is summarized from this book.三.系统启动流程:系统启动完成如下工作:.机器本身所进行的必要的有限的自检.查找一个可引导设备.从引导设备上读取内核.找到内核并初始化外设.启动基本系统任务.运行那些能提供服务的shell script.其他应用程序开始在系统启动过程中,绝大多数启动信息将同时写到主控台设备和系统日志文件,系统日志文件可以参考系统文档syslogd和配置文件/etc/syslog.conf, syslog.conf文件的最后一行为系统日志文件的位置.1.初始化过程内核:/vmunix,/unix或/boot/zImage,位于/文件系统,kernel执行后运行init,init将会创建一些正常操作所需要的子进程.这些操作包括设备主机名,检查相应的磁盘并加载文件系统,启动系统日志,配置网络接口,启动网络和邮件服务,清除临时分区,存储core dump,要理解这些功能是如何实现的,需要更深入的了解父进程是如何操作的,SYSV/BSD对这一父进程称之为init,但其各自的调用方式却差别很大..配置文件linux使用一个非常灵活的init进程,init会直接建立在/etc/inittab中所列出的任务,init通用运行级别如下:0shutdown1系统管理方式,所有的文件可以继续访问,但只有超级用户可以从主控台访问系统2系统进入正常多用户操作方式3多用户操作方式,但同时开启远程文件共享,启动一些额外的进程以允许远程文件共享,加载远程资源,同时向网上公布其远程资源(如nfs)4用户自定义多用户环境5自定义多用户环境,不同unix之间有所差别,如有些unix这试图关机并关闭电源6关闭机器后重新启动到运行级别2或3,不同unix不完全相同S/s单用户方式,通常在inittab丢失或不对时使用此方式Q/q不改变运行状态,重新检查inittab,不重启机器而开始新的改动.示列文件/etc/inittab##inittab This file describes how the INIT process should set up#the system in a certain run-level.##Author:Miquel van Smoorenburg,<miquels@>#Modified for RHS Linux by Marc Ewing and Donnie Barnes##Default runlevel.The runlevels used by RHS are:#0-halt(Do NOT set initdefault to this)#1-Single user mode#2-Multiuser,without NFS(The same as3,if you do not have networking)#3-Full multiuser mode#4-unused#5-X11#6-reboot(Do NOT set initdefault to this)#id:3:initdefault:#System initialization.si::sysinit:/etc/rc.d/rc.sysinitl0:0:wait:/etc/rc.d/rc0l1:1:wait:/etc/rc.d/rc1l2:2:wait:/etc/rc.d/rc2l3:3:wait:/etc/rc.d/rc3l4:4:wait:/etc/rc.d/rc4l5:5:wait:/etc/rc.d/rc5l6:6:wait:/etc/rc.d/rc6#Things to run in every runlevel.ud::once:/sbin/update#Trap CTRL-ALT-DELETEca::ctrlaltdel:/sbin/shutdown-t3-r now#When our UPS tells us power has failed,assume we have a few minutes#of power left.Schedule a shutdown for2minutes from now.#This does,of course,assume you have powerd installed and your#UPS connected and working correctly.pf::powerfail:/sbin/shutdown-f-h+2"Power Failure;System Shutting Down"#If power was restored before the shutdown kicked in,cancel it.pr:12345:powerokwait:/sbin/shutdown-c"Power Restored;Shutdown Cancelled"#Run gettys in standard runlevels1:12345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty23:2345:respawn:/sbin/mingetty tty34:2345:respawn:/sbin/mingetty tty45:2345:respawn:/sbin/mingetty tty56:2345:respawn:/sbin/mingetty tty6#Run xdm in runlevel5#x:5:respawn:/usr/bin/X11/xdm-nodaemonx:3:respawn:/usr/bin/env KDEDIR=/opt/kde/opt/kde/bin/kdm-nodaemon文件每一行格式如下:identifier:runlevel:action-keyword:processidentifier:最长4个字符,唯一标识一项run-level:为空表示所有级别,默认运行级别通常为2或3action-keyword:init所采取的动作。