北邮linux编程Chapter12_slides
- 格式:pdf
- 大小:221.46 KB
- 文档页数:14
Chapter 6 Basic Shell KnowledgeIn this chapter, we will learn :- The function of shell- The procedure when some one log in- User Environment variables- Shell variable- Specific environment variables- Set up user environmentShell :Shell : interactive program that serves as a command line interpreter.Separate from OSIt makes users to select user interface flexible.Shell allow you type command as your definition, perform several functions. It pass the interpreted command to the OS ( kernel ) for execution.Graph :Hardware -> OS , Kernel -> Shell -> UsersCommand executionEnvironment settingsVariable settingsVariable substitutionCommand substitutionFilename generationI/O redirectionPipelinesInterpretive programming languageOur chapter describe interactive features that are provided by POSIX shell (bash).The following summarizes the shell functionality :It searches for a command and executes the associated programIt substitutes shell variable valuesIt completes file name from file name generation charactersIt handles I/O redirection and pipelinesIt provides an interpreted programming interface, including tests, branched and loopsWhen you log in a Linux system, shell will define certain characteristics for your terminal session, and issue prompt. The default prompt is $ ( bash Shell ), the default prompt of C shell is $ .Examples:$ whereis shsh: /bin/sh /usr/share/man/man1/sh.1.gz$ whereis bashbash: /bin/bash /usr/lib/bash /usr/share/man/man1/bash.1.gz$ ll /bin/shlrwxrwxrwx 1 root root 4 10ÔÂ 13 12:24 /bin/sh -> bashStandard Shell is set to bash$ whereis cshcsh: /bin/csh /etc/csh.cshrc /etc/csh.login /usr/share/man/man1/csh.1.gz $ ll /bin/cshlrwxrwxrwx 1 root root 4 10ÔÂ 13 12:26 /bin/csh -> tcsh$ whereis tcshtcsh: /bin/tcsh /usr/share/man/man1/tcsh.1.gz$ whereis bshbsh: /bin/bsh /usr/share/man/man1/bsh.1.gz$ ll /bin/bshlrwxrwxrwx 1 root root 3 10ÔÂ 13 12:24 /bin/bsh -> ashSo,/bin/sh POSIX shell/bin/bash bash/bin/csh csh/bin/tcsh tcshPOSIX shell is a command programming language and commands interpreter. It execute commands read from a terminal or a file.It contains a history mechanism, supports job control, and provides various other useful features.The bsh is a command programming language and commands interpreter. It execute commands read from a terminal or a file. It lacks many features contained in the bash. B shell was developed by Stephen R.Bourne and was the original shell available on the AT&T releases of UNIX.C shell is a command language interpreter that incorporates a command history buffer, C-language-like syntax, and job control facilities. It was developed by William Joy of the University of California at Berkeley.POSIX Shell FeaturesShell user interface with some advanced features :Command aliasingFile name completionCommand history mechanismJob controlEnhanced cd capabilitiesAdvanced programming capabilitiesThis shell has many features that B shell does not have. Even if you do not use all of the advanced features, you will probably find the POSIX shell is a very convenient user interface.AliasingSyntax :alias [-p] [name[=value] ...]Alias with no arguments or with the -p option prints the list ofaliases in the form alias name=value on standard output. Whenarguments are supplied, an alias is defined for each name whosevalue is given. For each name in the argument list for which no value is sup-plied, the name and value of the alias is printed.Examples :$ aliasalias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'By default, color is not used to distinguish types of files. That is equivalent to using -- color=none. Using the -- color option without the optional WHEN argument is equivalent to using --color=always. With - -color=auto, color codes are output only if standard output is connected to a terminal ( tty ).$ alias dir=ls$ aliasalias dir='ls'alias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'$ alias mroe=more$ alias run.exe=/home/yd211/bin/run.exe$ aliasalias dir='ls'alias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias mroe='more'alias run.exe='/home/yd211/bin/run.exe'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'$ alias # displays aliases currently defined$ alias dir # displays value of alias diralias dir='ls'Aliasing is a method by which you can abbreviate long command lines, create new commands, or cause standard commands to perform differently by replacing the original command with a new command called an alias. The alias can be a letter or short word. Such as, “ps –ef ” command is quite often used. You could create aliases using the alias command.$ alias psf = ‘ps –ef’$ aliasalias dir='ls'alias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias mroe='more'alias psf='ps -ef'alias run.exe='/home/yd211/bin/run.exe'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'$ alias name = stringname is the name you are using for the alias, string is the command or character string that name is aliased to. If the string contains spaces, pls enclose the whole string in quotes. The alias is convenient to save typing, generate new commands.One find that this feature is so flexible that they make their UNIX system interface just like another OS, which they often use.Examples :$alias dir=ls$alias copy=’cp –i ’unalias :unalias [-a] [name ...]Remove each name from the list of defined aliases. If -a issupplied, all alias definitions are removed.Examples :$ aliasalias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias psf='ps -ef'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' $ unalias ll$ aliasalias l.='ls -d .* --color=tty'alias ls='ls --color=tty'alias psf='ps -ef'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' $ unalias –a$ alias # nothingExamples :Several aliases can also be entered on a single command line as shown below :$ alias go=‘cd ’$ alias my_dir=/home/yd211$ go my_dir$ pwd/home/yd211In order to reference more than one alias on a line, you must leave a space as the last character in the alias definition; otherwise, the shell will not recognize the next word as an alias.File Name completion$ lsfile file1 file2 file_stock bigfile$ more file[Tab][Tab] = [ESC] [ESC]File name completion is convenient when you want to access a file that has a long file name. Enough characters has to be provided to uniquely identify the file name. If the string is not unique, the shell will list the candidate that you could choose. Terminal will beep when it runs into a file name conflict.The shell will complete the file name as far as it can without a conflict.The File name completion can be used anywhere. On command line .Examples : …..Command HistoryShell keeps a history file of commands that you enteredThe history command displays the last 16 commandsSyntax :History nExample :$ history987 ls CALLHOME_MAN_1/988 ls CALLHOME_MAN_1/callhome/989 ls CALLHOME_MAN_1/callhome/doc/990 ls CALLHOME_MAN_1/callhome/mandarin/991 ls CALLHOME_MAN_1/992 ls CALLHOME_MAN_1/shorten/993 ls CALLHOME_MAN_1/sphere994 pwd995 ls996 cd ..997 ls998 ls mandarin/999 ls mandarin/callhome_mandarin_1000 ls mandarin/callhome_mandarin_lexicon_970831/ 1001 ls1002 history$ history 2 #list last 2 commands1002 history1003 history 2Bash keeps a history file that stores the commands you enter, allows you to re-enter them. The history file is maintained across login sessions.The history command display last 16 commands you have entered, each line is preceded with a command number. The HISTSIZE variable defines how many previous commands you will be able to access, in linux system,, the default value is 1000.The history command is saved at ~/.bash_history, it saved last HISTSIZE command you have entered.$ echo $HISTSIZE1000$ HISTSIZE=100$ echo $HISTSIZE100$echo $HISTFILE/home/yd211/.bash_historyIt disappears after you log out. It could be set in .bashrc or .bash_profile as default value.User EnvironmentThe environment describes your session to the programs you run.Syntax :$ envHOSTNAME=yd211-new-pcTERM=xtermSHELL=/bin/bashHISTSIZE=100SSH_CLIENT=202.204.13.127 3010 22OLDPWD=/home/yd211SSH_TTY=/dev/pts/0USER=yd211LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37 ;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz =00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=0 0;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;3 5:MAIL=/var/spool/mail/yd211PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/yd211/bin:.PWD=/home/yd211LANG=zh_CN.GB18030SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSHLVL=1HOME=/home/yd211LOGNAME=yd211SSH_CONNECTION=202.204.13.127 3010 202.204.13.104 22LESSOPEN=|/usr/bin/lesspipe.sh %sG_BROKEN_FILENAMES=1_=/bin/envYour environment describes many things on your session. It describes your session to the system. It contains the following information.The path name to your home directoryWho you logged in asWhere your shell will search for commandsYour terminal type and sizeOther things your applications may needFor example, the command vi and more need to know what kind of terminal you are using so they can format the output correctly.Many applications require you to customize your environment in some way. This is done by modifying your .profile file. When you log in, you can check your environment by running the env command. It will display every characteristic that is set in your environment.In the env listing, the words to the left of the = are the names of the different environment variables that you have set. Everything to the right of the = is the value associated with each variable.# env - print the resulting environment.Each one of these environment variables is set for a reason. Here are a few common environment variables and their meanings :HOME Path name to your home directoryPATH List of places to find commandsLOGNAME User name you used to log in.HISTFILE shell variablesSome of system set-up is set by the system, such as in /etc/profile$ vi /etc/profile# /etc/profile# System wide environment and startup programs, for login setup# Functions and aliases go in /etc/bashrcpathmunge () {if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; thenif [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfifi}……….Setting Shell VariablesA shell variable is a name that represents a valueThe value associated with the name can be modifiedSome shell variables are defined during the login processA user can define new shell variablesSyntax :Name = valueExamples:$ PATH=/usr/bin/X11:/usr/binA shell variable is similar to a variable in algebra. It is a name that represents a value. Variable assignment allows a value to be associated with a variable name. The value can then be accessed through the variable name. It the values is modified, the new value can still be accessed through the same variable name. The syntax for assigning a value to a shell variable is name = valueThis can be typed in at the terminal after a shell prompt or as a line in a shell program. Notice that there is no white space either before or after the equal sign (=).It is important to distinguish between the name of a shell variable and the value of a shell variable. When the variable value is set by performing an assignment statement, such as HOME=/home/yd211This tells the shell to remember the name HOME is /home/yd211Variable Naming RestrictionsVariable names must start with an character [a-z] and [A-Z] and can contain numeric, or underscore characters. There is no restriction on the number of characters that a variable name can contain.Two Important VariablesThe PATH variableA list of directories where the shell will search for the commands you typeThe TERM variableDescribes your terminal type and screen size to the programs you run$ env…PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/yd211/bin:.…$ TERM=xtermTERM is the environment variable that describes the type of terminal yon have. For many commands to run correctly, they need to know what kind of terminal you arc using. For example, the ls command needs to know how many columns there are on the screen, more needs to know how many lines there are, and vi needs to know both how many columns and how many lines there are plus much more information about your terminal type in order to work properly.The default method of setting up the terminal variable is by prompting the user for the proper terminal type in the following fashion;TERM=xtermxterm :terminal emulator for XThe xterm program is a terminal emulator for the X Window System.Itprovides DEC VT102/VT220 (VTxxx) and Tektronix 4014 compatible termi-nals for programs that cannot use the window system directly. If theunderlying operating system supports terminal resizing capabilities(for example, the SIGWINCH signal in systems derived from 4.3bsd),xterm will use the facilities to notify programs running in the windowwhenever it is resized.Xterm automatically highlights the text cursor when the pointer entersthe window (selected) and unhighlights it when the pointer leaves thewindow (unselected). If the window is the focus window, then the textcursor is highlighted no matter where the pointer is.On IBM-UX, you have to set TERM to some TERM value.PATH variable is a list, of directories that the shell will search through to find commands. It gives us the ability to type just a command name instead of the full path name to that command (for example, vi instead of /usr/bin/vi). This is an example of the default PATH variablePATH=/usr/bin:/usr/contrib/bin:/usr/local/binThis means that when you type a command, the shell will search for that command in /usr/bin, then /usr /contrib/bin, and so on until it either find the command or it runs out of directories to look in. If the command you are trying to run cannot be found in any of the PATH directories, yon will get the command : not found error message on your screen.Common Variable AssignmentsHOME=/home/yd211 # designates your login directoryHISTSIZE=1000HISTFILE = /home/yd211/.bash_history #defines file that stores all interactive commands enteredLOGNAME=yd211 #designates your login identifier or user nameOLDPWD=/home/yd211/linux_practise #designates previous directory location , cd –PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/yd211/bin:. # designates directories to search for commands PWD=/home/yd211/linux_practise/863_fenci/l1 #designates your present working directorySHELL=/bin/bash # designate your command interpreter programTERM=xterm # designate the terminal type of your terminalWhat happens when Login ?When you sit down to do work on the system, you see the login: prompt on the screen. When you type your user name, the system read your name and prompts you for a password. After you enter your password, the system checks your user name and password in the system password file (/etc/passwd). If the user name and password you entered are valid, the system will place you in your home directory and start the shell for you. We have seen this happen each time we logged in. Our question is—What really happens when the shell is started?1.Displays the contents of /etc/issue$ vi /etc/issueRed Hat Linux release 9 (Shrike)Kernel \r on an \mIssues the login promptruns login2. loginValidates user name and passwordPlaces user in home directoryRuns the user's shell3. shellexecutes /etc/profile ( POSIX, Bourne, and Korn shells) or /etc/csh.login (C shell)executes .profile or .login in the user's home directory$ cd$ ls –a./bash_profile ./bashrc ./bash_logout ./bash_historyexecutes .bashrc in the user's home directory (POSIX shells) if the user has created this file.Issues the shell promptOnce the shell starts running, it will read commands from a system command file called /etc/profile. Whenever someone logs in and starts a shell, this file will be read. There is also a file called .profile in your home directory. After /etc/profile is read, the shell reads your own profile . These two shell programs are used to customize a user's environment./etc/profile sets up the basic environment used by everyone on the system and .profile further tailors that environment to your specific needs. Since everyone uses /etc/profile. the system administrator will take care of it. It is your responsibility, however, to maintain you own .profile to set up your user environment.When these two programs are finished, the shell issues the first shell prompt.Some environment variables are required to configure your session (for example: PATH).As you may have seen, when these variables are defined interactively, they must be redefined every time you log in. To assist you in customizing your session, the files .bash_profile and .bashrc are available. These are simple shell scripts that will define environment variables, define aliases, and execute programs upon login. Remember that the POS1X shell originated from Bourne shell. Therefore, it supports the same configuration files in addition to the .bashrc file..bash_profileAny user who wishes to customize the default environment provided by his or her system administrator will create or modify .profile . This file commonly will define or customize environment variables, set up the user's terminal, and execute programs such as “date” during session log in./etc/profileThe file /etc/profile is a system-wide startup file that is executed by all users who are running under the Bourne, or POSIX shell. The system administrator may customize this to provide all users with a consistent user environment necessary to run their applications. Regular users generally do not have write access to this file, so they are not allowed to modify its content. Users will customize their environment through their personal copies of .bash_profile or .bashrcThe POSIX shells have an optional configuration file called .bashrc. It is used much like .bash_profile to configure your user environment. Unlike .bash_profile , however, .bashrc is read every time you start a new shell, not just when you log in. This allows you to set up your aliases or even your prompt every time you start a shell. The file .bashrc is not a required file . $ whereis cshcsh: /bin/csh /etc/csh.cshrc /etc/csh.login /usr/share/man/man1/csh.1.gz.cshrc and .loginWhen you log into the system with the C shell as your login shell, the shell will first search system csh file, then your home directory for a file named .login . If found, the commands in the file are executed before you get your first shell prompt. This is exactly the same as the .profile file for the POSIX. If found, the commands in the file .cshrc are also executed before you get your first C shell prompt.Examples :$ cd$ ls –a.bash_history .bash_logout .bash_profile .bashrc $ more .bash_historyhistoryvi ~/.bash_historylsenvecho $ENVcdls -avi .bashrcvi /etc/bashrclsumask -Slsenvecho $HISTFILEls…….$ more .bash_logout# ~/.bash_logoutclear$ more .bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programs PATH=$PATH:$HOME/bin:.export PATH$ more .bashrc# .bashrc# User specific aliases and functions# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrcfi。
Fundamentals of Linux Operation System and ProgrammingPresenter : Yuan Dong , yuandong@Keywords : Linux ; Fundamentals ; ProgrammingTeach format : By English and ChineseAiming : Linux R&D software engineer, follow the step of international development work How to learn it well : more practise in your computer, exercise explanation every chapter, Linux serverExamination :Reference book :(1) My textbook based on my development experience(2)Using Linux , by Tackett, Jack.; Burnett, Steven Forrest. Indianapolis, Ind. Que, 1998.(3)Using Linux , by Ball, Bill. Indianapolis, Ind. Que, 1998.UNIX Operation SystemAn operating system is a special computer program (software) that controls the computer (hardware). The operating system serves as a connection between the consumers and the resources, often coordinating the allocation of limited resources among numerous consumers. The resources include, for example, the CPU, disks, memory, and printers and the consumers are running programs requiring, access to the resources. As an example, a user (or a program} requests to store a file on the disk, the operating system intervenes to manage the allocation of space on the disk, and the transfer of the information from memory to the disk.When a user requests program execution, the operating system must allocate space in memory to load and access the program. As the program executes, it is allowed access to the Central Processing Unit(CPU). In a time-sharing system, there are often several programs trying to access the CPU at the same time.The operating system controls how and when a program will have its turn in the CPU, similar to a policeman directing traffic in a complex intersection. The intersection is analogous to the CPU; there is only one available. Each road entering the intersection is like a program. Traffic from only one road can access the intersection at any one time, and the policeman specifies which road has access to the intersection, eventually giving all roads access through the intersection.History of the UNIX Operating SystemLate 1960s A T&T development of MULTICS1969 A T&T Bell Labs UNIX system startsEarly 1970s AT&T development of UNIX systemMid 1970s University of California at Berkeley (BSD) and other universities also research and develop UNIX systemEarly 1980s Commercial interest in UNIX systemDARPA interest in BSDLate 1980s Development of standardsOpen Software Foundation (OSF) foundedEarly 1990s POS1X, standardization of the interactive user interfaceThe UNIX operating system started at Bell Laboratories in 1969. Ken Thompson, supported by Rudd Canaday, Doug Mcllroy, Joe Ossana, and Dennis Ritchie, wrote a small general purpose time-sharing system which started to attract attention.When the UNIX operating system reached the University of California at Berkeley,the Berkeley users created their own version of the system. Supported by the Department of Defense, they incorporated many new features.AT&T recognized the potential of the operating system and started licensing the system commercially. To enhance their product, they united internal UNIX system development that was being completed in different departments within AT&T, and also started to incorporate enhancements that Berkeley had developed.Later success can be attributed to• A flexible user interface, and an operating environment that includes numerous utilities.• The modularity of the system design that allows new utilities to be added.• Capability to support multiple processes and multiple users concurrently.• DARP A support at Berkeley.• Standardization of the interface definition to promote application portability.Features of UNIXThe UNIX system provides a time-sharing operating system that controls the activities and resources of the computer, and an interactive ,flexible operating interface. It was designed to run multiple processes concurrently and support multiple users to facilitate the sharing of data between members of a project team. The operating environment was designed with a modular architecture at all levels. When installing the UNIX system, you only need install the pieces that are relevant to your operating needs, and omit the excess. For example, the UNIX system supplies a large collection of programdevelopment utilities, but if you are not doing program development you need only to install the minimal compiler. The user interface also effectively supports the modular philosophy. Commands that know nothing about each other can be easily combined through pipelines, to perform quite complex manipulations.The Operating SystemThe kernel is the operating system. It is responsible for managing the available resources and access to the hardware. The kernel contains modules for each hardware component that it interfaces with. These modules provide the functionality that allows programs access to the CPU, memory, disks, terminals, the network; and so forth. As new types of hardware are installed on the system, new modules can be incorporated into the kernel.The ShellThe shell is an interactive command interpreter. Commands are entered at the shell prompt, and acted upon as they are issued. A user communicates with the computer through the shell. The shell gathers the input the user enters at the keyboard and translates the command into a form the kernel can understand. Then the system will execute the command.You should notice that the shell is separate from the kernel. If you do not like the interface provided by the supplied shell, you can easily replace it with another shell.Many shells are currently available. Some are command driven and some provide a menu interface. The common shells that are supplied with the UNIX system include both a command interpreter and a programmable interface.There are four shells that are commonly available in the UNIX system environment. They are• Bourne Shell (/usr/old/bin/sh)—the original shell provided on AT&T based systems developed by Stephen Bourne at Bell Laboratories. It provides a UNIX system command interpreter and supports a programmable interface to develop shell programs, or scripts as they are commonly called. The programmable and interactive interfaces provide capabilities such as variable definition and substitution, variable and file testing, branching, and loops.• C Shell (/usr/bin/csh)—the shell developed at the University of California Berkeley by Bill Joy, and is provided on BSD-based systems. This shell was referred to as the California Shell, which was shortened to just the C Shell. It was considered an improvement over the Boirne Shell because it offered interactive features such as a command stack which allows simple recalling and editing of previously entered commands, and aliasing which provides personalized alternative names for existing commands.• Korn shell (/usr/bin/ksh)—is a more recent development from Bell Laboratories developed by David Korn. It can be considered an enhanced Bourne Shell because it supports the simple programmable interface of the Bourne Shell, but has the convenient interactive features of the C Shell. The code has also been optimized to provide a faster, more efficient shell.• POSIX Shell (/usr/bin/sh)—POSIX-conformant command programming language and command interpreter residing in file /usr/bin/sh. This shell is similar to the Korn shell in many respects; it provides a history mechanism, supports job control, and provides various other useful features.What is POSIX?The IEEE POSIX Standards are all currently entitledInformation Technology --Portable Operating System Interface (POSIX) --Part xx: ....The original, trial-use, standard published in 1986 was actually called IEEE-IX (IEEE's version of UNIX). However, this was rapidly changed to POSIX in time for the second printing (also in 1986).The standard is heavily influenced by UNIX® -- and in the latest revision now merges with The Open Group's Base Specifications which comprise the coreof the Single UNIX Specification -- in the mid eighties there was a plethora of UNIX operating systems, most of which had names ending in X (e.g. HPUX, AIX, PNX, Xenix, etc), and that certainly influenced the naming decision.The following quote appears in the Introduction to POSIX.1: "The name POSIX was suggested by Richard Stallman. It is expected to be pronounced pahz-icks as in positive, not poh-six, or other variations. The pronounciation has been published in an attempt to promulgate a standardized way of referring to a standard operating system interface".More Features of UNIX• Hierarchical file system• Multi-tasking• Multi-userHierarchical File SystemInformation is stored on the disk in containers known as files. Every file is assigned a name, and a user accesses a file by referencing its name. Files normally contain data, text, programs, and so on. A UNIX system normally contains hundreds of files, so another container, the directory is provided that allows users to organize their files into logical groupings. In the UNIX system, a directory can be used to store files or other directories.The file system structure is very flexible, so if a user's organizational needs change, files and directories can be easily moved, renamed, or grouped into new or different directories through simple UNIX system commands. The file system, therefore, is like an electronic filing cabinet. It allows users to separate and organize their information into directories that are most appropriate for their environment and application.Multi-taskingIn the UNIX system several tasks can be performed at the same time. From a single terminal, a single user can execute several programs that all seem to be running simultaneously. This means that a user can edit a text file, while another file is being formatted, while yet another file is being printed.In actuality, the CPU can execute only one task at a time, but the UNIX operating system has thecapability to time-shareing the CPU between multiple processes that are scheduled to run at the same time. So, to the user, it appears that all programs are executing simultaneously.Multi-userMulti-user capability allows more than one user to log in and use the system at the same time. Multiple terminals and keyboards can be attached to the same computer. This is a natural extension of the multi-tasking capability. If the system can run multiple programs simultaneously, some of those multiple programs should be able to support other user sessions, In addition, a single user could log in multiple times to the same system through multiple terminals. A big advantage of this architecture is that members of a work group can have access to the same data at the same time, either from a development or a user viewpoint.Here are a few reasons why people are switching to Linux:• It's free. That is, Linux is a freely redistributable clone of the UNIX operating system (OS). You can get it free from someone who has it, or download it from an Internet site or a bulletin board system (BBS), or you can buy it at a reasonable price from a vendor who has packaged it (probably with added value) and who may also provide support services.What makes Linux so different is that it's a free implementation of UNIX. It was and still is developed by a group of volunteers, primarily on the Internet, who exchange code, report bugs, and fix problems in an open-ended environment. Anyone is welcome to join in the Linux development effort .• It's popular. It runs on the inexpensive Intel 386/486/Pentium PC architecture and supports a broad range of video cards, audio cards, CD-ROM drives, disk drives, and other devices.There are some very busy Internet newsgroups where Linux is discussed by an international community of users and developers, as well as email mailing lists .• It's powerful. You will be pleased to see how fast the system runs, even with many processes running and with multiple windows open. Linux makes excellent use of the hardware. Manycommercial operating systems (namely MSDOS) make little use of the advanced multitasking capabilities of the 80x86 processor. Linux is native to this architecture and uses them all. A Linux machine with a reasonably fast processor and a sufficient amount of RAM can perform as well, or better, than expensive UNIX workstations. Linux on a modest PC runs faster than many UNIX workstations.• It is of good quality, and runs high-quality software applications. Linux is being developed publicly with hundreds of programmers and users refining it, but with the individual vision and focus of its originator, Linus Torvalds.• It has full UNIX features. Linux is a true multi-user, multitasking operating system that supports multiple processors on some systems. It uses the X Window System graphical user interface (GUI) and offers several different easy-to-use, configurable window managers. Full networking support (TCP/IP, SLIP, PPP, UUCP, among others) is available.• It's highly compatible with MS-DOS, Windows, and Windows 95. You can install Linux along with other disk partitions that contain MS-DOS or other operating systems. Linux can directly access MS-DOS files from the floppy or hard drive. Linux does not run under MS-DOS, Windows, or any other operating system: it is completely independent of them, but features have been added to allow the separate systems to work together.• It is small. The basic OS will run on 2 MB of system memory, and a carefully configured system complete with GUI and window manager will run well on 4 MB. A good basic distribution will fit in 40 MB of disk storage. (If this seems like a lot of disk space, it's because Linux provides a lot of utilities.)• Full source code is freely available for Linux• It's supported. The biggest line of support is the Internet and its many thousands of Linux newsgroup participants, but you can also contract for support from an independent company or buy a supported version of Linux from one of its distributors.INTRODUCTION TO LINUXIn narrow technical terms, Linux is just the operating system kernel, offering the basic services of process scheduling, virtual memory, file management, and device I/O. In other words, Linux itself is the lowest-level part of the operating system. However, most people use the term "Linux" to refer to the complete system-the kernel along with the many applications that it runs: a complete development and work environment including compilers, editors, graphical interfaces, text processors, games, and more.Linux can turn any 386, 486, Pentium, or Pentium Pro PC into a workstation. It will give you the full power of UNIX at your fingertips.A Brief History of LinuxUNIX is one of the most popular operating systems worldwide because of its large support base and distribution. It was originally developed as a multitasking system for minicomputers and mainframes in the mid-1970s.The real reason for UNIX's popularity? Many hackers feel that UNIX is the Right Thing-the One True Operating System. Most versions of UNIX for personal computers are quite expensive and cumbersome.Linux is a freely distributable version of UNIX developed primarily by Linus Torvalds* at the University of Helsinki in Finland. Linux was originally developed as a hobby project by Linus. It was inspired by Minix, a small UNIX system developed by Andy Tanenbaum, and the first discussions about Linux were on the Usenet newsgroup comp.os.minix.Linux was developed with the help of many UNIX programmers and wizards across the Internet.Today, Linux is a complete UNIX clone, capable of running the X Window System, TCP/IP, Emacs, UUCP, mail and news software. Almost all major free software packages have been ported to Linux, and commercial software is becoming available. More hardware is supported than in original versions of the kernel. Who would have ever guessed that this "little" UNIX clone would have grown up to take on the entire world of personal computing?System FeaturesLinux is a complete multitasking, multi-user operating system (just like all other versions of UNIX). Linux also supports multi-processor systems (such as dual-Pentium motherboards).The Linux system is mostly compatible with a number of UNIX standards (inasmuch as UNIX has standards) on the source level, including IEEE POSIX.1, and BSD features.In order to increase the amount of available memory, Linux also implements disk paging: that is, up to 256 MB of swap spaceSoftware FeaturesBasic Commands and UtilitiesVirtually every utility you would expect to find on standard implementations of UNIX has been ported to Linux. This includes basic commands such as ls, awk, tr, sed, bc, more, and so on.Many text editors are available, including vi, GNU Emacs.One interesting note is that most of the basic Linux utilities are GNU software. These GNU utilities support advanced features not found in the standard versions from BSD or AT&T.Programming Languages and UtilitiesLinux provides a complete UNIX programming environment, including all of the standard libraries, programming tools, compilers, and debuggers that you would expect to find on other UNIX systems. Within the UNIX software development world, applications and systems programming is usually done in C or C++. The standard C and C++ compiler for Linux is GNU's gcc, which is an advanced, modern compiler supporting many options. It's also capable of compiling C++ (including AT&T 3.0 features) as well as Objective-C, another object-oriented dialect of C.Besides C and C++, many other compiled and interpreted programming languages have been ported to Linux, such as Smalltalk, FORTRAN, Pascal, LISP, Scheme, and Ada.The advanced gdb debugger has been ported, which allows you to step through a program to find bugs. Other tools include GNU make , used to manage compilation of large applicationsNetworkingLinux supports the two primary networking protocols for UNIX systems: TCP/IP and UUCP. TCP/IP (Transmission Control Protocol/Internet Protocol) is the set of networking paradigms that allow systems all over the world to communicate on a single network known as the Internet. With Linux, TCP/IP, and a connection to the network, you can communicate with users and machines across the Internet via electronic mail, Usenet news, file transfers with FTP, and more. There are many Linux systems currently on the Internet.How to get Linux operation systemLinux is free software, no single organization or entity is responsible for releasing and distributing the software. Anyone is free to put together and distribute the Linux software, as long as the restrictions of GPL ((General Public License,简称 GPL)。
Linux内核编程目录1.HELLO, WORLD ................................................................................................ 一EXHELLO.C .............................................................................................................. 一1.1内核模块的编译文件........................................................................................................ 二1.2多文件内核模块.................................................................................................................. 三2.字符设备文件 ....................................................................................................... 六2.1多内核版本源文件........................................................................................................ 十四3./PROC文件系统 .............................................................................................. 十五4.使用/PROC进行输入 ...................................................................................... 二十5.和设备文件对话(写和IOCTLS) ........................................................... 二十八6.启动参数 ....................................................................................................... 四十二7.系统调用 ....................................................................................................... 四十五8.阻塞进程 ....................................................................................................... 五十一9.替换PRINTK’S ........................................................................................... 六十一10.调度任务 ..................................................................................................... 六十四11.中断处理程序.............................................................................................. 六十九11.1I NTEL 结构上的键盘 ......................................................................................... 六十九12.对称多处理 ................................................................................................. 七十三常见的错误 ......................................................................................................... 七十四2.0和2.2版本的区别 ........................................................................................ 七十四除此以外.............................................................................................................. 七十四其他...................................................................................................................... 七十六G OODS AND S ERVICES..................................................................................................... 七十六GNU GENERAL PUBLIC LICENSE........................................................................ 七十六注.......................................................................................................................... 八十二1.Hello, world当第一个穴居的原始人程序员在墙上凿出第一个―洞穴计算机‖的程序时,那是一个打印出用羚羊角上的图案表示的―Hello world‖的程序。
运维工程师必会的109个Linux命令版本1.0崔存新更新于2009-12-26目录1文件管理 ........................................................................................................................ 错误!未定义书签。
1.1basename..................................................................................................... 错误!未定义书签。
1.2cat................................................................................................................. 错误!未定义书签。
1.3cd.................................................................................................................. 错误!未定义书签。
1.4chgrp ............................................................................................................ 错误!未定义书签。
1.5chmod .......................................................................................................... 错误!未定义书签。
1.6chown........................................................................................................... 错误!未定义书签。
本文由pascale0701贡献ppt文档可能在WAP端浏览体验不佳。
建议您优先选择TXT,或下载源文件到本机查看。
第5章 Linux内核简介 Linux内核简介主要内容Linux核心的一般结构 Linux核心的一般结构 ? 进程的概念、进程的调度和进程通信 ? 文件系统的构成和管理 ? 内存管理 ? 设备驱动及中断处理5.1 概述Linux系统大致可分为三层: Linux系统大致可分为三层:靠近硬件的底层是内核,即Linux操作系统常驻内存部分。
靠近硬件的底层是内核,即Linux操作系统常驻内存部分。
? 中间层是内核之外的shell层,即操作系统的系统程序部分。
中间层是内核之外的shell层,即操作系统的系统程序部分。
? 最高层是应用层,即用户程序部分从结构上看,Linux操作系统是采用单块结构的操作系统。
从结构上看,Linux操作系统是采用单块结构的操作系统。
? 一般说来,可以将操作系统划分为内核和系统程序两部分。
●进程控制系统用于进程管理、进程同步、进程通信、进程调度和内存管理等。
●内存管理控制内存分配与回收。
●文件系统管理文件、分配文件空间、管理空闲空间、控制对文件的访问并为用户检索数据。
●Linux系统支持三种类型的硬件设备:字符设备、块设备和网络设备。
Linux系统支持三种类型的硬件设备:字符设备、块设备和网络设备。
●核心底层的硬件控制负责处理中断以及与机器通信。
5.2 进程管理5.2.1 进程和线程的概念1.进程及其状态 ? 简单说来,进程就是程序的一次执行过程。
简单说来,进程就是程序的一次执行过程进程就是程序的一次执行过程。
? 进程至少要有三种基本状态。
这三种基本状态是:运行态、就绪态和封锁态(或等待态)。
进程的状态可依据一定的条件和原因而变化2.Linux进程状态 Linux进程状态3.进程的模式和类型 ? 在Linux系统中,进程的执行模式划分为用户模式和内核 Linux 系统中,进程的执行模式划分为用户模式用户模式和模式 ? 按照进程的功能和运行的程序来分,进程划分为两大类:按照进程的功能和运行的程序来分,进程划分为两大类:一类是系统进程,一类是系统进程,另一类是用户进程4.Linux线程 Linux线程Linux把线程定义为进程的“执行上下文” Linux把线程定义为进程的“执行上下文” ? 具有一段可执行的程序、专用的系统堆栈空间、私有的“线程控制块”(即thread_struct数据结构)线程控制块”(即thread_struct数据结构)缺少自己的存储空间5.2.2 进程的结构1.task_struct结构 task_struct结构 ? task_struct结构包含下列几方面的信息:task_struct结构包含下列几方面的信息: ? ·进程状态 ? ·调度信息 ? ·标志符 ? ·内部进程通讯 ? ·链接信息 ? ·时间和计时器 ? ·文件系统 ? ·虚拟内存 ? ·处理器信息 2.进程系统堆栈 ? 每个进程都有一个系统堆栈,用来保存中断现场信息和进程进入内核模式后执行子程序(函数)嵌套调用的返回现场信息。
Chapter 12 Network ServiceNetwork principle :1. telnet –telnet - user interface to the TELNET protocol.DESCRIPTIONThe telnet command is used to communicate with another host using the TELNET protocol. If telnet is invoked without the host argument, itenters command mode, indicated by its prompt (telnet>). In this mode, it accepts and executes the commands listed below. If it is invoked witharguments, it performs an open command with those arguments.2. ssh - OpenSSH SSH client (remote login program)ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.3. ftp - Internet file transfer program , run vsftp ( very secure ftp )Ftp is the user interface to the Internet standard File Transfer Proto-col. The program allows a user to transfer files to and from a remotenetwork site.Options may be specified at the command line, or to the command inter-preter.ssh远程复制命令scp的用法SSH提供了一些命令和shell用来登录远程服务器。
在默认情况下它不允许你拷贝文件,但是还是提供了一个"scp"命令。
scp命令是SSH中最方便有用的命令了,试想,在两台服务器之间直接传送文件,仅仅用scp一个命令就完全解决了。
上传:scp 源文件用户名@主机:目的文件名copy 本地的档案到远程的机器上scp /etc/lilo.conf k@.tw:/home/k会将本地的 /etc/lilo.conf 这个档案 copy 到 .tw,使用者 k 的家目录下。
scp -r 原文件夹用户名@主机:目的文件夹下载:scp 用户名@主机:/path/文件名/path/文件名copy远程机器上的档案到本地来scp k@.tw:/etc/lilo.conf /etc会将 net67.ee.oitdu.tw 中 /etc/lilo.conf 档案 copy 到本地的 /etc 目录下。
保持从来源 host 档案的属性scp –p k@net67.ee.tw:/etc/lilo.conf /etc在此必须注意使用者的权限是否可读取远程上的档案,若想知道更多关于 scp 的使用方法,可去看看 scp 的使用手册。
作业: 设置ssh,使得scp无需输入密码便可以进行拷贝动作。
( 参考: rsync -avl k@.tw:/etc/lilo.conf /etc , 可以复制目录,但不复制 symbolic link 中的内容,而只是复制一个软链接的指向;用scp 复制,会把symbolic link 中的内容真实的复制进备份目录中。
SCP manualNAMEscp - secure copy (remote file copy program)SYNOPSISscp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port][-S program] [[user@]host1:]file1 [...] [[user@]host2:]file2DESCRIPTIONscp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the sameauthentication and provides the same security as ssh(1). Unlike rcp(1), scp will ask for passwords orpassphrases if they are needed for authentication.-p Preserves modification times, access times, and modes from the original file.-r Recursively copy entire directories.-v Verbose mode. Causes scp and ssh(1) to print debugging messages about their progress. This ishelpful in debugging connection, authentication, and configuration problems.hostname - show or set the system’s host nameHostname is the program that is used to either set or display the current host of the system. These names are used by many of the networking programs to identify the machine.$ hostnameyd211pc/etc/hosts文件,用来解释局网中的主机名和ip地址直接的关系$ vi /etc/hosts# ip addree Name10.193.251.186 img18610.193.251.177 demo$ ssh img186 等价于 $ssh 10.193.251.186telnet - user interface to the TELNET protocol$ telnet hostname$ telnet ip_addressThe telnet command is used to communicate with another host using theTELNET protocol. Telnet uses at least the HOME, SHELL, DISPLAY, and TERM environment variables. Use exit or ctrl+d to log off the remote computer.ftp - Internet file transfer program$ ftp hostname$ ftp id_addressFtp is the user interface to the Internet standard File Transfer Protocol. The program allows a user to transfer files to and from a remote network site.Some basic and common ftp command :get remote-file [local-file]Retrieve the remote-file and store it on the local machine.If the local file name is not specified, it is given the samename it has on the remote machine.mget remote-filesExpand the remote-files on the remote machine and do a getfor each file name thus produced. Files are transferred into thelocal working directoryput local-file [remote-file]Store a local file on the remote machine. If remote-file isleft unspecified, the local file name is used after process-ing.mput local-filesExpand wildcards in the list of local files given as argu-ments and do a put for each file in the resulting listquit A synonym for bye.$ ftp 1.1.1.1 # log in to ftp serverConnected to 1.1.1.1 (1.1.1.1).220 (vsFTPd 1.1.3)Name (1.1.1.1:yd211): yd211 # input user name331 Please specify the password.Password: # input password230 Login successful. Have fun.Remote system type is UNIX.Using binary mode to transfer files.ftp>ls # already login, ftp prompt ; list the content of current directory. 227 Entering Passive Mode (1,1,1,1,51,1)150 Here comes the directory listing.-rw-r--r-- 1 500 500 65 Nov 20 05:24 1drwxr-xr-x 5 500 500 4096 Nov 12 23:43 Buptdrwxr-xr-x 12 500 500 4096 Oct 05 15:11 Camdrwxrwxr-x 3 500 500 4096 Jul 29 18:43 Nokia-rw-r--r-- 1 500 500 12 Nov 20 05:37 ls-rw-r--r-- 1 500 500 438 Sep 28 03:08 monophone.listdrwxr-xr-x 2 500 500 4096 Aug 05 22:42 spqadrwxr-xr-x 2 500 500 4096 Dec 11 16:33 temp226 Directory send OKftp > help # online help of ftp , list all command offtp helpCommands may be abbreviated. Commands are:! debug mdir sendport site$ dir mget put sizeaccount disconnect mkdir pwd statusappend exit mls quit structascii form mode quote systembell get modtime recv suniquebinary glob mput reget tenexbye hash newer rstatus tickcase help nmap rhelp tracecd idle nlist rename typecdup image ntrans reset userchmod lcd open restart umaskclose ls prompt rmdir verbosecr macdef passive runique ?delete mdelete proxy sendftp > help ls # print the function of command ls# you can also get help from$ man ftp in linux shellls list contents of remote directoryfpt > help get # print the function of command getget receive fileftp> help mget # print the function of command mget mget get multiple filesftp> get 1 # download file 1 from remote linuxserver to your current workingdirectoryftp > quit # quit to ftpThe data verification after transferring by ftp or scp.Usually, the error may happen after large amount of data transferring, let us introduce a kind of verification method by "md5sum".Let us show an example on how to use "md5sum".Request : transfer the data in /var/www to another server, then verify the data integrity by MD5 testing.Step :1. cd /var/wwwmd5sum * > MD5SUMif there are sub-dir in /var/www, you could use$ find /var/www -name "*" > file.list$ vi file.listIn VI environment , add "md5sum " before every line, by :%s/^/md5sum(white space)/g$ chmod 700 file.list$ ./file.list > MD5SUM_SOURCE2.After the data is transferred to destination, use the same way in step 1 to generate file MD2SUM_DESTINATION.pare the file between MD5SUM_SOURCE and MD2SUM_DESTINATION by "$ diff MD5SUM_SOURCEMD2SUM_DESTINATION " to see if the data transferring integrity.TRUE , the data transferring integrity is ok.FALSE, the data transferring integrity has problem.Let us see the content of file MD5SUM.Such as :MD5 result file_name40491cfa725e9f612597e18e5cb5a731 cube-bot-0.32.tar.gz71b29ffeaa264caecaeb6f30813814f5 hack2.jsd779f6b43832a6d65610840fbeb0c398 hack2.js~9dccf462d245f55ac3e0cdb0e5401f5b index2.php4afbe1cc2bd8eb97a4e6c51e6aaf5202 ipgw.pyaf7788191a44f32a9d4aafa3aa7f61c4 md5sum9dccf462d245f55ac3e0cdb0e5401f5b phpinfo.php2883fddfd2a1c9a448ab03862beeda81 r.php60880cf2774e5f54159f0e7555a6c723 r.php~6bb2397f1cd9922c46e15c2253a99c27 s.htmld3e265f801b2f3b7fa37b5578ad83ffb s.html~9175a6e91a1a909eae8baf532c44eccf ss.htmla6cc3964fb87d1b3ed3829b0c2d1b4c6 ss.html~aee6dc4a6715f93747137c19d151c51c w21~4e313605a6602d7c516ec6bb32a43bad w21.html4e313605a6602d7c516ec6bb32a43bad w21.html~20a76cde1afb88df632c5a13d0bff1c5 w2.htmla7f67a7d9b59e2c2dae6f917e9b59934 w2.html~If there is sub-dir in the file.list , there is error hint, such as md5sum: ajax: Is a directorymd5sum: apache2-default: Is a directorymd5sum: blog: Is a directorymd5sum: codeblocks: Is a directorymd5sum: download: Is a directorymd5sum: php100: Is a directoryA remind is : when the amount of data is very huge, there will need a long time to do the verification, but it is worth to be sure the integrity of the data transferring.# ls /etc/init.d/*# ls /etc/rc.d/init.d/*# 启动脚本放置处, 有很多默认服务httpd nfs smb sshd vsftpd …restart of every kinds of service :for examples :[root@img177 init.d]# vsftpd restartShutting down vsftpd: [ OK ]Starting vsftpd for vsftpd: [ OK ]Or# service vsftpd restart# ls /etc/rc.d/etc/rc.d/init.d/ rc.local rc.sysinit # 启动脚本 , 可以由root 自己添加机器启动后需要自动执行的脚本 For example :$ more rc.local#!/bin/sh# This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff. touch /var/lock/subsys/local/usr/local/webserver/nginx/sbin/nginx/etc/init.d/mysql start/usr/local/webserver/php/sbin/php-fpm start service nfs start。