存档:2009年六月

数码摄影技巧

六月 27, 2009 | mysql | RSS 2.0

 今日看了看《跟我学数码摄影》,里面还是有些东西值得学习的,豆瓣的地址:http://www.douban.com/subject/3208677。
记录下他们介绍的原则:
摄影构图的基本原则
1 选择和突出主体
2 尽可能简单
3 似与不似之间
4 确定注视中心的位置:由矩形的某一个顶点向对角线做一条垂线,垂线与对角线相交的这个点式确定注视中心的最合适的位置
5 三分之一原则。
6 黄金分割和井字格
7中心对称构图
8对称与非对称
9 运用前景
10 明暗对比
11 虚实对比
12 横与竖
13 视线牵引
14 近大远小
15 抽象的摄影
16 视线运动的前方要留白
17 色彩的冷暖和象征
18 色彩的和谐
19 色彩的互补和对比
20 倾斜和动感

没有评论 »

awk使用找到mysql结果的数据

六月 26, 2009 | 读书 | RSS 2.0

mysql选择出的数据格式为:
*************************** 1. row ***************************
url_id: 1#386808#267#雕刻时光推荐好声音(第二场)—台湾组合乐队专场
*************************** 2. row ***************************
url_id: 1#387113#267#感恩父亲节专场演唱会温馨谢幕~
找出相应的uid的shell
sed ‘/*/d’ topiclist.txt |awk -F ‘#’ ‘BEGIN{ORS=”,”}{print $2}’|sort|uniq|sed ’s/.$//’

没有评论 »

数据库永久连接mysql_pconnect和mysql_connect

六月 25, 2009 | mysql | RSS 2.0

    一直对mysql_pconnect 和mysql_connect的理解差别是p链接是持久链接,不会关闭了链接,即使你使用了mysql_close();建立连接后,将保持sleep状态,可以使用show processlist 查看有哪些正在sleep的连接,这样p链接的好处是当有新的请求时就直接返回这个连接,感觉类似于http1.1协议的keep-alive的用法,到底是开个还是关呢还是不关呢,一般那些专家都告诉你,当用户量不多时,可以打开keep -live 这样可以,在http协议进行打交道时,给用户保持连接。用户第一次打开建立,然后继续浏览时就不需要新开了,提高用户的体验,当用户量比较大的时候,大量的保持连接会给服务器造成很大压力,所以需要找一个平衡点是开到底还是不开,回过头来看mysql_p 的理解:    在网上搜索了没有找到满意的答案,但是在竟然在手册上看到一个解释的比较完美的,看来以后应该多看手册:
    定义:
    永久的数据库连接是指在脚本结束运行时不关闭的连接。当收到一个永久连接的请求时。PHP 将检查是否已经存在一个(前面已经开启的)相同的永久连接。如果存在,将直接使用这个连接;如果不存在,则建立一个新的连接。所谓“相同”的连接是指用相同的用户名和密码到相同主机的连接。
    先看看手册上的总结:
    永久连接是为通常连接建立一对一的分布而设计的。这意味着必须能够保证在将永久连接替换为非永久连接时,脚本的行为不会改变。使用永久连接将(非常)有可能改变脚本的效率,但不改变其行为!
    如果永久连接并没有任何附加的功能,那么使用它有什么好处?
    答案非常简单――效率。当客户端对 SQL 服务器的连接请求非常频繁时,永久连接将更加高效。连接请求频繁的标准取决于很多因素。例如,数据库的种类,数据库服务和 web 服务是否在同一台服务器上,SQL 服务器如何加载负载等。但我们至少知道,当连接请求很频繁时,永久连接将显著的提高效率。它使得每个子进程在其生命周期中只做一次连接操作,而非每次在处理一个页面时都要向 SQL 服务器提出连接请求。这也就是说,每个子进程将对服务器建立各自独立的永久连接。例如,如果有 20 个不同的子进程运行某脚本建立了永久的 SQL 服务器永久连接,那么实际上向该 SQL 服务器建立了 20 个不同的永久连接,每个进程占有一个。
    注意,如果永久连接的子进程数目超过了设定的数据库连接数限制,系统将会产生一些缺陷。如果数据库的同时连接数限制为 16,而在繁忙会话的情况下,有 17 个线程试图连接,那么有一个线程将无法连接。如果这个时候,在脚本中出现了使得连接无法关闭的错误(例如无限循环),则该数据库的 16 个连接将迅速地受到影响。请查阅使用的数据库的文档,以获取关于如何处理已放弃的及闲置的连接的方法。
    警告:在使用永久连接时还有一些特别的问题需要注意。例如在永久连接中使用数据表锁时,如果脚本不管什么原因无法释放该数据表锁,其随后使用相同连接的脚本将会被永久的阻塞,使得需要重新启动 httpd 服务或者数据库服务。另外,在使用事务处理时,如果脚本在事务阻塞产生前结束,则该阻塞也会影响到使用相同连接的下一个脚本。不管在什么情况下,都可以通过使用 register_shutdown_function() 函数来注册一个简单的清理函数来打开数据表锁,或者回滚事务。或者更好的处理方法,是不在使用数据表锁或者事务处理的脚本中使用永久连接,这可以从根本上解决这个问题(当然还可以在其它地方使用永久连接)。

 web 服务器可以用三种方法来利用 PHP 生成 web 页面。

 第一种方法是将 PHP 用作一个“外壳”。以这种方法运行,PHP 会为向 web 服务器提出的每个 PHP 页面请求生成并结束一个 PHP 解释器线程。由于该线程会随每个请求的结束而结束,因此任何在这个线程中利用的任何资源(例如指向 SQL 数据库服务器的连接)都会随线程的结束而关闭。在这种情况下,使用永久连接不会获得任何地改变――因为它们根本不是永久的。

第二,也是最常用的方法,是把 PHP 用作多进程 web 服务器的一个模块,这种方法目前只适用于 Apache。对于一个多进程的服务器,其典型特征是有一个父进程和一组子进程协调运行,其中实际生成 web 页面的是子进程。每当客户端向父进程提出请求时,该请求会被传递给还没有被其它的客户端请求占用的子进程。这也就是说当相同的客户端第二次向服务端提出请求时,它将有可能被一个不同的子进程来处理。在开启了一个永久连接后,所有请求 SQL 服务的后继页面都能够重新使用这个已经建立的 SQL Server 连接。

最后一种方法是将 PHP 用作多线程 web 服务器的一个插件。目前 PHP 4 已经支持 ISAPI、WSAPI 和 NSAPI(在 Windows 环境下),这些使得 PHP 可以被用作诸如 Netscape FastTrack (iPlanet)、Microsoft’s Internet Information Server (IIS) 和 O’Reilly’s WebSite Pro 等多线程 web 服务器的插件。永久连接的行为和前面所描述的多过程模型在本质上是相同的。注意 PHP 3 不支持 SAPI。

没有评论 »

ssh tar 合作处理网络文件

六月 24, 2009 | linux | RSS 2.0

传输文件有时候考ryscn完成,但是有一些功能可以通过tar和ssh完成
先看一句 :
sed “s%^$BASEDIR/$module_name/%%” $listOP |
ssh $REMOTE_USER@$remote_host “mkdir -p $bpath $REMOTE_PATH; tar -C $REMOTE_PATH -jcf $bpath/$tarball.$VER_TODAY$remove_suffix -T – $remove_args”;
这句就是别人写的一个应用:
然后我发现了一些:
tar和find结合的:

find / -type f -print | grep -iE ‘.doc$’ | tar -czvf test.tar.gz
find . -mount -depth -type f -print | grep -iE ‘*.txt$’ | tar -czvf ../test.tar.gz
继续一些别的命令学习:
ssh myname@boxname ‘cd /logs; tar cf – $(find . -name “*.Z”)’ |tar xf –
(cd /bkup02/ieee/fulltext/2007; tar cf – $(find . -name xml.zip) ) | ssh user@xxx.xxx.xxx.xxx”tar -C /opt/backup/ieee/2007 -xf -”
user@xxx.xxx.xxx.xxx’s password:
tar cf – files | ssh surtr “(cd /home/joe/tmp; tar xf -)”
$ ssh surtr “(cd /home/joe/tmp; tar cvjf /home/joe/pkgs.tar.bz2 pkgs)”
&& scp surtr:~/pkgs.tar.bz2
学会了这些我们来个终极版:
地址在:
    http://ultra.ap.krakow.pl/~bar/DOC/ssh_backup.html

PUSH 将本地文件到远程服务器:

    * tar cvf – . | gzip -c -1 | ssh user@host cat “>” remotefile.gz
    * ssh target_address cat <localfile “>” remotefile
    * ssh target_address cat <localfile – “>” remotefile
    * cat localfile | ssh target_address cat “>” remotefile
    * cat localfile | ssh target_address cat – “>” remotefile
    * dd if=localfile | ssh target_address dd of=remotefile
    * ssh target_address cat <localfile “|” dd of=remotefile
    * ssh target_address cat – <localfile “|” dd of=remotefile
    * ( cd SOURCEDIR && tar cf – . ) | ssh target_address “(cd DESTDIR && tar xvpf – )”
    * ( cd SOURCEDIR && tar cvf – . ) | ssh target_address “(cd DESTDIR && cat – > remotefile.tar )”
    * ( cd SOURCEDIR && tar czvf – . ) | ssh target_address “(cd DESTDIR && cat – > remotefile.tgz )”
    * ( cd SOURCEDIR && tar cvf – . | gzip -1 -) | ssh target_address “(cd DESTDIR && cat – > remotefile.tgz )”
    * ssh target_address “( nc -l -p 9210 > remotefile & )” && cat source-file | gzip -1 – | nc target_address 9210
    * cat localfile | gzip -1 – | ssh target_address cat “>” remotefile.gz

PULL:把远程文件从远程弄到本地
    # ssh target_address cat remotefile > localfile
    # ssh target_address dd if=remotefile | dd of=localfile
    # ssh target_address cat “<” remotefile >localfile
    # ssh target_address cat “<” remotefile.gz | gunzip >localfile

COMPARE:
    # ###This one uses CPU cycles on the remote server to compare the files:
    # ssh target_address cat remotefile | diff – localfile
    # cat localfile | ssh target_address diff – remotefile
    # ###This one uses CPU cycles on the local server to compare the files:
    # ssh target_address cat <localfile “|” diff – remotefile
本地文件的一些操作
复制
    * ( cd SOURCEDIR && tar cf – . ) | (cd DESTDIR && tar xvpf – )

FTP VIEW:

    * ftp> get file.gif “| xv -”
    * ftp> get README “| more”

FTP PUSH:

    * ftp> put “| tar cvf – .” myfile.tar
    * ftp> put “| tar cvf – . | gzip ” myfile.tar.gz

FTP PULL:

    * ftp> get myfile.tar “| tar xvf -”

Pipes and Redirects:

    * zcat Fig.ps.Z | gv -
    * gunzip -c Fig.ps.gz | gv -
    * tar xvf mydir.tar
    * tar xvf – < mydir.tar
    * cat mydir.tar | tar xvf -
    * tar cvf mydir.tar .
    * tar cvf – . > mydir.tar
    * tar cf – . | (cd ~/newdir; tar xf -)
    * gunzip -c foo.gz > bar
    * cat foo.gz | gunzip > bar
    * zcat foo.gz > bar
    * gzip -c foo > bar.gz
    * cat foo | gzip > bar.gz
    * cat foo | gzip > bar.gz
相信你能学会很多从这里

没有评论 »

统计php.err里的错误

六月 23, 2009 | mysql, php | RSS 2.0

如题:

awk -F “]” ‘{if(NF>=3){print $3} else{print $2}}’ php.err |sort|uniq|sed -e ’s/^:/ /’|awk ‘{print NR,$0}’ >only.err

删除掉script里的文本:

:%s#(<script contentType=”application/x-javascript”>)[^<]*(</script>)#12#

同事学习了下awk里的循环:
(摘抄)
awk条件语句

条件语句

awk中的条件语句是从C语言中借鉴过来的,可控制程序的流程。
14.5.1. if语句

格式:
{if (expression){
statement; statement; …
}
}

$ awk ‘{if ($1 <$2) print $2 “too high”}’ test。如果第一个域小于第二个域则打印。

$ awk ‘{if ($1 < $2) {count++; print “ok”}}’ test.如果第一个域小于第二个域,则count加一,并打印ok。
14.5.2. if/else语句,用于双重判断。

格式:
{if (expression){
statement; statement; …
}
else{
statement; statement; …
}
}

$ awk ‘{if ($1 > 100) print $1 “bad” ; else print “ok”}’ test。如果$1大于100则打印$1 bad,否则打印ok。

$ awk ‘{if ($1 > 100){ count++; print $1} else {count–; print $2}’ test。如果$1大于100,则count加一,并打印$1,否则count减一,并打印$1。
14.5.3. if/else else if语句,用于多重判断。

格式:
{if (expression){
statement; statement; …
}
else if (expression){
statement; statement; …
}
else if (expression){
statement; statement; …
}
else {
statement; statement; …
}
}

14.6. 循环

*

awk有三种循环:while循环;for循环;special for循环。
*

$ awk ‘{ i = 1; while ( i <= NF ) { print NF,$i; i++}}’ test。变量的初始值为1,若i小于可等于NF(记录中域的个数),则执行打印语句,且i增加1。直到i的值大于NF.
*

$ awk ‘{for (i = 1; i *

breadkcontinue语句。break用于在满足条件的情况下跳出循环;continue用于在满足条件的情况下忽略后面的语句,直接返回循环的顶端。如:

{for ( x=3; x<=NF; x++)
if ($x<0){print “Bottomed out!”; break}}
{for ( x=3; x<=NF; x++)
if ($x==0){print “Get next item”; continue}}

*

next语句从输入文件中读取一行,然后从头开始执行awk脚本。如:

{if ($1 ~/test/){next}
else {print}
}

*

exit语句用于结束awk程序,但不会略过END块。退出状态为0代表成功,非零值表示出错。

14.7. 数组

awk中的数组的下标可以是数字和字母,称为关联数组。
14.7.1. 下标与关联数组

*

用变量作为数组下标。如:$ awk {name[x++]=$2};END{for(i=0;i *

special for循环用于读取关联数组中的元素。格式如下:

{for (item in arrayname){
print arrayname[item]
}
}

$ awk ‘/^tom/{name[NR]=$1}; END{for(i in name){print name[i]}}’ test。打印有值的数组元素。打印的顺序是随机的。
*

用字符串作为下标。如:count["test"]
*

用域值作为数组的下标。一种新的for循环方式,for (index_value in array) statement。如:$ awk’{count[$1]++} END{for(name in count) print name,count[name]}’test。该语句将打印$1中字符串出现的次数。它首先以第一个域作数组count的下标,第一个域变化,索引就变化。
*

delete函数用于删除数组元素。如:$ awk ‘{line[x++]=$1} END{for(x in line) delete(line[x])}’test。分配给数组line的是第一个域的值,所有记录处理完成后,special for循环将删除每一个元素。

没有评论 »

Crucial Concepts Behind Advanced Regular Expressions(转)

六月 20, 2009 | linux | RSS 2.0

转:http://www.smashingmagazine.com/2009/05/06/introduction-to-advanced-regular-expressions/

Regular expressions (or regex) are a powerful way to traverse largestrings in order to find information. They rely on underlying patternsin a string’s structure to work their magic. Unfortunately, simpleregular expressions are unable to cope with complex patterns andsymbols. To deal with this dilemma, you can use advanced regular expressions.

Below, we present an introduction to advanced regular expressions,with eight commonly used concepts and examples. Each example outlines asimple way to match patterns in complex strings. If you do not yet haveexperience with basic regular expressions, have a look at this article to get started. The syntax used here matches PHP’s Perl-compatible regular expressions.

1. Greediness/Laziness

Greed

All regex repetition operators are greedy. They tryto match as much as possible in a string. Unfortunately, this might notalways be a desired effect. Thus, lazy operators are used to solve thisproblem. They only match the smallest possible pattern and are used byadding a ‘?’ after the respective greedy operator. Alternatively, the‘U’ modifier may be used to make all repetiton operators lazy.Differentiating between greediness and laziness is key to fullyunderstanding advanced regular expressions.

Greedy Operators

The * operator matches the previous expression 0 or more times. It is a greedy operator. Consider the following expression:

  1. preg_match( ‘/<h1>.*</h1>/’, ’<h1>This is a heading.</h1>
  2. <h1>This is another one.</h1>’, $matches );
preg_match( '/<h1>.*</h1>/', '<h1>This is a heading.</h1><h1>This is another one.</h1>', $matches );

Recall that a . means any character except a new line. The aboveregular expression is looking for an h1 tag and all of its contents. Ituses the . and * operators to constantly match anything inside the tag.This pattern will match:

  1. <h1>This is a heading.</h1><h1>This is another one.</h1>
<h1>This is a heading.</h1><h1>This is another one.</h1>

It returns the whole string. The * operator will continuously matcheverything — even the middle closing h1 tag — because it is greedy.Matching the whole string is the best it can do.

Lazy Operators

Let’s change the above operator by adding a ‘?’ after it. This will make it lazy:

  1. /<h1>.*?</h1>/
/<h1>.*?</h1>/

The regex now fulfills its duty and matches only the first h1 tag.Another greedy operator that uses this same property is {n,}. Thismatches the previous expression n or more times. If it is used withouta question mark, it looks for the most repetitions possible. Otherwise,it starts from n repetitions:

  1. # Set up a String
  2. $str = ‘hihi’;
  3. # Match it using the greedy {n,} operator
  4. preg_match( ‘/(hi){1,}/’, $str, $matches ); # matches[0] will be ’hihi’
  5. # Match it with the lazy {n,}? operator
  6. preg_match( ‘/(hi){1,}?/’, $str, $matches ); # matches[0] will be ’hi’
# Set up a String$str = 'hihi';# Match it using the greedy {n,} operatorpreg_match( '/(hi){1,}/', $str, $matches ); # matches[0] will be 'hihi'# Match it with the lazy {n,}? operatorpreg_match( '/(hi){1,}?/', $str, $matches ); # matches[0] will be 'hi'

2. Back Referencing

Back Referencing

What it does

Back referencing is a way to refer to previouslymatched patterns inside a regular expression. For example, take a lookat this simple regex that matches an expression in quotes:

  1. # Set up an array of matches
  2. $matches = array();
  3. # Create a String
  4. $str = “”This is a ’string’”";
  5. # Traverse it with regular expressions
  6. preg_match( “/(”|’).*?(”|’)/”, $str, $matches );
  7. # Print the whole match
  8. echo $matches[0];
# Set up an array of matches$matches = array();# Create a String$str = ""This is a 'string'"";# Traverse it with regular expressionspreg_match( "/("|').*?("|')/", $str, $matches );# Print the whole matchecho  $matches[0];

Unfortunately, this will not correctly match the string. Instead, it will print:

  1. “This is a ’
"This is a '

This regular expression matches the opening double quote but finds adifferent type of quote to close it. This is because it was given theoption of picking a double or single quote at the end. In order to fixthis, you can use back referencing. The expressions 1, 2, …., 9 hold references to previously captured subpatterns. The first matched quote, in this case, will be held by the variable 1.

How to Use It

In order to apply this concept to the aforementioned example, use 1 in place of the last quote:

  1. preg_match( ‘/(”|’).*?1/’, $str, $matches );
preg_match( '/("|').*?1/', $str, $matches );

This will now correctly return:

  1. “This is a ’string’”
"This is a 'string'"

Remember that back referencing may also be used by preg_replace.Note that instead of 1 … 9, you should use $1 … $9 … $n (any numberof these will work). For example, if you want to replace all paragraphtags with text that represents them, use:

  1. $text = preg_replace( ‘/<p>(.*?)</p>/’,
  2. “&lt;p&gt;$1&lt;/p&gt;”, $html );
$text = preg_replace( '/<p>(.*?)</p>/',"&lt;p&gt;$1&lt;/p&gt;", $html );

The $1 back reference holds the text inside the paragraph and isbeing used in the replace pattern itself. This completely validexpression shows an easy way to access matched patterns even whilereplacing.

3. Named Groups

When using multiple back references, a regular expression canquickly become confusing and hard to understand. An alternative way toback reference is by using named groups. A named group is specified by using (?P<name>pattern),where name is the name of the group and pattern is the regularexpression in the group itself. The group can then be referred to by(?P=name). For example, consider the following:

  1. /(?P<quote>”|’).*?(?P=quote)/
/(?P<quote>"|').*?(?P=quote)/

The above expression will create the same effect as the previousback reference example, but by instead using named groups. It is alsosignificantly easier to read.

Named groups are also useful when sifting through the array ofmatches. The name given to a specific pattern is also the key of thecorresponding matches array.

  1. preg_match( ‘/(?P<quote>”|’)/’, ”‘String’“, $matches );
  2. # This will print ”‘”
  3. echo $matches[1];
  4. # This will also print ”‘”, as it is a named group
  5. echo $matches['quote'];
preg_match( '/(?P<quote>"|')/', "'String'", $matches );# This will print "'"echo $matches[1];# This will also print "'", as it is a named groupecho $matches['quote'];

Thus, named groups not only make code easier to read but also organize it.

4. Word Boundaries

Word Boundaries

Word boundaries are places in a string that comebetween a word character and a non-word character. The specialty ofthese boundaries is the fact that they don’t actually match acharacter. Their length is zero. The b regular expression matches any word boundary.

Unfortunately, boundaries are so often skimmed over that many do notrecognize their real significance. For example, let’s say you want tomatch the word “import”:

  1. /import/
/import/

Watch out! Regular expressions can be tricky. The above expression will also match:

  1. important
important

You may think it is as simple as adding a space before and after import to prevent these bogus matches:

没有评论 »

dd奇技淫巧

六月 18, 2009 | linux | RSS 2.0

在cu上看到一个帖子:http://bbs2.chinaunix.net/viewthread.php?tid=1469980&extra=&page=1

怎样对一个大文件的某个字节进行直接修改

比如我想把一个很大的视频文件中的第i个字节的值改成0×41(也就是大写字母A的ASCII值),
不用C的话,有没有现成的工具可以用啊?
实现:echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc
股沟之:

一、dd命令的解释。

dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。

注意:指定数字的地方若以下列字符结尾则乘以相应的数字:b=512;c=1;k=1024;w=2

参数:

1. if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >

2. of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >

3. ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。

    obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。

    bs=bytes:同时设置读入/输出的块大小为bytes个字节。

4. cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。

5. skip=blocks:从输入文件开头跳过blocks个块后再开始复制。

6. seek=blocks:从输出文件开头跳过blocks个块后再开始复制。

注意:通常只用当输出文件是磁盘或磁带时才有效,即备份到磁盘或磁带时才有效。

7. count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。

8. conv=conversion:用指定的参数转换文件。

    ascii:转换ebcdic为ascii

     ebcdic:转换ascii为ebcdic

    ibm:转换ascii为alternate ebcdic

    block:把每一行转换为长度为cbs,不足部分用空格填充

    unblock:使每一行的长度都为cbs,不足部分用空格填充

    lcase:把大写字符转换为小写字符

    ucase:把小写字符转换为大写字符

    swab:交换输入的每对字节

     noerror:出错时不停止

     notrunc:不截短输出文件

    sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

二、dd应用实例。

1.将本地的/dev/hdb整盘备份到/dev/hdd

   dd if=/dev/hdb of=/dev/hdd

2.将/dev/hdb全盘数据备份到指定路径的image文件

   dd if=/dev/hdb of=/root/image

3.将备份文件恢复到指定盘

   dd if=/root/image of=/dev/hdb

4.备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径

    dd if=/dev/hdb | gzip > /root/image.gz

5.将压缩的备份文件恢复到指定盘

   gzip -dc /root/image.gz | dd of=/dev/hdb

6.备份磁盘开始的512个字节大小的MBR信息到指定文件

   dd if=/dev/hda of=/root/image count=1 bs=512

   count=1指仅拷贝一个块;bs=512指块大小为512个字节

   恢复:dd if=/root/image of=/dev/hda

7.备份软盘

   dd if=/dev/fd0 of=disk.img count=1 bs=1440k (即块大小为1.44M)

8.拷贝内存内容到硬盘

   dd if=/dev/mem of=/root/mem.bin bs=1024 (指定块大小为1k)  

9.拷贝光盘内容到指定文件夹,并保存为cd.iso文件

   dd if=/dev/cdrom(hdc) of=/root/cd.iso

10.增加swap分区文件大小

第一步:创建一个大小为256M的文件:

dd if=/dev/zero of=/swapfile bs=1024 count=262144

第二步:把这个文件变成swap文件:

mkswap /swapfile

第三步:启用这个swap文件:

swapon /swapfile

第四步:编辑/etc/fstab文件,使在每次开机时自动加载swap文件:

/swapfile    swap    swap    default   0 0

11.销毁磁盘数据

     dd if=/dev/urandom of=/dev/hda1

注意:利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据。

12.测试硬盘的读写速度

     dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

      dd if=/root/1Gb.file bs=64k | dd of=/dev/null

通过以上两个命令输出的命令执行时间,可以计算出硬盘的读、写速度。

13.确定硬盘的最佳块大小:

     dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

     dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file

     dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file

     dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

通过比较以上命令输出中所显示的命令执行时间,即可确定系统最佳的块大小。

14.修复硬盘:

     dd if=/dev/sda of=/dev/sda 或dd if=/dev/hda of=/dev/hda

当硬盘较长时间(一年以上)放置不使用后,磁盘上会产生magnetic fluxpoint,当磁头读到这些区域时会遇到困难,并可能导致I/O错误。当这种情况影响到硬盘的第一个扇区时,可能导致硬盘报废。上边的命令有可能使这些数据起死回生。并且这个过程是安全、高效的。

/dev/null和/dev/zero的区别

/dev/null,外号叫无底洞,你可以向它输出任何数据,它通吃,并且不会撑着!
/dev/zero,是一个输入设备,你可你用它来初始化文件。

/dev/null,外号叫无底洞,你可以向它输出任何数据,它通吃,并且不会撑着!
/dev/zero,是一个输入设备,你可你用它来初始化文件。

/dev/null——它是空设备,也称为位桶(bit bucket)。任何写入它的输出都会被抛弃。如果不想让消息以标准输出显示或写入文件,那么可以将消息重定向到位桶。
/dev/zero——该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。

oracle@localhost oracle]$if=/dev/zero of=./test.txt bs=1k count=1
oracle@localhost oracle]$ ls -l
total 4
-rw-r–r–    1 oracle   dba          1024 Jul 15 16:56 test.txt

eg,

    find / -name access_log  2>/dev/null

使用/dev/null
把/dev/null看作”黑洞”. 它非常等价于一个只写文件. 所有写入它的内容都会永远丢失. 而尝试从它那儿读取内容则什么也读不到. 然而, /dev/null对命令行和脚本都非常的有用.

禁止标准输出.    1 cat $filename >/dev/null
   2 # 文件内容丢失,而不会输出到标准输出.
 

禁止标准错误 (来自例子 12-3).    1 rm $badname 2>/dev/null
   2 #           这样错误信息[标准错误]就被丢到太平洋去了.
 

禁止标准输出和标准错误的输出.    1 cat $filename 2>/dev/null >/dev/null
   2 # 如果”$filename”不存在,将不会有任何错误信息提示.
   3 # 如果”$filename”存在, 文件的内容不会打印到标准输出.
   4 # 因此Therefore, 上面的代码根本不会输出任何信息.
   5 #
   6 #  当只想测试命令的退出码而不想有任何输出时非常有用。
   7 #
   8 #
   9 # cat $filename &>/dev/null
  10 #     也可以, 由 Baris Cicek 指出.
 

Deleting contents of a file, but preserving the file itself,with all attendant permissions (from Example 2-1 and Example 2-3):    1cat /dev/null > /var/log/messages
   2 #  : > /var/log/messages   有同样的效果, 但不会产生新的进程.(因为:是内建的)
   3
   4 cat /dev/null > /var/log/wtmp
 

自动清空日志文件的内容 (特别适合处理这些由商业Web站点发送的讨厌的”cookies”):

——————————————————————————–

例子 28-1. 隐藏cookie而不再使用

   1 if [ -f ~/.netscape/cookies ]  # 如果存在则删除.
   2 then
   3   rm -f ~/.netscape/cookies
   4 fi
   5
   6 ln -s /dev/null ~/.netscape/cookies
   7 # 现在所有的cookies都会丢入黑洞而不会保存在磁盘上了.
 

——————————————————————————–

使用/dev/zero
像/dev/null一样, /dev/zero也是一个伪文件,但它实际上产生连续不断的null的流(二进制的零流,而不是ASCII型的). 写入它的输出会丢失不见,而从/dev/zero读出一连串的null也比较困难, 虽然这也能通过od或一个十六进制编辑器来做到./dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件.

——————————————————————————–

例子 28-2. 用/dev/zero创建一个交换临时文件

   1 #!/bin/bash
   2 # 创建一个交换文件.
   3
   4 ROOT_UID=0         # Root 用户的 $UID 是 0.
   5 E_WRONG_USER=65    # 不是 root?
   6
   7 FILE=/swap
   8 BLOCKSIZE=1024
   9 MINBLOCKS=40
  10 SUCCESS=0
  11
  12
  13 # 这个脚本必须用root来运行.
  14 if [ "$UID" -ne "$ROOT_UID" ]
  15 then
  16   echo; echo “You must be root to run this script.”; echo
  17   exit $E_WRONG_USER
  18 fi 
  19  
  20
  21 blocks=${1:-$MINBLOCKS}          #  如果命令行没有指定,
  22                                  #+ 则设置为默认的40块.
  23 # 上面这句等同如:
  24 # ————————————————–
  25 # if [ -n "$1" ]
  26 # then
  27 #   blocks=$1
  28 # else
  29 #   blocks=$MINBLOCKS
  30 # fi
  31 # ————————————————–
  32
  33
  34 if [ "$blocks" -lt $MINBLOCKS ]
  35 then
  36   blocks=$MINBLOCKS              # 最少要有 40 个块长.
  37 fi 
  38
  39
  40 echo “Creating swap file of size $blocks blocks (KB).”
  41 dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks  # 把零写入文件.
  42
  43 mkswap $FILE $blocks             # 将此文件建为交换文件(或称交换分区).
  44 swapon $FILE                     # 激活交换文件.
  45
  46 echo “Swap file created and activated.”
  47
  48 exit $SUCCESS
 

——————————————————————————–

关于 /dev/zero 的另一个应用是为特定的目的而用零去填充一个指定大小的文件, 如挂载一个文件系统到环回设备 (loopback device) (参考例子 13-8) 或”安全地” 删除一个文件(参考例子 12-55).

——————————————————————————–

例子 28-3. 创建ramdisk

   1 #!/bin/bash
   2 # ramdisk.sh
   3
   4 #  “ramdisk”是系统RAM内存的一段,
   5 #+ 它可以被当成是一个文件系统来操作.
   6 #  它的优点是存取速度非常快 (包括读和写).
   7 #  缺点: 易失性, 当计算机重启或关机时会丢失数据.
   8 #+       会减少系统可用的RAM.
   9 #
  10 #  那么ramdisk有什么作用呢?
  11 #  保存一个较大的数据集在ramdisk, 比如一张表或字典,
  12 #+ 这样可以加速数据查询, 因为在内存里查找比在磁盘里查找快得多.
  13
  14
  15 E_NON_ROOT_USER=70             # 必须用root来运行.
  16 ROOTUSER_NAME=root
  17
  18 MOUNTPT=/mnt/ramdisk
  19 SIZE=2000                      # 2K 个块 (可以合适的做修改)
  20 BLOCKSIZE=1024                 # 每块有1K (1024 byte) 的大小
  21 DEVICE=/dev/ram0               # 第一个 ram 设备
  22
  23 username=`id -nu`
  24 if [ "$username" != "$ROOTUSER_NAME" ]
  25 then
  26   echo “Must be root to run “`basename $0`”.”
  27   exit $E_NON_ROOT_USER
  28 fi
  29
  30 if [ ! -d "$MOUNTPT" ]         #  测试挂载点是否已经存在了,
  31 then                           #+ 如果这个脚本已经运行了好几次了就不会再建这个目录了
  32   mkdir $MOUNTPT               #+ 因为前面已经建立了.
  33 fi
  34
  35 dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE  # 把RAM设备的内容用零填充.
  36                                                       # 为何需要这么做?
  37 mke2fs $DEVICE                 # 在RAM设备上创建一个ext2文件系统.
  38 mount $DEVICE $MOUNTPT         # 挂载设备.
  39 chmod 777 $MOUNTPT             # 使普通用户也可以存取这个ramdisk.
  40                                # 但是, 只能由root来缷载它.
  41
  42 echo “”$MOUNTPT” now available for use.”
  43 # 现在 ramdisk 即使普通用户也可以用来存取文件了.
  44
  45 #  注意, ramdisk是易失的, 所以当计算机系统重启或关机时ramdisk里的内容会消失.
  46 #
  47 #  拷贝所有你想保存文件到一个常规的磁盘目录下.
  48
  49 # 重启之后, 运行这个脚本再次建立起一个 ramdisk.
  50 # 仅重新加载 /mnt/ramdisk 而没有其他的步骤将不会正确工作.
  51
  52 #  如果加以改进, 这个脚本可以放在 /etc/rc.d/rc.local,
  53 #+ 以使系统启动时能自动设立一个ramdisk.
  54 #  这样很合适速度要求高的数据库服务器.
  55
  56 exit 0

这样,一些诸如一些错误信息就不会显示出来

没有评论 »