<?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>Subjectively &#187; bash shell getopts positional parameters eval</title>
	<atom:link href="http://kirk.webfinish.com/tag/bash-shell-getopts-positional-parameters-eval/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirk.webfinish.com</link>
	<description>dd if=/dev/random &#124; kirk &#62; blog</description>
	<lastBuildDate>Wed, 04 Jan 2012 15:48:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<atom:link rel='hub' href='http://kirk.webfinish.com/?pushpress=hub'/>
		<item>
		<title>bash shell script to use getopts with gnu style long positional parameters</title>
		<link>http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/</link>
		<comments>http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 00:55:10 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[bash shell getopts positional parameters eval]]></category>

		<guid isPermaLink="false">http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/</guid>
		<description><![CDATA[Problem: You want to support &#8211;really-long-option-names in your bash script and getopts won&#8217;t do it. More problems: You don&#8217;t want to use getopt because you want cross platform support or your platform isn&#8217;t supported. More problems than that: I can&#8217;t help with existential issues. Solution: Rewrite the long options into short options and then use [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: You want to support &#8211;really-long-option-names in your bash script and getopts won&#8217;t do it.<br />
More problems: You don&#8217;t want to use getopt because you want cross platform support or your platform isn&#8217;t supported.<br />
More problems than that: I can&#8217;t help with existential issues.<br />
Solution:  Rewrite the long options into short options and then use getopts.</p>
<p>./args.sh -b -c -a &#8220;yada yada&#8221; &#8211;some-other-gnu &#8211;long-gnu-option</p>
<p>content of args.sh:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">for</span> arg
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">delim</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$arg</span>&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
    <span style="color: #666666; font-style: italic;">#translate --gnu-long-options to -g (short options)</span>
        --some-other-gnu<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">args</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${args}</span>-g &quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
       --long-gnu-option<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">args</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${args}</span>-l &quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
       <span style="color: #666666; font-style: italic;">#pass through anything else</span>
       <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${arg:0:1}</span>&quot;</span> == <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">delim</span>=<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
           <span style="color: #007800;">args</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${args}</span><span style="color: #007800;">${delim}</span><span style="color: #007800;">${arg}</span><span style="color: #007800;">${delim}</span> &quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Reset the positional parameters to the short options</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$args</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">getopts</span> <span style="color: #ff0000;">&quot;:a:gl&quot;</span> option <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$option</span> <span style="color: #000000; font-weight: bold;">in</span>
        g<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;some other gnu option&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
        l<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;long gnu option&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
        a<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${OPTARG[@]}</span><span style="color: #000000; font-weight: bold;">;;</span>
        <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$OPTARG</span> is an unrecognized option<span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Output:</p>
<p>b is an unrecognized option<br />
c is an unrecognized option<br />
yada yada<br />
some other gnu option<br />
long gnu option</p>
]]></content:encoded>
			<wfw:commentRss>http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

