{"id":971,"date":"2016-07-04T22:40:51","date_gmt":"2016-07-04T22:40:51","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=971"},"modified":"2016-07-04T22:40:51","modified_gmt":"2016-07-04T22:40:51","slug":"command-line-fun-spaces-and-arguments","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/07\/04\/command-line-fun-spaces-and-arguments\/","title":{"rendered":"command line fun &#8211; spaces and arguments"},"content":{"rendered":"<p>I normally get to control most aspects of the program. \u00a0The most important yet trivial is the parameters that are being passed to the program. \u00a0Because I know what I want it is usually possible to coerce the calling script into giving me what I need.<\/p>\n<p>Unfortunately there are always exceptions. \u00a0The most common problem is with dealing with normal users from the Windows world. \u00a0There are a lot of characters that they have no compunction about putting into a filename with the most common being the space.<\/p>\n<p>If I am expecting a lot of files (and no spaces) then this extra space or six really can create some havoc. I would normally have a simple loop to iterate through my list.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nPARMS=$*\r\n\r\nfor item in $PARMS\r\ndo\r\n  echo $item\r\n  echo \"(some processing goes here)\"\r\n  echo \" \"\r\ndone\r\n<\/code><\/pre>\n<\/div>\n<p>Works great with commands that looks like any of the following<\/p>\n<ul>\n<li>filename=receivables.txt<\/li>\n<li>receivables.txt<\/li>\n<li>-name=receivables.txt<\/li>\n<\/ul>\n<p>When the space is introduced into the parameter value then the value gets split up with this type of processing.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; .\/parser.sh filename=receivables.txt receivables.txt -name=receivables.txt \"account receivables.txt\"\r\nfilename=receivables.txt\r\n(some processing goes here)\r\n \r\nreceivables.txt\r\n(some processing goes here)\r\n \r\n-name=receivables.txt\r\n(some processing goes here)\r\n \r\n<strong>account\r\n(some processing goes here)\r\n \r\nreceivables.txt\r\n(some processing goes here)\r\n\r\n<\/strong><\/code><\/pre>\n<\/div>\n<p>The bash shell actually does know that there are only four parameters that were passed, the problem is with this rather simplistic method of parsing. \u00a0Simply accessing the parameters directly will actually return the exact parameter including any spaces or special characters.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nfilename1=$1\r\nfilename2=$2\r\nfilename3=$3\r\nfilename4=$4\r\nfilename11=$11\r\n\r\necho filename1 $filename1\r\necho filename2 $filename2\r\necho filename3 $filename3\r\necho filename4 $filename4\r\necho filename11 $filename11\r\n<\/code><\/pre>\n<\/div>\n<p>It does work yet in a inflexible manner, besides this will work for the parameters up to 9 but not beyond that. \u00a0The easiest way would be to take the first parameter from the command line, process\u00a0the file\u00a0and then move to the next file. \u00a0The bash shell allows us to do that using the shift command.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nPARMS=$*\r\n\r\nwhile [ $# -gt 0 ]\r\ndo\r\n  filename=$1\r\n  shift\r\n  echo filename is \"'$filename'\"\r\n  echo \"(some processing goes here)\"\r\n  echo \" \"\r\ndone\r\n<\/code><\/pre>\n<\/div>\n<p>In this way we simply continue taking arguments until there are no arguments left to take.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nDEBUG=0\r\n\r\nidx=0\r\npresident=false\r\n\r\nwhile [ $# -gt 0 ]\r\ndo\r\n   item=$1\r\n   if [ $DEBUG -ne 0 ]\r\n   then\r\n      echo ${idx}. $item\r\n   fi\r\n\r\n   VARIABLE=`echo $item | nawk -F=  '{print ($1)}'`\r\n   VALUE=`echo $item | nawk -F=  '{print ($2)}'`\r\n\r\n   case $VARIABLE in\r\n\r\n      -president) president=true;;\r\n      first) firstname=$VALUE;;\r\n      last)  lastname=$VALUE;;\r\n      addr)  address=$VALUE;;\r\n      city)  city=$VALUE;;\r\n      state)  state=$VALUE;;\r\n      zip)  zipcode=$VALUE;;\r\n\r\n      *) echo \"unknown parm set ($VARIABLE\/$VALUE)\";;\r\n\r\n   esac\r\n   idx=$((idx + 1))\r\n   shift\r\ndone\r\n\r\necho User Info\r\nif [ $president == \"true\" ]\r\nthen\r\n  echo President of the United States\r\nfi\r\necho $firstname $lastname\r\necho $address\r\necho $city $state $zipcode\r\n\r\n<\/code><\/pre>\n<\/div>\n<p>This method of parsing out the parameters supports both parameter=value as well as the more classic parameter preceded with a dash followed by a value.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; .\/parser.sh first=bob last=johnson addr=\"123 main street\" -president city=smallville state=IL zip=12321\r\nUser Info\r\nPresident of the United States\r\nbob johnson\r\n123 main street\r\nsmallville IL 12321\r\n&gt;\r\n<\/code><\/pre>\n<\/div>\n<p>To support the more classic method of parameter passing would require a few small changes, but that is left as an example to the reader.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I normally get to control most aspects of the program. \u00a0The most important yet trivial is the parameters that are being passed to the program. \u00a0Because I know what I want it is usually possible to coerce the calling script &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/07\/04\/command-line-fun-spaces-and-arguments\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/971"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=971"}],"version-history":[{"count":3,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/971\/revisions"}],"predecessor-version":[{"id":974,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/971\/revisions\/974"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}