Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271745
  • 博文数量: 101
  • 博客积分: 4245
  • 博客等级: 上校
  • 技术积分: 1085
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-24 00:28
文章分类

全部博文(101)

文章存档

2012年(1)

2011年(16)

2010年(34)

2009年(50)

我的朋友

分类: LINUX

2009-12-15 09:01:54

鸟哥私房菜笔记

9.Linux的文件属性与目录配置

10.Linux磁盘与文件系统管理

12档案的压缩与打包

常见压缩指令:compressgzipbzip2tarddcpio
.Z  : compress 压缩的文件,此命令现在基本上不用了
.bz2 : bzip2压缩的文件
.gz :  gzip压缩的文件
.tar :  tar打包的文件,并没有压缩
.tar.gz : 先经tar打包,然后在gzip压缩后产生的文件
.tgz : .tar.gz文件
 
在用tar打包的时候,有一个绝对路径的问题,最好是采用默认的非绝对路径打包,如果要用绝对路径打包,需要选项 –P
另外还有一个选项-p,使得打包文件文件使用原来属性,即不会依据使用者而变
--N选项:
--exclude选项
tar cvf - /etc | tar xvf – 不产生中间打包文件的方式进行压缩解压缩过程
 
dd命令常常用来进行备份,
dd if= input_file of=out_file bs= block_size count=number
使用总结:tar可以用来备份关键数据,dd则用来备份整个partition或整个磁盘

13 vi编辑器

14.认识Bash Shell编程

1.         给变量赋值时,=号两边不能有空格
2.         定义变量时,双引号内可以保有变量特性,而单引号内则看做一般字符
3.         要为变量扩增内容的时候,采用这样的形式:var=”$var”add 其中var是变量名,add是想要扩增的内容。或者:var=${var}add
4.         当在一串指令中,要藉由其它指令提供的信息,可以使用`command`的方式,`是数字1下面那个符号,也就是说先执行command,再执行整个命令,其实这是Bourne sh的内容,现在的bash已经换成这样一种方式:$(command)
5.         通常系统预设变量是大写字母,用户自定义变量是小写字母,这只是习惯,不是原则
6.         系统预设变量和用户自定义变量的区别是,变量是否能被子程序所引用,自定义变量是不能被子程序引用的,但是当经过export之后,自定义变量就可以被子程序引用
7.         取消变量的方法是:unset 变量名
8.         产生随机数:declare –i number=$RANDOM*10/32767; echo $number
9.         setenv的区别:"set" gives both environment and shell variables. "env" only gives
environment variables, which are passed along to child processes.
10.     read使用-p选项后面可以接提示字符,常常用在要从键盘上输入前的提示
11.     declare –a variable:把variable定义为数组
12.     declare –i variable:把variable定义成为整数数字
13.     declare –x variable:把variable定义为环境变量,相当于定义了一个local变量之后用export使之成为env变量
14.     declare –r variable:把variable定义为只读属性
15.     typesetdeclare
16.     export
17.     set
18.     unset
19.     typeset
20.     ulimit
21.     登录信息显示数据的两个文件:/etc/issue /etc/motd , 前者显示在tty1—tty6登入时,后者在登录之后显示。 当用telnet登录之后,显示 /etc/issue.net的内容
22.     常见的系统环境设置文件
23.     常见的个人用户环境设置文件
24.     ls -1只列出每个文件名,并且每个文件成一排?
25.     双引号不能阻止对那些由$符号开始的变量的代换(单引号可以),但是双引号可以阻止其中的wildcard原本应该起的作用

15正则表达式

11metacharacters [ ^ $ . + ( ) * ? | \
If you forget to escape a special character where its use is not allowed, such as in +1, then you will get an error message.
Most regular expression flavors treat the brace { as a literal character, unless it is part of a repetition operator like {1,3}. An exception to this rule is the java.util.regex package: it requires all literal braces to be escaped.
All other characters should not be escaped with a backslash. That is because the backslash is also a special character. The backslash in combination with a literal character can create a regex token with a special meaning. E.g. \d will match a single digit from 0 to 9.
Escaping a single metacharacter with a backslash works in all regular expression flavors. Many flavors also support the \Q...\E escape sequence. All the characters between the \Q and the \E are interpreted as literal characters. E.g. \Q*\d+*\E matches the literal text *\d+*. The \E may be omitted at the end of the regex, so \Q*\d+* is the same as \Q*\d+*\E. This syntax is supported by the JGsoft engine, Perl, PCRE and Java, both inside and outside character classes. However, in Java, this feature does not work correctly in JDK 1.4 and 1.5 when used in a character class or followed by a quantifier.
you may be surprised that characters like the single quote and double quote are not special characters. That is correct. When using a regular expression or grep tool like PowerGREP or the search function of a text editor like EditPad Pro, you should not escape or repeat the quote characters like you do in a programming language.
In your source code, you have to keep in mind which characters get special treatment inside strings by your programming language. That is because those characters will be processed by the compiler, before the regex library sees the string. So the regex 1\+1=2 must be written as "1\\+1=2" in C++ code. The C++ compiler will turn the escaped backslash in the source code into a single backslash in the string that is passed on to the regex library. To match c:\temp, you need to use the regex c:\\temp. As a string in C++ source code, this regex becomes "c:\\\\temp". Four backslashes to match a single one indeed.
Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-).

Basic Syntax Reference

Character
Description
Example
Any character except [\^$.|?*+()
All characters except the listed special characters match a single instance of themselves. { and } are literal characters, unless they're part of a valid regular expression token (e.g. the {n} quantifier).
a matches a
\ (backslash) followed by any of [\^$.|?*+(){}
A backslash escapes special characters to suppress their special meaning.
\+ matches +
\Q...\E
Matches the characters between \Q and \E literally, suppressing the meaning of special characters.
\Q+-*/\E matches +-*/
\xFF where FF are 2 hexadecimal digits
Matches the character with the specified ASCII/ANSI value, which depends on the code page used. Can be used in character classes.
\xA9 matches © when using the Latin-1 code page.
\n, \r and \t
Match an LF character, CR character and a tab character respectively. Can be used in character classes.
\r\n matches a DOS/Windows CRLF line break.
\a, \e, \f and \v
Match a bell character (\x07), escape character (\x1B), form feed (\x0C) and vertical tab (\x0B) respectively. Can be used in character classes.
 
\cA through \cZ
Match an ASCII character Control+A through Control+Z, equivalent to \x01 through \x1A. Can be used in character classes.
\cM\cJ matches a DOS/Windows CRLF line break.
Character
Description
Example
[ (opening square bracket)
Starts a character class. A character class matches a single character out of all the possibilities offered by the character class. Inside a character class, different rules apply. The rules in this section are only valid inside character classes. The rules outside this section are not valid in character classes, except \n, \r, \t and \xFF
 
Any character except ^-]\ add that character to the possible matches for the character class.
All characters except the listed special characters.
[abc] matches a, b or c
\ (backslash) followed by any of ^-]\
A backslash escapes special characters to suppress their special meaning.
[\^\]] matches ^ or ]
- (hyphen) except immediately after the opening [
Specifies a range of characters. (Specifies a hyphen if placed immediately after the opening [)
[a-zA-Z0-9] matches any letter or digit
^ (caret) immediately after the opening [
Negates the character class, causing it to match a single character not listed in the character class. (Specifies a caret if placed anywhere except after the opening [)
[^a-d] matches x (any character except a, b, c or d)
\d, \w and \s
Shorthand character classes matching digits, word characters, and whitespace. Can be used inside and outside character classes.
[\d\s] matches a character that is a digit or whitespace
\D, \W and \S
Negated versions of the above. Should be used only outside character classes. (Can be used inside, but that is confusing.)
\D matches a character that is not a digit
[\b]
Inside a character class, \b is a backspace character.
[\b\t] matches a backspace or tab character
Character
Description
Example
. (dot)
Matches any single character except line break characters \r and \n. Most regex flavors have an option to make the dot match line break characters too.
. matches x or (almost) any other character
Character
Description
Example
^ (caret)
Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.
^. matches a in abc\ndef. Also matches d in "multi-line" mode.
$ (dollar)
Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.
.$ matches f in abc\ndef. Also matches c in "multi-line" mode.
\A
Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Never matches after line breaks.
\A. matches a in abc
\Z
Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Never matches before line breaks, except for the very last line break if the string ends with a line break.
.\Z matches f in abc\ndef
\z
Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Never matches before line breaks.
.\z matches f in abc\ndef
Character
Description
Example
\b
Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.
.\b matches c in abc
\B
Matches at the position between two word characters (i.e the position between \w\w) as well as at the position between two non-word characters (i.e. \W\W).
\B.\B matches b in abc
Character
Description
Example
| (pipe)
Causes the regex engine to match either the part on the left side, or the part on the right side. Can be strung together into a series of options.
abc|def|xyz matches abc, def or xyz
| (pipe)
The pipe has the lowest precedence of all operators. Use grouping to alternate only part of the regular expression.
abc(def|xyz) matches abcdef or abcxyz
Character
Description
Example
? (question mark)
Makes the preceding item optional. Greedy, so the optional item is included in the match if possible.
abc? matches ab or abc
??
Makes the preceding item optional. Lazy, so the optional item is excluded in the match if possible. This construct is often excluded from documentation because of its limited use.
abc?? matches ab or abc
* (star)
Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all.
".*" matches "def" "ghi" in abc "def" "ghi" jkl
*? (lazy star)
Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.
".*?" matches "def" in abc "def" "ghi" jkl
+ (plus)
Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once.
".+" matches "def" "ghi" in abc "def" "ghi" jkl
+? (lazy plus)
Repeats the previous item once or more. Lazy, so the engine first matches the previous item only once, before trying permutations with ever increasing matches of the preceding item.
".+?" matches "def" in abc "def" "ghi" jkl
{n} where n is an integer >= 1
Repeats the previous item exactly n times.
a{3} matches aaa
{n,m} where n >= 0 and m >= n
Repeats the previous item between n and m times. Greedy, so repeating m times is tried before reducing the repetition to n times.
a{2,4} matches aaaa, aaa or aa
{n,m}? where n >= 0 and m >= n
Repeats the previous item between n and m times. Lazy, so repeating n times is tried before increasing the repetition to m times.
a{2,4}? matches aa, aaa or aaaa
{n,} where n >= 0
Repeats the previous item at least n times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only n times.
a{2,} matches aaaaa in aaaaa
{n,}? where n >= 0
Repeats the previous item n or more times. Lazy, so the engine first matches the previous item n times, before trying permutations with ever increasing matches of the preceding item.
a{2,}? matches aa in aaaaa
 
ls命令是不支持正则表达式的,所以才有*的不同含义
在正则表达式中,!和>并不是特殊字符

16.学习shell script

Double quotes do not suppress the substitution of words that begin with "$" but they do suppress the expansion of wildcard characters.
echo –e “Hello World! \a \n”
其中,-e表示:enable interpretation of backslash escapes
\a表示:alert
\n 表示: new line
 date1=`date –date=’2 days ago’ +%Y%m%d`
file1=”@filename””date1”
在数字运算上,可以用 var=$((运算内容))的方式
$?  exit status variable. The holds the of a command, a , or of the script itself.
Test的测试功能
 
 
exit 0什么意思?
$?
$0:shell脚本文件名
$1shell脚本文件的第一个参数
$2shell脚本文件的第二个参数
 
 
 
 
declare
 
功能说明:声明 shell 变量。
语  法:declare [+/-][rxi][变量名称=设置值 declare -f
补充说明:declareshell指令,在第一种语法中可用来声明变量并设置变量的属性([rix]即为变量的属性),在第二种语法中可用来显示shell函数。若不加上任何参数,则会显示全部的shell变量与函数(与执行set指令的效果相同)

参  数:
 +/-  "-"可用来指定变量的属性,"+"则是取消变量所设的属性。 
 -f  仅显示函数。
 
  将变量设置为只读。
 
  指定的变量会成为环境变量,可供shell以外的程序来使用。
 
  [设置值]可以是数值,字符串或运算式。

17Linux账号与身份管理

1.         UID0的时候,就是root
2.         通常UID1-99保留给系统预设账号,100-499保留给一些服务来使用
3.         2.6.x版本的kernel可以支持到2^32-1这么多个UID
4.         如果把用户的shell改成/sbin/nologin,则可以让这个账号无法登陆。这也可以用来制作纯pop邮件账号者的数据??
5.         对于/etc/shadow文件,在密码栏的第一个字符为*或者!时,表示这个账号不能用来登录
6.         /etc/shadow文件第三个部分,表示最近密码更改的日期,这个日期是从197011日开始作为1,这样来算时间的
7.         /etc/shadow文件第四个部分,表示该用户密码需要经过多少天才可以被更改,如果是0则表示随时可以改动
8.         /etc/shadow文件第五部分,表示密码需要重新变更的天数,如果是99999就不需要更新
9.         第六部分,密码需要变更期限前的警告期限
10.     第七部分,密码过期的宽恕时间
11.     第八部分,账户失效时间,和第三部分一样从1970.1.1开始的天数计算,这个字段被使用在收费服务中,让用户在规定时间之后不能再使用
12.     第九部分,是保留的部分,目前没有使用
13.     初始组
14.     有效组
15.     useradd在建立用户时,会参考这样几个文件
1/etc/default/useradd
2/etc/login.defs
(3) /etc/skel/*
 

18.Linux磁盘配额quota管理

19.例行性命令的建立

20.程序与资源管理

21.开机关机流程与loader

整个开机过程:
1.         bios信息
2.         POSTpower on self test
3.         读取MBR的信息,main boot record,大小不超过512byte
4.         boot loader
5.         init程序,它的设定文档是/etc/inittabrunlevel
6.         init[0-6]可以暂时更改系统的runlevel,下次重新开机之后恢复到原来的runlevel
7.         grub的作用,可以具有选项功能,识别硬盘的filesystem,指向内核,把内核读入内存
8.         由于MBR很小,boot loader就分为两个stageStage1loader的最小主程序放入MBR(或Super block,每一个partitionfirst sector),stage2loader的配置文件放在/boot下面
9.         Grub对磁盘的认识和Linux是不一样的,主要不同的地方是:
(1)    硬盘代号以( )括起来
(2)    hd表示硬盘(不管IDE还是SCSI,这点和Linux是不同的),后面接一组数字,以表示不同的硬盘和硬盘上的不同分区
(3)    以搜寻顺序作为硬盘的代号,而不是按照硬盘的排线顺序(这又是和Linux明显不同的地方),搜寻顺序是由BIOS设定的
(4)    第一个搜寻到的硬盘编号为0,每个硬盘上的第一个parttion编号为0,以此类推
(5)     
10.     ramdisk:
RamDisk也就是内存盘的意思.
  所谓的RAM驱动器,实际上是把系统内存划出一部分当作硬盘使用。对于操作系统来讲内存的存取速度远远大于机械磁盘,所以RAM驱动器肯定要比机械的硬盘快得多。你可以把整个应用程序都安装在RamDisk的驱动器中,然后用内存的速度运行它。使用RAM驱动器技术对于延长笔记本电脑电池使用时间也是十分有利的,因为这样做可以减少访问“耗电大户”——硬盘的次数。
11.     sfdfsd

22原始码和tarball套件管理员

1.         Makefile文件是由运行configure之后生成(或修改)的?
2.         binary file升级,目前有:RHup2dateyumMandrakeurpmiDebiandpkgSunUnixpkg,以及apt在线更新模式
3.         gcc的常用选项:
-Wall 产生更详细的编译过程信息
-O 产生最佳化的参数
-c 产生目标文件
 
 
4.         makefile文件中的变量和shell编程中的变量有所不同:
5.         tarball的源代码可以在windows下面编译吗?鸟歌的说法是理论上可以
6.         如果我在系统里面需要安装gcc,因为gcc也需要编译,是不是就只能也rpm等形式的包来安装,而不能由源代码安装?
7.         ff
8.        
9.        
10.    
 
 
 

23RPMSRPM管理

RPM包安装是基于这样一个设想:那就是我们使用的系统和厂商(提供RPM的商家)使用的系统是一样的,所以厂商就先在自己的系统上便宜之后,再提供给我们使用。
RPM是以一种数据库记录的方式来将你所需要的软件包安装到你的主机的一套管理程序。Rpm套件的相关信息在/varlibrpm里面
如果你自己主机的系统环境和产生rpm包的那台机器的环境不一样,就有可能出现问题。也就是说不同distribution下发布的RPM包,并不能用在其它的distribution上面。
SRPM里面就有原始码,但它仍然含有该软件包所需要的依赖的软件包的说明。。
直接由网络上免的某个安装文件,以网址来安装:
rpm –ivh
如果我们知道安装过程中会发生一些问题,但是我们还是执意要安装,则,可以使用—nodeps选项,其实这个选项就是要求不要区顾及软件包的依赖性问题
--nomd5 不检查rpm包的MD5信息
--noscripts 不想让该套件自行启用或者自行执行某些系统指令
--replacefiles 安装过程中出现了某个档案已经被安装在你的系统里面的信息时或者出现版本不合的信息(conflicting files)时,可以使用这个参数来直接覆盖档案,这个动作要慎用,必须要去顶被覆盖的档案是不是真的不重要,否则会欲哭无泪的
--test 测试一下该套件是否可以被安装到你的主机里面
 
RPM的升级和更新
-Uvh 若已经安装软件包,则升级,如没安装,就直接安装
-Fvh 若原本没安装,则不安装,只有已经安装了的才能升级
如何安装SRPMS
rpmbuild –rebuild  xxx.src.rpm 这个命令将进行编译和打包的动作,结束之后会产生RPM文件,但是rpm文件并没有安装到系统上,通常会生成在/usr/src/RPM/RPMS/i386/xxx.i386.rpm
rpmbuild –recompile 这个动作会直接地编译,打包然后安装
 

25.认识和分析日志文件

一个好的管理员应该认识到,一部主机负责的服务能够少就尽量少,比如邮件服务器就不要再当做WEB服务器,除了系统的安全性好(开的port少了)之外,日志文件也比较简单
 
Logrotate将旧的log文件改名,并建立新的log文件,并视设定将最旧的log文件删除
 

26.Linux的备份策略

 

28.Linux硬件检测和维护

 
 

29.内核编译与管理

内核2.42.6的差异是很大的,它们使用到的函数库基本上都不一样,所以在升级内核的时候,2.4.XX版就升级到2.4.XX版本的最新版,而不能由2.4.xx升级到2.6.xx。当然理论上还是可以,只不过过程很复杂。如果从2.4升级到2.6,绝大部分已经安装了的软件都需要重新安装。
2.6版本不一定就比2.4版本要新,因为这两个版本都在同时进行维护和升级。
每个内核的patch仅仅是针对前一版本的内核来进行的,所以,如果要从2.6.10升级到2.6.14,就得下载多个patch然后一级一级地进行升级。
 
kernel-devel包只包含用于内核开发环境所需的内核头文件以及Makefile,而kernel-souce包含所有内核源代码,用于重新编译内核。
如果仅仅是用于你自己编写的模块开发的话,因为只需引用相应的内核头文件,所以只有devel包即可,如果你要修改现有的内核源代码并重新编译,那必须是kernel-souce

kernel-souceRH某些版本之后不再附带在发行版中了,必须自己通过kernel-XXX.src.rpm做出来。
 
Diff 命令的用法
    该命令的功能为逐行比较两个文本文件,列出其不同之处。它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序。

    语法:diff [选项] file1 file2

   
说明:该命令告诉用户,为了使两个文件 file1 file2 一致,需要修改它们的哪些行。如果用 - ”表示 file1 file2,则表示标准输入。如果 file1 file2 是目录,那么 diff 将使用该目录中的同名文件进行比较。


    通常输出由下述形式的行组成:
    n1 a n3n4
    n1
n2 d n3
    n1
n2 c n3
n4

   
字母(adc)之前的行号(n1n2)是针对file1 的,其后面的行号(n3n4)是针对 file2 的。字母 ad c 分别表示附加、删除和修改操作。


    在上述形式的每一行的后面跟随受到影响的若干行,以 “<” 打头的行属于第一个文件,以 “>” 打头的行属于第二个文件。

    diff 能区别块和字符设备文件以及 FIFO(管道文件),不会把它们与普通文件进行比较。

    如果 file1 file2 都是目录,则 diff 会产生很多信息。如果一个目录中只有一个文件,则产生一条信息,指出该目录路径名和其中的文件名。

    该命令的各选项含义如下:

    -b 忽略行尾的空格,而字符串中的一个或多个空格符都视为相等。
    -c 采用上下文输出格式(提供三行上下文)。
    -C n 采用上下文输出格式(提供 n 行上下文)。
    -e 产生一个合法的 ed 脚本作为输出。
    -r file1 file2 是目录时,递归作用到各文件和目录上。
 

高级shell编程笔记

Chapter 3. Special Characters

#

Comments. Lines beginning with a # (with the exception of ) are comments and will not be executed.
Comments may also occur following the end of a command.在这种情况下#前面不能有空格
Comments may even be embedded within a .
Of course, a or an # in an statement does not begin a comment. Likewise, a # appears in and in .
The standard characters (" ' \) escape the #.

;

Command separator [semicolon]. Permits putting two or more commands on the same line.

;;

Terminator in a option [double semicolon].

;;&, ;&

in a case option ( of Bash).

.

"dot" command [period]. Equivalent to (see ). This is a bash .

.

"dot", as a component of a filename. When working with filenames, a leading dot is the prefix of a "hidden" file, a file that an will not normally show.
When considering directory names, a single dot represents the current working directory, and two dots denote the parent directory.
The dot often appears as the destination (directory) of a file movement command, in this context meaning current directory.

.

"dot" character match. When , as part of a , a "dot" .

"

[double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING. See .

'

[single quote]. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than "STRING". See .

,

. The comma operator links together a series of arithmetic operations. All are evaluated, but only the last one is returned.

,, ,

in parameter substitution (added in of Bash).

`

. The `command` construct makes available the output of command for assignment to a variable. This is also known as or backticks.

:

null command [colon]. This is the shell equivalent of a "NOP" (no op, a do-nothing operation). It may be considered a synonym for the shell builtin . The ":" command is itself a Bash , and its is true (0).
??????????这个符号还有很多意思,一时吸收不完

!

reverse (or negate) the sense of a test or exit status [bang].
In a different context, the ! also appears in .
In yet another context, from the command line, the ! invokes the Bash history mechanism (see ). Note that within a script, the history mechanism is disabled.

UNIX Shells by Example Fourth Edition

Chapter 1. Introduction to UNIX/Linux Shells

谈到shell的时候,他们总是和各种不同的Unix/Linux联系起来的:
Bourne and Korn shells are associated with AT&T UNIX,
C shell with Berkeley UNIX
Bash shell with Linux.
Unix的心脏是kernel,它在系统启动的时候装载而入。Shell是用来使用户和操作系统之间进行打交道的。
Shell是一个特殊的工具程序,它用做用户和操作系统心脏之间的接口,
可以用cat /etc/shells查看你的系统里面有哪些shell
当你启动操作系统的时候,第一个进程是init,它的PID1
The order of processing the command line is as follows:
The process of breaking the line up into tokens is called lexical analysis.
1.    History substitution is performed (if applicable).
2.    Command line is broken up into tokens, or words.
3.    History is updated (if applicable).
4.    Quotes are processed.
5.    Alias substitution and functions are defined (if applicable).
6.    Redirection, background, and pipes are set up.
7.    Variable substitution ($user, $name, etc.) is performed.
8.    Command substitution (echo "Today is `date`") is performed.
9.    Filename substitution, called globbing (cat abc.??, rm *.c, etc.) is performed.
10.Command is executed.
When the shell is ready to execute the command, it evaluates command types in the following order:
Numbers 3 and 4 are reversed for Bourne and Korn(88) shells. Number 3 does not apply for C and TC shells.
1.    Aliases
2.    Keywords
3.    Functions
4.    Built-in commands
5.    Executable programs
 
The ps Command
The ps command with its many options displays a list of the processes currently running in a number of formats. shows all processes that are running by users on a Linux system. (See for ps and its options.)
 
Note that "ps -aux" is distinct from "ps aux". The POSIX and UNIX standards require that "ps -aux" print all processes owned by a user named "x", as well as printing all processes that would be selected by the -a option. If the user named "x" does not exist, this ps may interpret the command as "ps aux" instead and print a warning. This behavior is intended to aid in transitioning old scripts and habits. It is fragile, subject to change, and thus should not be relied upon.
 
The pstree/ptree Command
Another way to see what processes are running and what processes are child processes is to use the pstree (Linux) or ptree (Solaris) command. The pstree command displays all processes as a tree with its root being the first process that runs, called init. If a user name is specified, then that user's processes are at the root of the tree. If a process spawns more than one process of the same name, pstree visually merges the identical branches by putting them in square brackets and prefixing them with the number of times the processes are repeated. To illustrate, in the httpd server process has started up 10 child processes. (See for a list of pstree options.)
 
System calls
退出状态时一个0-255之间的数,0代表程序成功运行,其它非0的数代表程序运行失败。
 
我一直对subshell的理解不深,难道在一个shell的命令行提示符下输入一条命令并执行它的时候就产生了一个subshell???
UID GID EUID EGID???????
The EUID and EGID determine what permissions a process has access to when reading, writing, or executing files. If the EUID of a process and the real UID of the owner of the file are the same, the process has the owner's access permissions for the file. If the EGID and real GID of a process are the same, the process has the owner's group privileges.
The chown Command
在Linux中,只有root才可以使用chown, 在UNIX中文件主人和root可以使用chown
 
Shell可以定义两种类型的变量:本地变量和环境变量. set命令可以显示出环境变量和本地变量。Local variables are private to the shell in which they are created and not passed on to any processes spawned from that shell. Environment variables, on the other hand, are passed from parent to child process, from child to grandchild, and so on. Some of the environment variables are inherited by the login shell from the /bin/login program. Others are created in the user initialization files, in scripts, or at the command line. If an environment variable is set in the child shell, it is not passed back to the parent. The shell env command will display the environment variables.
 
 
 
 
 
 
 
 
Table 1.2. Standard Signals
Number
Name
Description
Action
0
EXIT
Shell exits
Termination
1
SIGHUP
Terminal has disconnected
Termination
2
SIGINT
User presses Ctrl-C
Termination
3
SIGQUIT
User presses Ctrl-\
Termination
4
SIGILL
Illegal hardware instruction
Program error
5
SIGTRAP
Produced by debugger
Program error
8
SIGFPE
Arithmetic error; e.g., division by zero
Program error
9
SIGKILL
Cannot be caught or ignored
Termination
 

Chapter 2. Shell Programming QuickStart

#!/bin/bash
# GNU bash versions 2.x
# The Party Program––Invitations to friends from the "guest" file
guestfile=~/shell/guests
 
if [[ ! –e "$guestfile" ]]
 
    then
 
5       printf "${guestfile##*/} non–existent"
 
        exit 1
 
    fi
 
6   export PLACE="Sarotini's"
 
7   (( Time=$(date +%H) + 1 ))
 
8   declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
 
9   declare -i  n=0
 
10  for person in $(cat $guestfile)
 
    do
 
11      if  [[ $person == root ]]
 
        then
 
              continue
 
        else
 
              # Start of here document
 
12            mail –v –s "Party" $person <<- FINIS
 
              Hi $person! Please join me at $PLACE for a party!
 
              Meet me at $Time o'clock.
 
              I'll bring the ice cream. Would you please bring
 
              ${foods[$n] and anything else you would like to eat?
 
              Let me know if you can make it.
 
                     Hope to see you soon.
 
                          Your pal,
 
                          ellie@$(hostname)
 
              FINIS
 
13            n=n+1
 
14            if (( ${#foods[*]} ==  $n ))
 
              then
 
15               declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`
 
                                   sandwiches)
 
16            n=0
 
              fi
 
        fi
 
17  done
 
    printf "Bye..."

Chapter 3. Regular Expressions and Pattern Matching

/^[A–Z][a–z ]*3[0–5]/
/[a–z]*\ ./

Chapter 4. The grep Family

 

Unix环境下的高级编程

文件描述符:

对于 Linux 而言,所有对设备和文件的操作都使用文件描述符来进行的。文件描述符是一个非负的整数,它是一个索引值,并指向内核中每个进程打开文件的记录表。当打开一个现存文件或创建一个新文件时,内核就向进程返回一个文件描述符;当需要读写文件时,也需要把文件描述符作为参数传递给相应的函数。
   通常,一个进程启动时,都会打开 3 个文件:标准输入、标准输出和标准出错处理。这3 个文件分别对应文件描述符为 0、1和2也就是宏替换 STDIN_FILENO、STDOUT_FILENO和STDERR_FILENO,鼓励读者使用这些宏替换)。
可以如下查看LINUX默认的文件描述符,总共有1024个,对于大多数情况下是够用的:
# ulimit –n
1024

LinuxC语言

第一部分

 

7 结构体

简单的说:
结构体中每一个成员都有自己的内存空间。因此,结构体总大小一般情况下等于各成员大小之和(先不考虑内存对齐)。
共用体又叫联合体,每一个成员都共享内存空间。因此,共用体大小等于成员中最大的那个大小。
 
 

8 数组

1.       数组的下标也可以是表达式,但表达式的值必须是整型或字符型的。例如:
int i = 10;
count[i] = count[i+1];
 
2.       对于数组类型有一条特殊规则:数组名做右值使用时,自动转换成指向数组首元素的指针。所以上面的函数调用其实是传一个指针类型的参数,而不是数组类型的参数。接下来的几章里有的函数需要访问数组,我们就把数组定义为全局变量给函数访问,等以后我们讲了指针再使用传参的办法。这也解释了为什么数组类型不能互相赋值,上面提到的a = b这个表达式,ab都是数组类型的变量,但是b做右值使用,自动转换成指针类型,而左边仍是数组类型,所以编译器报的错误信息是error: incompatible types in assignment
3.        

2. 数组应用实例:统计随机数

随机数在某些场合(例如游戏程序)中是非常有用的,但是计算机执行每一条指令的结果都是确定的,没有一条指令产生的是随机数,调用C标准库得到的随机数其实是伪随机(Pseudorandom)数,是用数学公式算出来的确定的数,只不过这些数看起来很随机,并且从统计意义上也很接近均匀分布(Uniform Distribution)的随机数。
C标准库中生成伪随机数的是rand函数,使用这个函数需要包含头文件stdlib.h,它没有参数,返回值是一个介于0RAND_MAX之间的接近均匀分布的整数。RAND_MAX是头文件中定义的一个常量,在不同的平台上有不同的取值,但可以肯定它是一个非常大的整数。通常我们用到的随机数是限定在某个范围之中的,例如0~9,而不是0~RAND_MAX,我们可以用%运算符将rand函数的返回值处理一下int x = rand() % 10;
1、用rand函数生成10~20之间的随机整数,表达式应该怎么写?

3. 数组应用实例:直方图

有个例题没有搞定

4.    字符串

有一个容易出错的问题,就是打印字符数组的时候

5.    多维数组

除了第一维的长度可以由编译器自动计算而不需要指定,其余各维都必须明确指定长
度。
留给读者的思考问题是:(man - computer + 4) % 3 - 1这个神奇的表达式是如何比较
012这三个数字在“剪刀石头布”意义上的大小的?
 

9 编码风格

1.     缩进和空白

关于空白符并没有特别规定,主要有以下几条。
1、关键字if, while, for与其后的控制表达式的(括号之间插入一个空格分隔,但括号内的表达式应紧贴括号。例如:while (1);
2、双目运算符的两侧插入一个空格分隔,单目运算符和操作数之间不加空格,例
i = i + 1++i!(i < 1)-x&a[1]等。
3、后缀运算符和操作数之间也不加空格,例如取结构体成员s.a、函数调用foo(arg1)、取数组成员a[i]
4,号和;号之后要加空格,这是英文的书写习惯,例如for (i = 1; i < 10; i++)foo(arg1, arg2)
5、以上关于双目运算符和后缀运算符的规则不是严格要求,有时候为了突出优先级也可以写得更紧凑一些,例如for (i=1; i<10; i++)distance = sqrt(x*x + y*y)等。但是省略的空格一定不要误导了读代码的人,例如a||b && c很容易让人理解成错误的优先级。
6、由于标准的Linux终端是2480列的,接近或大于80个字符的较长语句要折行写,折行后用空格和上面的表达式或参数对齐,例如:
if (sqrt(x*x + y*y) > 5.0
    && x < 0.0
    && y > 0.0)
再比如:
foo(sqrt(x*x + y*y),
    a[i-1] + b[i-1] + c[i-1])
7、较长的字符串可以断成多个字符串然后分行书写,例如:
printf("This is such a long sentence that "
       "it cannot be held within a line\n");
C编译器会自动把相邻的多个字符串接在一起,以上两个字符串相当于一个字符串"This is
such a long sentence that it cannot be held within a line\n"
8、有的人喜欢在变量定义语句中用Tab字符,使变量名对齐,这样看起来也很好,但不是严格
要求的。
       int    a, b;
       double  c;
内核关于缩进的规则有以下几条。
1、要用缩进体现出语句块的层次关系,使用Tab字符缩进,不能用空格代替Tab。在标准
Linux终端上,一个Tab看起来是8个空格的宽度,有些编辑器可以设置一个Tab看起来是几个空格的宽度,建议设成8,这样大的缩进使代码看起来非常清晰。规定不能用空格代替Tab主要是不希望空格和Tab混在一起做缩进,如果混在一起用了,在某些编辑器里把Tab的宽度改了就会看起来非常混乱。
2if/elsewhiledo/whileforswitch这些可以带语句块的语句,语句块的{}应该和关
键字写在一起,用空格隔开,而不是单独占一行。例如应该这样写:
if (...) {
       →语句列表
} else if (...) {
       →语句列表 }
很多人习惯这样写:
if (...)
{
       →语句列表}
else if (...)
{
       →语句列表 }
内核的写法和[K&R]一致,好处是不必占用太多空行,使得一屏能显示更多代码。这两种写法用得都很广泛,只要在同一个项目中能保持统一就可以了。
3、函数定义的{}单独占一行,这一点和语句块的规定不同,例如:
int foo(int a, int b)
{
       →语句列表 }
4switch和语句块里的casedefault对齐写,也就是说语句块里的casedefault相对
switch不往里缩进。例如:
      switch (c) {
      case  'A':
             →语句列表       case  'B':
             →语句列表       default:
             →语句列表
      }
自己命名的标号(用于goto)必须顶头写不缩进,而不管标号下的语句缩进到第几层。
5、代码中每个逻辑段落之间应该用一个空行分隔开。例如每个函数定义之间应该插入一个空行,头文件、全局变量定义和函数定义之间也应该插入空行,例如:
6、一个函数的语句列表如果很长,也可以根据相关性分成若干组,用空行分隔,这条规定不是严格要求,一般变量定义语句组成一组,后面要加空行,return之前要加空行。
 

10 gdb

1.     单步执行和跟踪函数调用

11 排序与查找

 

12 栈与队列

2.     堆栈

 

第二部分. C语言本质

14 计算机中数的表示

 

2. 不同进制之间的换算

二进制小数可以这样定义:(0.A1A2A3...)2=A1×2-1+A2×2-2+A3×2-3+...
这也是从二进制小数到十进制小数的换算公式。从本节讲的十进制转二进制的推导过程出发,类比一下,十进制小数换算成二进制小数应该怎么计算?
 

3.     整数的加减运算

 

17 计算机体系结构基础

1. 内存与地址

与邮箱不同的是,一个地址所对应的存储单元只能存一个字节(byte),所以以前讲过的intfloat等多字节的数据类型保存在内存中要占用多个地址,这种情况下把起始地址当作这个数据的地址。
内存地址是从0开始编号的整数,最大编到多少取决于CPU的地址空间(Address Space)有多大。目前主流的处理器是32位或64位的,本书主要以32位的x86平台为例,所谓32位就是指地址是32位的,从0x0000 00000xffff ffff

2.     CPU

CPU周而复始地做同一件事:从内存取指令,然后解释执行它,然后再取下一条指令,再
解释执行。
CPU包含以下功能单元:
寄存器(Register),是CPU内部的高速存储器,像内存一样可以存取数据,但比访问内
存快得多。我们马上会讲到x86的寄存器如eaxebpeip等等,有些寄存器保存的数据只
能用于某种特定的用途,比如eip寄存器用作程序计数器,这称为特殊寄存器(Special-
purpose Register),而另外一些寄存器保存的数据可以用在各种运算和读写内存的指令
中,比如eax寄存器,这称为通用寄存器(General-purpose Register)。
程序计数器(PCProgram Counter),保存着CPU取指令的地址,每次CPU读出程序计
数器中保存的地址,然后按这个地址去内存中取指令,这时程序计数器保存的地址会自动
加上该指令的长度,指向内存中的下一条指令。
程序计数器通常是CPU的一个特殊寄存器,x86的程序计数器是特殊寄存器eip,由于地址
32位的,所以这个寄存器也是32位的,事实上通用寄存器也是32位的,所以也可以说处理器的位数是指它的寄存器的位数。处理器的位数也叫做字长,字(Word)这个概念用
得比较混乱,在有些上下文中指16位,在有些上下文中指32位(这种情况下16位被称为半字Half Word),在有些上下文中指处理器的字长,如果处理器是32位那么一个字就
32位,如果处理器是64位那么一个字就是64位。
指令解码器(Instruction Decoder)。CPU取上来的指令由若干个字节组成,这些字节中
有些位表示内存地址,有些位表示寄存器编号,有些位表示这种指令做什么操作,是加、
减、乘、除还是读、写,指令解码器负责解释这条指令的含义,然后调动相应的执行单元
去执行它。
算术逻辑单元(ALUArithmetic and Logic Unit)。如果解码器将一条指令解释为运算指
令,就调动算术逻辑单元去做运算,比如加减乘除、位运算、判断一个条件是否成立等。
运算结果可能保存在寄存器中,也可能保存到内存中。
地址和数据总线(Bus)。CPU和内存之间用地址总线、数据总线和控制线连接起
来,32位处理器有32条地址线和32条数据线[24],每条线上有10两种状态,32条线的状态就可以表示一个32位的数。如果在执行指令过程中需要访问内存,比如从内存读一个数到寄存器,则执行过程可以想像成这样:
 

3. 设备

 
有些设备像内存芯片一样连接到处理器的地址总线和数据总线,正因为地址线和数据线上可以挂多个设备和内存芯片所以才叫“总线”,但不同的设备和内存应该占不同的地址范围。访问这种设备就像访问内存一样,按地址读写即可,和访问内存不同的是,往一个地址写数据只是给设备发一个命令,数据不一定要保存,从一个地址读出的数据也不一定是先前保存在这个地址的数据,而是设备的某个状态。设备中可供读写访问的单元通常称为设备寄存器,操作设备的过程就是对这些设备寄存器做读写操作的过程,比如向串口发送寄存器里写数据,串口设备就会把数据发送出去,读串口接收寄存器的值,就可以读取串口设备接收到的数据。
 
 
CPU的角度来看,访问设备只有内存映射I/O和端口I/O两种,要么像内存一样访问,要么用一
种专用的指令访问。其实访问设备是相当复杂的,由于计算机的设备五花八门,各种设备的性
能要求都不一样,有的要求带宽大,有的要求响应快,有的要求热插拔,于是出现了各种适应
不同要求的设备总线,比如PCIAGPUSB1394SATA等等,这些设备总线并不直接
CPU相连,CPU通过内存映射I/O或端口I/O访问相应的总线控制器,通过它再去访问挂在总
线上的设备。所以上图中标有“设备”的框,可能是实际的设备,也可能是设备总线的控制器。
x86平台上,硬盘是ATASATASCSI总线上的设备,保存在硬盘上的程序是不能被CPU
接取指令执行的,操作系统在执行程序时会把它从硬盘拷到内存,这样CPU才可以取指令执
行,这个过程称为加载(Load)。程序加载到内存之后,成为操作系统调度执行的一个任务,就称为进程(Process)。进程和程序不是一一对应的。一个程序可以多次加载到内存,成为同
时运行的多个进程,例如可以同时开多个终端窗口,每个窗口都运行一个Shell进程,而它们对
应的程序都是磁盘上的/bin/bash
访问设备还有一点和访问内存不同。内存只是保存数据而不会产生新的数据,如果CPU不去读
它,它也不需要主动提供数据给CPU,所以内存总是被动地等待被读或被写。而设备往往会自
己产生数据,并且需要主动通知CPU来读这些数据,例如敲键盘产生一个输入字符,用户希望
计算机马上响应自己的输入,这就要求键盘设备主动通知CPU来读这个字符并做相应处理,给
用户响应。这是由中断(Interrupt)机制实现的,每个设备都有一条中断线,通过中断控制器连
接到CPU,当设备需要主动通知CPU时就引发一个中断信号,CPU正在执行的指令将被打断,
程序计数器会设置成某个固定的地址(这个地址由体系结构定义),于是CPU从这个地址开始
取指令(或者说跳转到这个地址),执行中断服务程序(ISRInterrupt Service Routine),完
成中断处理之后再返回先前被打断的地方执行后续指令。比如某种体系结构规定发生中断时跳
转到地址0x0000 0010执行,那么就要事先把一段ISR程序加载到这个地址,ISR程序是由内核
代码提供的,中断处理的步骤通常是先判断哪个设备引发了中断,然后调用该设备驱动程序提
供的中断处理函数(Interrupt Handler)做进一步处理。
由于各种设备的用途各不相同,设备寄存器中每个位的定义和操作方法也各不相同,所以每种
设备都需要专门的设备驱动程序(Device Driver),一个操作系统为了支持广泛的设备就需要
有大量的设备驱动程序,事实上,Linux内核源代码中绝大部分是设备驱动程序。设备驱动程序
通常是操作系统内核里的一组函数,主要是通过对设备寄存器的读写实现对设备的初始化、
读、写等操作,有些设备还要提供一个中断处理函数供ISR调用。

其它杂项

Linux文件系统的反删除方法

Linux下的文件一旦被删除,是难以恢复的。尽管删除命令只是在文件节点中作删除标记,并不真正清除文件内容,但是 其他用户和一些有写盘动作的进程会很快覆盖这些数据。不过,对于家庭单机使用的Linux,或者误删文件后及时补救,还是可以恢复的。对于很多用户在使用的系统,只能立即采用措施禁止其它用户有写盘的动作和写盘的进程。

1
Ext2文件系统结构的简单介绍

Linux所用的Ext2文件系统中,文件是以块为单位存储的,默认情况下每个块的大小是1K,不同的块以块号区分。每个文件还有一个节点,节点中包含 有文件所有者,读写权限,文件类型等信息。对于一个小于12个块的文件,在节点中直接存储文件数据块的块号。如果文件大于12个块,那么节点在12个块号 之后存储一个间接块的块号,在这个间接块号所对应的块中,存储有256个文件数据块的块号(Ext2fs中每个块号占用4字节,这样一个块中所能存储的块 号就是1024/4=256)。如果有更大的文件,那么还会在节点中出现二级间接块和三级间接块。


2
。恢复被误删文件的方法 (针对ext2/ext3文件系统)


首先以只读方式重新挂载被误删的文件所在分区。使用如下命令:(假设文件在/usr分区)
mount –r –n –o remount /usr
-r
表示只读方式挂载;-n表示不写入/etc/mtab,如果是恢复/etc上的文件,就加上这个参数。如果系统说xxx partion busy,可以用fuser命令查看一下是哪些进程使用这个分区上的文件:

fuser –v –m /usr
如果没有什么重要的进程,用以下命令停掉它们:

fuser -k –v –m /usr
然后就可以重新挂载这些文件系统了。

如果是把所有的文件统一安装在一个大的/分区当中,可以在boot提示符下用linux single进入单用户模式,尽量减少系统进程向硬盘写入数据的机会,要不干脆把硬盘挂在别的机器上。另外,恢复出来的数据不要写到/上面,避免破坏那些 有用的数据。如果机器上有dos/windows,可以写到这些分区上面:

mount –r –n /dev/hda1 /mnt/had
然后就可以执行debugfs:(假设Linux /dev/hda5

#debugfs /dev/hda5
就会出现debugfs提示符debugfs

使用lsdel命令可以列出很多被删除的文件的信息:

debugfs
lsdel
debugfs: 2692 deleted inodes found.
Inode Owner Mode Size Blocks Time deleted
164821 0 100600 8192 1/ 1 Sun May 13 19:22:46 2001
…………………………………………………………………………………
36137 0 100644 4 1/ 1 Tue Apr 24 10:11:15 2001
196829 0 100644 149500 38/ 38 Mon May 27 13:52:04 2001

debugfs:
列出的文件有很多(这里找到2692个),第一字段是文件节点号,第二字段是文件所有者,第三字段是读写权限,接下来是文件大小,占用块数,删除时间。然后就可以根据文件大小和删除日期判断那些是我们需要的。比如我们要恢复节点是196829的文件:

可以先看看文件数据状态:

debugfs
stat <196829>;
Inode: 196829 Type: regular Mode: 0644 Flags: 0x0 Version: 1
User: 0 Group: 0 Size: 149500
File ACL: 0 Directory ACL: 0
Links: 0 Blockcount: 38
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x31a9a574 -- Mon May 27 13:52:04 2001
atime: 0x31a21dd1 -- Tue May 21 20:47:29 2001
mtime: 0x313bf4d7 -- Tue Mar 5 08:01:27 2001
dtime: 0x31a9a574 -- Mon May 27 13:52:04 2001
BLOCKS:
594810 594811 594814 594815 594816 594817 ………………………………….
TOTAL: 38
然后就可以用dump指令恢复文件:

debugfs
dump <196829>; /mnt/hda/01.sav
这样就把文件恢复出来了。退出debugfs

debugfs
quit
另一种方法是手工编辑inode

debugfs
mi <196829>;
Mode [0100644]
User ID [0]
Group ID [0]
Size [149500]
Creation time [0x31a9a574]
Modification time [0x31a9a574]
Access time [0x31a21dd1]
Deletion time [0x31a9a574] 0
Link count [0] 1
Block count [38]
File flags [0x0]
Reserved1 [0]
File acl [0]
Directory acl [0]
Fragment address [0]
Fragment number [0]
Fragment size [0]
Direct Block #0 [594810]
…………………………….
Triple Indirect Block [0]
使用mi指令后每次显示一行信息以供编辑,其它行可以直接按回车表示确认,把deletion time改成0(未删除),Link count改成1。改好后退出debugfs

debugfs
quit
然后用fsck检查
/dev/hda5
fsck /dev/hda5
程序会说找到丢失的数据块,放在lost+found里面。这个目录里的文件就是我们要的东东。

Now all O.K. Good Luck.

在网上找到的,没有试过。
 
阅读(1847) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~