<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[shell的getopts：用getopts获取命令行参数]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 28 Dec 2011 12:38:31 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	常用的参数<br/>$#&nbsp;&nbsp;参数的个数<br/>$* 所有的参数<br/>$@ 所有的参数<br/>shift来处理参数 一般两种形式<br/>============================<br/>while [ -n $# ]<br/>do<br/>&nbsp;&nbsp; :<br/>&nbsp;&nbsp; shift<br/>done<br/>=============================<br/>while [ $1 ]<br/>do<br/>&nbsp;&nbsp; :<br/>&nbsp;&nbsp; shift n<br/>done<br/>==============================<br/>使用getopts来取参数<br/>getopts string options<br/>例如<br/>while getopts &quot;lur:&quot; opt<br/>do<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case $opt in<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l) list=&quot;menu&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u) user=&quot;$OPTARG&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r) ro=&quot;$OPTARG&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?) echo &quot;Usage: `basename $0` -l -u user -r yes&#124;no&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esac<br/>done<br/># 说明<br/>while getopts &quot;lur:&quot; opt<br/>string 部分 &quot;lur&quot; 指定可以带的参数为 -l -u -r 并保存在opt这个变量中 ,<br/>r后面的冒号表示 -r 后面必须指定一个参数 比如 -r yes<br/>并且这个参数yes默认是保存在OPTARG变量中的<br/>如果 -r 后面没有指定参数 那么会提示出错 并opt被设置为?重新处理 这个opt<br/>对于必须制定参数 却没有读取到的错误 可以在string的开始加上: 来处理<br/>修改后<br/>#!/bin/bash<br/>while getopts &quot;:lu:r:&quot; opt<br/>do<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case $opt in<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l) list=&quot;menu&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u) user=&quot;$OPTARG&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r) ro=&quot;$OPTARG&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?) echo &quot;Usage: `basename $0` -l -u user -r yes&#124;no&quot;;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esac<br/>done<br/>运行一下<br/>[root@localhost code]# ./sc.sh -l -u<br/>Usage: sc.sh -l -u user -r yes&#124;no<br/>[root@localhost code]# ./sc.sh -u -r -l<br/>[root@localhost code]#<br/>./sc.sh -u -r -l 没有出错 这时<br/>来自：http://linux.chinaunix.net/techdoc/system/2006/01/18/926938.shtml
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] shell的getopts：用getopts获取命令行参数]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>