﻿<?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++博客-让梦想自由的飞翔     garland-随笔分类-Linux</title><link>http://www.cppblog.com/garland/category/1984.html</link><description>garland</description><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 19:46:58 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 19:46:58 GMT</pubDate><ttl>60</ttl><item><title>Shell</title><link>http://www.cppblog.com/garland/archive/2007/08/29/31154.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Wed, 29 Aug 2007 06:41:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2007/08/29/31154.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/31154.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2007/08/29/31154.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/31154.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/31154.html</trackback:ping><description><![CDATA[1.test测试<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong><br>test<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>用于检查某个条件是否成立，它可以进行数值、字符和文件三个方面的测试，<br>其测试符和相应的功能分别如下：<br>(1)数值测试：<br>-eq：等于则为真<br>-ne：不等于则为真<br>-gt：大于则为真<br>-ge：大于等于则为真<br>-lt：小于则为真<br>-le：小于等于则为真<br>(2)字符串测试：<br>=：等于则为真<br>!=：不相等则为真<br>-z 字符串：字符串长度伪则为真<br>-n 字符串：字符串长度不伪则为真<br>(3)文件测试：<br>-e 文件名：如果文件存在则为真<br>-r 文件名：如果文件存在且可读则为真<br>-w 文件名：如果文件存在且可写则为真<br>-x 文件名：如果文件存在且可执行则为真<br>-s 文件名：如果文件存在且至少有一个字符则为真<br>-d 文件名：如果文件存在且为目录则为真<br>-f 文件名：如果文件存在且为普通文件则为真<br>-c 文件名：如果文件存在且为字符型特殊文件则为真<br>-b 文件名：如果文件存在且为块特殊文件则为真<br>另外，Linux还提供了与(&#8220;！&#8221;)、或(&#8220;-o)、非(&#8220;-a&#8221;)三个逻辑操作符用于将测试条件连接起来，<br>其优先级为：&#8220;！&#8221;最高，&#8220;-a&#8221;次之，&#8220;-o&#8221;最低。<br>同时，bash也能完成简单的算术运算，格式如下：<br>$[expression]<br>例如：var1=2<br>var2=$[var1*10+1]<br>则：var2的值为21。<br><br>2.if条件语句<br>if [ -x /sbin/quotaon ]; then <br>echo "Turning on Quota for root filesystem" <br>/sbin/quotaon / <br>elif [ -x /sbin/quotaon ]; then<br>/usr/bin/bash<br>else<br>echo "ok"<br>fi <br><br>3.for 循环<br>#!/bin/sh<br>WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"<br>for i in $WORD ; do <br>echo $i <br>done <br><br>#!/bin/sh <br>FILES=`ls /txt/*.txt`<br>for txt in $FILES ; do <br>doc=`echo $txt | sed "s/.txt/.doc/"` <br>mv $txt $doc <br>done <br><br>4.while和until 循环<br>#!/bin/sh <br>while [ -f /var/run/ppp0.pid ] ; do <br>killall pppd<br>done<br><br>#!/bin/sh <br>until [ -f /var/run/ppp0.pid ] ; do <br>sleep 1 <br>done<br><br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">Shell</strong>还提供了true和false两条<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>用于建立无限循环结构的需要，<br>它们的返回状态分别是总为0或总为非0<br><br>5.case 条件选择<br>#!/bin/sh <br>case $1 in <br>start | begin) <br>echo "start something" <br>;; <br>stop | end) <br>echo "stop something" <br>;; <br>*) <br>echo "Ignorant" <br>;; <br>esac<br>case表达式中也可以使用<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>的通配符(&#8220;*&#8221;、&#8220;？&#8221;、&#8220;[ ]&#8221;)。<br><br>6.无条件控制语句break和continue<br>break 用于立即终止当前循环的执行，而contiune用于不执行循环中后面的语句<br>而立即开始下一个循环的执行。这两个语句只有放在do和done之间才有效。<br><br>7.函数定义<br>在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>中还可以定义函数。函数实际上也是由若干条<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>组成的，<br>因此它与<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序形式上是相似的，不同的是它不是一个单独的进程，<br>而是<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的一部分。函数定义的基本格式为：<br>functionname<br>{<br>若干<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>行<br>}<br>调用函数的格式为：<br>functionname param1 param2 &#8230;&#8230;<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>函数可以完成某些例行的工作，而且还可以有自己的退出状态，<br>因此函数也可以作为if、while等控制结构的条件。<br>在函数定义时不用带参数说明，但在调用函数时可以带有参数，此时<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>将把这些参数分别赋予相应的位置参数$1、$2、...及$*。<br><br>8.<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>分组<br>在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>中有两种<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>分组的方法：&#8220;()&#8221;和&#8220;{}&#8221;，前者当<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>执行()<br>中的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>时将再创建一个新的子进程，然后这个子进程去执行圆括弧中的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>。<br>当用户在执行某个<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>时不想让<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>运行时对状态集合(如位置参数、环境变量、<br>当前工作目录等)的改变影响到下面语句的执行时，就应该把这些<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>放在圆括<br>弧中，这样就能保证所有的改变只对子进程产生影响，而父进程不受任何干扰；<br>{}用于将顺序执行的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>的输出结果用于另一个<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>的输入(管道方式)。当我们<br>要真正使用圆括弧和花括弧时(如计算表达式的优先级)，则需要在其前面加上转<br>义符(\)以便让<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>知道它们不是用于<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>执行的控制所用。<br><br>9.信号<br>trap<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>用于在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序中捕捉到信号，之后可以有三种反应方式：<br>(1)执行一段程序来处理这一信号<br>(2)接受信号的默认操作<br>(3)忽视这一信号<br>trap对上面三种方式提供了三种基本形式：<br>第一种形式的trap<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>接收到signal list清单中数值相同的信号时，<br>将执行双引号中的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>串。<br>trap 'commands' signal-list<br>trap "commands" signal-list<br>为了恢复信号的默认操作，使用第二种形式的trap<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>：<br>trap signal-list<br>第三种形式的trap<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>允许忽视信号：<br>trap " " signal-list<br>注意：<br>(1)对信号11(段违例)不能捕捉，因为<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>本身需要捕捉该信号去进行内存的转储。<br>(2)在trap中可以定义对信号0的处理(实际上没有这个信号)，<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序在其终止<br>(如执行exit语句)时发出该信号。<br>(3)在捕捉到signal-list中指定的信号并执行完相应的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>之后，如果这些<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>没有将<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序终止的话，<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序将继续执行收到信号时所执行的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>后面的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>，这样<br>将很容易导致<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序无法终止。<br>另外，在trap语句中，单引号和双引号是不同的，当<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序第一次碰到trap语句时，<br>将把commands中的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>扫描一遍。此时若commands是用单引号括起来的话，那么<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><br>不会对commands中的变量和<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>进行替换，否则commands中的变量和<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>将用当时具体<br>的值来替换。<br><br>10. 运行<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的方法<br>执行<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的方法有三种：<br>(1)sh <strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序文件名<br>格式为：<br>bash <strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong> 程序文件名<br>这实际上是调用一个新的bash<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>解释程序，而把<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序文件名作为参数传递给它。<br>新启动的<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>将去读指定的文件，执行文件中列出的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>，当所有的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>都执行完结束。<br>该方法的优点是可以利用<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>调试功能。<br>(2)sh&lt;<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序文件名<br>格式为：<br>bash&lt;<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong> 程序文件名<br>这种方式就是利用输入重定向，使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>解释程序的输入取自指定的程序文件。<br>(3)用chmod<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序成为可执行的<br><br>11. bash程序的调试<br>bash -选择项 <strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序文件名<br>几个常用的选择项是：<br>-e：如果一个<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>失败就立即退出<br>-n：读入<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>但是不执行它们<br>-u：置换时把未设置的变量看作出错<br>-v：当读入<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>输入行时把它们显示出来<br>-x：执行<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>时把<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>和它们的参数显示出来<br>上面的所有选项也可以在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序内部用&#8220;set -选择项&#8221;的形式引用，而&#8220;set +选择项&#8221;则<br>将禁止该选择项起作用。如果只想对程序的某一部分使用某些选择项时，则可以将该部分用<br>上面两个语句包围起来。<br>1.未置变量退出和立即退出<br>未置变量退出特性允许用户对所有变量进行检查，如果引用了一个未赋值的变量就终止<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><br>程序的执行。<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>通常允许未置变量的使用，在这种情况下，变量的值为空。如果设置了未<br>置变量退出选择项，则一旦使用了未置变量就显示错误信息，并终止程序的运行。未置变量退<br>出选择项为&#8220;-u&#8221;。<br>当<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>运行时，若遇到不存在或不可执行的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>、重定向失败或<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>非正常结束等情况时，如<br>果未经重新定向，该出错信息会打印在终端屏幕上，而<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序仍将继续执行。要想在错误发<br>生时迫使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序立即结束，可以使用&#8220;-e&#8221;选项将<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的执行立即终止。<br>2.<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的跟踪<br>调试<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的主要方法是利用<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>解释程序的&#8220;-v&#8221;或&#8220;-x&#8221;选项来跟踪程序的执行。&#8220;-v&#8221;<br>选择项使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>在执行程序的过程中，把它读入的每一个<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>行都显示出来，而&#8220;-x&#8221;选择项使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><br>在执行程序的过程中把它执行的每一个<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>在行首用一个&#8220;+&#8221;加上<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>名显示出来。并把每一个变量<br>和该变量所取的值也显示出来，因此，它们的主要区别在于：在执行<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>行之前无&#8220;-v&#8221;则打印出命<br>令行的原始内容，而有&#8220;-v&#8221;则打印出经过替换后的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>行的内容。<br>除了使用<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>的&#8220;-v&#8221;和&#8220;-x&#8221;选择项以外，还可以在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序内部采取一些辅助调试的措施。<br>例如，可以在<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序的一些关键地方使用echo<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>把必要的信息显示出来，它的作用相当于C语<br>言中的printf语句，这样就可以知道程序运行到什么地方及程序目前的状态。<br><br>12. bash的内部<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong><br>bash<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>解释程序包含了一些内部<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>。内部<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>在目录列表时是看不见的，它们由<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>本身提供。<br>常用的内部<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>有：echo、eval、exec、export、readonly、read、shift、wait和点(.)。<br>下面简单介绍其<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式和功能。<br>1.echo<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：echo arg<br>功能：在屏幕上打印出由arg指定的字符串。<br>2.eval<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：eval args<br>功能：当<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序执行到eval语句时，<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>读入参数args，并将它们组合成一个新的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>，然后<br>执行。<br>3.exec<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：exec <strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong> <strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>参数<br>功能：当<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>执行到exec语句时，不会去创建新的子进程，而是转去执行指定的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>，<br>当指定的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>执行完时，该进程，也就是最初的<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>就终止了，所以<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序中exec<br>后面的语句将不再被执行。<br>4.export<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：export 变量名 或：export 变量名=变量值<br>功能：<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>可以用export把它的变量向下带入子<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>从而让子进程继承父进程中的环境变量。<br>但子<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>不能用export把它的变量向上带入父<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>。<br>注意：不带任何变量名的export语句将显示出当前所有的export变量。<br>5.readonly<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：readonly 变量名<br>功能：将一个用户定义的<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>变量标识为不可变的。不带任何参数的readonly<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>将显示出<br>所有只读的<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>变量。<br>6.read<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：<br>read变量名表<br>功能：从标准输入设备读入一行，分解成若干字，赋值给<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序内部定义的变量。<br>7.shift语句<br>功能：shift语句按如下方式重新命名所有的位置参数变量：$2成为$1，$3成为$2&#8230;&#8230;在程序中<br>每使用一次shift语句，都使所有的位置参数依次向左移动一个位置，并使位置参数&#8220;$#&#8221;减一，<br>直到减到0。<br>8.wait<br>功能：是<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong><a name=baidusnap2></a><strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">等待</strong>在后台启动的所有子<a name=baidusnap3></a><strong style="COLOR: black; BACKGROUND-COLOR: #ff9999">进程结束</strong>。Wait的返回值总是真。<br>9.exit<br>功能：退出<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序。在exit之后可有选择地指定一个数字作为返回状态。<br>10.&#8220;.&#8221;(点)<br><strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong>格式：. <strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">Shell</strong>程序文件名<br>功能：使<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>读入指定的<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">shell</strong>程序文件并依次执行文件中的所有语句。<br><br>13. 特殊参数：<br>1. $*: 代表所有参数，其间隔为IFS内定参数的第一个字元 <br>2. $@: 与*星号类同。不同之处在於不参照IFS<br>3. $#: 代表参数数量<br>4. $?: 执行上一个指令的返回值<br>5. $-: 最近执行的foreground pipeline的选项参数<br>6. $$: 本身的Process ID<br>7. $!: 执行上一个背景指令的PID<br>8. $_: 显示出最后一个执行的<strong style="COLOR: white; BACKGROUND-COLOR: #880000">命令</strong><br>
<img src ="http://www.cppblog.com/garland/aggbug/31154.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2007-08-29 14:41 <a href="http://www.cppblog.com/garland/archive/2007/08/29/31154.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QT技巧</title><link>http://www.cppblog.com/garland/archive/2007/05/10/23804.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Thu, 10 May 2007 08:17:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2007/05/10/23804.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/23804.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2007/05/10/23804.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/23804.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/23804.html</trackback:ping><description><![CDATA[Qt&amp;Kdevelop技巧集(原创)<br>发表：2004-1-21 13:39:23 出处：你的博客网(yourblog.org)<br>1． 如何在Qt程序中加入OpenGL支持。<br>在QT程序中加入OpenGL支持很简单，只需要在Kdevelop连接的库中加入&#8220;-lGL -lGLU&#8221;即可，如果需要glut支持，还可以加入&#8220;-lglut&#8221;。具体操作是在kdevelop集成编译环境中按下&#8221;F7&#8221;，在弹出的对话框中选择 &#8220;Linker&#8221;一项，在输入栏输入你想添加的库即可，写法与gcc/g++一致。<br>一般在类QGLWidget中使用OpenGL,调用此类的头文件是qgl.h,具体写法请参考qt例程中的gear,texture,box等程序(在RedHat7.2中,它们在/usr/lib/qt-2.3.1/doc/examples下).<br><br>2． 检验linux/Unix环境是否支持OpenGL.<br>Qt中的QGLFormat类可以帮助我们轻易检验系统是否支持OpenGL，载入头文件（#include &lt;qgl.h&gt;）后，我们就可以使用QGLFormat的静态函数hasOpenGL来检验，具体写法如下例：<br>if (!QGLFormat::hasOpenGL()) //Test OpenGL Environment<br>{<br>qWarning( "This system has no OpenGL support. Exiting." );//弹出警告对话框<br>return -1;<br>}<br><br>3.获得屏幕的高和宽.<br>一般我们可以通过QT的Qapplication类来获得系统的一些信息,载入头文件(#include &lt;qapplication.h&gt;)我们就可以调用它,下例是使主程序充满整个屏幕的代码:<br>Gui_MainForm gui_mainform;<br>a.setMainWidget( &amp;gui_mainform );<br>gui_mainform.resize( QApplication::desktop()-&gt;width(), QApplication::desktop()-&gt;height() ); gui_mainform.show();<br><br>4.关于信号和槽.<br>信号和槽机制是QT库的重要特性,可以说不了解它就不了解Qt.此机制能在各类间建立方便快捷的通信联系,只要类中加载了Q_OBJECT宏并用 connect函数正确连接在一起即可,具体写法这里就不赘述了.但本人在使用过程中发现使用此机制容易破坏程序的结构性和封装性,速度也不是很让人满意,尤其是在跨多类调用时.鄙人的一孔之见是: 信号和槽机制不可不用,但不可多用.<br><br>5.QT程序中界面的设计.<br>尽管Kdevelop是一个优秀的集成编译环境,可遗憾的是它不是一个可视化的编译环境,好在有Qdesigner来帮助我们完成界面设计,该程序的使用很简单,使用过VB,VC和Delphi的程序员能很快其操作方式,操作完成后存盘会生成一个扩展名为&#8221;ui&#8221;的文件,你接下来的任务就是把它解析成 cpp和h文件,假设文件名为myform.ui,解析方法如下:<br>$uic myform.ui &#8211;I myform.h &#8211;o myform..cpp //这句生成cpp文件<br>$uic myform.ui &#8211;o myform.h //这句生成h文件.<br><br>6.由pro文件生成Makefile.<br>对于Linux/Unix程序员来说编写Makefile文件是一项令人烦恼的任务,而qt程序员就没有这样的烦恼,一句$qmake &#8211;o Makefile myprogram.pro就可以轻松愉快的完成任务,而pro文件的编写也很容易,其核心是h和cpp文件的简单列表.具体写法请参考一下qt自带的样例和教程吧(在RedHat7.2中,它在/usr/lib/qt-2.3.1/doc/examples下),相对Makefile文件简直没有什么难度.<br><br>7.主组件的选择.<br>一般我们在编程是使用继承Qwidget类的类作为主组件,这当然未可厚非.但在制作典型的多文档和单文档程序时我们有更好的选择— QmainWindow类,它可以方便的管理其中的菜单工具条主窗口和状态条等,在窗体几何属性发生变化时也能完美的实现内部组件缩放,这比用传统的几何布局类来管理要方便得多,而且不用写什么代码.关于它的具体细节请查阅QT的帮组文档,这里就不赘述了.<br><br>8.菜单项中加入Checked项.<br>在QT中,菜单项中加入Checked有点麻烦,具体写法如下:<br>1&gt; 定义int型成员变量,并在创建菜单项中写:<br>displayGeometryMode=new QPopupMenu(this); //这里创建弹出菜单组displayGeometryMode<br>m_menuIDWire=displayGeometryMode-&gt;insertItem("Wire",this,SLOT(slt_Change2WireMode()));.//创建弹出菜单子项<br>displayGeometryMode-&gt;setItemChecked(m_ menuIDWire,true);//设定此子项为选择状态<br><br>2&gt; 再在槽函数中写:<br>displayGeometryMode-&gt;setItemChecked(m_menuIDWire,false);//这里设定此子项为非选择状态<br><br>9.截获程序即将退出的信号.<br>有些时候我们需要在程序即将退出时进行一些处理,如保存文件等等.如何截获程序退出的信号呢?还是要用到Qapplication类的aboutToQuit()信号,程序中可以这样写:<br>connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(Slot_SaveActions()));<br>在槽函数Slot_SaveActions()就可以进行相关处理了,注意,使用全局对象qApp需要加载头文件(#include &lt;qapplication.h&gt;).<br><br>10.弹出标准文件对话框.<br>在程序中弹出文件对话框是很容易处理的,举例如下:<br>QString filter="Txt files(*.txt)\n" //设置文件过滤,缺省显示文本文件<br>"All files(*)" ; //可选择显示所有文件<br>QString Filepathname=QFileDialog::getOpenFileName(" ",filter,this);//弹出对话框,这句需要加载头文件(#include &lt; qfiledialog.h &gt;)<br><br><br>11.将当前日期时间转化为标准Qstring.<br>QDateTime currentdatetime =QDateTime::currentDateTime();//需要加载头文件(#include &lt; qdatetime.h &gt;)<br>QString strDateTime=currentdatetime.toString();<br><br>12.设置定时器<br>所有Qobject的子类在设置定时器时都不必加载一个Qtimer对象,因为这样造成了资源浪费且需要书写多余的函数,很不方便.最好的办法是重载timerEvent函数,具体写法如下:<br>class Gui_DlgViewCtrlDatum : public QDialog<br>{<br>Q_OBJECT<br>public:<br>Gui_DlgViewCtrlDatum( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );<br>~Gui_DlgViewCtrlDatum();<br>protected:<br>void timerEvent( QTimerEvent * );<br>};<br>void Gui_DlgViewCtrlDatum::timerEvent( QTimerEvent *e )<br>{<br>//statements<br>}<br>再在Gui_DlgViewCtrlDatum的构造函数中设置时间间隔:<br>startTimer(50);//单位为毫秒<br><br>这样,每隔50毫秒,函数timerEvent便会被调用一次.<br><br>13.最方便的几何布局类QGridLayout<br>在QT的几何布局类中,笔者认为QgridLayout使用最为方便,举例如下:<br>QGridLayout* layout=new QGridLayout(this,10,10);//创建一个10*10的QgridLayout实例<br>layout-&gt;addMultiCellWidget(gui_dlgslab_glwnd,1,8,0,7);//将OpenGL窗口固定在QgridLayout中的(1,0)单元格到(8,7)单元格中<br>layout-&gt;addMultiCellWidget(Slider1,0,9,8,8);//将一个slider固定在单元格(0,8)到(9,8)中<br>layout-&gt;addWidget(UpLimitLbl,1,9);//将一个label(UpLimitLbl)固定在单元格(1,9)中<br>这样,无论窗体大小如何改变,它们的布局方式都不会发生改变,这比反复使用QvboxLayout和QhboxLayout要方便快捷许多.<br>注:使用几何布局类需要调用头文件(#include &lt;qlayout.h&gt;)<br><br>14.字符串类Qstring和字符串链表类QstringList.<br>Qstring是Qt中标准字符串类,下面列出它的一些常用函数:<br>toInt():将字符串转化成int类型.<br>ToFloat():将字符串转化成float类型.<br>ToDouble():将字符串转化成double类型.<br>Left(n):从左起取n个字符<br>Right(n):从右起取n个字符<br>SetNum(n):将实数n(包括int,float,double等)转化为Qsting型.<br><br>QstringList是大家比较少使用的类,它可以看成Qstring组成的链表(QT中标准链表类Qlist的函数对它都适用,它的单个节点是Qstring类型的),特别适合与处理文本,下面一段代码就可见其方便快捷:<br>Qstring strtmp=&#8221;abc|b|c|d&#8221;;<br>QstringList strlsttmp;<br>Strlsttmp =QStringList::split("|", strtmp);<br>For(unsigned int I=0;I&lt; Strlsttmp.count();I++)<br>{<br>cout&lt;&lt; Strlsttmp.at(I);<br>}<br>结果输出为:abc b c d,也就是说,通过一个函数split,一行文本就被符号&#8221;|&#8221;自动分割成了单个字符串.这在文本处理时特别省力.(请参考c语言大全第四版中用&#8221;strtok&#8221;函数分割文本的例程,将双方比较一下)<br><br>15. QGLWidget类如何加入鼠标支持.<br>QGLWidget类加入鼠标支持需要重载以下函数:<br>void mousePressEvent(QMouseEvent*);<br>void mouseMoveEvent(QMouseEvent*);<br>void mouseReleaseEvent(QMouseEvent*);<br>请具体看一个实例:<br>class Gui_WgtMain_GLWnd : public QGLWidget {<br>Q_OBJECT<br>public:<br>Gui_WgtMain_GLWnd(QWidget *parent=0, const char *name=0);<br>~Gui_WgtMain_GLWnd();<br>protected:<br>void initializeGL();<br>void paintGL();<br>void resizeGL( int w, int h );<br>void mousePressEvent(QMouseEvent*);<br>void mouseMoveEvent(QMouseEvent*);<br>void mouseReleaseEvent(QMouseEvent*);<br>private:<br>int m_nCnt;<br>};<br>void Gui_WgtMain_GLWnd::mousePressEvent(QMouseEvent* e)<br>{<br>//statements<br>}<br>void Gui_WgtMain_GLWnd:: mouseMoveEvent (QMouseEvent* e)<br>{<br>//statements<br>}<br>void Gui_WgtMain_GLWnd:: mouseReleaseEvent (QMouseEvent* e)<br>{<br>//statements<br>}<br>其中, e-&gt;x();e-&gt;y();可以获得鼠标的位置, e-&gt;button()可以取得鼠标按键的状态(左中右键以及ctrl,alt,shift等组合键),灵活使用他们就可以在用鼠标操作OpenGL画面了.<br><br>16.由ui文件生成.h和.cpp文件<br>生成.cpp文件<br>$uic myform.ui -i myform.h -o myform.cpp<br><br>生成.h文件<br>$uic myform.ui -o myform.h<br>
<img src ="http://www.cppblog.com/garland/aggbug/23804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2007-05-10 16:17 <a href="http://www.cppblog.com/garland/archive/2007/05/10/23804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux 下安装Mysql</title><link>http://www.cppblog.com/garland/archive/2007/03/30/20923.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Fri, 30 Mar 2007 02:30:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2007/03/30/20923.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/20923.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2007/03/30/20923.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/20923.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/20923.html</trackback:ping><description><![CDATA[引言 <br /><br />　　想使用Linux已经很长时间了，由于没有硬性任务一直也没有系统学习，近日由于工作需要必须使用Linux下的MySQL。本以为有Windows下使用SQL Server的经验，觉得在Linux下安装MySql应该是易如反掌的事，可在真正安装和使用MySQL时走了很多弯路，遇见很多问题，毕竟Linux 和Windows本身就有很大区别。为了让和我一样的初学者在学习的过程中少走弯路，尽快入门，写了此文，希望对您有所帮助。本文的Linux环境是 Red Hat 9.0，MySQL是4.0.16。 <br /><br />　　 二、安装Mysql <br /><br />　　 1、下载MySQL的安装文件 <br />　　 安装MySQL需要下面两个文件： <br />　　 MySQL-server-4.0.16-0.i386.rpm　　　 <br />　　 MySQL-client-4.0.16-0.i386.rpm <br />　　 下载地址为：http://www.mysql.com/downloads/mysql-4.0.html，打开此网页，下拉网页找到“Linux x86 RPM downloads”项，找到“Server”和“Client programs”项，下载需要的上述两个rpm文件。 <br /><br />　　 2、安装MySQL <br />　　 rpm文件是Red Hat公司开发的软件安装包，rpm可让Linux在安装软件包时免除许多复杂的手续。该命令在安装时常用的参数是 –ivh ,其中i表示将安装指定的rmp软件包，V表示安装时的详细信息，h表示在安装期间出现“#”符号来显示目前的安装过程。这个符号将持续到安装完成后才停止。 <br />　　 1）安装服务器端 <br />　　 在有两个rmp文件的目录下运行如下命令： <br />　　 [root@test1 local]# rpm -ivh MySQL-server-4.0.16-0.i386.rpm <br />　　 显示如下信息。 <br />　　　 warning: MySQL-server-4.0.16-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5 <br />　　 Preparing...　　　　　　　########################################### [100%] <br />　　 1:MySQL-server　　　　　########################################### [100%] <br />　　　 。。。。。。（省略显示） <br />　　 /usr/bin/mysqladmin -u root password 'new-password' <br />　　 /usr/bin/mysqladmin -u root -h test1 password 'new-password' <br />　　　 。。。。。。（省略显示） <br />　　 Starting mysqld daemon with databases from /var/lib/mysql <br />　　 如出现如上信息，服务端安装完毕。测试是否成功可运行netstat看Mysql端口是否打开，如打开表示服务已经启动，安装成功。Mysql默认的端口是3306。 <br />　　 [root@test1 local]# netstat -nat <br />　　 Active Internet connections (servers and established) <br />　　 Proto Recv-Q Send-Q Local Address　　　　　 Foreign Address　　　　 State　　　 <br />　　 tcp　　0　　0 0.0.0.0:3306　　　　 0.0.0.0:*　　　　　 LISTEN　　　 <br />　　 上面显示可以看出MySQL服务已经启动。 <br />　　 2）安装客户端 <br />　　 运行如下命令： <br />　　 [root@test1 local]# rpm -ivh MySQL-client-4.0.16-0.i386.rpm <br />　　 warning: MySQL-client-4.0.16-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5 <br />　　 Preparing...　　　　########################################### [100%] <br />　　 1:MySQL-client　 ########################################### [100%] <br />　　 显示安装完毕。 <br />　　 用下面的命令连接mysql,测试是否成功。 <br />　 三、登录MySQL <br /><br />　　 登录MySQL的命令是mysql， mysql 的使用语法如下： <br />　　 mysql [-u username] [-h host] [-p[password]] [dbname] <br />　　 username 与 password 分别是 MySQL 的用户名与密码，mysql的初始管理帐号是root，没有密码，注意：这个root用户不是Linux的系统用户。MySQL默认用户是root，由于初始没有密码，第一次进时只需键入mysql即可。 <br />　　 [root@test1 local]# mysql <br />　　 Welcome to the MySQL monitor.　Commands end with ; or \g. <br />　　 Your MySQL connection id is 1 to server version: 4.0.16-standard <br />　　 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. <br />　　 mysql&gt; <br />　　 出现了“mysql&gt;”提示符，恭喜你，安装成功！ <br />　　 增加了密码后的登录格式如下： <br />　　 mysql -u root -p <br />　　 Enter password: (输入密码) <br />　　 其中-u后跟的是用户名，-p要求输入密码，回车后在输入密码处输入密码。 <br /><br />　　 注意：这个mysql文件在/usr/bin目录下，与后面讲的启动文件/etc/init.d/mysql不是一个文件。 <br /><br />　　 四、MySQL的几个重要目录 <br /><br />　　 MySQL安装完成后不象SQL Server默认安装在一个目录，它的数据库文件、配置文件和命令文件分别在不同的目录，了解这些目录非常重要，尤其对于Linux的初学者，因为 Linux本身的目录结构就比较复杂，如果搞不清楚MySQL的安装目录那就无从谈起深入学习。 <br /><br />　　 下面就介绍一下这几个目录。 <br /><br />　　 1、数据库目录 <br />　　 /var/lib/mysql/ <br /><br />　　 2、配置文件 <br />　　 /usr/share/mysql（mysql.server命令及配置文件） <br /><br />　　 3、相关命令 <br />　　 /usr/bin(mysqladmin mysqldump等命令) <br /><br />　　 4、启动脚本 <br />　　 /etc/rc.d/init.d/（启动脚本文件mysql的目录） <br />　 五、修改登录密码 <br /><br />　　 MySQL默认没有密码，安装完毕增加密码的重要性是不言而喻的。 <br /><br />　　 1、命令 <br />　　 usr/bin/mysqladmin -u root password 'new-password' <br />　　 格式：mysqladmin -u用户名 -p旧密码 password 新密码 <br /><br />　　 2、例子 <br />　　 例1：给root加个密码123456。 <br />　　 键入以下命令 ： <br />　　 [root@test1 local]# /usr/bin/mysqladmin -u root password 123456 <br />　　 注：因为开始时root没有密码，所以-p旧密码一项就可以省略了。 <br /><br />　　 3、测试是否修改成功 <br />　　 1）不用密码登录 <br />　　 [root@test1 local]# mysql <br />　　 ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO) <br />　　 显示错误，说明密码已经修改。 <br />　　 2）用修改后的密码登录 <br />　　 [root@test1 local]# mysql -u root -p <br />　　 Enter password: (输入修改后的密码123456) <br />　　 Welcome to the MySQL monitor.　Commands end with ; or \g. <br />　　 Your MySQL connection id is 4 to server version: 4.0.16-standard <br />　　 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. <br />　　 mysql&gt; <br />　　 成功！ <br />　　 这是通过mysqladmin命令修改口令，也可通过修改库来更改口令。 <br /><br />　　 六、启动与停止 <br /><br />　　 1、启动 <br />　　 MySQL安装完成后启动文件mysql在/etc/init.d目录下，在需要启动时运行下面命令即可。 <br />　　 [root@test1 init.d]# /etc/init.d/mysql start <br /><br />　　 2、停止 <br />　　 /usr/bin/mysqladmin -u root -p shutdown <br /><br />　　 3、自动启动 <br />　　 1）察看mysql是否在自动启动列表中 <br />　　 [root@test1 local]#　/sbin/chkconfig –list <br />　　 2）把MySQL添加到你系统的启动服务组里面去 <br />　　 [root@test1 local]#　/sbin/chkconfig　– add　mysql <br />　　 3）把MySQL从启动服务组里面删除。 <br />　　 [root@test1 local]#　/sbin/chkconfig　– del　mysql <br />七、更改MySQL目录 <br /><br />　　 MySQL默认的数据文件存储目录为/var/lib/mysql。假如要把目录移到/home/data下需要进行下面几步： <br /><br />　　 1、home目录下建立data目录 <br />　　 cd /home <br />　　 mkdir data <br /><br />　　 2、把MySQL服务进程停掉： <br />　　 mysqladmin -u root -p shutdown <br /><br />　　 3、把/var/lib/mysql整个目录移到/home/data <br />　　 mv /var/lib/mysql　/home/data/ <br />　　 这样就把MySQL的数据文件移动到了/home/data/mysql下 <br /><br />　　 4、找到my.cnf配置文件 <br />　　 如果/etc/目录下没有my.cnf配置文件，请到/usr/share/mysql/下找到*.cnf文件，拷贝其中一个到/etc/并改名为my.cnf)中。命令如下： <br />　　 [root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf　/etc/my.cnf <br /><br />　　 5、编辑MySQL的配置文件/etc/my.cnf <br />　　 为保证MySQL能够正常工作，需要指明mysql.sock文件的产生位置。 修改socket=/var/lib/mysql/mysql.sock一行中等号右边的值为：/home/mysql/mysql.sock 。操作如下： <br />　　 vi　 my.cnf　　　 (用vi工具编辑my.cnf文件，找到下列数据修改之) <br />　　 # The MySQL server <br />　　　 [mysqld] <br />　　　 port　　　= 3306 <br />　　　 #socket　 = /var/lib/mysql/mysql.sock（原内容，为了更稳妥用“#”注释此行） <br />　　　 socket　 = /home/data/mysql/mysql.sock　　　（加上此行） <br /><br />　　 6、修改MySQL启动脚本/etc/rc.d/init.d/mysql <br />　　 最后，需要修改MySQL启动脚本/etc/rc.d/init.d/mysql，把其中datadir=/var/lib/mysql一行中，等号右边的路径改成你现在的实际存放路径：home/data/mysql。 <br />　　 [root@test1 etc]# vi　/etc/rc.d/init.d/mysql <br />　　 #datadir=/var/lib/mysql　　　　（注释此行） <br />　　 datadir=/home/data/mysql　　 （加上此行） <br /><br />　　 7、重新启动MySQL服务 <br />　　 /etc/rc.d/init.d/mysql　start <br />　　 或用reboot命令重启Linux <br />　　 如果工作正常移动就成功了，否则对照前面的7步再检查一下。 <br /><br />　　 八、MySQL的常用操作 <br /><br />　　 注意：MySQL中每个命令后都要以分号；结尾。 <br /><br />　　 1、显示数据库 <br />　　 mysql&gt; show databases; <br />　　 +----------+ <br />　　 | Database | <br />　　 +----------+ <br />　　 | mysql　　| <br />　　 | test　　 | <br />　　 +----------+ <br />　　 2 rows in set (0.04 sec) <br />　　 Mysql刚安装完有两个数据库：mysql和test。mysql库非常重要，它里面有MySQL的系统信息，我们改密码和新增用户，实际上就是用这个库中的相关表进行操作。 <br /><br />　　 2、显示数据库中的表 <br />　　 mysql&gt; use mysql; （打开库，对每个库进行操作就要打开此库，类似于foxpro ） <br />　　 Database changed <br /><br />　　 mysql&gt; show tables; <br />　　 +-----------------+ <br />　　 | Tables_in_mysql | <br />　　 +-----------------+ <br />　　 | columns_priv　　| <br />　　 | db　　　　　　　| <br />　　 | func　　　　　　| <br />　　 | host　　　　　　| <br />　　 | tables_priv　　 | <br />　　 | user　　　　　　| <br />　　 +-----------------+ <br />　　 6 rows in set (0.01 sec) <br /><br />　　 3、显示数据表的结构： <br />　　 describe 表名; <br /><br />　　 4、显示表中的记录： <br />　　 select * from 表名; <br />　　 例如：显示mysql库中user表中的纪录。所有能对MySQL用户操作的用户都在此表中。 <br />　　 Select * from user; <br /><br />　　 5、建库： <br />　　 create database 库名; <br />　　 例如：创建一个名字位aaa的库 <br />　　 mysql&gt; create databases aaa; <br />6、建表： <br />　　 use 库名； <br />　　 create table 表名 (字段设定列表)； <br />　　 例如：在刚创建的aaa库中建立表name,表中有id(序号，自动增长)，xm（姓名）,xb（性别）,csny（出身年月）四个字段 <br />　　 use aaa; <br />　　 mysql&gt; create table name (id int(3) auto_increment not null primary key, xm char(8),xb char(2),csny date); <br />　　 可以用describe命令察看刚建立的表结构。 <br />　　 mysql&gt; describe name; <br /><br />　　 +-------+---------+------+-----+---------+----------------+ <br />　　 | Field | Type　　| Null | Key | Default | Extra　　　　　| <br />　　 +-------+---------+------+-----+---------+----------------+ <br />　　 | id　　| int(3)　|　　　| PRI | NULL　　| auto_increment | <br />　　 | xm　　| char(8) | YES　|　　 | NULL　　|　　　　　　　　| <br />　　 | xb　　| char(2) | YES　|　　 | NULL　　|　　　　　　　　| <br />　　 | csny　| date　　| YES　|　　 | NULL　　|　　　　　　　　| <br />　　 +-------+---------+------+-----+---------+----------------+ <br /><br />　　 7、增加记录 <br />　　 例如：增加几条相关纪录。 <br />　　 mysql&gt; insert into name values('','张三','男','1971-10-01'); <br />　　 mysql&gt; insert into name values('','白云','女','1972-05-20'); <br />　　 可用select命令来验证结果。 <br />　　 mysql&gt; select * from name; <br />　　 +----+------+------+------------+ <br />　　 | id | xm　 | xb　 | csny　　　 | <br />　　 +----+------+------+------------+ <br />　　 |　1 | 张三 | 男　 | 1971-10-01 | <br />　　 |　2 | 白云 | 女　 | 1972-05-20 | <br />　　 +----+------+------+------------+ <br /><br />　　 8、修改纪录 <br />　　 例如：将张三的出生年月改为1971-01-10 <br />　　 mysql&gt; update name set csny='1971-01-10' where xm='张三'; <br /><br />　　 9、删除纪录 <br />　　 例如：删除张三的纪录。 <br />　　 mysql&gt; delete from name where xm='张三'; <br /><br />　　 10、删库和删表 <br />　　 drop database 库名; <br />　　 drop table 表名； <br /><br />　　 九、增加MySQL用户 <br /><br />　　 格式：grant select on 数据库.* to 用户名@登录主机 identified by "密码" <br />例1、增加一个用户user_1密码为123，让他可以在任何主机上登录，并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL，然后键入以下命令： <br /><br />　　 mysql&gt; grant select,insert,update,delete on *.* to user_1@"%" Identified by "123"; <br />例1增加的用户是十分危险的，如果知道了user_1的密码，那么他就可以在网上的任何一台电脑上登录你的MySQL数据库并对你的数据为所欲为了，解决办法见例2。 <br /><br />　　例2、增加一个用户user_2密码为123,让此用户只可以在localhost上登录，并可以对数据库aaa进行查询、插入、修改、删除的操作（localhost指本地主机，即MySQL数据库所在的那台主机），这样用户即使用知道user_2的密码，他也无法从网上直接访问数据库，只能通过 MYSQL主机来操作aaa库。 <br /><br />　　 mysql&gt;grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123"; <br /><br />　　 用新增的用户如果登录不了MySQL，在登录时用如下命令： <br /><br />　　 mysql -u user_1 -p　-h 192.168.113.50　（-h后跟的是要登录主机的ip地址） <br /><br />　　 十、备份与恢复 <br /><br />　　 1、备份 <br /><br />　　 例如：将上例创建的aaa库备份到文件back_aaa中 <br /><br />　　 [root@test1 root]# cd　/home/data/mysql　(进入到库目录，本例库已由val/lib/mysql转到/home/data/mysql，见上述第七部分内容) <br />　　 [root@test1 mysql]# mysqldump -u root -p --opt aaa &gt; back_aaa <br /><br />　　 2、恢复 <br /><br />　　 [root@test mysql]# mysql -u root -p ccc &lt; back_aaa <br /><br /><p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=229637</p><img src ="http://www.cppblog.com/garland/aggbug/20923.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2007-03-30 10:30 <a href="http://www.cppblog.com/garland/archive/2007/03/30/20923.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Shell命令</title><link>http://www.cppblog.com/garland/archive/2006/10/27/14275.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Fri, 27 Oct 2006 04:02:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2006/10/27/14275.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/14275.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2006/10/27/14275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/14275.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/14275.html</trackback:ping><description><![CDATA[
		<p>ls -l | grep ^d<br />ls -l | grep '^[^d]'<br />ls -l | grep '^d.....x..x'<br /><br />grep "garland" /etc/password<br />grep -s "garland" /etc/password<br /><br />ps ax | grep "named"<br /><br />#!/bin/sh<br />echo "first name:\c"<br />read name<br />echo "second name:\c"<br />read sexname<br />echo "third name:\c"<br />read manname</p>
		<p>cp justis.txt garland.txt &amp;&amp; echo "if you are seeing this cp OK"<br />cp wopper.txt garland.txt || echo "if you are seeing this cp failed"<br />echo &amp;name</p>
		<p>echo please input buddy count:<br />count =<br />read count</p>
		<p>if count == 5<br />{<br />        echo galrnad 5<br />}<br /></p>
<img src ="http://www.cppblog.com/garland/aggbug/14275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2006-10-27 12:02 <a href="http://www.cppblog.com/garland/archive/2006/10/27/14275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell编程</title><link>http://www.cppblog.com/garland/archive/2006/10/27/14274.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Fri, 27 Oct 2006 03:25:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2006/10/27/14274.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/14274.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2006/10/27/14274.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/14274.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/14274.html</trackback:ping><description><![CDATA[
		<p>#!/bin/sh<br />echo "first name:\c"<br />read name<br />echo "second name:\c"<br />read sexname<br />echo "third name:\c"<br />read manname</p>
		<p>cp justis.txt garland.txt &amp;&amp; echo "if you are seeing this cp OK"<br />cp wopper.txt garland.txt || echo "if you are seeing this cp failed"<br />echo &amp;name</p>
<img src ="http://www.cppblog.com/garland/aggbug/14274.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2006-10-27 11:25 <a href="http://www.cppblog.com/garland/archive/2006/10/27/14274.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我的第一个Linux下C的程序</title><link>http://www.cppblog.com/garland/archive/2006/06/19/8714.html</link><dc:creator>郭大伟</dc:creator><author>郭大伟</author><pubDate>Mon, 19 Jun 2006 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/garland/archive/2006/06/19/8714.html</guid><wfw:comment>http://www.cppblog.com/garland/comments/8714.html</wfw:comment><comments>http://www.cppblog.com/garland/archive/2006/06/19/8714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/garland/comments/commentRss/8714.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/garland/services/trackbacks/8714.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->						  1												#include  						&lt;						stdio.h						&gt;																				...&nbsp;&nbsp;<a href='http://www.cppblog.com/garland/archive/2006/06/19/8714.html'>阅读全文</a><img src ="http://www.cppblog.com/garland/aggbug/8714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/garland/" target="_blank">郭大伟</a> 2006-06-19 17:37 <a href="http://www.cppblog.com/garland/archive/2006/06/19/8714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>