<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>陈志伟博客 &#187; shell</title>
	<atom:link href="http://chenzhiwei.net/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://chenzhiwei.net</link>
	<description>善始者实繁，克终者盖寡。</description>
	<lastBuildDate>Tue, 03 Jan 2012 09:14:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Shell I/O重定向</title>
		<link>http://chenzhiwei.net/2011/08/shell-io-redirection/</link>
		<comments>http://chenzhiwei.net/2011/08/shell-io-redirection/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 11:23:39 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30432</guid>
		<description><![CDATA[Unuix/Linux系统有三个文件是默认被打开着的，它们的分别是stdin(标准输入文件，键盘)，stdout（标准输出文件，屏幕）和stderr（标准错误文件，屏幕）。这些文件和其他打开的文件都可以被重定向。I/O重定向简单的说就是用一个脚本来捕获一个文件、命令、程序、脚本甚至是代码块的输出，然后将该输出写入到另一个文件、命令、程序或脚本。 每个打开的文件都被赋予一个文件描述符。0,1,2分别是标准输入、标准输出、标准错误的文件描述符。 1. 标准输出 &#62; (COMMAND_OUTPUT &#62;) 重定向标准输出到一个文件，如果这个文件不存在则创建，存在则覆盖。 ls -lR &#62; dir-tree.list # 将当前目录的目录树写入文件dir-tree.list : &#62; filename # 删除filename里的所有内容（即将filename置空）。如果文件不存在则创建一个空文件（与touch filename命令效果相同）。 &#62; filename # 与上条命令意思相同，但是该命令在某些shell中不可用。 2. 标准输出 &#62;&#62; (COMMAND_OUTPUT &#62;&#62;) 重定标准输出到一个文件，如果该文件不存在则创建，存在则追加到该文件末尾。 只对单行重定向命令起作用（即只对命令所在的行起作用） 1 &#62;&#62; filename # 重定向并追加标准输出到filename 2 &#62;&#62; filename # 重定向前追加标准错误输出到filename &#38; &#62; filename # 重定向标准输出和标准错误输出到filename，该命令可在Bash 4下使用。 M &#62; N # “M” 是一个文件描述符，默认为1；”N”是一个文件名。意思是将M的内容重定向到N M&#62;&#38;N # [...]]]></description>
			<content:encoded><![CDATA[<p>Unuix/Linux系统有三个文件是默认被打开着的，它们的分别是stdin(标准输入文件，键盘)，stdout（标准输出文件，屏幕）和stderr（标准错误文件，屏幕）。这些文件和其他打开的文件都可以被重定向。I/O重定向简单的说就是用一个脚本来捕获一个文件、命令、程序、脚本甚至是代码块的输出，然后将该输出写入到另一个文件、命令、程序或脚本。<span id="more-30432"></span></p>
<p>每个打开的文件都被赋予一个文件描述符。0,1,2分别是标准输入、标准输出、标准错误的文件描述符。</p>
<p><strong>1. 标准输出 &gt; </strong>(COMMAND_OUTPUT &gt;)<br />
重定向标准输出到一个文件，如果这个文件不存在则创建，存在则覆盖。</p>
<p>ls -lR &gt; dir-tree.list<br />
# 将当前目录的目录树写入文件dir-tree.list</p>
<p>: &gt; filename<br />
# 删除filename里的所有内容（即将filename置空）。如果文件不存在则创建一个空文件（与touch filename命令效果相同）。</p>
<p>&gt; filename<br />
# 与上条命令意思相同，但是该命令在某些shell中不可用。</p>
<p><strong>2. 标准输出 &gt;&gt; (COMMAND_OUTPUT &gt;&gt;)</strong><br />
重定标准输出到一个文件，如果该文件不存在则创建，存在则追加到该文件末尾。</p>
<p>只对单行重定向命令起作用（即只对命令所在的行起作用）<br />
1 &gt;&gt; filename<br />
# 重定向并追加标准输出到filename</p>
<p>2 &gt;&gt; filename<br />
# 重定向前追加标准错误输出到filename</p>
<p>&amp; &gt; filename<br />
# 重定向标准输出和标准错误输出到filename，该命令可在Bash 4下使用。</p>
<p>M &gt; N<br />
# “M” 是一个文件描述符，默认为1；”N”是一个文件名。意思是将M的内容重定向到N</p>
<p>M&gt;&amp;N<br />
# “M”是一个文件描述符，默认为1；”N”是另一个文件描述符。意思是将文件描述符M重定向到N，即所有M指向的文件输出都会发给N所指向的文件。</p>
<p>2&gt;&amp;1<br />
# 将标准错误输出重定向到标准输出，一般用法如下：<br />
&gt;&gt; filename 2&gt;&amp;1<br />
# 意思是将标准错误输出重定向到标准输出，再将标准输出重定向到文件filename</p>
<p>&gt;&amp;j<br />
# 等价于 1&gt;&amp;j ，即将标准输出定向到j所指向的文件</p>
<p>0&lt;filename 或 &lt;filename<br />
# 以文件filename内容为标准输入。<br />
# grep search-word &lt; filename</p>
<p>[j]&lt;&gt;filename<br />
# 以读写方式打开filename，并且将j作为文件描述符赋值给filename，如果j没有被指定，默认为0，即标准输入。</p>
<p>下面的例子会让你更清楚理解其含义<br />
echo “1234567890&#8243; &gt; file	# 将字符串写入file<br />
exec 3&lt;&gt;file				# 打开file并且将其文件描述符设置成3<br />
read -n 4 &lt;&amp;3				# 仅读取4个字符<br />
echo -n . &gt;&amp;3				# 写入一个英文句号<br />
exec 3&gt;&amp;-				# 关于文件描述符3<br />
cat file					# 结果会是：1234.567890</p>
<p>|<br />
# 管道，用来定向命令的，与”&gt;”相似，但是更实用。不细说了。</p>
<p>ls -yz &gt;&gt; command.log 2&gt;&amp;1<br />
# 捕获非法选项yz的标准错误输出及标准输出，并写入command.log文件。</p>
<p>ls -yz 2&gt;&amp;1 &gt;&gt; command.log<br />
# 输出错误信息，但是不写入文件command.log。这一行命令的意思是将标准输出写入command.log，而标准错误输出则定向到标准输出。虽然这条命令和并一条命令一样，都是将标准错误输出定向到标准输出，但是顺序不一样，所以意义也不一样。</p>
<p><strong>3. 关闭文件描述符</strong><br />
n&lt;&amp;-<br />
# 关闭输入文件描述符n</p>
<p>0&lt;&amp;-,&lt;&amp;-<br />
# 关闭标准输入。</p>
<p>n&gt;&amp;-<br />
# 关闭标准输出文件描述符n.</p>
<p>1&gt;&amp;-,&gt;&amp;-<br />
# 关闭标准标准输出</p>
<p>子进程继承打开的文件描述符，这就是pipe工作的原理，要阻止一个文件描述符被继承，那么请关闭它。</p>
<p># 仅重定向标准错误输出到管道</p>
<p>exec 3&gt;&amp;1 						# 保存当前标准输出的值<br />
ls -l 2&gt;&amp;1 &gt;&amp;3 3&gt;&amp;- | grep bad 3&gt;&amp;-	# 为grep关闭文件描述符3（但并不是&#8217;ls&#8217;命令）<br />
exec 3&gt;&amp;- 						# 关闭文件描述符3</p>
<p>注：这是ABS Guide第20章的内容，我在上周六翻译出来的。<br />
英文原文地址：<a href="http://tldp.org/LDP/abs/html/io-redirection.html">http://tldp.org/LDP/abs/html/io-redirection.html</a></p>
<p style="font-weight: bold;">&copy; 2011, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="Shell I/O重定向" href="http://chenzhiwei.net/2011/08/shell-io-redirection/">http://chenzhiwei.net/2011/08/shell-io-redirection/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li><li><a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/" title="linux shell 学习笔记(四)">linux shell 学习笔记(四)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2011/08/shell-io-redirection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>几个简单的shell脚本</title>
		<link>http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/</link>
		<comments>http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 07:10:19 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30389</guid>
		<description><![CDATA[最近一直没有更新博客，这次更新就写几个shell脚本，很简单的那种，大牛们可以略过了。这里的几个脚本分别是用for、while、until循环写的从1加到100，判断当前目录下的文件是否为普通文件，如果是则移动到家目录，还有一个是在网上看到的面试题目。 从1加到100的三种实现方法： for循环 1 2 3 4 5 6 7 #!/bin/bash sum=0 for count in `seq 1 100` do sum=$((sum+count)) done echo &#34;1+2+...+100=$sum&#34; while循环 1 2 3 4 5 6 7 8 9 #!/bin/bash count=1 sum=0 while test $count -le 100 do sum=$((sum+count)) count=$((count+1)) done echo &#34;1+2+...+100=$sum&#34; until循环 1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p>最近一直没有更新博客，这次更新就写几个shell脚本，很简单的那种，大牛们可以略过了。这里的几个脚本分别是用for、while、until循环写的从1加到100，判断当前目录下的文件是否为普通文件，如果是则移动到家目录，还有一个是在网上看到的面试题目。<span id="more-30389"></span></p>
<p><strong>从1加到100的三种实现方法：</strong></p>
<p>for循环</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
sum=0
for count in `seq 1 100`
do
    sum=$((sum+count))
done
echo &quot;1+2+...+100=$sum&quot;</pre></td></tr></table></div>

<p>while循环</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
count=1
sum=0
while test $count -le 100
do
    sum=$((sum+count))
    count=$((count+1))
done
echo &quot;1+2+...+100=$sum&quot;</pre></td></tr></table></div>

<p>until循环</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
count=1
sum=0
until test $count -gt 100
do
    sum=$((sum+count))
    count=$((count+1))
done
echo &quot;1+2+...+100=$sum&quot;</pre></td></tr></table></div>

<p><strong>判断文件是否为普通文件并移动文件：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
read -p &quot;filename:&quot; filename
if [ -f &quot;$filename&quot; ]
then
    cp $filename ~/test
fi</pre></td></tr></table></div>

<p><strong>有两个文本文件中存放着N多QQ号，每行一个。用shell脚本找出两个文件中相同的QQ号：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
for stra in `cat a.txt`
do
    for strb in `cat b.txt`
    do
        if [ &quot;$stra&quot; -eq &quot;$strb&quot; ]
        then
            echo $stra &gt;&gt; c.txt
            echo &quot;$stra&quot;
        fi
    done
done</pre></td></tr></table></div>

<p>听说这是腾讯的面试题目。<br />
注：这些都是超级简单的脚本，入门专用的，一看就明白。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="几个简单的shell脚本" href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/">http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li><li><a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/" title="linux shell 学习笔记(四)">linux shell 学习笔记(四)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>shell 脚本学习之判断条件</title>
		<link>http://chenzhiwei.net/2010/11/shell-script-study-notes-2/</link>
		<comments>http://chenzhiwei.net/2010/11/shell-script-study-notes-2/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 02:54:09 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[脚本]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30391</guid>
		<description><![CDATA[在之前的shell语言学习笔记中已经写过shell的几种判断语句及循环语句，也简单的介绍了shell语言判断语句和判断条件。在此再做进一步学习。 test命令的测试功能 test命令用于检测系统文件及其相关属性，如检查某文件是否存在，检查某文件的权限等。判断当前目录下是否存在某文件study可以用以下命令： 1 #test -e study -e 选项是测试某文件是否存在（包括文件和文件夹），该执行结果不会输出任何信息，但是我们可以通过&#38;&#38;及&#124;&#124;来改写使其展现相关信息，改写后的命令如下： 1 #test -e study &#38;&#38; echo &#34;exist!&#34; &#124;&#124; echo &#34;not exist!&#34; 该命令的作用就是当study文件存在时就输出字符串”exist!”，当不存在时就输出字符串”not exist!” 以下是test命令常用的测试标志： 1. 某文件名的类型检测（存在与否及文件类型）（test -e filename） -e :该“文件名”是否存在。 -d :该文件名是否为目录。 -f  :该文件名是否为普通文件。 b,c,S,p,L分别指的是块设备、字符设备、套接字文件、管道文件及链接文件。 2. 文件权限的检测（test -r filename） -r :该文件是否具有可读属性 -w :该文件是否具有可写属性 -x :该文件是否具有可执行属性 -s  :该文件是否为非空白文件 3. 比较两个文件（test file_a nt file_b） -nt :文件file_a是否比file_b新 -ot :文件file_a是否比file_b旧 [...]]]></description>
			<content:encoded><![CDATA[<p>在之前的shell语言学习笔记中已经写过shell的几种判断语句及循环语句，也简单的介绍了shell语言判断语句和判断条件。在此再做进一步学习。<br />
<span id="more-30391"></span><br />
<strong>test命令的测试功能</strong></p>
<p>test命令用于检测系统文件及其相关属性，如检查某文件是否存在，检查某文件的权限等。判断当前目录下是否存在某文件study可以用以下命令：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#test -e study</pre></td></tr></table></div>

<p>-e 选项是测试某文件是否存在（包括文件和文件夹），该执行结果不会输出任何信息，但是我们可以通过&amp;&amp;及||来改写使其展现相关信息，改写后的命令如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#test -e study &amp;&amp; echo &quot;exist!&quot; || echo &quot;not exist!&quot;</pre></td></tr></table></div>

<p>该命令的作用就是当study文件存在时就输出字符串”exist!”，当不存在时就输出字符串”not exist!”</p>
<p>以下是test命令常用的测试标志：</p>
<p>1. 某文件名的类型检测（存在与否及文件类型）（test -e filename）</p>
<p>-e :该“文件名”是否存在。<br />
-d :该文件名是否为目录。<br />
-f  :该文件名是否为普通文件。<br />
b,c,S,p,L分别指的是块设备、字符设备、套接字文件、管道文件及链接文件。</p>
<p>2. 文件权限的检测（test -r filename）</p>
<p>-r :该文件是否具有可读属性<br />
-w :该文件是否具有可写属性<br />
-x :该文件是否具有可执行属性<br />
-s  :该文件是否为非空白文件</p>
<p>3. 比较两个文件（test file_a nt file_b）</p>
<p>-nt :文件file_a是否比file_b新<br />
-ot :文件file_a是否比file_b旧<br />
-ef :判断两个文件是否为同一文件，可用于判断硬连接。（主要判断两个文件是否均指向同一个inode）</p>
<p>4. 两个整数之间的判断（test n1 -eq n2）</p>
<p>-eq :两个数相等（equal）<br />
-ne :两个数不相等（not equal）<br />
-gt :前者大于后者（greater than）<br />
-lt :前者小于后者（less than）<br />
-ge :前者大于等后者<br />
-le :前者小于等于后者</p>
<p>5. 判断字符串</p>
<p>test -z str :判断字符串是否为空，若为空则回传true<br />
test -n str :判断字符串是否为非空，左路为非空则回传true（-n亦可省略）<br />
test str_a = str_b及test str_a != str_b:判断两字条串是否相等及不相等。</p>
<p>6. 多重判断条件（test -r file -a -w file）</p>
<p>-a :and，当两个条件都满足时才回传true，即file具有读和写权限<br />
-o : or，当两个条件满足其一时即回传true<br />
-! :条件求反，test -! -x file，即当file不具有执行权限时才回传true</p>
<p>下面是两个简单的shell脚本，判断其功能：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo &quot;Please input a filename:&quot;
read -e  filename
(test -e $filename &amp;&amp; (test -d $filename &amp;&amp; echo &quot;directory&quot; ||( test -f $filename &amp;&amp; echo &quot;regular&quot;)) )||echo &quot;$filename does not exist&quot;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo -e &quot;The program will show you that filename is exist which input by you.&quot;
read -p &quot;Input a filename:&quot; filename
test -z $filename &amp;&amp; echo &quot;You must input a filename.&quot; &amp;&amp; exit 0
test ! -e $filename &amp;&amp; echo &quot;The filename $filename DO NOT exist&quot; &amp;&amp; exit 0
test -f $filename &amp;&amp; filetype=&quot;Regulare file&quot;
test -d $filename &amp;&amp; filetype=&quot;directory&quot;;
test -r $filename &amp;&amp; perm=&quot;readable&quot;
test -w $filename &amp;&amp; perm=&quot;$perm writable&quot;
test -x $filename &amp;&amp; perm=&quot;$perm executable&quot;
echo &quot;The filename:$filename is a $filetype&quot;
echo &quot;And the permission are:$perm&quot;</pre></td></tr></table></div>

<p><strong>使用判断符号[ ]</strong></p>
<p>可以使用判断符号进行数据的判断，如检查某变量是否为空 [ -z $SHELL ]，需要注意的是中括号（“[]”）内的组件必须以空格隔开。有以下脚本：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
read -p &quot;input you choice:&quot; choice
[ &quot;$choice&quot; == &quot;y&quot; ] || [ &quot;$choice&quot; == &quot;Y&quot; ] &amp;&amp; echo &quot;OK,continue&quot; &amp;&amp; exit 0
[ &quot;$choice&quot; == &quot;n&quot; ] || [ &quot;$choice&quot; == &quot;N&quot; ] &amp;&amp; echo &quot;Oh,interrupt!&quot; &amp;&amp; exit 0
echo &quot;I don't know what is your choice&quot; &amp;&amp; exit 0</pre></td></tr></table></div>

<p>注：这些内容是参考的《鸟哥的Linux私房菜》一书中的部分章节。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="shell 脚本学习之判断条件" href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/">http://chenzhiwei.net/2010/11/shell-script-study-notes-2/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li><li><a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/" title="linux shell 学习笔记(四)">linux shell 学习笔记(四)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/11/shell-script-study-notes-2/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>shell脚本学习</title>
		<link>http://chenzhiwei.net/2010/11/shell-script-study-notes/</link>
		<comments>http://chenzhiwei.net/2010/11/shell-script-study-notes/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 02:43:01 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[脚本]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30390</guid>
		<description><![CDATA[之前已经写过shell做为一门编程语言的语法和shell做为一个可执行程序的用法。接下来就写一下shell脚本的相关内容，shell脚本可以进行机器的自动化管理、追踪和管理系统重要工作、简单的入侵检测、命令执行一体化及简单的数据处理等，帮助系统管理员快速的管理机器。 什么是shell脚本 shell脚本可以说成是使用shell功能所编写的“程序”，这个程序与其他程序的不同之处就是该程序是纯文本文件，将一些shell的语法和命令写在里面，与正则表达式、管道命令及数据流重导向一起实现我们目的。shell脚本可以看成是简单的批处理文件，我们将很多命令写在一起，让用户通过shell脚本一次实现多个命令，因此shell脚本不需要进行编译就能运行。 下面是一个简单的shell脚本 1 2 3 #!/bin/bash # this script will print &#34;hello,world!&#34; on your screen echo &#34;hello,world!&#34; 程序第一行就是指定你所使用的shell。 第二行是shell脚本的注释语句，可以看出shell是用#做为单行注释开头的。 第三行就是shell脚本的执行语句了，很简单，就是向屏幕输出一个字符串。 含有变量的shell脚本一 1 2 3 4 #!/bin/bash read -p &#34;Please input your name:&#34; name read -p &#34;Please input your age:&#34; age echo &#34;Your name is $name,and your age is $age&#34; 该shell脚本的功能就是让用户输入自己的名字和年龄，然后输出至屏幕。这里read后面的-p参数意思是后跟提示语句。 含有变量的shell脚本二 1 2 [...]]]></description>
			<content:encoded><![CDATA[<p>之前已经写过shell做为一门编程语言的语法和shell做为一个可执行程序的用法。接下来就写一下shell脚本的相关内容，shell脚本可以进行机器的自动化管理、追踪和管理系统重要工作、简单的入侵检测、命令执行一体化及简单的数据处理等，帮助系统管理员快速的管理机器。</p>
<p><strong>什么是shell脚本</strong></p>
<p>shell脚本可以说成是使用shell功能所编写的“程序”，这个程序与其他程序的不同之处就是该程序是纯文本文件，将一些shell的语法和命令写在里面，与正则表达式、管道命令及数据流重导向一起实现我们目的。shell脚本可以看成是简单的批处理文件，我们将很多命令写在一起，让用户通过shell脚本一次实现多个命令，因此shell脚本不需要进行编译就能运行。<span id="more-30390"></span></p>
<p>下面是一个简单的shell脚本</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
# this script will print &quot;hello,world!&quot; on your screen
echo &quot;hello,world!&quot;</pre></td></tr></table></div>

<p>程序第一行就是指定你所使用的shell。<br />
第二行是shell脚本的注释语句，可以看出shell是用#做为单行注释开头的。<br />
第三行就是shell脚本的执行语句了，很简单，就是向屏幕输出一个字符串。</p>
<p><strong>含有变量的shell脚本一</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
read -p &quot;Please input your name:&quot; name
read -p &quot;Please input your age:&quot; age
echo &quot;Your name is $name,and your age is $age&quot;</pre></td></tr></table></div>

<p>该shell脚本的功能就是让用户输入自己的名字和年龄，然后输出至屏幕。这里read后面的-p参数意思是后跟提示语句。</p>
<p><strong>含有变量的shell脚本二</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo &quot;shell script is $0&quot;
echo &quot;Your name is $1,and your age is $2&quot;</pre></td></tr></table></div>

<p>该脚本的的输入格式是“脚本名 参数1 参数2”，功能也是让用户输入自己的名字和年龄，然后输出，只不过这里参数是在执行脚本之前输入的。</p>
<p>shell脚本中的简单数值运算</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
read -p &quot;first num:&quot; first
read -p &quot;second num:&quot; second
echo &quot;$first+$second=$(($first+$second))&quot;</pre></td></tr></table></div>

<p>该脚本功能是提示用户输入两个数值，然后得出他们的和。</p>
<p>注：这些内容是参考的《鸟哥的Linux私房菜》一书中的部分章节。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="shell脚本学习" href="http://chenzhiwei.net/2010/11/shell-script-study-notes/">http://chenzhiwei.net/2010/11/shell-script-study-notes/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li><li><a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/" title="linux shell 学习笔记(四)">linux shell 学习笔记(四)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/11/shell-script-study-notes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Linux Shell学习笔记（五）</title>
		<link>http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/</link>
		<comments>http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 06:36:07 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[软件应用]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30380</guid>
		<description><![CDATA[在这里的linux shell是做为shell程序来讲，不是shell语言，之前写的都是关于shell编程的。 shell是读取并解释命令的程序 shell是一个程序，充当界面和脚本解释器，允许用户输入命令以及间接地访问内核的服务。shell有很多种，常见的有Bash、Bourne Shell、C-Shell、FreeBSD Shell、Korn Shell、Pdksh、Tcsh、Zsh等，而目前常用的有Bash和FreeBSD Shell。一般Linux系统中的Shell是Bash，FreeBSD中是FreeBSD Shell，而商业Unix中是Korn Shell。如果你不知道自己正在使用的是哪个Shell，可以使用echo $SHELL来显示当前所使用的shell名称。 shell中的变量 假设创建一个名为KT的变量，并将值kitty赋给它：KT=kitty，此时KT只是一个shell变量。如果启动一个新的shell，则这个新的shell不能访问TK变量，因为KT变量还不是环境变量。执行export KT后，KT才会变成环境变量，unset KT表示复位变量，即删除变量。 用echo命令来显示变量的值，如显示普通字符：echo I love linux ，如果要显示变量的值，则需要使用$（美元符号）字符，后面跟用花括号括起来的变量名。例如：显示变量PATH的值可输入echo ${PATH}，在没有歧义的情况下还可以省略花括号，如：echo $PATH。 $符号会跟一个名称时，指的是该变量的值，如果没有$符号，则就是一个字符串。 shell中的引用和转义字符 如果想输出“ Today is a fine day; go and visit my house”句子时，需要这样写：echo Today is a fine day\; go and visit my house ，该语句使用了反斜线（\）元字符，意思是转义特殊字符。 如果想输出“ Today is (a fine day); go and visit [...]]]></description>
			<content:encoded><![CDATA[<p>在这里的linux shell是做为shell程序来讲，不是shell语言，之前写的都是关于shell编程的。</p>
<p><strong>shell是读取并解释命令的程序</strong></p>
<p>shell是一个程序，充当界面和脚本解释器，允许用户输入命令以及间接地访问内核的服务。shell有很多种，常见的有Bash、Bourne Shell、C-Shell、FreeBSD Shell、Korn Shell、Pdksh、Tcsh、Zsh等，而目前常用的有Bash和FreeBSD Shell。一般Linux系统中的Shell是Bash，FreeBSD中是FreeBSD Shell，而商业Unix中是Korn Shell。如果你不知道自己正在使用的是哪个Shell，可以使用<code>echo $SHELL</code>来显示当前所使用的shell名称。<span id="more-30380"></span></p>
<p><strong>shell中的变量</strong></p>
<p>假设创建一个名为KT的变量，并将值kitty赋给它：<code>KT=kitty</code>，此时KT只是一个shell变量。如果启动一个新的shell，则这个新的shell不能访问TK变量，因为KT变量还不是环境变量。执行<code>export KT</code>后，KT才会变成环境变量，<code>unset KT</code>表示复位变量，即删除变量。</p>
<p>用<strong>echo</strong>命令来显示变量的值，如显示普通字符：<code>echo I love linux</code> ，如果要显示变量的值，则需要使用<strong>$</strong>（美元符号）字符，后面跟用花括号括起来的变量名。例如：显示变量PATH的值可输入<code>echo ${PATH}</code>，在没有歧义的情况下还可以省略花括号，如：<code>echo $PATH</code>。</p>
<p>$符号会跟一个名称时，指的是该变量的值，如果没有$符号，则就是一个字符串。</p>
<p><strong>shell中的引用和转义字符</strong></p>
<p>如果想输出“ Today is a fine day; go and visit my house”句子时，需要这样写：<code>echo Today is a fine day\; go and visit my house</code> ，该语句使用了反斜线（\）元字符，意思是转义特殊字符。</p>
<p>如果想输出“ Today is (a fine day); go and visit my house” 时，则需要这样写：<code>echo Today is \(a fine day\)\; go and visit my house</code> ，虽然这条命令可以正确执行，但是易读性很差。这时单引号就派上用场了，我们可以直接这样写这条命令：<code>echo 'Today is a fine day; go and visit my house'</code> ，单引号内的内容原样输出。</p>
<p>如果想输出“My userid is <$USER>; my shell is <$SHELL>”时，可以这样写：<code>echo My userid is \<$USER\>\; my shell is \<$SHELL\></code>。但是这样写太复杂，这时，我们就可以使用双引号来重写命令：<code>echo  "My userid is <$USER>; my shell is <$SHELL>"</code>，双引号中的元字符除美元符号（$）、反斜线（\）和反引号（`）三个字符保留特殊含义外，其他字符均原样输出。</p>
<p><strong>shell中的别名：alias、unalias</strong></p>
<p>alias语法为：<code>alias [name=commands]</code>，等号两边没有空格。如：列出当前目录下所有以xyz开头的文件名：<code>ls xyz*</code>。使用<code>alias lx='ls xyz*'</code> 来为此命令定义别名lx，这样在命令行直接输入lx即可列出当前目录下的所有以xyz开头的文件。使用<code>unalias lx</code>来移除别名。</p>
<p>alias还可以重新定义现有的命令，如：<code>alias ls="ls -l"</code>，这样，每次直接输入ls就可以列出当前目录下文件的“长”列表，也就是详细信息。但有时候，我们希望运行的是原始命令而不是别名，这时我们可以临时挂起别名，在命令行输入<code>\ls</code>即可，<code>\ls</code>告诉shell运行的是实际命令而不是别名。</p>
<p>注：关于shell程序的用法还有很多，这只列出了常用的几个。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="Linux Shell学习笔记（五）" href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/">http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/" title="linux shell 学习笔记(四)">linux shell 学习笔记(四)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>linux shell 学习笔记(四)</title>
		<link>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/</link>
		<comments>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 08:09:46 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[默认分类]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30329</guid>
		<description><![CDATA[九、linux中的函数 函数代表一种模块化的设计思想，可以将一些常用的、内聚度高的操作封装成函数，在需要时进行调用。 1. 函数原型 1 2 3 4 function 函数名() { 语句 } 在此，function可以不显示指定。函数应先定义后使用，在调用时只需指定函数名即可，不用写()。 示例： 1 2 3 4 5 6 #!/bin/bash function printAddress() { echo &#34;your address is: beijing&#34; } printAddress 2. 函数的参数和返回值 在shell函数中，有两种方法使用参数：一种是变量直接传递法，另一种采用位置参数。 变量直接传递法是通过直接在外部声明变量，在函数体里面通过操作这些变量，达到向函数传递参数的目的。 例如： 1 2 3 4 5 6 7 8 9 10 #!/bin/bash address=&#34;beijing&#34; function printAddress() { echo &#34;your address_1 [...]]]></description>
			<content:encoded><![CDATA[<p>九、linux中的函数</p>
<p>函数代表一种模块化的设计思想，可以将一些常用的、内聚度高的操作封装成函数，在需要时进行调用。<br />
<span id="more-30329"></span><br />
1. 函数原型</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">function 函数名()
{
	语句
}</pre></td></tr></table></div>

<p>在此，function可以不显示指定。函数应先定义后使用，在调用时只需指定函数名即可，不用写()。</p>
<p>示例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
function printAddress()
{
	echo &quot;your address is: beijing&quot;
}
printAddress</pre></td></tr></table></div>

<p>2. 函数的参数和返回值</p>
<p>在shell函数中，有两种方法使用参数：一种是变量直接传递法，另一种采用位置参数。</p>
<p>变量直接传递法是通过直接在外部声明变量，在函数体里面通过操作这些变量，达到向函数传递参数的目的。</p>
<p>例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
address=&quot;beijing&quot;
function printAddress()
{
	echo &quot;your address_1 is: &quot;$address
	address=&quot;shanghai&quot;
	echo &quot;your address_2 is: &quot;$address
}
printAddress
echo &quot;your address_3 is: &quot;$address</pre></td></tr></table></div>

<p>由此可见，函数中进行变量修改也作全局使用，所以可以通过在函数中修改参数变量来达到函数返回目的，将这些参数作为返回值。</p>
<p>通过下面的例子可以更好的了解位置参数：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
function printAddress()
{
	echo $1
	echo $2
}
printAddress &quot;beijing&quot; &quot;shanghai&quot;</pre></td></tr></table></div>

<p>注：以前学习shell时写的笔记，没有拿到电脑前练习过。这次写在博客上的这些都是我在电脑上(ubuntu8.04)练习通过后贴出来的，单引号和双引号类似于php中的单引号和双引号的用法，在用echo进行标准输出时，如果不涉及变量的话双引号、单引号都是可有可无的，还有那个数组赋值与遍历也是让我琢磨不透，所以这些细微差别还是日后慢慢的在实践中掌握比较好。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="linux shell 学习笔记(四)" href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/">http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-4/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>linux shell 学习笔记(三)</title>
		<link>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-3/</link>
		<comments>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-3/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 07:31:58 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[默认分类]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30324</guid>
		<description><![CDATA[八、shell中的循环语句 shell中的循环语句有for循环语句、while循环语句和until循环语句。 1. for循环语句 原型一： 1 2 3 4 5 for 变量 in 取值集合 for 变量 in 取值集合 do 语句 done 原型二： 1 2 3 4 for 变量 in 文件正则表达式 do 语句 done 原型三： 1 2 3 4 for 变量 in $* do 语句 done 例： 1 2 3 4 5 6 #!/bin/bash address=(beijing tianjin [...]]]></description>
			<content:encoded><![CDATA[<p>八、shell中的循环语句</p>
<p>shell中的循环语句有for循环语句、while循环语句和until循环语句。<br />
<span id="more-30324"></span><br />
1. for循环语句</p>
<p>原型一：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">for 变量 in 取值集合
for 变量 in 取值集合
do
	语句
done</pre></td></tr></table></div>

<p>原型二：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">for 变量 in 文件正则表达式
do
	语句
done</pre></td></tr></table></div>

<p>原型三：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">for 变量 in $*
do
	语句
done</pre></td></tr></table></div>

<p>例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
address=(beijing tianjin shandong)
for i in ${address[*]}
do
	echo $i
done</pre></td></tr></table></div>

<p>说明：<br />
循环输出数组中的值。</p>
<p>2. while循环语句</p>
<p>原型：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">while 条件测试语句
do
	语句
done</pre></td></tr></table></div>

<p>while语句的执行过程是：先测试测试语句是否为真，若为真则执行循环体，当执行完当前命令后，再进行条件测试，直到条件结果为假，循环结束。条件测试语句既可以是test语句也可以是运行命令的返回值，若返回值大于0，则表示条件为真，否则条件为假。</p>
<p>例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
str=&quot;&quot;
echo &quot;input a str:&quot;
read str
while [ $str != &quot;quit&quot; ]
do
	echo &quot;your input is :&quot;$str
	echo &quot;input your str:&quot;
	read str
done</pre></td></tr></table></div>

<p>说明：</p>
<p>3. until循环语句</p>
<p>until语句是while语句的一种变形，原型如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">until 条件测试语句
do
	语句
done</pre></td></tr></table></div>

<p>如果条件为假则执行，否则不执行。</p>
<p>另外还有以下语句和循环语句合用。</p>
<p>①、break语句</p>
<p>break语句是一个退出循环的命令，主要用于多层循环的嵌套退出，一般用法如下：</p>
<p>break [n]</p>
<p>其中，n用来表示跳出几层循环，默认值为1，即退出本次循环。</p>
<p>②、continue语句</p>
<p>continue语句与break语句有相同之处，都用于终止本次循环，区别在于，bread语句是退出整个循环而continue语句只是退出本次循环，继续执行下一循环体。continue语句原型如下：</p>
<p>continue [n]</p>
<p>其中，n用来表示跳出几层循环，默认值为1，即退出本次循环。</p>
<p>③exit语句</p>
<p>exit语句是退出正在执行的shell脚本，可以主动指定返回值，其原型如下：</p>
<p>exit [n]</p>
<p>其中，n是主动设定的返回值。如果未显示给定n的值，则该值默认为最后一次命令的执行状态作为返回值。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="linux shell 学习笔记(三)" href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-3/">http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-3/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>linux shell学习笔记(二)</title>
		<link>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-2/</link>
		<comments>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-2/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 10:32:00 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[编程相关]]></category>
		<category><![CDATA[默认分类]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30318</guid>
		<description><![CDATA[接上篇日志：linux shell学习笔记(一) 这篇日志主要写shell中的控制语句，有两种判断语句，if语句和case语句，学过C语言的同学一定不会陌生，很简单的用法。 七、shell中的控制判断语句 1. if语句 原型一 1 2 3 if 测试条件 then 语句1 fi 原型二 1 2 3 4 if 测试条件 then 语句1 else 语句2 fi 原型三 1 2 3 4 5 6 if 测试条件1 then 语句1 elif 测试条件2 then 语句2 else 语句3 fi 在if语句中一个重要的环节是编写测试条件，也就是程序执行的判断条件。if语句中可以将命令执行结果当作测试条件。例如：若命令正常结束，则表示测试成功，其返回值为0，条件测试为真；否则条件测试为假。测试条件编写一般有以下3种形式： （1）. 用test命令 （2）. 用方括号把测试条件括起来。 （3）. 采用[[条件表达式]]方式，条件表达式用来测试文件的属性和进行字符串比较。 test命令原型如下： test 表达式 [...]]]></description>
			<content:encoded><![CDATA[<p>接上篇日志：<a href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/" target="_blank">linux shell学习笔记(一)</a></p>
<p>这篇日志主要写shell中的控制语句，有两种判断语句，if语句和case语句，学过C语言的同学一定不会陌生，很简单的用法。<span id="more-30318"></span></p>
<p><strong>七、shell中的控制判断语句</strong></p>
<p>1. if语句</p>
<p>原型一</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">if 测试条件
then 语句1
fi</pre></td></tr></table></div>

<p>原型二</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">if 测试条件
then 语句1
else 语句2
fi</pre></td></tr></table></div>

<p>原型三</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">if 测试条件1
then 语句1
elif 测试条件2
then 语句2
else 语句3
fi</pre></td></tr></table></div>

<p>在if语句中一个重要的环节是编写测试条件，也就是程序执行的判断条件。if语句中可以将命令执行结果当作测试条件。例如：若命令正常结束，则表示测试成功，其返回值为0，条件测试为真；否则条件测试为假。测试条件编写一般有以下3种形式：</p>
<p>（1）. 用test命令</p>
<p>（2）. 用方括号把测试条件括起来。</p>
<p>（3）. 采用[[条件表达式]]方式，条件表达式用来测试文件的属性和进行字符串比较。</p>
<p>test命令原型如下：</p>
<p>test 表达式</p>
<p>test一般与系统运算搭配起来使用，它可以与如下四类运算符共同使用。</p>
<p>第一类：字符测试运算符</p>
<p>字符测试运算符的作用是用于测试字符串操作的返回值，具体参数如下：</p>
<p>-z str1 ：如果字符串str1的长度为0，则测试条件为真<br />
-n str1 ：如果字符串str1的长度大于0，则测试条件为真<br />
str1       ：如果字符串str1不是空串，则测试条件为真<br />
str1 = str2 ：如果字符串str1等于字符串str2，则测试条件为真<br />
str1 != str2 ：如果字符串str1不等于字符串str2，则测试条件为真<br />
str1 &gt; str2 ：如果按字典顺序字符串str1排在str2之后，则测试条件为真<br />
str1 &lt; str2 ：如果按字典顺序字符串str1排在str2之前，则测试条件为真</p>
<p>注：=、!=、&lt;、&gt;，这些符号在使用时两边要加空格</p>
<p>例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo &quot;input your address:&quot;
read address
if address
then
	echo &quot;your address is: &quot;$address
else
	echo &quot;your address is null&quot;
fi</pre></td></tr></table></div>

<p>以上程序意思是用户输入一字符串，如果输入字符串长度大于0，则输出用户输入的字符串，否则输出空。</p>
<p>第二类：数值测试运算符</p>
<p>数值测试运算符主要用于数值运算时的判断，具体参数如下：</p>
<p>num1 -eq num2 ：如果整数num1等于num2，则测试条件为真<br />
num1 -ne num2 ：如果整数num1不等于num2，则测试条件为真<br />
num1 -lt num2 ：如果整数num1小于num2，则测试条件为真<br />
num1 -le num2 ：如果整数num1小于或等于num2，则测试条件为真<br />
num1 -gt num2 ：如果整数num1大于num2，则测试条件为真<br />
num1 -ge num2 ：如果整数num1大于或等于num2，则测试条件为真</p>
<p>第三类：逻辑运算符</p>
<p>逻辑运算符用于逻辑语句的判断，常用的逻辑运算符如下：</p>
<p>! ：逻辑“非”，放在任意逻辑表达式的前面，全原来为真的表达式为假，原来为假的表达式为真<br />
-a ：逻辑“与”，放在两个逻辑表达式之间，表示只有两个表达式都为真时，结果才为真<br />
-o ：逻辑“或”，放在两个逻辑表达式之间，表示只有两个表达式都为假时，结果才为假<br />
() ：圆括号可以把逻辑表达式括起来，使用之成为一个整体，优先进行运算</p>
<p>注：逻辑表达式中运算符的优先关系如下：”()”&gt;”!”&gt;”-a”&gt;”-o”。</p>
<p>第四类：文件运算符</p>
<p>文件运算符用于测试文件或目录的操作，常用文件运算符如下：</p>
<p>-r 文件名：若文件存在并且是用户可读的，则测试条件为真<br />
-w 文件名：若文件存在并且是用户可写的，则测试条件为真<br />
-x 文件名：若文件存在并且是用户可执行的，则测试条件为真<br />
-f  文件名：若文件存在并且是普通文件，则测试条件为真<br />
-d 文件名：若文件存在并且是目录文件，则测试条件为真<br />
-p 文件名：若文件存在并且是命名的FIFO文件，则测试条件为真<br />
-b 文件名：若文件存在并且是块设备文件，则测试条件为真<br />
-c 文件名：若文件存在并且是字符设备文件，则测试条件为真<br />
-s 文件名：若文件存在并且长度大于0，则测试条件为真<br />
-t 文件描述字：若文件被打开并且文件描述字是与终端设备相关的，则测试条件为真，默认的文件描述字是1。</p>
<p>2. case语句</p>
<p>case语句是一种多重判断语句，类似于多个if elif语句，其原型如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">case 字符串 in
	模式表达式1) 语句1;;
	模式表达式2) 语句2;;
	模式表达式3) 语句3;;
	...
esac</pre></td></tr></table></div>

<p>case语句执行原理是将字符串与各个模式串顺次匹配，若满足则执行，否则继续查找，如果没有匹配成功的，则不执行任何语句，直接退出。</p>
<p>注：每个模式处理语句是以”;;”两个分号结束的，模式表达式应该唯一，一个模式表达式可以含有多个模式串，但它们之间要用”|”隔开。</p>
<p>例：</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="linux shell学习笔记(二)" href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-2/">http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-2/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>linux shell学习笔记(一)</title>
		<link>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/</link>
		<comments>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 01:32:43 +0000</pubDate>
		<dc:creator>zhiwei</dc:creator>
				<category><![CDATA[默认分类]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://chenzhiwei.net/?p=30311</guid>
		<description><![CDATA[昨天逛dengmin同学的博客，发现他把自己的php学习笔记都放在的博客上，我想了一下，感觉还是将我学习shell时做的笔记写在博客上吧，虽然很简单，但是毕竟写在纸上不如放在博客上，写在博客上自己随时都可以查看。 在计算机科学中，Shell俗称壳（用来区别于核），是指“提供使用者使用界面”的软件（命令解析器）。它类似于DOS下的command.com。它接收用户命令，然后调用相应的应用程序。同时它又是一种程序设计语言。作为命令语言，它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令；作为程序设计语言，它定义了各种变量和参数，并提供了许多在高阶语言中才具有的控制结构，包括循环和分支。shell是一种解释型编程语言，这些都不重要，重要的是怎么使用它。 一、 执行shell脚本的方法 1. 输入重定向到shell脚本，具体来讲是利用输入重定向机制，让shell解释器顺序读取每一行脚本命令，进行执行，使用原型为bash&#62;脚本文件名，例如：bash&#62;test.sh。 2. 类似于方法1，不过它是以脚本文件名为参数来执行的，原型：bash test.sh 。 3. 将脚本文件设置成可执行文件，直接调用执行，./test.sh 。(用chmod +x test.sh将脚本文件加上可执行权限) 二、shell的基本语法 赋值一般采用以下形式：变量名=字符串 1. “=”号两边是不能有空格的，不然会出错的。 2. 若赋值语句中，“=”后面没有任何内容，则该变量为一个空字符串，若只声明而没赋值，则该变量默认也是一个空字符串。 3. 若一个变量中含有空格、制表符、换行符，则要用双引号括起来，不然会出错。 4. 在shell程序文件中，如果想引用已经定义的变量，一般要在变量名前加“$”符号，这个符号含义是告诉shell，后面是一个变量。 5. 单引号(&#8216;&#8230;&#8217;)：单引号也称为强引用，引用所有内容。在单引号中，没有字符拥有特殊含义。 6. 双引号(“&#8230;”)：双引号也称为弱引用，除了3个元字符$(美元符号)、`(反引号)和\(反斜线)外引用所有内容。在双引号中，这3个字符还保留它们各自的特殊含义。 7. 反引号(`&#8230;`)：命令替换，命令替换允许在一条命令中嵌入一条命令。shell首先执行嵌入的命令，并且用输出替换该命令。然后shell再执行整个命令。 8. 在shell变量引用中，一个变量与一个长字符串的组合，如果当前变量处在字符串的最后，可以利用直接引用的方式；如果处在中间或开头的位置，则可以用花括号将变量名包含起来。如下： 程序： 1 2 3 4 5 #!/bin/bash address=beijing echo $address echo ${address}test echo test$address 输出： 1 2 3 beijing beijingtest testbeijing 三、shell中的通配符(用于模式匹配) [...]]]></description>
			<content:encoded><![CDATA[<p>昨天逛<a href="http://www.iyouf.info/" target="_blank">dengmin</a>同学的博客，发现他把自己的php学习笔记都放在的博客上，我想了一下，感觉还是将我学习shell时做的笔记写在博客上吧，虽然很简单，但是毕竟写在纸上不如放在博客上，写在博客上自己随时都可以查看。</p>
<p>在计算机科学中，Shell俗称壳（用来区别于核），是指“提供使用者使用界面”的软件（命令解析器）。它类似于DOS下的command.com。它接收用户命令，然后调用相应的应用程序。同时它又是一种程序设计语言。作为命令语言，它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令；作为程序设计语言，它定义了各种变量和参数，并提供了许多在高阶语言中才具有的控制结构，包括循环和分支。shell是一种解释型编程语言，这些都不重要，重要的是怎么使用它。<br />
<span id="more-30311"></span><br />
<strong>一、 执行shell脚本的方法</strong></p>
<p>1.<del datetime="2010-07-25T00:01:18+00:00"> 输入重定向到shell脚本，具体来讲是利用输入重定向机制，让shell解释器顺序读取每一行脚本命令，进行执行，使用原型为bash&gt;脚本文件名，例如：bash&gt;test.sh</del>。</p>
<p>2. 类似于方法1，不过它是以脚本文件名为参数来执行的，原型：bash test.sh 。</p>
<p>3. 将脚本文件设置成可执行文件，直接调用执行，./test.sh 。(用chmod +x test.sh将脚本文件加上可执行权限)</p>
<p><strong>二、shell的基本语法</strong></p>
<p>赋值一般采用以下形式：变量名=字符串</p>
<p>1. “=”号两边是不能有空格的，不然会出错的。</p>
<p>2. 若赋值语句中，“=”后面没有任何内容，则该变量为一个空字符串，若只声明而没赋值，则该变量默认也是一个空字符串。</p>
<p>3. 若一个变量中含有空格、制表符、换行符，则要用双引号括起来，不然会出错。</p>
<p>4. 在shell程序文件中，如果想引用已经定义的变量，一般要在变量名前加“$”符号，这个符号含义是告诉shell，后面是一个变量。</p>
<p>5. 单引号(&#8216;&#8230;&#8217;)：单引号也称为强引用，引用所有内容。在单引号中，没有字符拥有特殊含义。</p>
<p>6. 双引号(“&#8230;”)：双引号也称为弱引用，除了3个元字符$(美元符号)、`(反引号)和\(反斜线)外引用所有内容。在双引号中，这3个字符还保留它们各自的特殊含义。</p>
<p>7. 反引号(`&#8230;`)：命令替换，命令替换允许在一条命令中嵌入一条命令。shell首先执行嵌入的命令，并且用输出替换该命令。然后shell再执行整个命令。</p>
<p>8. 在shell变量引用中，一个变量与一个长字符串的组合，如果当前变量处在字符串的最后，可以利用直接引用的方式；如果处在中间或开头的位置，则可以用花括号将变量名包含起来。如下：<br />
程序：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
address=beijing
echo $address
echo ${address}test
echo test$address</pre></td></tr></table></div>

<p>输出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">beijing
beijingtest
testbeijing</pre></td></tr></table></div>

<p><strong>三、shell中的通配符(用于模式匹配)</strong></p>
<p>1. “*”符号</p>
<p>“*”符号用于匹配字符串中0次或多次出现的字符，如：s*可以匹配shell、shanghai等。在使用“*”符号时要注意一点，在匹配文件名与路径名时，“.”符号与“\”必须显示匹配，如：*test不能匹配“.httest”文件，而要用“.*test”来匹配，同时“/home/test”需要用”/*/test”来匹配。</p>
<p>2. “?”符号</p>
<p>“?”符号仅匹配对应位置的一个字符。如：m?ke可匹配”mike”、”make”等，但不能匹配”mooke”。</p>
<p>3. “[]“符号</p>
<p>“[]“称号的作用是匹配该字符组所限定范围内的任何一个字符，方括号中的字符可以由直接级出的字符组成，如：[adehk]；也可以由表示限定范围的起始字符和终止字符及中间的连接字符”-”组成。如：[a-zA-H]、[0-9]等。</p>
<p>4. “!”符号</p>
<p>“!”符号是与”[]“符号配合使用的，”!”的作用是匹配不在方括号中列出的字符。例如：t[!a-h]st，则可表示tyst、t9st，但不能表示test。</p>
<p><strong>四、shell中的输入</strong></p>
<p>shell中输入是由函数read实现，原型为：read 变量1 [变量2]</p>
<p>利用read函数可以交互地为变量赋值，当然也可以通过制表符或空格为多变量赋值，说明如下：</p>
<p>1. 如果变量个数多于输入串中字符串个数，则依次赋值，剩下变量取空值。</p>
<p>2. 如果变量个数等于输入串中字符串个数，则一一对应赋值。</p>
<p>3. 如果变量个数少于输入串中字符串个数，刚除依次赋值外，最后一个变量接纳剩下的字符串。</p>
<p>例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo &quot;input your name and age:&quot;
read name age
echo &quot;your name is: &quot;$name
echo &quot;your age is: &quot;$age</pre></td></tr></table></div>

<p>如果你输入Jim 15 则输出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">input your name and age:
your name is: Jim
your age is: 15</pre></td></tr></table></div>

<p><strong>五、shell中的输出</strong></p>
<p>shell中输出是由echo函数实现的，echo可直接输出其后面所跟变量的值或直接输出其后面的字符串。echo函数后面以空格隔开，以换行符终止。如果数据之间要保留多个空格，则要用双引号把它们括起来以便shell对它们进行正确的操作。另：echo函数还定义了一组转义字符，在使用转义字符时要加入”-e”选项。其转义字符如下：</p>
<p>“\a” ：响铃报警，”\b” ：后退一字符，”\f” ：换页，”\n” ：显示换行，”\t” ：制表符，”\v” ：垂直制表符，”\r” ：回车符，”\\” ：反斜线。<br />
例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
echo -e hello,'\n'world!
echo hello,'\n'world!
echo '-e' hello,'\n'world!
echo -e hello,&quot;\n&quot;world!
echo -e hello,\nworld!
echo hello,\nworld!</pre></td></tr></table></div>

<p>输出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">hello,
world!
hello,\nworld!
hello,
world!
hello,
world!
hello,nworld!
hello,nworld!</pre></td></tr></table></div>

<p><strong>六、shell中的数组</strong></p>
<p>shell支持一维数组，但并不限定数组大小，数组下标从0开始。</p>
<p>在操作数组时，取值方式是：${数组名[下标]} ；赋值方式是：数组名[下标]=值 (为单个数组元素赋值)；如果要对整个数组的所有元素赋值，可以采用：数组名=(值1，值2，值3，&#8230;)，值与值之间要用空格隔开。</p>
<p>遍历数组除用循环外还可用：“数组名[*]”或“数组名[@]”，例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
array1[0]=beijing
array1[1]=shanghai
array2=(guangzhou,shenzhen,chengdu)
echo &quot;\${array1[0]} = &quot; ${array1[0]}
echo &quot;\$array1[1] = &quot;$array1[1]
echo &quot;\${array1[*]} = &quot;${array1[*]}
echo &quot;\$array1 = &quot;$array1
echo &quot;\${array2[*]} = &quot;${array2[*]}
echo &quot;\${array2[@]} = &quot;${array2[@]}
echo &quot;\$array2 = &quot;$array2
echo &quot;\$array2[1] = &quot;$array2[1]</pre></td></tr></table></div>

<p>输出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">${array1[0]} =  beijing
$array1[1] = beijing[1]
${array1[*]} = beijing shanghai
$array1 = beijing
${array2[*]} = guangzhou,shenzhen,chengdu
${array2[@]} = guangzhou,shenzhen,chengdu
$array2 = guangzhou,shenzhen,chengdu
$array2[1] = guangzhou,shenzhen,chengdu[1]</pre></td></tr></table></div>

<p>注：数组的赋值与输出有点麻烦。<br />
对于数组修改操作，可以再对其重新赋值；但如果要删除一个已经赋值后的元素则需要借助一个外部命令：unset，如：unset array[0]可清空下标为0的元素，此时数组大小减一；unset array[@]可以清空整个数组元素所有元素。例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">#!/bin/bash
address=(beijing,shanghai,shandong)
address[0]=nanjing
echo ${address[*]}
unset address[0]
echo ${address[*]}</pre></td></tr></table></div>

<p>输出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">beijing shanghai shandong
shanghai shandong</pre></td></tr></table></div>

<p>注：关于数组输入与输出规则比较多，得多练习掌握。</p>
<p style="font-weight: bold;">&copy; 2010, <a href="http://chenzhiwei.net">chenzhiwei.net</a>. 版权所有.  <br />本文永久链接：<a title="linux shell学习笔记(一)" href="http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/">http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/</a></p><hr /><div  class="related_post_title">相关日志</div><ul class="related_post"><li><a href="http://chenzhiwei.net/2011/08/shell-io-redirection/" title="Shell I/O重定向">Shell I/O重定向</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-shell/" title="几个简单的shell脚本">几个简单的shell脚本</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes-2/" title="shell 脚本学习之判断条件">shell 脚本学习之判断条件</a></li><li><a href="http://chenzhiwei.net/2010/11/shell-script-study-notes/" title="shell脚本学习">shell脚本学习</a></li><li><a href="http://chenzhiwei.net/2010/10/linux-shell-study-notes-part-5/" title="Linux Shell学习笔记（五）">Linux Shell学习笔记（五）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://chenzhiwei.net/2010/07/linux-shell-study-notes-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

