<?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>一个故事@MySQL DBA &#187; shell</title>
	<atom:link href="http://www.orczhou.com/index.php/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.orczhou.com</link>
	<description>一个故事@MySQL DBA</description>
	<lastBuildDate>Tue, 24 Aug 2010 01:44:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>使用getopts处理Shell脚本参数</title>
		<link>http://www.orczhou.com/index.php/2010/04/linux-shell-getopt/</link>
		<comments>http://www.orczhou.com/index.php/2010/04/linux-shell-getopt/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 11:48:21 +0000</pubDate>
		<dc:creator>orczhou</dc:creator>
				<category><![CDATA[技术细节]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.orczhou.com/?p=1822</guid>
		<description><![CDATA[<p>编写一个shell脚本，做一些事；改进这个脚本，更好做这件事；再改进这个脚本，帮自己做些其他的事情；再改进这个脚本帮助其他人做一些事&#8230;&#8230;</p>
<p>简单的脚本处理，一般使用变量$0 $1 $2 &#8230;就可以依次获得全部参数，还可以通过$#获得这个脚本一共有多少个参数。如果你需要处理的情况（或者分支）更多的时候，这个方法就不凑效了，这时候，就可以考虑使用getopts了（man getopts）。[......]</p><p class='read-more'><a href='http://www.orczhou.com/index.php/2010/04/linux-shell-getopt/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>编写一个shell脚本，做一些事；改进这个脚本，更好做这件事；再改进这个脚本，帮自己做些其他的事情；再改进这个脚本帮助其他人做一些事&#8230;&#8230;</p>
<p>简单的脚本处理，一般使用变量$0 $1 $2 &#8230;就可以依次获得全部参数，还可以通过$#获得这个脚本一共有多少个参数。如果你需要处理的情况（或者分支）更多的时候，这个方法就不凑效了，这时候，就可以考虑使用getopts了（man getopts）。<span id="more-1822"></span></p>
<p>这里将通过一个示例来介绍getopts的用法。</p>
<p>下面的代码，可以通过&#8221;./sample -d 5&#8243;的方式获取参数：</p>
<pre>
<div class="mycode">vi sample.sh
#!/bin/sh
day=7  #default value
while getopts ":d:" opt; do
  case $opt in
    d)
      day=$OPTARG   #get the value
      ;;
    ?)
      echo "How to use: $0 [-d DAY]" >&#038;2
      exit 1
      ;;
    &#58;)
      echo "Option -$OPTARG requires an argument." >&#038;2
      exit 1
      ;;
  esac
done
echo $day
</div>
</pre>
<p>上面例子中需要解释的是下面的部分：</p>
<pre>
<div class="mycode">while getopts ":d:" opt; do</div>
</pre>
<p>这里，第一个冒号表示忽略错误（例如出现了不认识的参数），并在脚本中通过:&#58;)来处理这样的错误；字母d则表示，接受参数-d；d后面的冒号表示参数d接收值，即“-d 7”这样的形式；（这里opt变量，可以在while循环中引用当前找到的参数，试试输出$opt试试）</p>
<p>如果是要有很多参数，那么写法可能是：</p>
<pre>
<div class="mycode">while getopts ":ixarm:uneh" opt; do</div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.orczhou.com/index.php/2010/04/linux-shell-getopt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>迥异的Linux Shell Script</title>
		<link>http://www.orczhou.com/index.php/2009/09/linux-shell-script/</link>
		<comments>http://www.orczhou.com/index.php/2009/09/linux-shell-script/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 07:44:28 +0000</pubDate>
		<dc:creator>orczhou</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术细节]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://orczhou.com/?p=774</guid>
		<description><![CDATA[<p style="text-indent:0">
<strong>作者：</strong><a href="http://www.flickr.com/photos/26825745@N06/3933896112/" title="mail by orczhou, on Flickr"><img src="http://farm3.static.flickr.com/2614/3933896112_8ebfd42177_o.png" width="163" height="22" alt="mail" /></a><br />
<strong>摘要：</strong>如果你已经习惯编写Shell脚本，那么一切都显得很自然了。但像我这样，之前已经习惯了C语言(风格)的编程，就觉得Shell并不是那么友好。这里将记录一些Shell和&#8221;C风格&#8221;<strong>迥异</strong>的地方，以供参考。本文测试环境：GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)。文章中很多实例直接来在参考文件[1]、[2]。
</p>
<div style="margin-left:120px">
<a href="http://www.flickr.com/photos/26825745@N06/3930881408/" title="Linux by orczhou, on Flickr"><img src="http://farm3.static.flickr.com/2622/3930881408_5033a56de1_o.png" width="318" height="218" alt="Linux" /></a>
</div>
<p>[......]</p><p class='read-more'><a href='http://www.orczhou.com/index.php/2009/09/linux-shell-script/'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent:0">
<strong>作者：</strong><a href="http://www.flickr.com/photos/26825745@N06/3933896112/" title="mail by orczhou, on Flickr"><img src="http://farm3.static.flickr.com/2614/3933896112_8ebfd42177_o.png" width="163" height="22" alt="mail" /></a><br />
<strong>摘要：</strong>如果你已经习惯编写Shell脚本，那么一切都显得很自然了。但像我这样，之前已经习惯了C语言(风格)的编程，就觉得Shell并不是那么友好。这里将记录一些Shell和&#8221;C风格&#8221;<strong>迥异</strong>的地方，以供参考。本文测试环境：GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)。文章中很多实例直接来在参考文件[1]、[2]。
</p>
<div style="margin-left:120px">
<a href="http://www.flickr.com/photos/26825745@N06/3930881408/" title="Linux by orczhou, on Flickr"><img src="http://farm3.static.flickr.com/2622/3930881408_5033a56de1_o.png" width="318" height="218" alt="Linux" /></a>
</div>
<p><span id="more-774"></span></p>
<p style="text-indent:0"><font size=3>1、变量定义等号两边不能有空格</font></p>
<blockquote><p>
#!/bin/bash<br />
myvar=3        #正确<br />
<del datetime="2009-09-16T08:58:09+00:00">myvar   =   3;</del> #等号边多了空格，是错误的！很<strong>迥异</strong>吧
</p></blockquote>
<p style="text-indent:0"><font size=3>1.1、还是空格，这次是不能没有</font></p>
<blockquote><pre>
#!/bin/sh
myVar="OFF"
if [   $myVar = 'OFF'    ];then
#这里[]中括起来的内容<strong>两端必须有</strong>空格，<del datetime="2009-09-17T07:30:53+00:00">if [$myVar = 'OFF']</del>是不能正常工作的。也很<strong>迥异</strong>吧
#注意 if 和 [ 之间也是有空格的！
        echo "works"
else
        echo "Not works"
fi
</pre>
</blockquote>
<p style="text-indent:0"><font size=3>2、双引号和单引号</font></p>
<blockquote><p>
testvar=5<br />
myvar=&#8217;Haha$test&#8217;      #shell<strong>不</strong>会解释$test<br />
myvar2=&#8221;Haha$test&#8221;    #shell会解释$test<br />
echo $myvar $myvar2  #输出：Haha$test Haha5
</p></blockquote>
<p>双引号中的字符如果有变量，shell会尝试解释它，单引号中不会。所以，如果字符串中没有要解释的变量尽量使用单引号，据说速度会快些。</p>
<p>这个到不算很迥异，还有很多其他的语言也都有这样的约定。</p>
<p style="text-indent:0"><font size=3>3、奇怪的算术运算</font></p>
<p>shell中算术运算需要使用$((和))将算术运算括起来</p>
<blockquote><p>
$(( $myvar + 12 ))   #这个非常<strong>迥异</strong>
</p></blockquote>
<p style="text-indent:0"><font size=3>4、case语句</font></p>
<blockquote>
<pre>
case "${x##*.}" in
      gz)
            gzunpack ${SROOT}/${x}
            ;;
      bz2)
            bz2unpack ${SROOT}/${x}
            ;;
      *)
            echo "Archive format not recognized."
            exit
            ;;
esac           #这个比较<strong>迥异</strong> "esac"、";;"、"bz2)"
</pre>
</blockquote>
<p style="text-indent:0"><font size=3>5、函数中的变量作用范围</font></p>
<blockquote>
<pre>
#!/usr/bin/env bash
myvar="hello"
myfunc() {
     myvar="one two three"
     for x in $myvar
     do
         echo $x
     done
}
myfunc
echo $myvar $x
输出：
one
two
three
one two three three  #函数myfunc中的变量，在函数之外仍然存在。
#你可以通过关键字 local 限制变量只在函数中有效果 #这个比较<strong>迥异</strong>
</pre>
</blockquote>
<p>还有更迥异的吗？</p>
<p>参考文献：</p>
<ol>
<li>http://www.ibm.com/developerworks/cn/linux/shell/bash/bash-1/index.html</li>
<li>http://www.ibm.com/developerworks/cn/linux/shell/bash/bash-2/index.html</li>
<li>http://www.ibm.com/developerworks/cn/linux/shell/bash/bash-3/index.html</li>
<li>http://www.linuxsir.org/main/?q=node/135</li>
<li>BASH Programming − Introduction HOW−TO</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.orczhou.com/index.php/2009/09/linux-shell-script/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
