<?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>MemoSoup &#187; parameters</title>
	<atom:link href="http://www.memosoup.com/tag/parameters/feed" rel="self" type="application/rss+xml" />
	<link>http://www.memosoup.com</link>
	<description>Mix of the things I always forget</description>
	<lastBuildDate>Thu, 09 Feb 2012 12:24:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Parsing script parameters</title>
		<link>http://www.memosoup.com/20090912/parsing-script-parameters.html</link>
		<comments>http://www.memosoup.com/20090912/parsing-script-parameters.html#comments</comments>
		<pubDate>Sat, 12 Sep 2009 15:26:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.memosoup.com/?p=44</guid>
		<description><![CDATA[<p>Quick and dirty parsing procedure for unix shell scripts</p> parse_command_line &#40;&#41; &#123; typeset -i user_opt help_opt password_opt verbose_opt typeset errmsg arg_cou=$# while &#91; &#34;$#&#34; -gt 0 &#93; do &#60;strong&#62;case &#34;$1&#34; in&#60;/strong&#62; -@&#40;&#91;U&#93;&#41; &#41; &#160;let user_opt=user_opt+1 ;; -P &#160; &#160; &#160; &#41; &#160; &#160;let password_opt=password_opt+1 ; ask_pass=0 ;; -option1 &#160; &#160;&#41; &#160; &#160;;; -option2 &#160; &#160;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Quick and dirty <strong>parsing</strong> procedure for unix <strong>shell scripts</strong></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">parse_command_line <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
<span style="color: #7a0874; font-weight: bold;">typeset</span> <span style="color: #660033;">-i</span> user_opt help_opt password_opt verbose_opt<br />
<span style="color: #7a0874; font-weight: bold;">typeset</span> errmsg<br />
<br />
<span style="color: #007800;">arg_cou</span>=<span style="color: #007800;">$#</span><br />
<br />
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$#&quot;</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
<span style="color: #000000; font-weight: bold;">do</span><br />
<span style="color: #000000; font-weight: bold;">&lt;</span>strong<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span><span style="color: #000000; font-weight: bold;">&lt;/</span>strong<span style="color: #000000; font-weight: bold;">&gt;</span><br />
-<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>U<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp;<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">user_opt</span>=user_opt+<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #660033;">-P</span> &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">password_opt</span>=password_opt+<span style="color: #000000;">1</span> ; <span style="color: #007800;">ask_pass</span>=<span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #660033;">-option1</span> &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #660033;">-option2</span> &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #660033;">-option3</span> &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">;;</span><br />
-<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>hH?<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">help_opt</span>=<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">;;</span><br />
-v<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>erbose<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">verbose_opt</span>=verbose_opt+<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">if</span> &nbsp; <span style="color: #7a0874; font-weight: bold;">&#91;</span> user_opt <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">USER</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #007800;">user_opt</span>=<span style="color: #000000;">0</span><br />
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> password_opt <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">PASS</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #007800;">password_opt</span>=<span style="color: #000000;">0</span><br />
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> verbose_opt <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">verbose_level</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #007800;">verbose_opt</span>=<span style="color: #000000;">0</span><br />
<span style="color: #000000; font-weight: bold;">else</span><br />
<span style="color: #666666; font-style: italic;">### help_opt=1</span><br />
<span style="color: #007800;">CMD</span>=<span style="color: #007800;">$*</span> ; <span style="color: #7a0874; font-weight: bold;">break</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #000000; font-weight: bold;">;;</span><br />
<span style="color: #000000; font-weight: bold;">esac</span><br />
<span style="color: #7a0874; font-weight: bold;">shift</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> arg_cou <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">help_opt</span>=<span style="color: #000000;">1</span> ; <span style="color: #007800;">errmsg</span>=<span style="color: #ff0000;">&quot;Nothing to do&quot;</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #000000; font-weight: bold;">for</span> opt <span style="color: #000000; font-weight: bold;">in</span> user password ; <span style="color: #000000; font-weight: bold;">do</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #800000;">${opt}</span>_opt <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">help_opt</span>=<span style="color: #000000;">1</span> ; <span style="color: #007800;">errmsg</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${errmsg}</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #007800;">${opt}</span> is absent&quot;</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> verbose_opt <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #007800;">verbose_level</span>=<span style="color: #007800;">$verbose_opt</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> help_opt <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
<span style="color: #7a0874; font-weight: bold;">help</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$errmsg</span>&quot;</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></div>
<div style="height:33px; padding-top:2px; padding-bottom:2px; clear:both;" class="zare366"><div style="float:left; width:100px; " class="zare366_facebook_like"> 
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.memosoup.com%2F20090912%2Fparsing-script-parameters.html&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;height=27" 
					scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe>
			</div><div style="float:left; width:90px; padding-left:10px;" class="zare366_google1"> 
				<g:plusone size="medium" href="http://www.memosoup.com/20090912/parsing-script-parameters.html" ></g:plusone>
			</div><div style="float:left; width:110px; padding-left:10px;" class="zare366_twitter"> 
				<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
					data-text="Parsing script parameters" data-url="http://www.memosoup.com/20090912/parsing-script-parameters.html">Tweet</a> 
			</div></div>
		<div style="display:none;"><a href="http://www.news365live.com">news and informations</a>&nbsp;<a href="http://news365online.com">automotive,business,crime,health,life,politics,science,technology,travel</a><a href="http://worldnews365online.com">automotive,business,crime,health,life,politics,science,technology,travel</a></div><div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.memosoup.com/20090912/parsing-script-parameters.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

