{"id":625,"date":"2015-02-28T12:25:11","date_gmt":"2015-02-28T17:25:11","guid":{"rendered":"http:\/\/www.brunerd.com\/blog\/?p=625"},"modified":"2015-02-28T12:25:11","modified_gmt":"2015-02-28T17:25:11","slug":"bash-parameter-fun","status":"publish","type":"post","link":"https:\/\/www.brunerd.com\/blog\/2015\/02\/28\/bash-parameter-fun\/","title":{"rendered":"BASH parameter fun"},"content":{"rendered":"<p>Some\u00a0exercises in\u00a0handling parameters in a script:<\/p>\n<h2>Using BASH_ARGV to loop through all arguments<\/h2>\n<p><code>#!\/bin\/bash<br \/>\n#debugging output<br \/>\n[ -f \/tmp\/debug ] &amp;&amp; set -x<\/code><\/p>\n<p>#Loop Though the arguments provided<br \/>\n#BASH places arguments in reverse order in BASH_ARGV array<br \/>\n#Start at end of array (ARGC &#8211; 1)<br \/>\nfor (( i=$(( $BASH_ARGC &#8211; 1 )); i&gt;=0; i&#8211; )); do<br \/>\nitem=&#8221;${BASH_ARGV[$i]}&#8221;<br \/>\necho &#8220;\\$$(( $BASH_ARGC &#8211; $i )): $item&#8221;<br \/>\ndone<\/p>\n<h2>Loop through all arguments with special handling of first and last only<\/h2>\n<p><code>#!\/bin\/bash<br \/>\n[ -f \/tmp\/debug ] &amp;&amp; set -x<br \/>\n##loop through parameters, start with end of BASH_ARGV<br \/>\nfor (( loopNumber=1, i=$(( $BASH_ARGC - 1 )); i&gt;=0; i--, loopNumber++ )); do<br \/>\n#do something special for first and last<br \/>\nif [ $i -eq $(( $BASH_ARGC - 1 )) ]; then<br \/>\nconditionalString=\"(I am the first one!)\"<br \/>\nelif [ $i -eq 0 ]; then<br \/>\nconditionalString=\"(I am the last one!)\"<br \/>\nelse<br \/>\nunset conditionalString<br \/>\nfi<br \/>\necho Arg $loopNumber: ${BASH_ARGV[$i]} $conditionalString<br \/>\ndone<\/code><\/p>\n<h2>Loop through all arguments with special handling of first and everything else<\/h2>\n<p><code>#!\/bin\/bash<br \/>\n[ -f \/tmp\/debug ] &amp;&amp; set -x<\/code><\/p>\n<p>##loop through parameters, start with end of BASH_ARGV<br \/>\nfor (( loopNumber=1, i=$(( $BASH_ARGC &#8211; 1 )); i&gt;=0; i&#8211;, loopNumber++ )); do<\/p>\n<p>#if not the last don&#8217;t restart dock<br \/>\nif [ $i -eq $(( $BASH_ARGC &#8211; 1 )) ]; then<br \/>\nunset conditionalString<br \/>\nconditionalString=&#8221;(I am the first)&#8221;<br \/>\nelse<br \/>\nconditionalString=&#8221;(I am not the first)&#8221;<br \/>\nfi<\/p>\n<p>echo Arg $loopNumber: ${BASH_ARGV[$i]} $conditionalString<br \/>\ndone<\/p>\n<h2>Loop through all arguments with special handling of the last and everything else<\/h2>\n<p><code>#!\/bin\/bash<br \/>\n[ -f \/tmp\/debug ] &amp;&amp; set -x<\/code><\/p>\n<p>##loop through parameters, start with end of BASH_ARGV<br \/>\nfor (( loopNumber=1, i=$(( $BASH_ARGC &#8211; 1 )); i&gt;=0; i&#8211;, loopNumber++ )); do<\/p>\n<p>#if not the last don&#8217;t restart dock<br \/>\nif [ ! $i -eq 0 ]; then<br \/>\nconditionalString=&#8221;(I am not the last)&#8221;<br \/>\nelse<br \/>\nunset conditionalString<br \/>\n#conditionalString=&#8221;(I am the last)&#8221;<br \/>\nfi<\/p>\n<p>echo Arg $loopNumber: ${BASH_ARGV[$i]} $conditionalString<br \/>\ndone<\/p>\n<p>(WordPress is bugging the heck out of me with the code tags breaking when their are line breaks&#8230; sorry, that&#8217;s an exercise for the Googler)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some\u00a0exercises in\u00a0handling parameters in a script: Using BASH_ARGV to loop through all arguments #!\/bin\/bash #debugging output [ -f \/tmp\/debug ] &amp;&amp; set -x #Loop Though the arguments provided #BASH places arguments in reverse order in BASH_ARGV array #Start at end of array (ARGC &#8211; 1) for (( i=$(( $BASH_ARGC &#8211; 1 )); i&gt;=0; i&#8211; )); [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-os-x","category-scripting"],"_links":{"self":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/comments?post=625"}],"version-history":[{"count":5,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}