<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Parsing parameters in a BASH shell script</title>
	<atom:link href="http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/</link>
	<description>Open Source Support and Consultancy</description>
	<lastBuildDate>Wed, 26 May 2010 16:38:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Christopher Lewis</title>
		<link>http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/comment-page-1/#comment-27</link>
		<dc:creator>Christopher Lewis</dc:creator>
		<pubDate>Wed, 26 May 2010 16:38:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.opensourcery.co.uk/?p=40#comment-27</guid>
		<description>Here are some changes to the parsing functions:

1) allow only parms that are defined in PARM_ARRAY
2) Syntax function to print out the function commands.

#Syntax of script
syntax() {
  #redirect STDOUT to STDERR so that this can be called via another script
  t=&quot;&quot;
  echo &quot;`basename $0` - Script to xxx1&gt;&amp;2
  echo 1&gt;&amp;2
  echo &quot;`basename $0`  --Parm1= --Parm2= [--Parm3] [--Debug]&quot;1&gt;&amp;2
  echo 1&gt;&amp;2
  if [ -n &quot;$1&quot; ] ; then
    echo &quot;  $1&quot;  1&gt;&amp;2
  fi
  exit 0
}

#Function to verify that only paramters in PARM_ARRAY are passed
PARM_ARRAY=(PARM1 PARM2 PARM3 DEBUG HELP)
function ParmExists() {
  if [ -z &quot;$1&quot; ]; then 
    return
  fi
  for i in ${PARM_ARRAY[@]}; do
    if [ $i == $1 ]; then
      return 1
    fi
  done
  return 0
}

#FROM http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/
# all parms are in the form _PARM
# Parms not starting with &#039;--&#039; result in error
# NOTE: PARM_ARRAY + ParmExists are used to prevent extraneous parms i.e. --NotAParm=Anything
until [[ ! &quot;$*&quot; ]]; do
  if [[ ${1:0:2} = &#039;--&#039; ]]; then
    PAIR=${1:2}
    PARAMETER=`echo ${PAIR%=*} &#124; tr [:lower:] [:upper:]`
    ParmExists $PARAMETER
    retval=$?
    if [ $retval -eq 0 ] ; then
      syntax &quot;Unknown parameter $1&quot;
    fi
    eval _$PARAMETER=&#039;${PAIR##*=}&#039;
  else 
    syntax &quot;Invalid parameter $1&quot;
  fi
  shift
done

#Show Help
if [ -n &quot;$_HELP&quot; ] ; then
  syntax
fi</description>
		<content:encoded><![CDATA[<p>Here are some changes to the parsing functions:</p>
<p>1) allow only parms that are defined in PARM_ARRAY<br />
2) Syntax function to print out the function commands.</p>
<p>#Syntax of script<br />
syntax() {<br />
  #redirect STDOUT to STDERR so that this can be called via another script<br />
  t=&#8221;"<br />
  echo &#8220;`basename $0` &#8211; Script to xxx1&gt;&amp;2<br />
  echo 1&gt;&amp;2<br />
  echo &#8220;`basename $0`  &#8211;Parm1= &#8211;Parm2= [--Parm3] [--Debug]&#8220;1&gt;&amp;2<br />
  echo 1&gt;&amp;2<br />
  if [ -n "$1" ] ; then<br />
    echo &#8221;  $1&#8243;  1&gt;&amp;2<br />
  fi<br />
  exit 0<br />
}</p>
<p>#Function to verify that only paramters in PARM_ARRAY are passed<br />
PARM_ARRAY=(PARM1 PARM2 PARM3 DEBUG HELP)<br />
function ParmExists() {<br />
  if [ -z "$1" ]; then<br />
    return<br />
  fi<br />
  for i in ${PARM_ARRAY[@]}; do<br />
    if [ $i == $1 ]; then<br />
      return 1<br />
    fi<br />
  done<br />
  return 0<br />
}</p>
<p>#FROM <a href="http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/" rel="nofollow">http://www.opensourcery.co.uk/2008/04/parsing-parameters-bash-shell-script/</a><br />
# all parms are in the form _PARM<br />
# Parms not starting with &#8216;&#8211;&#8217; result in error<br />
# NOTE: PARM_ARRAY + ParmExists are used to prevent extraneous parms i.e. &#8211;NotAParm=Anything<br />
until [[ ! "$*" ]]; do<br />
  if [[ ${1:0:2} = '--' ]]; then<br />
    PAIR=${1:2}<br />
    PARAMETER=`echo ${PAIR%=*} | tr [:lower:] [:upper:]`<br />
    ParmExists $PARAMETER<br />
    retval=$?<br />
    if [ $retval -eq 0 ] ; then<br />
      syntax &#8220;Unknown parameter $1&#8243;<br />
    fi<br />
    eval _$PARAMETER=&#8217;${PAIR##*=}&#8217;<br />
  else<br />
    syntax &#8220;Invalid parameter $1&#8243;<br />
  fi<br />
  shift<br />
done</p>
<p>#Show Help<br />
if [ -n "$_HELP" ] ; then<br />
  syntax<br />
fi</p>
]]></content:encoded>
	</item>
</channel>
</rss>

