﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-I smell magic in the air-随笔分类-shell</title><link>http://www.cppblog.com/izualzhy/category/16309.html</link><description>坚持 相信自己</description><language>zh-cn</language><lastBuildDate>Thu, 06 Dec 2012 22:51:47 GMT</lastBuildDate><pubDate>Thu, 06 Dec 2012 22:51:47 GMT</pubDate><ttl>60</ttl><item><title>linux&amp;shell下的字符串操作</title><link>http://www.cppblog.com/izualzhy/archive/2012/12/01/195859.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 01 Dec 2012 04:24:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/12/01/195859.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/195859.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/12/01/195859.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/195859.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/195859.html</trackback:ping><description><![CDATA[shell本身对于字符串的操作已经是非常强大的了，很久以来都只是用到什么就查下什么，今天总结了一下，希望对看到这篇文章的读者有所帮助。<br />例子和说明都写在一起了，感觉这样有助于记忆。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->string=<span style="color: #800000; ">"</span><span style="color: #800000; ">This&nbsp;is&nbsp;a&nbsp;test&nbsp;string</span><span style="color: #800000; ">"</span><br /><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">string的长度</span><span style="color: #008000; "><br /></span>echo&nbsp;${<span style="color: #008000; ">#</span><span style="color: #008000; ">string}</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">expr同样可以获取长度</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">但在文件里执行下面这句会出错，为什么？请指教</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">expr&nbsp;length&nbsp;${string}</span><span style="color: #008000; "><br /></span><br />echo<br /><span style="color: #008000; ">#</span><span style="color: #008000; ">提取子串</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${string:position}&nbsp;在$string中，从位置$position开始提取子串</span><span style="color: #008000; "><br /></span>echo&nbsp;${string:2}<br /><span style="color: #008000; ">#</span><span style="color: #008000; ">${string:poistion:length}&nbsp;在$string中，从位置$position开始提取长度为$length的子串</span><span style="color: #008000; "><br /></span>echo&nbsp;${string:2:5}<br /><br />echo<br /><span style="color: #008000; ">#</span><span style="color: #008000; ">删除操作</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${string#substring}&nbsp;从变量$string的开头，删除最短匹配$substring的子串</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${string##substring}&nbsp;从变量$string的开头，删除最长匹配$substring的子串</span><span style="color: #008000; "><br /></span>echo&nbsp;${string<span style="color: #008000; ">#</span><span style="color: #008000; ">This&nbsp;is&nbsp;a&nbsp;test}</span><span style="color: #008000; "><br /></span>echo&nbsp;${string<span style="color: #008000; ">#</span><span style="color: #008000; ">*is}</span><span style="color: #008000; "><br /></span>echo&nbsp;${string<span style="color: #008000; ">#</span><span style="color: #008000; ">#*is}</span><span style="color: #008000; "><br /></span><br />echo<br /><span style="color: #008000; ">#</span><span style="color: #008000; ">${string%substring}&nbsp;从变量$string的结尾，删除最短匹配$substring的子串</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${string%%substring}&nbsp;从变量$string的结尾，删除最长匹配$substring的子串</span><span style="color: #008000; "><br /></span>string=<span style="color: #800000; ">"</span><span style="color: #800000; ">i|ban|banana</span><span style="color: #800000; ">"</span><br />echo&nbsp;${string%ban*}<br />echo&nbsp;${string%%ban*}<br /><span style="color: #008000; ">#</span><span style="color: #008000; ">关于#%这个如果觉得难记，可以看下键盘,这么来记：</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">&nbsp;#在$前面，%在$后面，$的作用就是取字符串</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">使用下上面介绍过的功能，猜下输出?</span><span style="color: #008000; "><br /></span>string=<span style="color: #800000; ">"</span><span style="color: #800000; ">/etc/init.d/profile</span><span style="color: #800000; ">"</span><br />echo&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">Dir:&nbsp;</span><span style="color: #800000; ">"</span>&nbsp;${string%/*}<br />echo&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">File:&nbsp;</span><span style="color: #800000; ">"</span>&nbsp;${string<span style="color: #008000; ">#</span><span style="color: #008000; ">#*/}</span><span style="color: #008000; "><br /></span>FileName=${string<span style="color: #008000; ">#</span><span style="color: #008000; ">#*/}</span><span style="color: #008000; "><br /></span>echo&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">FileName&nbsp;Length:&nbsp;</span><span style="color: #800000; ">"</span>&nbsp;${<span style="color: #008000; ">#</span><span style="color: #008000; ">FileName}</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">Note:</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${变量名#substring正则表达式}&nbsp;从字符串开头开始匹配substirng,删除匹配上的表达式</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${变量名%substring正则表达式}&nbsp;从字符串结尾开始匹配substring,删除匹配上的表达式</span><span style="color: #008000; "><br /></span><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">${string/substring/replacement}&nbsp;使用$replacement,来代替第一个匹配的$substring</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">${string//substring/replacement}&nbsp;使用$replacement,来代替每一个匹配的$substring</span><span style="color: #008000; "><br /></span>string=<span style="color: #800000; ">"</span><span style="color: #800000; ">piapiapia</span><span style="color: #800000; ">"</span><br />echo&nbsp;${string/pia/ufo}<br />echo&nbsp;${string//pia/ufo}<br /><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">{string/#substring/replacement}&nbsp;如果$stirng的前缀匹配$substring,那么就用$replacement来代替匹配到的$substring</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">{string/%substring/replacement}&nbsp;如果$stirng的后缀匹配$substring,那么就用$replacement来代替匹配到的$substring</span><span style="color: #008000; "><br /></span>string=<span style="color: #800000; ">"</span><span style="color: #800000; ">i&nbsp;hate&nbsp;myself</span><span style="color: #800000; ">"</span><br />echo&nbsp;${string/<span style="color: #008000; ">#</span><span style="color: #008000; ">you/ufo}</span><span style="color: #008000; "><br /></span>echo&nbsp;${string/<span style="color: #008000; ">#</span><span style="color: #008000; ">i/ufo}</span><span style="color: #008000; "><br /></span>echo&nbsp;${string/%self/slefish}<br /><br />string=<span style="color: #800000; ">"</span><span style="color: #800000; ">/etc/init.d/profile</span><span style="color: #800000; ">"</span><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">更改第一个文件夹</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">当查找内容中出现了/,使用\/转义</span><span style="color: #008000; "><br /></span>echo&nbsp;${string/<span style="color: #008000; ">#</span><span style="color: #008000; ">\/etc/etc1}</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">如果是下面的语句则会输出：</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">/etc/etc1/etc/init.d/profile</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">因为&nbsp;/#/被处理掉,/etc/ect1被当作了$replacement</span><span style="color: #008000; "><br /></span>echo&nbsp;${string/<span style="color: #008000; ">#</span><span style="color: #008000; ">/etc/etc1}</span><span style="color: #008000; "><br /></span></div><img src ="http://www.cppblog.com/izualzhy/aggbug/195859.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-12-01 12:24 <a href="http://www.cppblog.com/izualzhy/archive/2012/12/01/195859.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]Linux grep命令用法</title><link>http://www.cppblog.com/izualzhy/archive/2012/08/29/188661.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 29 Aug 2012 10:42:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/08/29/188661.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/188661.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/08/29/188661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/188661.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/188661.html</trackback:ping><description><![CDATA[<h3> <p><a href="http://blog.csdn.net/mark_qi/article/details/7919498">http://blog.csdn.net/mark_qi/article/details/7919498</a> <p>首先创建我们练习grep命令时需要用到的demo文件demo_file。<pre>$ cat demo_file<br>THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.<br>this line is the 1st lower case line in this file.<br>This Line Has All Its First Character Of The Word With Upper Case.<br><br>Two lines above this line is empty.<br>And this is the last line.</pre></h3>
<h5>1.从单个文件中搜索指定的字串</h5>
<p>grep的基础用法是如下例的从指定的文件中搜索特定的字串。<pre>语法：<br>grep "literal_string" filename</pre><pre>$ grep "this" demo_file<br>this line is the 1st lower case line in this file.<br>Two lines above this line is empty.<br>And this is the last line.</pre>
<h5>2. 在多个文件中检索指定的字串</h5><pre>语法：<br>grep "string" FILE_PATTERN</pre>
<p>先拷贝demo_file为demo_file1。grep的结果在符合条件的行前将包括文件名。当文件名包含元字符时，linux shell会将匹配的所有文件作为输入到grep中去。<pre>$ cp demo_file demo_file1<br><br>$ grep "this" demo_*<br>demo_file:this line is the 1st lower case line in this file.<br>demo_file:Two lines above this line is empty.<br>demo_file:And this is the last line.<br>demo_file1:this line is the 1st lower case line in this file.<br>demo_file1:Two lines above this line is empty.<br>demo_file1:And this is the last line.</pre>
<h5>3. 用 grep -i 进行大小写无关的搜索</h5><pre>语法：<br>grep -i "string" FILE</pre>
<p>也是一个基本用法，对搜索的字串忽略大小写，因此下例中匹配“the”, “THE” and “The”。<pre>$ grep -i "the" demo_file<br>THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.<br>this line is the 1st lower case line in this file.<br>This Line Has All Its First Character Of The Word With Upper Case.<br>And this is the last line.</pre>
<h5>4. 使用用正则表达式</h5><pre>语法：<br>grep "REGEX" filename</pre>
<p>如果你能有效地利用正则表达式，这是个很有用的特点。在下面的例子中，搜索全部以“lines”开始以“empty”结束的字串，如搜索“lines[之间任意字]empty” ，并且忽略大小写。<pre>$ grep -i "lines.*empty" demo_file<br>Two lines above this line is empty.</pre>
<p>正则表达式遵循的几个重复的操作
<ul>
<li>? 最多匹配一次
<li>* 匹配零次或者任意多次
<li>+ 匹配一次以上
<li>{n} 匹配n次
<li>{n,} 最少匹配n次
<li>{,m} 最多匹配m次
<li>{n,m} 匹配n到m次</li></ul>
<h5>5. 用grep -w搜索整个词，而不是词中的部分字串</h5>
<p>使用-w选项搜索一个单词，并且避免搜索到词中的部分字串。<br>下例搜索"is"。如果不加-w选项，将显示“is”, “his”, “this” 等所有包含“is”的行。<pre>$ grep -i "is" demo_file<br>THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.<br><strong>this</strong> line is the 1st lower case line in this file.<br><strong>This Line Has All Its First Character Of The Word With Upper Case.</strong><br>Two lines above this line is empty.<br>And this is the last line.</pre>
<p>下例使用了-w选项，请注意结果中不包含 “This Line Has All Its First Character Of The Word With Upper Case”, 虽然 “This”中包含“is”。<pre>$ grep -iw "is" demo_file<br>THIS LINE <strong>IS</strong> THE 1ST UPPER CASE LINE IN THIS FILE.<br>this line <strong>is</strong> the 1st lower case line in this file.<br>Two lines above this line is empty.<br>And this is the last line.</pre>
<h5>6. 使用grep -A, -B and -C显示之前、之后、前后的几行</h5>
<p>当使用grep搜索大文件时，显示匹配行附近的多行数据是一个很有用的功能。
<p>创建如下文件<pre>$ cat demo_text<br>4. Vim Word Navigation<br><br>You may want to do several navigation in relation to the words, such as:<br><br> * e - go to the end of the current word.<br> * E - go to the end of the current WORD.<br> * b - go to the previous (before) word.<br> * B - go to the previous (before) WORD.<br> * w - go to the next word.<br> * W - go to the next WORD.<br><br>WORD - WORD consists of a sequence of non-blank characters, separated with white space.<br>word - word consists of a sequence of letters, digits and underscores.<br><br>Example to show the difference between WORD and word<br><br> * 192.168.1.1 - single WORD<br> * 192.168.1.1 - seven words.</pre>
<h6>6.1 显示匹配行之后的N行</h6>
<p>-A<pre>语法：<br>grep -A  "string" FILENAME</pre>
<p>下例显示匹配行和之后的3行数据<pre>$ grep -A 3 -i "example" demo_text<br>Example to show the difference between WORD and word<br><br>* 192.168.1.1 - single WORD<br>* 192.168.1.1 - seven words.</pre>
<h6>6.2显示匹配行之前的N行</h6>
<p>-B<pre>语法：<br>grep -B  "string" FILENAME</pre>
<p>下例显示匹配行和之前的2行数据<pre>$ grep -B 2 "single WORD" demo_text<br>Example to show the difference between WORD and word<br><br>* 192.168.1.1 - single WORD</pre>
<h6>6.3显示匹配行前后的N行</h6>
<p>-C 显示之前的n行，之后的n行数据.<pre>$ grep -C 2 "Example" demo_text<br>word - word consists of a sequence of letters, digits and underscores.<br><br>Example to show the difference between WORD and word<br><br>* 192.168.1.1 - single WORD</pre>
<h5>7.通过GREP_OPTIONS高亮显示搜索的字串</h5>
<p>如果你希望搜索的字串高亮显示在结果中，可以试用以下的办法。
<p>通过修改GREP_OPTIONS对搜索字串高亮显示。<pre>$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'<br><br>$ grep this demo_file<br><strong>this</strong> line is the 1st lower case line in this file.<br>Two lines above <strong>this</strong> line is empty.<br>And <strong>this</strong> is the last line.</pre>
<h5>8. 用grep -r递归搜索全部的文件</h5>
<p>如果想查找当前目前以及其子目录的全部文件时，可以使用 -r 选项。如下例<pre>$ grep -r "ramesh" *</pre>
<h5>9. 使用grep -v进行不匹配</h5>
<p>可以使用-v选项显示不匹配搜索字串的行。下例显示demo_text文件中不包含“go”的行<pre>$ grep -v "go" demo_text<br>4. Vim Word Navigation<br><br>You may want to do several navigation in relation to the words, such as:<br><br>WORD - WORD consists of a sequence of non-blank characters, separated with white space.<br>word - word consists of a sequence of letters, digits and underscores.<br><br>Example to show the difference between WORD and word<br><br>* 192.168.1.1 - single WORD<br>* 192.168.1.1 - seven words.</pre>
<h5>10. 显示不匹配全部模式的行</h5><pre>语法：<br>grep -v -e "pattern" -e "pattern"</pre>
<p>创建如下例子文件<pre>$ cat test-file.txt<br>a<br>b<br>c<br>d<br><br>$ grep -v -e "a" -e "b" -e "c" test-file.txt<br>d</pre>
<h5>11.用grep -c 统计匹配的行数</h5><pre>语法：<br>grep -c "pattern" filename</pre><pre>$ grep -c "go" demo_text<br>6</pre>
<p>统计不匹配的行数<pre>$ grep -v -c this demo_file<br>4</pre>
<h5>12. 用grep -l 只显示文件名</h5><pre>$ grep -l this demo_*<br>demo_file<br>demo_file1</pre>
<h5>13. 只显示匹配的字串</h5>
<p>缺省显示匹配字串的所在行，可以使用-o选项只显示匹配的字串。这项功能当使用正则表达式时比较有用处。
<p>$ grep -o "is.*line" demo_file<pre>is line is the 1st lower case line<br>is line<br>is is the last line</pre>
<h5>14. 显示匹配的位置</h5><pre>语法：<br>grep -o -b "pattern" file</pre><pre>$ cat temp-file.txt<br>12345<br>12345<br><br>$ grep -o -b "3" temp-file.txt<br>0:3<br>6:3</pre>
<p><strong>注意:</strong> 以上输出显示的不是行内的位置，而是整个文件中的字节byte位置
<h5>15. 用 grep -n 在输出时显示行号</h5>
<p>行号从1开始<pre>$ grep -n "go" demo_text<br>5: * e - go to the end of the current word.<br>6: * E - go to the end of the current WORD.<br>7: * b - go to the previous (before) word.<br>8: * B - go to the previous (before) WORD.<br>9: * w - go to the next word.<br>10: * W - go to the next WORD.</pre><img src ="http://www.cppblog.com/izualzhy/aggbug/188661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-08-29 18:42 <a href="http://www.cppblog.com/izualzhy/archive/2012/08/29/188661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]【原创翻译】《在linux中显示所有正在运行的进程》</title><link>http://www.cppblog.com/izualzhy/archive/2012/08/25/188261.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 25 Aug 2012 10:05:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/08/25/188261.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/188261.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/08/25/188261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/188261.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/188261.html</trackback:ping><description><![CDATA[<p><em>翻译：飞哥 (</em><a href="http://hi.baidu.com/imlidapeng"><em>http://hi.baidu.com/imlidapeng</em></a><em>)</em> <p><em>版权所有，尊重他人劳动成果，转载时请注明作者和原始出处及本声明。</em> <p><em>原文名称：《Show All Running Processes in Linux》</em> <p><em>原文地址：http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/</em> <p>在linux中怎样查看所有运行中的进程？ <p>你可以使用ps命令。它能显示当前运行中进程的相关信息，包括进程的PID。Linux和UNIX都支持ps命令，显示所有运行中进程的相关信息。ps命令能提供一份当前进程的快照。如果你想状态可以自动刷新，可以使用top命令。 <p>ps命令 <p>输入下面的ps命令，显示所有运行中的进程： <p># ps aux | less <p>其中， <p>-A：显示所有进程 <p>a：显示终端中包括其它用户的所有进程 <p>x：显示无控制终端的进程 <p>任务：查看系统中的每个进程。 <p># ps -A <p># ps -e <p>任务：查看非root运行的进程 <p># ps -U root -u root -N <p>任务：查看用户vivek运行的进程 <p># ps -u vivek <p>任务：top命令 <p>top命令提供了运行中系统的动态实时视图。在命令提示行中输入top： <p># top <p>输出： <p><img border="0" src="http://hiphotos.baidu.com/imlidapeng/pic/item/1ec9bed02d775e65960a16f0.jpg"> <p>图1：top命令：显示Linux任务 <p>按q退出，按h进入帮助。 <p>任务：显示进程的树状图。 <p>pstree以树状显示正在运行的进程。树的根节点为pid或init。如果指定了用户名，进程树将以用户所拥有的进程作为根节点。 <p>$ pstree <p>输出示例： <p><img border="0" src="http://hiphotos.baidu.com/imlidapeng/pic/item/8e04c33ace6a25a23b87cef2.jpg"> <p>图2：pstree - 显示进程的树状图 <p>任务：使用ps列印进程树 <p># ps -ejH <p># ps axjf <p>任务：获得线程信息 <p>输入下列命令： <p># ps -eLf <p># ps axms <p>任务：获得安全信息 <p>输入下列命令： <p># ps -eo euser,ruser,suser,fuser,f,comm,label <p># ps axZ <p># ps -eM <p>任务：将进程快照储存到文件中 <p>输入下列命令： <p># top -b -n1 &gt; /tmp/process.log <p>你也可以将结果通过邮件发给自己： <p># top -b -n1 | mail -s 'Process snapshot' you@example.com <p>任务：查找进程 <p>使用pgrep命令。pgrep能查找当前正在运行的进程并列出符合条件的进程ID。例如显示firefox的进程ID： <p>$ pgrep firefox <p>下面命令将显示进程名为sshd、所有者为root的进程。 <p>$ pgrep -u root sshd <p>向htop和atop说hello <p>htop是一个类似top的交互式进程查看工具，但是可以垂直和水平滚动来查看所有进程和他们的命令行。进程的相关操作（killing，renicing）不需要输入PID。要安装htop输入命令： <p># apt-get install htop <p>或 <p># yum install htop <p>在命令提示行中输入htop： <p># htop <p>输出示例： <p><img border="0" src="http://hiphotos.baidu.com/imlidapeng/pic/item/21f2aa32e40b2bf95fdf0ef3.jpg"> <p>图3：htop - Interactive Linux / UNIX process viewer <p>atop工具 <p>atop是一个用来查看Linux系统负载的交互式监控工具。它能展现系统层级的关键硬件资源（从性能角度）的使用情况，如CPU、内存、硬盘和网络。 <p>它也可以根据进程层级的CPU和内存负载显示哪个进程造成了特定的负载；如果已经安装内核补丁可以显示每个进程的硬盘和网络负载。输入下面的命令启动atop： <p># atop <p>输出示例： <p><img border="0" src="http://hiphotos.baidu.com/imlidapeng/pic/item/ce11b1ec64778247fcfa3cfc.jpg"> <p>图4：AT Computing's System &amp; Process Monitor <img src ="http://www.cppblog.com/izualzhy/aggbug/188261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-08-25 18:05 <a href="http://www.cppblog.com/izualzhy/archive/2012/08/25/188261.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]一个fork的面试题</title><link>http://www.cppblog.com/izualzhy/archive/2012/08/01/185882.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 01 Aug 2012 05:55:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/08/01/185882.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/185882.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/08/01/185882.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/185882.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/185882.html</trackback:ping><description><![CDATA[<p>转自：<a href="http://coolshell.cn/articles/7965.html">http://coolshell.cn/articles/7965.html</a> <p>前两天有人问了个关于Unix的fork()系统调用的面试题，这个题正好是我大约十年前找工作时某公司问我的一个题，我觉得比较有趣，写篇文章与大家分享一下。这个题是这样的： <p><strong>题目：请问下面的程序一共输出多少个“-”？</strong> <p><code></code></p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> #include &lt;stdio.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> #include &lt;sys/types.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> #include &lt;unistd.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #0000ff">int</span> main(<span style="color: #0000ff">void</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #0000ff">int</span> i;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #0000ff">for</span>(i=0; i&lt;2; i++){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> fork();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> printf(<span style="color: #006080">"-"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #0000ff">return</span> 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> }</pre><!--CRLF--></div></div>
<p>如果你对fork()的机制比较熟悉的话，这个题并不难，输出应该是6个“-”，但是，实际上这个程序会很tricky地输出8个“-”。</p>
<p>要讲清这个题，我们首先需要知道fork()系统调用的特性，
<ul>
<li>fork()系统调用是Unix下以自身进程创建子进程的系统调用，一次调用，两次返回，如果返回是0，则是子进程，如果返回值&gt;0，则是父进程（返回值是子进程的pid），这是众为周知的。</li></ul>
<ul>
<li>还有一个很重要的东西是，在fork()的调用处，整个父进程空间会原模原样地复制到子进程中，包括指令，变量值，程序调用栈，环境变量，缓冲区，等等。</li></ul>
<p>所以，上面的那个程序为什么会输入8个“-”，这是因为printf(“-”);语句，我们知道，Unix下的设备有“<a href="http://en.wikipedia.org/wiki/Device_file#Block_devices">块设备</a>”和“<a href="http://en.wikipedia.org/wiki/Device_file#Character_devices">字符设备</a>”的概念，所谓块设备，就是以一块一块的数据存取的设备，字符设备是一次存取一个字符的设备。磁盘、内存、显示器都是块设备，字符设备如键盘和串口。<strong>块设备一般都有缓存，而字符设备一般都没有缓存</strong>。
<p>所以，对于上述程序，printf(“-”);把“-”放到了缓存中，并没有真正的输出（参看《<a href="http://coolshell.cn/articles/945.html">C语言的迷题</a>》中的第一题），<strong>在fork的时候，缓存被复制到了子进程空间</strong>，所以，就多了两个，就成了8个，而不是6个。
<p>我们如果修改一下上面的printf的那条语句为：
<p><code>printf</code><code>(</code><code>"-\n"</code><code>);</code>
<p>或是
<p><code>printf</code><code>(</code><code>"-"</code><code>);</code>
<p><code>flush();</code>
<p>就没有问题了，因为程序遇到“\n”或是EOF，或是缓中区满，或是文件描述符关闭，或是主动flush，就会把数据刷出缓冲区。
<p>我估计有些朋友可能对于fork()还不是很了解，那么我们把上面的程序改成下面这样：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> #include &lt;stdio.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> #include &lt;sys/types.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> #include &lt;unistd.h&gt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #0000ff">int</span> main(<span style="color: #0000ff">void</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #0000ff">int</span> i;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #0000ff">for</span>(i=0; i&lt;2; i++){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> fork();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> <span style="color: #008000">//注意：下面的printf有“\n”</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> printf(<span style="color: #006080">"ppid=%d, pid=%d, i=%d \n"</span>, getppid(), getpid(), i);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> sleep(10); <span style="color: #008000">//让进程停留十秒，这样我们可以用pstree查看一下进程树</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span> <span style="color: #0000ff">return</span> 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>&nbsp; </pre><!--CRLF--></div></div>
<p>于是，上面这段程序会输出下面的结果，（注：编译出的可执行的程序名为fork）
<p><code>ppid=8858, pid=8518, i=0</code>
<p><code>ppid=8858, pid=8518, i=1</code>
<p><code>ppid=8518, pid=8519, i=0</code>
<p><code>ppid=8518, pid=8519, i=1</code>
<p><code>ppid=8518, pid=8520, i=1</code>
<p><code>ppid=8519, pid=8521, i=1</code>
<p><code>$ pstree -p | </code><code>grep</code> <code>fork</code>
<p><code>|-</code><code>bash</code><code>(8858)-+-fork(8518)-+-fork(8519)---fork(8521)</code>
<p><code>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; `-fork(8520)</code>
<p>面对这样的图你可能还是看不懂，没事，我好事做到底，画个图给你看看：
<p><img title="fork 程序调用图" alt="" src="http://coolshell.cn/wp-content/uploads/2012/07/fork01jpg.jpg" width="620" height="407">
<p>注意：上图中的我用了几个色彩，相同颜色的是同一个进程。于是，我们的pstree的图示就可以成为下面这个样子：（下图中的颜色与上图对应）
<p><img title="fork进程树" alt="" src="http://coolshell.cn/wp-content/uploads/2012/07/fork02.jpg" width="437" height="97">
<p>这样，对于printf(“-”);这个语句，我们就可以很清楚的知道，哪个子进程复制了父进程标准输出缓中区里的的内容，而导致了多次输出了。（如下图所示，就是我阴影并双边框了那两个子进程）
<p><img title="fork程序执行图" alt="" src="http://coolshell.cn/wp-content/uploads/2012/07/fork03.jpg" width="626" height="415">
<p>现在你明白了吧。（另，对于图中的我本人拙劣的配色，请见谅!）
<p>（全文完）

<img src ="http://www.cppblog.com/izualzhy/aggbug/185882.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-08-01 13:55 <a href="http://www.cppblog.com/izualzhy/archive/2012/08/01/185882.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Terminal比较常用的几个快捷键</title><link>http://www.cppblog.com/izualzhy/archive/2012/07/24/184870.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 24 Jul 2012 08:06:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/07/24/184870.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/184870.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/07/24/184870.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/184870.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/184870.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5a9527f3-6885-4793-a4f2-48cda3a7d890" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/terminal" rel="tag">terminal</a></div> <p>tab=补全 <br>ctrl+a=开始位置 <br>ctrl+e=最后位置 <br>ctrl+k=删除此处至末尾所有内容 <br>ctrl+u=删除此处至开始所有内容 <br>ctrl+d=删除当前字母 <br>ctrl+w=删除此处到左边的单词 <br>ctrl+y=粘贴由ctrl+u，ctrl+d，ctrl+w删除的单词 <br>ctrl+r=正在测试，还没弄明白，呵呵。 <br>ctrl+l=相当于clear <br>ctrl+b=向回移动 <br>ctrl+f=向前移动 <br>ctrl+p=向上显示缓存命令 <br>ctrl+n=向下显示缓存命令 <br>ctrl+d=关闭终端 <br>shift+上或下=终端上下滚动 <br>shift+pgup或pgdown=终端上下翻页滚动 <br>ctrl+shift+f=全屏（仅限于konsole） <br>ctrl+shift+n=新终端（terminal和konsole有所不同） <br>ctrl+c=终止</p><img src ="http://www.cppblog.com/izualzhy/aggbug/184870.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-07-24 16:06 <a href="http://www.cppblog.com/izualzhy/archive/2012/07/24/184870.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell中实现查找两个文件的不重复的行</title><link>http://www.cppblog.com/izualzhy/archive/2012/07/20/184379.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Fri, 20 Jul 2012 11:23:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/07/20/184379.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/184379.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/07/20/184379.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/184379.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/184379.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:2ef5ccb0-06e3-4d7c-8fcf-644a0369e856" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/sort" rel="tag">sort</a>,<a href="http://technorati.com/tags/uniq" rel="tag">uniq</a></div> <p align="left">朋友给了两个email文件，一个100行左右(a.txt)，一个不到1000行(b.txt)。两个有很多重复的email，要找出b.txt里除了a.txt之外的所有行。<br />根据评论，可以这么做：<span style="color: red; ">grep -vxFf a.txt b.txt</span></p> <p align="left">于是就想到了直接利用shell里的wc，uniq，sort等。</p> <p align="left">其实像这些命令，选项参数很多，大概要知道某个用法，可以实现什么，然后再查也不迟。</p> <p align="left">先写下我的方法，再分析：</p> <p align="left">cat a.txt &gt; total.txt</p> <p align="left">cat b.txt &gt;&gt; total.txt</p> <p align="left">sort total.txt &gt; total.txt.sort</p> <p align="left">uniq &#8211;d total.txt.sort &gt; total.txt.sort.uniq</p> <p align="left">cat b.txt &gt;&gt; total.txt.sort.uniq</p> <p align="left">sort total.txt.sort.uniq &gt; total.txt.sort.uniq.sort</p> <p align="left">uniq &#8211;u total.txt.sort.uniq.sort &gt; result.txt</p> <p align="left">中间生成了一些临时文件，不知道怎么样通过pipe能减少数量。</p> <p align="left"><font color="#ff0000" size="4"><strong>uniq介绍：</strong></font></p> <p>uniq [选项] 文件 </p><p>说明：这个命令读取输入文件，并比较相邻的行。在正常情况下，第二个及以后更多个重复行将被删去，行比较是根据所用字符集的排序序列进行的。该命令加工后的结果写到输出文件中。输入文件和输出文件必须不同。如果输入文件用&#8220;- &#8221;表示，则从标准输入读取。 </p><p>该命令各选项含义如下：、 </p><p>&#8211; c 显示输出中，在每行行首加上本行在文件中出现的次数。它可取代- u和- d选项。 </p><p>&#8211; d 只显示重复行。 </p><p>&#8211; u 只显示文件中不重复的各行。 </p><p>&#8211; n 前n个字段与每个字段前的空白一起被忽略。一个字段是一个非空格、非制表符的字符串，彼此由制表符和空格隔开(字段从0开始编号)。 </p><p>+n 前n个字符被忽略，之前的字符被跳过(字符从0开始编号)。 </p><p>&#8211; f n 与- n相同，这里n是字段数。 </p><p>&#8211; s n 与+n相同，这里n是字符数。 </p><p><font color="#ff0000" size="4"><strong>sort介绍：</strong></font> </p><p>sort的选项更多，大概浏览下有个印象知道可以做什么即可。 </p><p>语　　法：<strong><a href="http://www.linuxso.com/command/sort.html"><u>sort</u></a> </strong>[-b<a href="http://www.linuxso.com/command/cd.html"><u>cd</u></a>fimMnr][-o&lt;输出文件&gt;][-t&lt;分隔字符&gt;][+&lt;起始栏位&gt;-&lt;结束栏位&gt;][--help][--verison][文件]<br />补充说明：sort可针对文本文件的内容，以行为单位来排序。<br />参　　数：<br />&nbsp; -b&nbsp;&nbsp; 忽略每行前面开始出的空格字符。<br />&nbsp; -c&nbsp;&nbsp; 检查文件是否已经按照顺序排序。<br />&nbsp; -d&nbsp;&nbsp; 排序时，处理英文字母、数字及空格字符外，忽略其他的字符。<br />&nbsp; -f&nbsp;&nbsp; 排序时，将小写字母视为大写字母。<br />&nbsp; -i&nbsp;&nbsp; 排序时，除了040至176之间的ASCII字符外，忽略其他的字符。<br />&nbsp; -m&nbsp;&nbsp; 将几个排序好的文件进行合并。<br />&nbsp; -M&nbsp;&nbsp; 将前面3个字母依照月份的缩写进行排序。<br />&nbsp; -n&nbsp;&nbsp; 依照数值的大小排序。<br />&nbsp; -o&lt;输出文件&gt;&nbsp;&nbsp; 将排序后的结果存入指定的文件。<br />&nbsp; -r&nbsp;&nbsp; 以相反的顺序来排序。<br />&nbsp; -t&lt;分隔字符&gt;&nbsp;&nbsp; 指定排序时所用的栏位分隔字符。<br />&nbsp; +&lt;起始栏位&gt;-&lt;结束栏位&gt;&nbsp;&nbsp; 以指定的栏位来排序，范围由起始栏位到结束栏位的前一栏位。<br />&nbsp; --help&nbsp;&nbsp; 显示帮助。<br />&nbsp; --version&nbsp;&nbsp; 显示版本信息  </p><img src ="http://www.cppblog.com/izualzhy/aggbug/184379.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-07-20 19:23 <a href="http://www.cppblog.com/izualzhy/archive/2012/07/20/184379.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]获取当前系统时间（三种方法）</title><link>http://www.cppblog.com/izualzhy/archive/2012/07/10/182620.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 10 Jul 2012 07:11:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/07/10/182620.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/182620.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/07/10/182620.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/182620.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/182620.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:054504a1-3496-4295-9fad-e540bb6c45bd" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/time" rel="tag">time</a>,<a href="http://technorati.com/tags/localtime" rel="tag">localtime</a>,<a href="http://technorati.com/tags/strftime" rel="tag">strftime</a>,<a href="http://technorati.com/tags/asctime" rel="tag">asctime</a>,<a href="http://technorati.com/tags/ctime" rel="tag">ctime</a></div> <p><a href="http://blog.csdn.net/free_program_1314/article/details/7731073">http://blog.csdn.net/free_program_1314/article/details/7731073</a></p><pre class="code"><span style="color: #937a42">/*
 * =====================================================================================
 *       Filename:  getCurrentTime.c
 *    Description:  methods of get current time
 *
 *        Version:  1.0
 *        Created:  07/10/2012 09:50:09 AM
 *
 *         Author:  zhy (), izualzhy@163.com
 * =====================================================================================
 */

</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;time.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdio.h&gt;

</span><span style="color: #400080">int </span>main<span style="color: #2e53d1">()
{
    </span>time_t curTime <span style="color: #2e53d1">= </span>time<span style="color: #2e53d1">(</span>NULL<span style="color: #2e53d1">);
    </span><span style="color: #400080">char</span><span style="color: #2e53d1">* </span>curDate <span style="color: #2e53d1">= </span>ctime<span style="color: #2e53d1">(&amp;</span>curTime<span style="color: #2e53d1">);
    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"%s\n"</span><span style="color: #2e53d1">,</span>curDate<span style="color: #2e53d1">);

    </span><span style="color: #400080">struct </span>tm<span style="color: #2e53d1">* </span>localTime <span style="color: #2e53d1">= </span>localtime<span style="color: #2e53d1">(&amp;</span>curTime<span style="color: #2e53d1">);
    </span><span style="color: #400080">char </span>s<span style="color: #2e53d1">[</span><span style="color: #259241">50</span><span style="color: #2e53d1">];
    </span><span style="color: #400080">char</span><span style="color: #2e53d1">* </span>format <span style="color: #2e53d1">= </span><span style="color: #259241">"%y-%m-%d %H:%M:%S"</span><span style="color: #2e53d1">;
    </span>size_t max_size <span style="color: #2e53d1">= </span><span style="color: #259241">49</span><span style="color: #2e53d1">;
    </span>size_t result <span style="color: #2e53d1">= </span>strftime<span style="color: #2e53d1">(</span>s<span style="color: #2e53d1">, </span>max_size<span style="color: #2e53d1">, </span>format<span style="color: #2e53d1">, </span>localTime<span style="color: #2e53d1">);
    </span><span style="color: #400080">if </span><span style="color: #2e53d1">(</span>result<span style="color: #2e53d1">!=</span><span style="color: #259241">0</span><span style="color: #2e53d1">) {
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"%s\n"</span><span style="color: #2e53d1">,</span>s<span style="color: #2e53d1">);
    }

    </span><span style="color: #400080">char</span><span style="color: #2e53d1">* </span>cTime <span style="color: #2e53d1">= </span>asctime<span style="color: #2e53d1">(</span>localTime<span style="color: #2e53d1">);
    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"%s\n"</span><span style="color: #2e53d1">, </span>cTime<span style="color: #2e53d1">);

    </span><span style="color: #400080">return </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
}

</span></pre>
<p>总结下三种方法：</p>
<p>首先都需要使用time函数获取时间。</p>
<p>都在#include &lt;time.h&gt;里定义。</p>
<p>用到的函数原型：</p>
<p>time_t time(time_t *t);</p>
<p>char *ctime(const time_t *timep);</p>
<p>struct tm *localtime(const time_t *timep);</p>
<p>size_t strftime(char *s, size_t max, const char *format,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct tm *tm);</p>
<p>char *asctime(const struct tm *tm);<br></p>
<p>就我的感觉，第二种最为好用。格式有很多，man一下就看到了。摘抄下用到的：</p>
<p>%y&nbsp;&nbsp;&nbsp;&nbsp; The year as a decimal number without a century (range 00 to 99).</p>
<p>%m&nbsp;&nbsp;&nbsp;&nbsp; The month as a decimal number (range 01 to 12).</p>
<p>%d&nbsp;&nbsp;&nbsp;&nbsp; The day of the month as a decimal number (range 01 to 31).</p>
<p>%H&nbsp;&nbsp;&nbsp;&nbsp; The hour as a decimal number using a 24-hour clock (range 00 to 23).</p>
<p>%M&nbsp;&nbsp;&nbsp;&nbsp; The minute as a decimal number (range 00 to 59).</p>
<p>%S&nbsp;&nbsp;&nbsp;&nbsp; The second as a decimal number (range 00 to 60).&nbsp; (The range is up to 60 to allow for occasional leap seconds.)</p><img src ="http://www.cppblog.com/izualzhy/aggbug/182620.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-07-10 15:11 <a href="http://www.cppblog.com/izualzhy/archive/2012/07/10/182620.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]Classical 10 Examples for learning AWK</title><link>http://www.cppblog.com/izualzhy/archive/2012/07/02/181171.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Mon, 02 Jul 2012 12:20:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/07/02/181171.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/181171.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/07/02/181171.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/181171.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/181171.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:defd8948-7fbe-4408-9af8-39999f74ee74" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/awk" rel="tag">awk</a></div> <p>原帖地址：<a href="http://blog.csdn.net/poechant/article/details/7661648">Classical 10 Examples for learning AWK</a></p> <p>很久不用awk了，一直认为学习的最佳方式是通读-&gt;实践-&gt;总结的过程。</p> <p>原帖讲的很详细了，很不错，都是很简单的题目。我在这里记录下自己关于每个题目的总结：</p> <p>e1:</p> <p>1.“%-s”是左对齐，”%s”是右对齐，对齐方式由正负号表示。</p> <p>e2:</p> <p>note:grep "Europe" contries一样可以达到效果。</p> <p>e5:</p> <p>awk可以直接从命令行读取或从文件读取</p> <ul> <li> <p>awk [options] 'script' var=value file(s)</p> <li> <p>awk [options] -f scriptfile var=value file(s）</p></li></ul> <p>e6: <p>应该这么写？ <p>#!/usr/bin/awk<br>{print $0}</p> <p>note：print会自动换行，而printf不会。</p> <p>e7:</p> <p>FILENAME: 当前文件名</p> <p>OFS:输出字段分隔符（默认空格）</p> <p>ORS:输出记录分隔符（默认回车）</p> <p>比如可以这么重写下：</p><pre class="code"><span style="color: #400080">#</span><span style="color: #2e53d1">!/</span>usr<span style="color: #2e53d1">/</span>bin<span style="color: #2e53d1">/</span>awk

BEGIN<span style="color: #2e53d1">{</span>FS<span style="color: #2e53d1">=</span><span style="color: #259241">":"</span><span style="color: #2e53d1">;</span>OFS<span style="color: #2e53d1">=</span><span style="color: #259241">"-"</span><span style="color: #2e53d1">;</span>ORS<span style="color: #2e53d1">=</span><span style="color: #259241">"$\n"</span><span style="color: #2e53d1">}

{

    </span>printf FILENAME OFS\

        NR OFS\

        $1 OFS\

        $2 OFS\

        $3 OFS\

        $4 OFS\

        ORS

<span style="color: #2e53d1">}
</span></pre>
<p>输出结果：</p>
<p>contries-1-Canada-3852-25-North America-$<br>contries-2-USA-3615-237-North America-$<br>contries-3-Brazil-3286-134-South America-$<br>contries-4-England-94-56-Europe-$<br>contries-5-France-211-55-Europe-$<br>contries-6-Japan-144-120-Asia-$<br>contries-7-Mexico-762-78-North America-$<br>contries-8-China-3705-1032-Asia-$<br>contries-9-India-1267-746-Asia-$</p><img src ="http://www.cppblog.com/izualzhy/aggbug/181171.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-07-02 20:20 <a href="http://www.cppblog.com/izualzhy/archive/2012/07/02/181171.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>automake学习记录</title><link>http://www.cppblog.com/izualzhy/archive/2012/06/28/180680.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 28 Jun 2012 12:43:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/06/28/180680.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/180680.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/06/28/180680.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/180680.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/180680.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:fc41b35f-8160-4559-8f19-b6175137a749" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/automake" rel="tag">automake</a>,<a href="http://technorati.com/tags/shell" rel="tag">shell</a></div> <p>最近看到automake的几篇文章，顺便重新复习了下。</p> <p>感觉最主要的就是这张图了。</p> <p><img src="http://blog.chinaunix.net/attachment/201111/14/23225855_13212527245450.png"></p> <p>其中椭圆形的命令就是我们需要手动输入或者修改的。</p> <p>文章有很多：</p> <p><a href="http://www.cppblog.com/sunrise/archive/2012/06/27/180440.html">http://www.cppblog.com/sunrise/archive/2012/06/27/180440.html</a></p> <p><a href="http://www.cppblog.com/wc250en007/archive/2012/06/27/180478.aspx">http://www.cppblog.com/wc250en007/archive/2012/06/27/180478.aspx</a></p> <p>都是最近发在cpp主页的。</p> <p>以第一篇文章为例。</p> <p>第5步有些不明确</p> <p>我写的是：</p> <p>AUTOMAKE_OPTIONS=foreign<br>bin_PROGRAMS=hello<br>hello_SOURCES=hello.c</p> <p>这样写也是可以的，生成的可执行文件就是helloworld</p> <p>AUTOMAKE_OPTIONS=foreign<br>bin_PROGRAMS=helloworld<br>helloworld_SOURCES=hello.c</p><img src ="http://www.cppblog.com/izualzhy/aggbug/180680.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-06-28 20:43 <a href="http://www.cppblog.com/izualzhy/archive/2012/06/28/180680.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>automake提示错误installing `./depcomp'; error while making link: Read-only file system</title><link>http://www.cppblog.com/izualzhy/archive/2012/06/28/180677.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 28 Jun 2012 12:28:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/06/28/180677.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/180677.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/06/28/180677.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/180677.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/180677.html</trackback:ping><description><![CDATA[<p>以下为摘录： <p>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% <p>在生成Makefile时，运行automake --add-missing命令出现以下错误： <p>configure.in: installing './install-sh': error while making link: 不允许的操作<br>configure.in: installing './mkinstalldirs': error while making link: 不允许的操作<br>configure.in: installing './missing': error while making link: 不允许的操作<br>configure.in: installing './depcomp': error while making link: 不允许的操作 <p>经过在网上查找，发现有一篇文章也出现了类似的情况，以下为这篇文章的内容： <p>//=========================================================================== <h5><a href="http://menphix.ycool.com/post.3019423.html#">Linux下的GTK等等。</a></h5> <h6><a href="http://www.ycool.com/space.php?uid=1007272">menphix</a> 发表于 2008-06-03 21:09:14</h6> <p>fedora 9自带的Glade是2.xx版的，而且Anjuta根本就没有。删掉换成新的Glade 3.xx和Anjuta，准备试试在linux下写gtk+.<br>结果运行autogen.sh总也过不去。要不是Anjuta的输出高亮还真发现不了这样的错误：<br>configure.ac:6: installing `./install-sh'; error while making link: 不允许的操作<br>configure.ac:6: installing `./missing'; error while making link: 不允许的操作<br>src/Makefile.am: installing `./depcomp'; error while making link: 不允许的操作<br>Makefile.am: installing `./INSTALL'; error while making link: 不允许的操作<br>仔细分析才知道原来是因为把工程放在FAT32分区上了，而它不支持link!<br>挪到ext3上就解决了。呵呵。 <p>//========================================================================== <p>分析一下自己的原因，才知道原来是因为自己的代码文件放在linux和windows的共享目录下，也就是FAT32分区，最后把这些代码文件移到ext3上就解决了，呵呵，太谢谢这位哥们你了！ <p>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% <p>之前猜测是在虚拟机linux和主机win下共享文件的原因，看了下我的是NTFS的，看来同样不支持link。 <p>另外关于automake，感兴趣的可以看下这里： <p><a href="http://www.gnu.org/software/automake/manual/automake.html">http://www.gnu.org/software/automake/manual/automake.html</a> <p>&nbsp; <p>参考： <p><a href="http://hi.baidu.com/huaan031/blog/item/11849039056fc6c4d5622595.html">http://hi.baidu.com/huaan031/blog/item/11849039056fc6c4d5622595.html</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/180677.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-06-28 20:28 <a href="http://www.cppblog.com/izualzhy/archive/2012/06/28/180677.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>getopt_long介绍</title><link>http://www.cppblog.com/izualzhy/archive/2012/06/27/180536.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 27 Jun 2012 12:51:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/06/27/180536.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/180536.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/06/27/180536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/180536.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/180536.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:afb694a5-090d-4881-b2ca-2117fc4bcbca" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/getopt_long" rel="tag">getopt_long</a></div> <p>#include &lt;getopt.h&gt;</p> <p>int getopt_long(int argc, char * const argv[],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *optstring,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct option *longopts, int *longindex);</p> <p>int getopt_long_only(int argc, char * const argv[],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *optstring,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct option *longopts, int *longindex);</p> <p><strong>还是看下man里的解释：</strong></p> <p>The getopt_long() function works like getopt() except that it also accepts long options, started with two dashes.&nbsp; (If the program accepts only long options, then<br>optstring should be specified as an empty string (""), not NULL.)&nbsp; Long option names may be abbreviated if the abbreviation is unique or is&nbsp; an&nbsp; exact&nbsp; match&nbsp; for<br>some defined option.&nbsp; A long option may take a parameter, of the form --arg=param or --arg param.</p> <p>longopts is a pointer to the first element of an array of struct option declared in &lt;getopt.h&gt; as</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; struct option {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; has_arg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *flag;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; val;<br>&nbsp;&nbsp;&nbsp;&nbsp; };</p> <p>The meanings of the different fields are:</p> <p>name&nbsp;&nbsp; is the name of the long option.</p> <p>has_arg<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the option takes an optional argument.</p> <p>flag&nbsp;&nbsp; specifies how results are returned for a long option.&nbsp; If flag is NULL, then getopt_long() returns val.&nbsp; (For example, the calling program may set&nbsp; val&nbsp; to<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the&nbsp; equivalent short option character.)&nbsp; Otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; left unchanged if the option is not found.</p> <p>val&nbsp;&nbsp;&nbsp; is the value to return, or to load into the variable pointed to by flag.</p> <p>The last element of the array has to be filled with zeros.</p> <p>If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts.</p> <p>getopt_long_only() is like getopt_long(), but '-' as well as "--" can indicate a long option.&nbsp; If an option that starts with '-' (not "--") doesn't match&nbsp; a&nbsp; long<br>option, but does match a short option, it is parsed as a short option instead.</p> <p>我来翻译下：</p> <p>前面说的-h可以由getopt实现，那么—help则由getopt_long实现了。所谓的长选项即help，短选项就是h。</p> <p><strong>argc，argv，optstring</strong>都同前面。</p> <p><strong>name</strong>:如”help”,”version”等</p> <p><strong>has_arg:</strong></p> <p>[符号常量][数值][含义]</p> <p>no_argument&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 该选项没有参数</p> <p>required_argument&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 选项需要参数</p> <p>optional_argument&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 选项参数可选</p> <p><strong>int *flag，<strong>int val:</strong></strong></p> <p>flag和val相互依赖，主要分两种情况： <p>（1）、flag为NULL，val值用于确定该长选项，所以需要为长选项指定唯一的val值。这里也为长选项和短选项建立了桥梁。 <p>（2）、flag不为NULL，则将val值存放到flag所指向的存储空间，用于标识该长选项出现过。 <p><strong>返回值：</strong> <p>程序中使用短选项，则返回短选项字符（如‘n'），当需要参数是，则在返回之前将参数存入到optarg中。 </p> <p>程序中使用长选项，返回值根据flag和val确定。当flag为NULL，则返回val值。所以根据val值做不同的处理，这也说明了val必须唯一。当val值等于短选项值，则可以使用短选项解析函数解析长选项；当flag不为NULL，则将val值存入flag所指向的存储空间，getopt_long返回0 </p> <p>出现未定义的长选项或者短选项，getopt_long返回？ </p> <p>解析完毕，getopt_long返回-1</p> <p>&nbsp; <p>copy参考里的一个例子，略微修改了下：</p><pre class="code"><span style="color: #937a42">/*
 * =====================================================================================
 *       Filename:  getopt_long_l.c
 *    Description:  getopt_long() learn notes
 *
 *        Version:  1.0
 *        Created:  06/27/2012 08:25:20 PM
 *
 *         Author:  zhy (), izualzhy@163.com
 * =====================================================================================
 */

</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdio.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;getopt.h&gt;

</span><span style="color: #400080">int </span>main<span style="color: #2e53d1">(</span><span style="color: #400080">int </span>argc<span style="color: #2e53d1">, </span><span style="color: #400080">char</span><span style="color: #2e53d1">* </span>argv<span style="color: #2e53d1">[])
{
    </span><span style="color: #400080">int </span>result<span style="color: #2e53d1">, </span>deb<span style="color: #2e53d1">;
    </span><span style="color: #400080">struct </span>option opts<span style="color: #2e53d1">[] = {{</span><span style="color: #259241">"username"</span><span style="color: #2e53d1">, </span>required_argument<span style="color: #2e53d1">, </span>NULL<span style="color: #2e53d1">, </span><span style="color: #259241">'n'</span><span style="color: #2e53d1">},
                            {</span><span style="color: #259241">"version"</span><span style="color: #2e53d1">,  </span>optional_argument<span style="color: #2e53d1">, </span>NULL<span style="color: #2e53d1">, </span><span style="color: #259241">'v'</span><span style="color: #2e53d1">},
                            {</span><span style="color: #259241">"debug"</span><span style="color: #2e53d1">,    </span>no_argument<span style="color: #2e53d1">, &amp;</span>deb<span style="color: #2e53d1">, </span><span style="color: #259241">2</span><span style="color: #2e53d1">},</span><span style="color: #937a42">//flag!=NULL时，argument没有用？
                            </span><span style="color: #2e53d1">{</span><span style="color: #259241">0</span><span style="color: #2e53d1">,</span><span style="color: #259241">0</span><span style="color: #2e53d1">,</span><span style="color: #259241">0</span><span style="color: #2e53d1">,</span><span style="color: #259241">0</span><span style="color: #2e53d1">}</span><span style="color: #937a42">//why?
    </span><span style="color: #2e53d1">};
    
    </span><span style="color: #400080">while </span><span style="color: #2e53d1">((</span>result <span style="color: #2e53d1">= </span>getopt_long<span style="color: #2e53d1">(</span>argc<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">, </span><span style="color: #259241">"n:v::"</span><span style="color: #2e53d1">, </span>opts<span style="color: #2e53d1">, </span>NULL<span style="color: #2e53d1">))!=-</span><span style="color: #259241">1</span><span style="color: #2e53d1">) {
        </span><span style="color: #400080">switch </span><span style="color: #2e53d1">(</span>result<span style="color: #2e53d1">) {
            </span><span style="color: #400080">case </span><span style="color: #259241">'n'</span><span style="color: #2e53d1">:</span><span style="color: #937a42">//-n 或者--username
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"username is %s\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'v'</span><span style="color: #2e53d1">:</span><span style="color: #937a42">//-v 或者 --version
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"version is 1.0.0, %s\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">0</span><span style="color: #2e53d1">:</span><span style="color: #937a42">//flag不为NULL
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"debug is %d\targs:%s\n"</span><span style="color: #2e53d1">, </span>deb<span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'?'</span><span style="color: #2e53d1">:</span><span style="color: #937a42">//未定义的选项
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"?: %c %c\n"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">default</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"default: %c\n"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
        }
    }

    </span><span style="color: #400080">return </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
}

</span></pre>
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/93fd676d37d5_116DC/image_2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/93fd676d37d5_116DC/image_thumb.png" width="930" height="317"></a></p>
<p>&nbsp;</p>
<p>Q:</p>
<p>1.另外—version（即长选项）后不知道如何加可选参数？试了下修改程序后—debug –version后都是空格加参数的，参数用optarg读取。</p>
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/93fd676d37d5_116DC/image_4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/93fd676d37d5_116DC/image_thumb_1.png" width="744" height="140"></a></p>
<p>2.最后一个参数没看懂。参考里说是调试用。</p>
<p>参考：</p>
<p><a href="http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx">http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx</a></p>
<p><a href="http://en.wikipedia.org/wiki/Getopt">http://en.wikipedia.org/wiki/Getopt</a></p>
<p><a href="http://blog.csdn.net/lanyan822/article/details/7692013">http://blog.csdn.net/lanyan822/article/details/7692013</a></p>
<p><a href="http://hi.baidu.com/agodliness/blog/item/554a2d516366711b367abeb2.html">http://hi.baidu.com/agodliness/blog/item/554a2d516366711b367abeb2.html</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/180536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-06-27 20:51 <a href="http://www.cppblog.com/izualzhy/archive/2012/06/27/180536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>getopt介绍</title><link>http://www.cppblog.com/izualzhy/archive/2012/06/27/180521.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 27 Jun 2012 11:04:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/06/27/180521.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/180521.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/06/27/180521.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/180521.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/180521.html</trackback:ping><description><![CDATA[<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5f6726d7-a132-4228-81b1-0ded9a564e9c" class="wlWriterEditableSmartContent">Technorati 标签: <a href="http://technorati.com/tags/getopt" rel="tag">getopt</a></div> <p>&nbsp;</p> <p>首先man下看下基本的介绍，注意要</p> <p>$man 3 getopt</p> <p>因为getopt也是shell下的一个命令</p> <p><strong><font size="5">基本介绍：</font></strong></p> <p><strong>原型：</strong></p> <p>#include &lt;unistd.h&gt;</p> <p>int getopt(int argc, char * const argv[],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *optstring);</p> <p>extern char *optarg;<br>extern int optind, opterr, optopt;</p> <p>作用：</p> <p>可以用来解析命令行选项参数，而不用自己再去写程序分析了。</p> <p><strong>参数介绍：</strong></p> <p>1.argc:即传入main函数的argc</p> <p>2.argv:即传入main函数的argv</p> <p>3.optstring:指定预期的选项以及该选项是否存在参数。</p> <p>格式如下：</p> <p>1.单个字符，表示选项，如’x’表示’-x’<br>2.单个字符后接一个冒号：表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。如’x:’表示’-x argument’,但argument在这里可有可无。<br>3 单个字符后跟两个冒号，表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。如’x::’表示’-x argument’（这个特性是GNU的扩张）  <p>举个例子：  <p>如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha<br>host是b的参数，keke是c的参数。但haha并不是d的参数，因为它们中间有空格隔开。  <p align="left">英文好的直接看这里（从getopt.h里抄的）： <p><font color="#0000ff">/* For communication from `getopt' to the caller.<br>&nbsp;&nbsp; When `getopt' finds an option that takes an argument,<br>&nbsp;&nbsp; the argument value is returned here.<br>&nbsp;&nbsp; Also, when `ordering' is RETURN_IN_ORDER,<br>&nbsp;&nbsp; each non-option ARGV-element is returned here.&nbsp; */</font></p> <p><font color="#ff0000">extern char *optarg;</font></p> <p><font color="#0000ff">/* Index in ARGV of the next element to be scanned.<br>&nbsp;&nbsp; This is used for communication to and from the caller<br>&nbsp;&nbsp; and for communication between successive calls to `getopt'.</font></p> <p><font color="#0000ff">&nbsp;&nbsp; On entry to `getopt', zero means this is the first call; initialize.</font></p> <p><font color="#0000ff">&nbsp;&nbsp; When `getopt' returns -1, this is the index of the first of the<br>&nbsp;&nbsp; non-option elements that the caller should itself scan.</font></p> <p><font color="#0000ff">&nbsp;&nbsp; Otherwise, `optind' communicates from one call to the next<br>&nbsp;&nbsp; how much of ARGV has been scanned so far.&nbsp; */</font></p> <p><font color="#ff0000">extern int optind;</font></p> <p><font color="#0000ff">/* Callers store zero here to inhibit the error message `getopt' prints<br>&nbsp;&nbsp; for unrecognized options.&nbsp; */</font></p> <p><font color="#ff0000">extern int opterr;</font></p> <p><font color="#0000ff">/* Set to an option character which was unrecognized.&nbsp; */</font></p> <p><font color="#ff0000">extern int optopt;</font></p> <p>1.extern char* optarg;//选项的参数指针</p> <p>2.extern int optind;//储存该位置，下次将从该位置处开始检查选项（检查时遇到选项或者参数，位置+1）</p> <p>3.extern int opterr;//当opterr=0时，getopt不像stderr输出错误信息</p> <p>4.extern int optopt;//当选项字符不在optstring中或者缺少必要的参数时，该选项存储在optopt，getopt返回’?’</p> <p>调用一次，返回一个选项。</p> <p>在命令行选项参数再也检查不到optstring中包含的选项时，返回－1，同时optind储存第一个不包含选项的命令行参数。</p> <p>其实看个例子就比较容易了：</p><pre class="code"><span style="color: #937a42">/*
 * =====================================================================================
 *       Filename:  getopt_l.c
 *    Description:  getopt learning
 *
 *        Version:  1.0
 *        Created:  06/26/2012 08:13:49 PM
 *
 *         Author:  zhy (), izualzhy@163.com
 * =====================================================================================
 */

</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;unistd.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdio.h&gt;

</span><span style="color: #400080">int </span>main<span style="color: #2e53d1">(</span><span style="color: #400080">int </span>argc<span style="color: #2e53d1">, </span><span style="color: #400080">char </span><span style="color: #2e53d1">*</span>argv<span style="color: #2e53d1">[])
{
    </span><span style="color: #400080">int </span>aflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">, </span>bflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">, </span>cflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
    </span><span style="color: #400080">int </span>ch<span style="color: #2e53d1">;
    </span><span style="color: #400080">while </span><span style="color: #2e53d1">((</span>ch <span style="color: #2e53d1">= </span>getopt<span style="color: #2e53d1">(</span>argc<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">, </span><span style="color: #259241">"ab:c"</span><span style="color: #2e53d1">)) != -</span><span style="color: #259241">1</span><span style="color: #2e53d1">) {
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"optind: %d\n"</span><span style="color: #2e53d1">, </span>optind<span style="color: #2e53d1">);
        </span><span style="color: #400080">switch </span><span style="color: #2e53d1">(</span>ch<span style="color: #2e53d1">) {
            </span><span style="color: #400080">case </span><span style="color: #259241">'a'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"Have option: -a\n"</span><span style="color: #2e53d1">);
                </span>aflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"The argument of -a is %s\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'b'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"Have option: -b\n"</span><span style="color: #2e53d1">);
                </span>bflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"The argument of -b is %s\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'c'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"Have option: -c\n"</span><span style="color: #2e53d1">);
                </span>cflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'?'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"Unknown option: %c\n"</span><span style="color: #2e53d1">, (</span><span style="color: #400080">char</span><span style="color: #2e53d1">)</span>optopt<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
        }
    }
}

<font color="#000000">可以自己先试着写下输出的结果：</font></span></pre>
<p>y@y-VirtualBox:~/Training$ ./getopt_l -a -d -b foo<br>optind: 2<br>Have option: -a<br>The argument of -a is (null)<br>./getopt_l: invalid option -- 'd'<br>optind: 3<br>Unknown option: d<br>optind: 5<br>Have option: -b<br>The argument of -b is foo</p>
<p>逐行解释下输出：</p>
<p>1.-a在命令行里位置为1,下次读取位置为2，因此optind=2</p>
<p>2-3.optstring里有a，同时a是不带参数的，因此检测到-a选项，同时参数为null</p>
<p>4.optstring里没有d(注，这里的输出时getopt本身输出，通过设置opterr可以设置显示还是不显示)</p>
<p>5.下次从3位置，即-b开始读取</p>
<p>6.optstirng没有d，返回？，optopt会存储预期之外的上一个选项。</p>
<p>7.读取-b同时读取其参数foo，因此下一个位置为5</p>
<p>8optstring里有b，读取正确并打印</p>
<p>9.打印b的参数foo，读取不到其他预期之内的选项，完毕返回-1<br></p>
<p>&nbsp;</p>
<p>注意：</p>
<p>默认情况下getopt会重新排列命令行参数的顺序，所以到最后所有不包含选项的命令行参数都排到最后。<br>如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是： -a -b host -ckeke -d ima haha<br>如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数，getopt就会停止，返回-1。 
<p>看个例子：</p><pre class="code"><span style="color: #937a42">/*
 * =====================================================================================
 *       Filename:  getopt_l2.c
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  06/26/2012 08:37:11 PM
 *
 *         Author:  zhy (), izualzhy@163.com
 * =====================================================================================
 */

</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdio.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdlib.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;unistd.h&gt;

</span><span style="color: #400080">int </span>main<span style="color: #2e53d1">(</span><span style="color: #400080">int </span>argc<span style="color: #2e53d1">, </span><span style="color: #400080">char </span><span style="color: #2e53d1">*</span>argv<span style="color: #2e53d1">[])
{
    </span><span style="color: #400080">int </span>result<span style="color: #2e53d1">;

    </span>opterr <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;</span><span style="color: #937a42">//?

    </span><span style="color: #400080">while </span><span style="color: #2e53d1">((</span>result <span style="color: #2e53d1">= </span>getopt<span style="color: #2e53d1">(</span>argc<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">, </span><span style="color: #259241">"ab:c::"</span><span style="color: #2e53d1">)) != -</span><span style="color: #259241">1</span><span style="color: #2e53d1">) {
        </span><span style="color: #400080">switch </span><span style="color: #2e53d1">(</span>result<span style="color: #2e53d1">) {
            </span><span style="color: #400080">case </span><span style="color: #259241">'a'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"option=a, optopt=%c, optarg=%s\n"</span><span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'b'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"option=b, optopt=%c, optarg=%s\n"</span><span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'c'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"option=c, optopt=%c, optarg=%s\n"</span><span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'?'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"result=?, optopt=%c, optarg=%s\n"</span><span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
            </span><span style="color: #400080">default</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"default, result=%c\n"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
        }
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"argv[%d]=%s\n"</span><span style="color: #2e53d1">, </span>optind<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">[</span>optind<span style="color: #2e53d1">]);
    }

    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"result = -1, optind = %d\n"</span><span style="color: #2e53d1">, </span>optind<span style="color: #2e53d1">);
    </span><span style="color: #400080">for </span><span style="color: #2e53d1">( </span>result <span style="color: #2e53d1">= </span>optind<span style="color: #2e53d1">; </span>result<span style="color: #2e53d1">&lt;</span>argc<span style="color: #2e53d1">; </span>result<span style="color: #2e53d1">++)
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"------ argv[%d] = %s\n"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">[</span>result<span style="color: #2e53d1">]);
    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"\n"</span><span style="color: #2e53d1">);
    </span><span style="color: #400080">for </span><span style="color: #2e53d1">( </span>result <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">; </span>result <span style="color: #2e53d1">&lt; </span>argc<span style="color: #2e53d1">; </span>result<span style="color: #2e53d1">++)
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"at the end------argv[%d] = %s\n"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">[</span>result<span style="color: #2e53d1">]);

    </span><span style="color: #400080">return </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
}

</span></pre>
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/3779f94a2dd8_AF3D/image_2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/3779f94a2dd8_AF3D/image_thumb.png" width="929" height="447"></a></p>
<p>&nbsp;</p>
<p>总结：</p>
<p>函数说明&nbsp; getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母，此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”，则表示还有相关的参数，全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息，并将全域变量optopt设为“?”字符，如果不希望getopt()印出错信息，则只要将全域变量opterr设为0即可。<br>返回值&nbsp; 如果找到符合的参数则返回此参数字母，如果参数不包含在参数optstring 的选项字母则返回“?”字符，分析结束则返回-1。 
<p>&nbsp; <p>最后实战一下，假设有以下需求： 
<p>1.同大多数程序一样，-h输出help信息 
<p>2.-c 指定文件路径，如果没有默认为/tmp/c.conf 
<p>3.-d 指定目标文件，必须指定参数，同时该选项必须指定。 
<p>我实现的：<pre class="code"><span style="color: #937a42">/*
 * =====================================================================================
 *       Filename:  getopt_l3.c
 *    Description:  -h for help info
 *                    -c set the config file, default is /tmp/c.conf
 *                    -d set destination file, could not be empty.
 *
 *        Version:  1.0
 *        Created:  06/27/2012 07:12:00 PM
 *
 *         Author:  zhy (), izualzhy@163.com
 * =====================================================================================
 */

</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;unistd.h&gt;
</span><span style="color: #400080">#include </span><span style="color: #259241">&lt;stdio.h&gt;

</span><span style="color: #400080">int </span>main<span style="color: #2e53d1">(</span><span style="color: #400080">int </span>argc<span style="color: #2e53d1">, </span><span style="color: #400080">char </span><span style="color: #2e53d1">*</span>argv<span style="color: #2e53d1">)
{
    </span><span style="color: #400080">int </span>result<span style="color: #2e53d1">;
    </span><span style="color: #400080">int </span>hflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">, </span>cflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">, </span>dflag <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
    </span>opterr <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;

    </span><span style="color: #400080">char </span>helpinfo<span style="color: #2e53d1">[] = </span><span style="color: #259241">"[Usage]: ./a.out \n\
        -h, For Help Info\n\
        -c, Set the config file, default is /tmp/c.conf\n\
        -d, Set the destination file, could not be empty"</span><span style="color: #2e53d1">;

    </span><span style="color: #400080">while </span><span style="color: #2e53d1">((</span>result<span style="color: #2e53d1">=</span>getopt<span style="color: #2e53d1">(</span>argc<span style="color: #2e53d1">, </span>argv<span style="color: #2e53d1">, </span><span style="color: #259241">"hc::d:"</span><span style="color: #2e53d1">)) != -</span><span style="color: #259241">1</span><span style="color: #2e53d1">) {
        </span><span style="color: #400080">switch </span><span style="color: #2e53d1">(</span>result<span style="color: #2e53d1">) {
            </span><span style="color: #400080">case </span><span style="color: #259241">'h'</span><span style="color: #2e53d1">:
                </span>hflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'c'</span><span style="color: #2e53d1">:
                </span>cflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span><span style="color: #400080">if </span><span style="color: #2e53d1">(</span>NULL <span style="color: #2e53d1">!= </span>optarg<span style="color: #2e53d1">) {
                    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"config file set: %s.\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                } </span><span style="color: #400080">else </span><span style="color: #2e53d1">{
                    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"config file set to default: /tmp/c.conf.\n"</span><span style="color: #2e53d1">);
                }
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'d'</span><span style="color: #2e53d1">:
                </span>dflag <span style="color: #2e53d1">= </span><span style="color: #259241">1</span><span style="color: #2e53d1">;
                </span><span style="color: #400080">if </span><span style="color: #2e53d1">(</span>NULL <span style="color: #2e53d1">!= </span>optarg<span style="color: #2e53d1">) {
                    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"destination file set to: %s.\n"</span><span style="color: #2e53d1">, </span>optarg<span style="color: #2e53d1">);
                } </span><span style="color: #400080">else </span><span style="color: #2e53d1">{
                    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"args could not be empty for -d options.\n"</span><span style="color: #2e53d1">);
                    </span><span style="color: #400080">return</span><span style="color: #2e53d1">;
                }
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">case </span><span style="color: #259241">'?'</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"unexpected option: %c.\n"</span><span style="color: #2e53d1">, </span>optopt<span style="color: #2e53d1">);
                </span><span style="color: #400080">break</span><span style="color: #2e53d1">;
            </span><span style="color: #400080">default</span><span style="color: #2e53d1">:
                </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"default, %c"</span><span style="color: #2e53d1">, </span>result<span style="color: #2e53d1">);
        }
    }

    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"options(h:%d, c:%d, d:%d)\n"</span><span style="color: #2e53d1">, </span>hflag<span style="color: #2e53d1">, </span>cflag<span style="color: #2e53d1">, </span>dflag<span style="color: #2e53d1">);
    </span><span style="color: #400080">if </span><span style="color: #2e53d1">(</span>hflag <span style="color: #2e53d1">&amp;&amp; !</span>cflag <span style="color: #2e53d1">&amp;&amp; !</span>dflag<span style="color: #2e53d1">) {</span><span style="color: #937a42">//如果只有h选项，打印并直接返回，否则不打印.
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"%s.\n"</span><span style="color: #2e53d1">, </span>helpinfo<span style="color: #2e53d1">);
        </span><span style="color: #400080">return</span><span style="color: #2e53d1">;
    }
    </span><span style="color: #400080">if </span><span style="color: #2e53d1">(!</span>dflag<span style="color: #2e53d1">) {
        </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"-d option must be setted\n"</span><span style="color: #2e53d1">);
        </span><span style="color: #400080">return</span><span style="color: #2e53d1">;
    }

    </span><span style="color: #937a42">//参数检测完毕，程序真正开始干活了......
    //
    </span>printf<span style="color: #2e53d1">(</span><span style="color: #259241">"Args Setting Right.\n"</span><span style="color: #2e53d1">);
    </span><span style="color: #400080">return </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
}

</span></pre>
<p><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/3779f94a2dd8_AF3D/image_4.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/3779f94a2dd8_AF3D/image_thumb_1.png" width="845" height="315"></a></p>
<p>参考：</p>
<p><a href="http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx">http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx</a></p>
<p><a href="http://en.wikipedia.org/wiki/Getopt">http://en.wikipedia.org/wiki/Getopt</a></p>
<p><a href="http://hi.baidu.com/agodliness/blog/item/554a2d516366711b367abeb2.html">http://hi.baidu.com/agodliness/blog/item/554a2d516366711b367abeb2.html</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/180521.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-06-27 19:04 <a href="http://www.cppblog.com/izualzhy/archive/2012/06/27/180521.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]linux shell 字符串操作（长度，查找，替换）详解</title><link>http://www.cppblog.com/izualzhy/archive/2012/06/07/177962.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 07 Jun 2012 11:20:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/06/07/177962.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/177962.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/06/07/177962.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/177962.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/177962.html</trackback:ping><description><![CDATA[<p><a href="http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html">http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html</a></p> <p>&nbsp;</p> <p>在做shell批处理程序时候，经常会涉及到字符串相关操作。有很多命令语句，如：awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号，可以达到类似效果，大家知道，使用内部操作符会省略启动外部程序等时间，因此速度会非常的快。 <p><strong></strong> <p><strong>一、判断读取字符串值</strong> <blockquote> <p>表达式<br>含义 <p>${var}<br>变量var的值, 与$var相同 <p>${var-DEFAULT}<br>如果var没有被声明, 那么就以$DEFAULT作为其值 * <p>${var:-DEFAULT}<br>如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 * <p>${var=DEFAULT}<br>如果var没有被声明, 那么就以$DEFAULT作为其值 * <p>${var:=DEFAULT}<br>如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 * <p>${var+OTHER}<br>如果var声明了, 那么其值就是$OTHER, 否则就为null字符串 <p>${var:+OTHER}<br>如果var被设置了, 那么其值就是$OTHER, 否则就为null字符串 <p>${var?ERR_MSG}<br>如果var没被声明, 那么就打印$ERR_MSG * <p>${var:?ERR_MSG}<br>如果var没被设置, 那么就打印$ERR_MSG * <p>${!varprefix*}<br>匹配之前所有以varprefix开头进行声明的变量 <p>${!varprefix@}<br>匹配之前所有以varprefix开头进行声明的变量</p></blockquote> <p>加入了“*”&nbsp; 不是意思是： 当然, 如果变量var已经被设置的话, 那么其值就是$var. <blockquote> <p>[chengmo@localhost ~]$ echo ${abc-'ok'}<br>ok<br>[chengmo@localhost ~]$ echo $abc <p>[chengmo@localhost ~]$ echo ${abc='ok'}<br>ok<br>[chengmo@localhost ~]$ echo $abc<br>ok <p>如果abc 没有声明“=" 还会给abc赋值。 <p>[chengmo@localhost ~]$ var1=11;var2=12;var3=<br>[chengmo@localhost ~]$ echo ${!v@}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>var1 var2 var3<br>[chengmo@localhost ~]$ echo ${!v*}<br>var1 var2 var3 <p>${!varprefix*}与${!varprefix@}相似，可以通过变量名前缀字符，搜索已经定义的变量,无论是否为空值。</p></blockquote> <p><strong>二、字符串操作（长度，读取，替换）</strong> <blockquote> <p>表达式<br>含义 <p>${#string}<br>$string的长度 <p>${string:position}<br>在$string中, 从位置$position开始提取子串 <p>${string:position:length}<br>在$string中, 从位置$position开始提取长度为$length的子串 <p>${string#substring}<br>从变量$string的开头, 删除最短匹配$substring的子串 <p>${string##substring}<br>从变量$string的开头, 删除最长匹配$substring的子串 <p>${string%substring}<br>从变量$string的结尾, 删除最短匹配$substring的子串 <p>${string%%substring}<br>从变量$string的结尾, 删除最长匹配$substring的子串 <p>${string/substring/replacement}<br>使用$replacement, 来代替第一个匹配的$substring <p>${string//substring/replacement}<br>使用$replacement, 代替<em>所有</em>匹配的$substring <p>${string/#substring/replacement}<br>如果$string的<em>前缀</em>匹配$substring, 那么就用$replacement来代替匹配到的$substring <p>${string/%substring/replacement}<br>如果$string的<em>后缀</em>匹配$substring, 那么就用$replacement来代替匹配到的$substring</p></blockquote> <p><strong>说明："*</strong> $substring”可以是一个<em>正则表达式</em>. <blockquote> <p><strong>1.长度</strong> <p>[web97@salewell97 ~]$ test='I love china'<br>[web97@salewell97 ~]$ echo ${#test}<br>12 <p>${#变量名}得到字符串长度 <p><strong>2.截取字串</strong> <p>[chengmo@localhost ~]$ test='I love china'<br>[chengmo@localhost ~]$ echo ${test:5}&nbsp;&nbsp;&nbsp; <br>e china<br>[chengmo@localhost ~]$ echo ${test:5:10}<br>e china <p>${变量名:起始:长度}得到子字符串 <p><strong>3.字符串删除</strong> <p>[chengmo@localhost ~]$ test='c:/windows/boot.ini'<br>[chengmo@localhost ~]$ echo ${test#/}<br>c:/windows/boot.ini<br>[chengmo@localhost ~]$ echo ${test#*/}<br>windows/boot.ini<br>[chengmo@localhost ~]$ echo ${test##*/}<br>boot.ini <p>[chengmo@localhost ~]$ echo ${test%/*}<br>c:/windows<br>[chengmo@localhost ~]$ echo ${test%%/*} <p>${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。 <p>${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。 <p>注意：${test##*/},${test%/*} 分别是得到文件名，或者目录地址最简单方法。 <p><strong>4.字符串替换</strong> <p>[chengmo@localhost ~]$ test='c:/windows/boot.ini'<br>[chengmo@localhost ~]$ echo ${test/\//\\}<br>c:\windows/boot.ini<br>[chengmo@localhost ~]$ echo ${test//\//\\}<br>c:\windows\boot.ini <p>${变量/查找/替换值} 一个“/”表示替换第一个，”//”表示替换所有,当查找中出现了：”/”请加转义符”\/”表示。</p></blockquote> <p><strong>三、性能比较</strong> <blockquote> <p>在shell中，通过awk,sed,expr 等都可以实现，字符串上述操作。下面我们进行性能比较。 <p>[chengmo@localhost ~]$ test='c:/windows/boot.ini'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>[chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test};done;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <p>real&nbsp;&nbsp;&nbsp; 0m0.173s<br>user&nbsp;&nbsp;&nbsp; 0m0.139s<br>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m0.004s <p>[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $test);done;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <p>real&nbsp;&nbsp;&nbsp; 0m9.734s<br>user&nbsp;&nbsp;&nbsp; 0m1.628s <p>速度相差上百倍，调用外部命令处理，与内置操作符性能相差非常大。在shell编程中，尽量用内置操作符或者函数完成。使用awk,sed类似会出现这样结果。</p></blockquote> <p>分类: <a href="http://www.cnblogs.com/chengmo/category/251322.html">linux</a></p><img src ="http://www.cppblog.com/izualzhy/aggbug/177962.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-06-07 19:20 <a href="http://www.cppblog.com/izualzhy/archive/2012/06/07/177962.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>iwlist时的print_scanning_info错误</title><link>http://www.cppblog.com/izualzhy/archive/2012/05/26/176257.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 26 May 2012 08:47:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/05/26/176257.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/176257.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/05/26/176257.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/176257.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/176257.html</trackback:ping><description><![CDATA[<p>&nbsp;</p> <p>Wireless Tools For Linux：</p> <p><a href="http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html">http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html</a></p> <p>可以看到最新的稳定版本是v29，不过我在用的时候经常碰到这个错误：</p> <p>[root@y root]# iwlist scanning<br>lo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Interface doesn't support scanning.</p> <p>usb0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Interface doesn't support scanning.</p> <p><font color="#ff0000">print_scanning_info: Allocation failed</font></p> <p>&nbsp;</p> <p>于是下载了29的源码，错误的出处：</p> <p>&nbsp;</p><pre class="code">realloc<span style="color: #2e53d1">:
      </span><span style="color: #937a42">/* (Re)allocate the buffer - realloc(NULL, len) == malloc(len) */
      </span>newbuf <span style="color: #2e53d1">= </span>realloc<span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">, </span>buflen<span style="color: #2e53d1">);
      </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>newbuf <span style="color: #2e53d1">== </span>NULL<span style="color: #2e53d1">)
        {
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">)
        </span>free<span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">);
          </span>fprintf<span style="color: #2e53d1">(</span>stderr<span style="color: #2e53d1">, </span><span style="color: #259241">"%s: Allocation failed\n"</span><span style="color: #2e53d1">, </span>__FUNCTION__<span style="color: #2e53d1">);
          </span><span style="color: #400080">return</span><span style="color: #2e53d1">(-</span><span style="color: #259241">1</span><span style="color: #2e53d1">);
        }
      </span>buffer <span style="color: #2e53d1">= </span>newbuf<span style="color: #2e53d1">;

      </span><span style="color: #937a42">/* Try to read the results */
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>pointer <span style="color: #2e53d1">= </span>buffer<span style="color: #2e53d1">;
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>flags <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length <span style="color: #2e53d1">= </span>buflen<span style="color: #2e53d1">;
      </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>iw_get_ext<span style="color: #2e53d1">(</span>skfd<span style="color: #2e53d1">, </span>ifname<span style="color: #2e53d1">, </span>SIOCGIWSCAN<span style="color: #2e53d1">, &amp;</span>wrq<span style="color: #2e53d1">) &lt; </span><span style="color: #259241">0</span><span style="color: #2e53d1">)
        {
          </span><span style="color: #937a42">/* Check if buffer was too small (WE-17 only) */
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">((</span>errno <span style="color: #2e53d1">== </span>E2BIG<span style="color: #2e53d1">) &amp;&amp; (</span>range<span style="color: #2e53d1">.</span>we_version_compiled <span style="color: #2e53d1">&gt; </span><span style="color: #259241">16</span><span style="color: #2e53d1">))
        {
          </span><span style="color: #937a42">/* Some driver may return very large scan results, either
           * because there are many cells, or because they have many
           * large elements in cells (like IWEVCUSTOM). Most will
           * only need the regular sized buffer. We now use a dynamic
           * allocation of the buffer to satisfy everybody. Of course,
           * as we don't know in advance the size of the array, we try
           * various increasing sizes. Jean II */

          /* Check if the driver gave us any hints. */
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length <span style="color: #2e53d1">&gt; </span>buflen<span style="color: #2e53d1">)
            </span>buflen <span style="color: #2e53d1">= </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length<span style="color: #2e53d1">;
          </span><span style="color: #400080">else
            </span>buflen <span style="color: #2e53d1">*= </span><span style="color: #259241">2</span><span style="color: #2e53d1">;

          </span><span style="color: #937a42">/* Try again */
          </span><span style="color: #400080">goto </span>realloc<span style="color: #2e53d1">;
        }
<font color="#000000" face="微软雅黑">E2BIG错误发出后，会修改要realloc的内存块长度</font></span></pre><pre class="code"><span style="color: #2e53d1"><font color="#000000" face="微软雅黑">buflen*=2</font></span></pre><pre class="code"><span style="color: #2e53d1"><font color="#000000" face="微软雅黑">就很让人费解了，加入了些打印消息后编译了下发现，length=0时也会发出E2BIG的errno，很让人费解，不过以自己目前的知识积累，没法继续研究下去了。</font></span></pre><pre class="code"><span style="color: #2e53d1"><font color="#000000" face="微软雅黑">试着修改了下，修改的过程中，发现了最开始的网址的这么一块：</font></span></pre><pre class="code"><a href="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/8cc12d807ed1_E17B/image_2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/izualzhy/Windows-Live-Writer/8cc12d807ed1_E17B/image_thumb.png" width="934" height="51"></a></pre><pre class="code"><font color="#ff0000">slightly bigger scan buffer</font></pre><pre class="code"><font color="#000000">于是下载了该版本看了下，相关处已经改成了：</font></pre><pre class="code">realloc<span style="color: #2e53d1">:
      </span><span style="color: #937a42">/* (Re)allocate the buffer - realloc(NULL, len) == malloc(len) */
      </span>newbuf <span style="color: #2e53d1">= </span>realloc<span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">, </span>buflen<span style="color: #2e53d1">);
      </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>newbuf <span style="color: #2e53d1">== </span>NULL<span style="color: #2e53d1">)
        {
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">)
        </span>free<span style="color: #2e53d1">(</span>buffer<span style="color: #2e53d1">);
          </span>fprintf<span style="color: #2e53d1">(</span>stderr<span style="color: #2e53d1">, </span><span style="color: #259241">"%s: Allocation failed\n"</span><span style="color: #2e53d1">, </span>__FUNCTION__<span style="color: #2e53d1">);
          </span><span style="color: #400080">return</span><span style="color: #2e53d1">(-</span><span style="color: #259241">1</span><span style="color: #2e53d1">);
        }
      </span>buffer <span style="color: #2e53d1">= </span>newbuf<span style="color: #2e53d1">;

      </span><span style="color: #937a42">/* Try to read the results */
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>pointer <span style="color: #2e53d1">= </span>buffer<span style="color: #2e53d1">;
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>flags <span style="color: #2e53d1">= </span><span style="color: #259241">0</span><span style="color: #2e53d1">;
      </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length <span style="color: #2e53d1">= </span>buflen<span style="color: #2e53d1">;
      </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>iw_get_ext<span style="color: #2e53d1">(</span>skfd<span style="color: #2e53d1">, </span>ifname<span style="color: #2e53d1">, </span>SIOCGIWSCAN<span style="color: #2e53d1">, &amp;</span>wrq<span style="color: #2e53d1">) &lt; </span><span style="color: #259241">0</span><span style="color: #2e53d1">)
        {
          </span><span style="color: #937a42">/* Check if buffer was too small (WE-17 only) */
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">((</span>errno <span style="color: #2e53d1">== </span>E2BIG<span style="color: #2e53d1">) &amp;&amp; (</span>range<span style="color: #2e53d1">.</span>we_version_compiled <span style="color: #2e53d1">&gt; </span><span style="color: #259241">16</span><span style="color: #2e53d1">)
         &amp;&amp; (</span>buflen <span style="color: #2e53d1">&lt; </span><span style="color: #259241">0xFFFF</span><span style="color: #2e53d1">))
        {
          </span><span style="color: #937a42">/* Some driver may return very large scan results, either
           * because there are many cells, or because they have many
           * large elements in cells (like IWEVCUSTOM). Most will
           * only need the regular sized buffer. We now use a dynamic
           * allocation of the buffer to satisfy everybody. Of course,
           * as we don't know in advance the size of the array, we try
           * various increasing sizes. Jean II */

          /* Check if the driver gave us any hints. */
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length <span style="color: #2e53d1">&gt; </span>buflen<span style="color: #2e53d1">)
            </span>buflen <span style="color: #2e53d1">= </span>wrq<span style="color: #2e53d1">.</span>u<span style="color: #2e53d1">.</span>data<span style="color: #2e53d1">.</span>length<span style="color: #2e53d1">;
          </span><span style="color: #400080">else
            </span>buflen <span style="color: #2e53d1">*= </span><span style="color: #259241">2</span><span style="color: #2e53d1">;

          </span><span style="color: #937a42">/* wrq.u.data.length is 16 bits so max size is 65535 */
          </span><span style="color: #400080">if</span><span style="color: #2e53d1">(</span>buflen <span style="color: #2e53d1">&gt; </span><span style="color: #259241">0xFFFF</span><span style="color: #2e53d1">)
            </span>buflen <span style="color: #2e53d1">= </span><span style="color: #259241">0xFFFF</span><span style="color: #2e53d1">;

          </span><span style="color: #937a42">/* Try again */
          </span><span style="color: #400080">goto </span>realloc<span style="color: #2e53d1">;
        }
</span></pre>
<p>。</p>
<p>不过这里出错时，所得的buffer是空字符串，而不是截断的字符串，所以用处不大，对自己用处不大。o(╯□╰)o</p>
<p>做个记录，深感自己的知识积累太少，平时没事拿过这种代码来就该学习下。</p><pre class="code"><span style="color: #2e53d1"><font color="#000000" face="微软雅黑"></font></span>&nbsp;</pre><img src ="http://www.cppblog.com/izualzhy/aggbug/176257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-05-26 16:47 <a href="http://www.cppblog.com/izualzhy/archive/2012/05/26/176257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux命令iwconfig配置wlan的使用方法</title><link>http://www.cppblog.com/izualzhy/archive/2012/05/16/175108.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 16 May 2012 11:55:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2012/05/16/175108.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/175108.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2012/05/16/175108.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/175108.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/175108.html</trackback:ping><description><![CDATA[<p>Tags: linux iwconfig wlan 无线</p> <p>先看下help</p> <p># iwconfig -h<br>Usage: iwconfig [interface]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface essid {NNN|any|on|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface mode {managed|ad-hoc|master|...}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface freq N.NNN[k|M|G]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface channel N<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface bit {N[k|M|G]|auto|fixed}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface rate {N[k|M|G]|auto|fixed}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface enc {NNNN-NNNN|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface key {NNNN-NNNN|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface power {period N|timeout N|saving N|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface nickname NNN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface nwid {NN|on|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface ap {N|off|auto}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface txpower {NmW|NdBm|off|auto}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface sens N<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface retry {limit N|lifetime N}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface rts {N|auto|fixed|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface frag {N|auto|fixed|off}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface modulation {11g|11a|CCK|OFDMg|...}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interface commit<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Check man pages for more details.</p> <p><strong><font color="#ff0000" size="3">看了下比较需要注意的两个地方：</font></strong><br>1.essid即wifi热点名称，iwlist wlan0 scan可以查看当前搜索到的wifi点。其中wlan0是你的无线设备名称。</p> <p>[root@kindle /DuoKan]# iwlist<br>Usage: iwlist [interface] scanning [essid NNN] [last]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] frequency<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] channel<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] bitrate<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] rate<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] encryption<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] keys<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] power<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] txpower<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] retry<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] ap<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] accesspoints<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] peers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] event<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] auth<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] wpakeys<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] genie<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [interface] modulation</p> <p>2.key关键字的使用，数字与ascii是不同的。<br></p> <p><strong><font color="#ff0000" size="3">转篇文章，写的非常详细：</font></strong></p> <p>iwconfig是Linux Wireless Extensions(LWE)的用户层配置工具之一。LWE是Linux下对无线网络配置的工具，包括内核的支持、用户层配置工具和驱动接口的支持三部分。目前很多无线网卡都支持LWE，而且主流的Linux发布版本，比如Redhat Linux、Ubuntu Linux都已经带了这个配置工具。  <p>1、iwconfig<br>用法: iwconfig interface [essid {NN|on|off}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [nwid {NN|on|off}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [mode {managed|ad-hoc|...}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [freq N.NNNN[k|M|G]]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [channel N]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ap {N|off|auto}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [sens N]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [nick N]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [rate {N|auto|fixed}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [rts {N|auto|fixed|off}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [frag {N|auto|fixed|off}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [enc {NNNN-NNNN|off}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [power {period N|timeout N}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [retry {limit N|lifetime N}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [txpower N {mW|dBm}]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [commit]  <p>说明：iwconfig是LWE最主要的工具，可以对无线网卡的大部分参数进行配置。<br>参数：<br>essid：设置无线网卡的ESSID(Extension Service Set ID)。通过ESSID来区分不同的无线网络，正常情况下只有相同ESSID的无线站点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 才可以互相通讯，除非想监听无线网络。其后的参数为双引号括起的ESSID字符串，或者是any/on/off，如果ESSID字符串中包含<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; any/no/off，则需要在前面加"--"。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 essid any&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 允许任何ESSID，也就是混杂模式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 essid "My Network"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置ESSID为"My Network"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 essid -- "ANY"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置ESSID为"ANY"<br>&nbsp; nwid: Network ID，只用于pre-802.11的无线网卡，802.11网卡利用ESSID和AP的MAC地址来替换nwid，现在基本上不用设置。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 nwid AB34<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 nwid off<br>&nbsp; nick: Nickname，一些网卡需要设置该参数，但是802.11协议栈、MAC都没有用到该参数，一般也不用设置。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 nickname "My Linux Node"<br>&nbsp; mode：设置无线网卡的工作模式，可以是<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ad-hoc：不带AP的点对点无线网络<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Managed：通过多个AP组成的网络，无线设备可以在这个网络中漫游<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Master：设置该无线网卡为一个AP<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Repeater：设置为无线网络中继设备，可以转发网络包<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Secondary：设置为备份的AP/Repeater<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Monitor：监听模式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Auto：由无线网卡自动选择工作模式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 mode Managed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 mode Ad-Hoc<br>&nbsp; freq/channel：设置无线网卡的工作频率或者频道，小于1000的参数被认为是频道，大于10000的参数被认为是频率。频率单位为Hz，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以在数字后面附带k, M, G来改变数量级，比如2.4G。频道从1开始。使用lwlist工具可以查看无线网卡支持的频率<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 和频道。参数off/auto指示无线网络自动挑选频率。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 注意：如果是Managed模式，AP会指示无线网卡的工作频率，因此该设置的参数会被忽略。Ad-hoc模式下只使用该设定的频率<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 初始无线网络，如果加入已经存在的Ad-hoc网络则会忽略该设置的频率参数。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 freq 2422000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 freq 2.422G<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 channel 3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 channel auto  <p>&nbsp; ap：连接到指定的AP或者无线网络，后面的参数可以是AP的MAC地址，也可以是iwlist scan出来的标识符。如果是Ad-hoc，则连接到<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一个已经存在的Ad-hoc网络。使用off参数让无线网卡不改变当前已连接的AP下进入自动模式。any/auto参数，无线网卡自动选择<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最好的AP。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 注意：如果无线信号低到一定程度，无线网络会进入自动选择AP模式。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 ap 00:60:1D:01:23:45<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 ap any<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 ap off  <p>&nbsp; rate/bit：如果无线网卡支持多速率，则可以通过该命令设置工作的速率。小于1000的参数由具体的无线网卡驱动定义，一般是传输速<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 率的索引值，大于1000的为速率，单位bps，可以在数字后面附带k, M, G来指定数量级。auto参数让无线网卡自动选择速率<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fixed参数让无线网卡不使用自动速率模式。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 rate 11M<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 rate auto<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 rate 5.5M auto&nbsp;&nbsp;&nbsp; //自动选择5.5M以下的速率<br>&nbsp; txpower：如果无线网卡支持多发射功率设定，则使用该参数设定发射，单位为dBm，如果指定为W（毫瓦），只转换公式为：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dBm=30+log(W)。参数on/off可以打开和关闭发射单元，auto和fixed指定无线是否自动选择发射功率。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 txpower 15<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 txpower 30mW<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 txpower auto<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 txpower off&nbsp; <br>&nbsp; sens：设置接收灵敏度的下限，在该下限之下，无线网卡认为该无线网络信号太差，不同的网卡会采取不同的措施，一些现代的无线网卡<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 会自动选择新的AP。正的参数为raw data，直接传给无线网卡驱动处理，一般认为是百分比。负值表示dBm值。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 sens -80<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 sens 2<br>&nbsp; retry：设置无线网卡的重传机制。limit ‘value’ 指定最大重传次数；lifetime ‘value’指定最长重试时间，单位为秒，可以附带m和u来<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指定单位为毫秒和微秒。如果无线网卡支持自动模式，则在limit和lifetime之前还可以附加min和max来指定上下限值。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 retry 16<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 retry lifetime 300m<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 retry min limit 8<br>&nbsp; rts：指定RTS/CTS握手方式，使用RTS/CTS握手会增加额外开销，但如果无线网络中有隐藏无线节点或者有很多无线节点时可以提高性能。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 后面的参数指定一个使用该机制的最小包的大小，如果该值等于最大包大小，则相当于禁止使用该机制。可以使用auto/off/fixed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 参数。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 rts 250<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 rts off<br>&nbsp; frag：设置发送数据包的分片大小。设置分片会增加额外开销，但在噪声环境下可以提高数据包的到达率。一般情况下该参数小于最大包<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 大小，有些支持Burst模式的无线网卡可以设置大于最大包大小的值来允许Burst模式。还可以使用auto/fixed/off参数。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 frag 512<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 frag off&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; key/enc[ryption]：设置无线网卡使用的加密密钥，此处为设置WEP模式的加密key，如果要使用WPA，需要wpa_supplicant工具包。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 密钥参数可以是 XXXX-XXXX-XXXX-XXXX 或者 XXXXXXXX 格式的十六进制数值，也可以是s:xxxxxx的ASCII字符。如果在密钥参数之前<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 加了[index]，则只是设置该索引值对应的密钥，并不改变当前的密钥。直接指定[index]值可以设置当前使用哪一个密钥。指定on/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; off可以控制是否使用加密模式。open/restricted指定加密模式，取决于不同的无线网卡，大多数无线网卡的open模式不使用加密且<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 允许接收没有加密的数据包，restricted模式使用加密。可以使用多个key参数，但只有最后一个生效。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WEP密钥可以是40bit，用10个十六进制数字或者5个ASCII字符表示，也可以是128bit，用26个十六进制数字或者13个ASCII字符表<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示。  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key 0123-4567-89<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key [3] 0123-4567-89<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key s:password [2]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key [2]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key open<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key off<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key restricted [3] 0123456789<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 key 01-23 key 45-67 [4] key [4]<br>&nbsp; power：设置无线网卡的电源管理模式。period ‘value’ 指定唤醒的周期，timeout ‘value’指定进入休眠的等待时间，这两个参数之前可以<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 加min和max修饰，这些值的单位为秒，可以附加m和u来指定毫秒和微秒。off/on参数指定是否允许电源管理，all/unicast/multicast<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指定允许唤醒的数据包类型。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 power period 2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 power 500m unicast<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 power timeout 300u all<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 power off<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #iwconfig eth0 power min period 2 power max period 4<br>&nbsp; commit：提交所有的参数修改给无线网卡驱动。有些无线网卡驱动会先缓存无线网卡参数修，使用这个命令来让无线网卡的参数修改生效。不过一<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 般不需要使用该命令，因为无线网卡驱动最终都会是参数的修改生效，一般在debug时会用到。<br>2、参考文档：<br>iwconfig(8) 工具 Man手册。 </p><img src ="http://www.cppblog.com/izualzhy/aggbug/175108.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2012-05-16 19:55 <a href="http://www.cppblog.com/izualzhy/archive/2012/05/16/175108.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>RPC: Program not registered</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/23/160841.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 23 Nov 2011 15:17:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/23/160841.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/160841.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/23/160841.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/160841.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/160841.html</trackback:ping><description><![CDATA[<p>有时在机器上执行了mount命令后，会返回”RPC: Program not registered”这样的错误，这是因为在设置了共享文件的机器上上缺少mountd和nfsd 这两个守护进程(daemon)。UNIX系统启动时，系统会自动检查/etc/dfs/dfstab文件，如果文件中有要共享的资源，则启动这两个守护进程，反之，则不启动。所以如果我们在/etc/dfs/dfstab中没有内容，则系统不能启动mountd和nfsd这两个守护进程，用share命令共享的目录也不能真正被别的系统共享。 <p>sudo /etc/init.d/nfs-kernel-server restart <p>就可以了，如果还是不行，用rpc.mountd试一下。</p><img src ="http://www.cppblog.com/izualzhy/aggbug/160841.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-23 23:17 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/23/160841.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UNIX系统文件IO</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/23/160839.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 23 Nov 2011 15:14:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/23/160839.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/160839.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/23/160839.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/160839.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/160839.html</trackback:ping><description><![CDATA[<p><b>基本知识：</b> <p>对于内核而言，所有打开的文件都通过文件描述符引用。例如对于Linux应用程序员来讲，音频编程接口实际上就是一组音频设备文件，通过它们可以从声卡读取数据，或者向声卡写入数据，并且能够对声卡进行控制，设置采样频率和声道数目等等。 <p>当读或写一个文件时，使用open或create返回的文件描述符标识该文件，将其作为参数传送给read或write。 <p>UNIX系统shell使用文件描述符0与进程的标准输入相关联，文件描述符1与标准输出相关联，文件描述符2与标准出错相关联。在依从POSIX的应用程序中，幻数0，1，2应当替换成常量</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/* Standard file descriptors. */</span><br><br><span style="color: #cc6633">#define</span> STDIN_FILENO 0 <span style="color: #008000">/* Standard input. */</span><br><br><span style="color: #cc6633">#define</span> STDOUT_FILENO 1 <span style="color: #008000">/* Standard output. */</span><br><br><span style="color: #cc6633">#define</span> STDERR_FILENO 2 <span style="color: #008000">/* Standard error output. */</span><br><br><span style="color: #008000">//defined in /usr/include/unistd.h。</span><br></pre><br></div>
<p>1. open, creat - 用来 打开和创建 一个 文件或设备
<p>描述：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">int</span> open(<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *pathname, <span style="color: #0000ff">int</span> flags);<br><br><span style="color: #0000ff">int</span> open(<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *pathname, <span style="color: #0000ff">int</span> flags, mode_t mode)<br><br><span style="color: #0000ff">int</span> creat(<span style="color: #0000ff">const</span> <span style="color: #0000ff">char</span> *pathname, mode_t mode);<br></pre><br></div>
<p>open() 通常 用于 将 路径名 转换为 一个 文件描述符 (一个 非负的 小 整数, 在 read , write 等 I/O操作中 将会被使用). 当 open() 调用 成功, 它会 返回 一个 新的 文件描述符 (永远取 未用 描述符的 最小值). 这个调用 创建 一个 新的 打开文件, 即 分配 一个 新的 独一无 二的 文件描述符, 不会与 运行中的 任何 其他程序 共享 (但 可以 通过 fork (2) 系统调用 实现 共享). 这个 新的 文件描述符 在其后 对 打开文件操作 的函数 中 使用.(参考 fcntl(2)). 文件的 读写 指针 被 置于 文件头参数 flags 是通过 O_RDONLY, O_WRONLY 或 O_RDWR (指明 文件 是以 只读 , 只写 或 读写 方式 打开的) 与 下面的 零个
<p>或 多个 可选模式 按位 -or 操作 得到的（模式很多，就不贴了，直接man看下即可，以上描述也是从man 2 open里复制的）。
<p>create函数等效于 open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
<p>两个函数在&lt;fcntl.h&gt;都可以找到函数声明。
<p>2. close函数用来关闭一个文件
<p>描述：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;unistd.h&gt;<br><br><span style="color: #0000ff">int</span> close(<span style="color: #0000ff">int</span> fd);<br></pre><br></div>
<p>当一个进程终止时，内核自动关闭它所有打开的文件。很多程序利用了这一功能而不显式的用close关闭打开文件。
<p>3. lseek：设置文件偏移量
<p>描述：
<p>每个打开的文件都有一个与其相关联的“当前文件偏移量”(current file offset)。它通常是一个非负整数，用以度量从文件开始处计算的字节数。通常，读、写操作都从文件偏移量处开始，打开一个文件时，除非指定O_APPEND，否则该偏移量设置为0.
<p>#include&lt;unistd.h&gt;
<p>off_t lseek(int fileds, off_t offset, int whence);
<p>SEEK_SET
<p>The offset is set to offset bytes.
<p>SEEK_CUR
<p>The offset is set to its current location plus offset bytes.
<p>SEEK_END
<p>The offset is set to the size of the file plus offset bytes.
<p>可以用下列方式确定打开文件的当前偏移量：
<p>off_t currpos = lseek(fd, 0, SEEK_CUR);
<p>这种方法也可以用来确定所涉及的文件是否可以设置偏移量。如果文件描述符引用的是一个管道、FIFO或网络套接字，则lseek返回-1，并将errno设置为ESPIPE。
<p>lseek仅将当前的文件偏移量记录在内核中，它并不引起任何IO操作。然后，该偏移量用于下一个读写操作。
<p>4. read：从打开文件中读数据
<p>描述：
<p>ssize_t read(int fd, void *buf, size_t count);
<p>成功返回读到的字节数，若已到文件结尾则返回0，若出错则返回-1.
<p>5. write：向打开的文件写入数据。
<p>ssize_t write(int fd, const void *buf, size_t count);
<p><b>代码示例：</b>
<p>从标准输入读取，写到标准输出：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;unistd.h&gt; <br><br><span style="color: #cc6633">#include</span> &lt;stdio.h&gt; <br><br><span style="color: #cc6633">#include</span> &lt;stdlib.h&gt; <br><br><span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> BUFFSIZE = 4096; <br><br><span style="color: #0000ff">int</span> main() <br><br>{ <br><br><span style="color: #0000ff">int</span> n; <br><br><span style="color: #0000ff">char</span> buf[BUFFSIZE]; <br><br><span style="color: #0000ff">while</span> ((n=read(STDIN_FILENO, buf, BUFFSIZE)) &gt; 0) <br><br><span style="color: #0000ff">if</span> (write(STDOUT_FILENO, buf, n) != n) { <br><br>printf(<span style="color: #006080">"write error\n"</span>); <br><br>exit(1); <br><br>} <br><br><span style="color: #0000ff">if</span> (n &lt; 0) { <br><br>printf(<span style="color: #006080">"read error\n"</span>); <br><br>exit(1); <br><br>} <br><br>exit(0); <br><br>}<br></pre><br></div><img src ="http://www.cppblog.com/izualzhy/aggbug/160839.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-23 23:14 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/23/160839.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用shell搜索文本的几种方法 【转】</title><link>http://www.cppblog.com/izualzhy/archive/2011/08/21/153991.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Sat, 20 Aug 2011 17:05:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/08/21/153991.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/153991.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/08/21/153991.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/153991.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/153991.html</trackback:ping><description><![CDATA[<div class="postTitle"><a id="viewpost1_TitleUrl" class="postTitle2" href="http://www.cppblog.com/qinqing1984/archive/2011/08/20/153971.html"><font color="#1a8bc8">使用shell搜索文本的几种方法</font></a> </div><span>&nbsp;&nbsp;&nbsp;<strong>第1种方法是使用find和xargs命令，示例如下<br /></strong></span><span>&nbsp;&nbsp;&nbsp;&nbsp; find&nbsp;dir | xargs grep str，dir是指某个目录<br />&nbsp;&nbsp;&nbsp;&nbsp; find file | xargs grep str，file是指某个文件<br />&nbsp;&nbsp; 注意：这种方法，会递归搜索子目录<br /><br />&nbsp;&nbsp; <strong>第2种方法是直接使用grep命令，示例如下<br /></strong>&nbsp;&nbsp;&nbsp;&nbsp; grep str dir/*，dir是指某个目录，但不递归搜索其子目录<br />&nbsp;&nbsp;&nbsp;&nbsp; grep&nbsp;-r&nbsp;str dir/*，使用-r选项，递归搜索其子目录<br />&nbsp;&nbsp;&nbsp;&nbsp; grep&nbsp;str file，file是指某个文件<br /><br />&nbsp;&nbsp; <strong>第3种方法是综合以上两种，写一个shell脚本，代码如下</strong>&nbsp;&nbsp;&nbsp;<br />&nbsp; &nbsp;#!/bin/bash<br />&nbsp;&nbsp;&nbsp;#find_str.sh&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;if [ $# -lt "2" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "Usage: `basename $0` path name [option]"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1<br />&nbsp;&nbsp;&nbsp;fi&nbsp;&nbsp;&nbsp;<br />&nbsp; &nbsp;#!-r表示递归处理子目录,-i表示忽略大小写<br />&nbsp;&nbsp;&nbsp;path=$1<br />&nbsp;&nbsp;&nbsp;name=$2&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;shift&nbsp;<br />&nbsp;&nbsp;&nbsp;shift&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;<br />&nbsp; &nbsp;for option in "$@"<br />&nbsp;&nbsp;&nbsp;do<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case $option in<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-r) dir_op="-r"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-i) lu_op="-i"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*) if [ -n "$option" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "invalid option"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;;<br />&nbsp;&nbsp;&nbsp;&nbsp;esac<br />&nbsp;&nbsp;&nbsp;done&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp; grep_str_of_file()<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file=$1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str=$2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out=$(grep -n $lu_op "$str" "$file")<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ -n "$out" -a "$file" != "$0" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "$file: $out"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;find_str()<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ -d "$1" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for file in $1/*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ "$dir_op" = "-r" -a -d "$file" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find_str $file $2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif [ -f "$file" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grep_str_of_file $file $2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; done<br />&nbsp;&nbsp;&nbsp; elif [ -f "$1" ]; then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grep_str_of_file $1 $2&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;fi<br />&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;find_str $path $name<br />&nbsp; <br />&nbsp;&nbsp;&nbsp;这样一来，不管$1参数是目录还是文件，都能处理，使用示例如下：<br />&nbsp;&nbsp; ./find_str /usr/include main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不递归搜索子目录，大小写敏感<br />&nbsp;&nbsp;&nbsp;./find_str /usr/include main -i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不递归搜索子目录，忽略大小写<br />&nbsp;&nbsp; ./find_str /usr/include main -r&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 递归搜索子目录，大小写敏感<br />&nbsp;&nbsp;&nbsp;./find_str /usr/include main -r&nbsp;&nbsp;-i&nbsp;&nbsp; 递归搜索子目录，忽略大小写</span>&nbsp;<br />&nbsp;&nbsp; ./find_str main.cpp main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在文件中搜索，大小写敏感<br />&nbsp;&nbsp; ./find_str main.cpp main -i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在文件中搜索，忽略大小写 <br /><br />&nbsp;&nbsp; 上面所述的示例中，str不限于特定的文本，可以是带正则表达式的匹配模式。<br />&nbsp;&nbsp;&nbsp;而第3种方法，也可以用sed替换grep来显示文本行，在此基础上能作更多的处理，<br />&nbsp;&nbsp; 比如格式化显示、统计匹配的文本个数、搜索策略等，在此就不详究了&nbsp;<img src ="http://www.cppblog.com/izualzhy/aggbug/153991.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-08-21 01:05 <a href="http://www.cppblog.com/izualzhy/archive/2011/08/21/153991.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vim里插入当前日期续</title><link>http://www.cppblog.com/izualzhy/archive/2011/03/22/142511.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 22 Mar 2011 12:29:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/03/22/142511.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/142511.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/03/22/142511.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/142511.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/142511.html</trackback:ping><description><![CDATA[想在vim里插入当前日期<br>不过date显示的格式不是很满意，就想用date +%Y_%m_%d-%H:%M的格式来显示<br>结果用了很多步才达到一般的效果。。。。<br>1.于是在。bashrc里修改alias vdt = &#8216;date +%Y_%m_%d-%H:%M&#8217;，一直提示不对。结果花了很长时间才发现=两边不能有空格，真是不仔细阿，想不到规则要求的这么严格。<br>2.不过这么改通过了后，发现vdt输出的时间总是不对，没想明白为什么。<br>于是在/usr/bin下建了各vdt文件，写入date +%Y_%m_%d-%H:%M<br>这样vdt就显示当前时间了。<br>3.在vim里输入:r! vdt就会插入当前时间了<br>4.但这么输入似乎太费劲了，于是想到了vim的映射快捷键<br>修改。vimrc<br>map &lt;F3&gt;d :r! vdt<br>按下F3 d后，就会有 :r! vdt，再按下回车就插入时间了。<br>再结合下<a href="http://www.cppblog.com/izualzhy/archive/2010/12/29/137694.html">http://www.cppblog.com/izualzhy/archive/2010/12/29/137694.html</a>这篇就可以省略Enter了~~
<img src ="http://www.cppblog.com/izualzhy/aggbug/142511.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-03-22 20:29 <a href="http://www.cppblog.com/izualzhy/archive/2011/03/22/142511.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell学习笔记~~</title><link>http://www.cppblog.com/izualzhy/archive/2011/03/18/142169.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Fri, 18 Mar 2011 13:44:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/03/18/142169.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/142169.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/03/18/142169.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/142169.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/142169.html</trackback:ping><description><![CDATA[<p>有时候在文件夹下有一堆cpp文件，虽然有些地方加了输出语句，但打印出该语句时却总是忘了具体在哪个文件里。于是自己写了个非常非常简单的sh，不过还是有用的。。。算是无聊的时间学点别的调剂下了<br>用法就是myfind &#8220;string&#8221;<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">&nbsp;`ls&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">.cpp`<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">do</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>cat&nbsp;$i&nbsp;</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">&nbsp;grep&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">$*</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;echo&nbsp;$i<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>done<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p>&nbsp;</p>
<p><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#122;&#104;&#121;&#64;&#100;&#101;&#115;&#107;&#116;&#111;&#112;&#58;&#47;&#109;&#101;&#100;&#105;&#97;&#47;&#67;&#48;&#70;&#69;&#45;&#70;&#55;&#54;&#65;&#47;&#97;&#36;">@desktop:/media/C0FE-F76A/a$</a> ls<br>createFile.cpp&nbsp; doubleherit2.cpp&nbsp; doubleherit.cpp&nbsp; myfind.sh&nbsp; test.os&nbsp; test.tt&nbsp; translation如何加下划线&nbsp; 新文件<br><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#122;&#104;&#121;&#64;&#100;&#101;&#115;&#107;&#116;&#111;&#112;&#58;&#47;&#109;&#101;&#100;&#105;&#97;&#47;&#67;&#48;&#70;&#69;&#45;&#70;&#55;&#54;&#65;&#47;&#104;&#97;&#110;&#118;&#111;&#110;&#47;&#97;&#36;">@desktop:/media/C0FE-F76A/hanvon/a$</a> myfind void fun_a<br>&nbsp;&nbsp;virtual void fun_a() {<br>&nbsp;&nbsp;void fun_a() {<br>doubleherit2.cpp<br>&nbsp;&nbsp;void fun_a() {<br>&nbsp;&nbsp;void fun_a() {<br>doubleherit.cpp<br><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#122;&#104;&#121;&#64;&#100;&#101;&#115;&#107;&#116;&#111;&#112;&#58;&#47;&#109;&#101;&#100;&#105;&#97;&#47;&#67;&#48;&#70;&#69;&#45;&#70;&#55;&#54;&#65;&#47;&#97;&#36;">@desktop:/media/C0FE-F76A/a$</a><br></p>
<img src ="http://www.cppblog.com/izualzhy/aggbug/142169.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-03-18 21:44 <a href="http://www.cppblog.com/izualzhy/archive/2011/03/18/142169.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>