{"id":860,"date":"2016-04-13T19:26:01","date_gmt":"2016-04-13T19:26:01","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=860"},"modified":"2016-04-13T19:26:01","modified_gmt":"2016-04-13T19:26:01","slug":"command-line-fun-testing-for-existence","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/04\/13\/command-line-fun-testing-for-existence\/","title":{"rendered":"command line fun &#8211; testing for existence"},"content":{"rendered":"<p>I remember quite some time ago when I was developing a an application when the personal computer was actually fairly slow. \u00a0Of course I happened to pick a technology that worked just fine on a newer computer but it actually ran quite pokey on my machine.<\/p>\n<p>The decision was made to never develop on a machine less than or equal to the target platform. \u00a0Fast forward a few years to discover that I periodically get stuck with the slow development environment and there is nothing that I can do about it.<\/p>\n<p>Well, I can&#8217;t speed up the machine but I can make my programs and scripts a bit smarter. \u00a0The target of my focus was a small script that called some horribly inefficient java program. \u00a0Not all the programs dawdled, just this one.<\/p>\n<p>The problem was the script called this program even though in most cases the input directory was empty &#8211; not a problem on production but really slow on development.<\/p>\n<p>The input directory can hold quite a few data files and the names are never the same. \u00a0I ran through a few different ideas.<\/p>\n<h2>Idea #1<\/h2>\n<p>Well, it seems to be a pretty strait forward solution. \u00a0Get a count of the number of files and then you will know how many things there are to process and thus if there is anything to process.<\/p>\n<p>It almost makes sense until you try this. \u00a0It works just great when data does exist but when no data exists then it doesn&#8217;t work very well.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nEXT=xml\r\nINPUT=\/data\/input\r\n\r\nCNT=`ls -1 $INPUT\/*$EXT | wc -l`\r\nif [ $CNT -gt 0 ]\r\nthen\r\n echo $CNT items\r\nelse\r\n echo \"files don't exist\"\r\nfi\r\n<\/code><\/pre>\n<\/div>\n<p>The count variable is set to zero but the the shell informs us that no data exists.<\/p>\n<p><strong><em>ls: cannot access \/data\/input\/*xml: No such file or directory<\/em><\/strong><\/p>\n<p>Not a serious issue but depending on the person very distracting.<\/p>\n<p>&nbsp;<\/p>\n<h2>Idea #2<\/h2>\n<p>This error message can be easily gotten rid of. \u00a0Simply redirect the error output to a file or to \/dev\/null.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nEXT=xml\r\nINPUT=\/data\/input\r\n\r\nCNT=`ls -1 $INPUT\/*$EXT 2&gt;\/dev\/null| wc -l`\r\nif [ $CNT -gt 0 ]\r\nthen\r\n echo $CNT items\r\nelse\r\n echo \"files don't exist\"\r\nfi\r\n<\/code><\/pre>\n<\/div>\n<p>Not a bad idea if you know that the directory permissions for the input directory will always be OK. \u00a0If not, then you will be (possibly) throwing away the only help to diagnosing an odd behavior.<\/p>\n<h2>testing\u00a0existence\u00a0&#8211; Extra credit edition<\/h2>\n<p>This second solution is quite acceptable and unless it is called a lot it shouldn&#8217;t be too much overhead. \u00a0Yet there is a much easier solution that is probably even more efficient.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\nEXT=xml\r\nINPUT=\/data\/input\r\n\r\nls $INPUT\/*$EXT &gt;\/dev\/null 2&gt;&amp;1  \r\nif [ $? -eq 0 ]\r\nthen\r\n echo $EXT files exist\r\nelse\r\n echo \"$EXT files don't exist \"\r\nfi\r\n<\/code><\/pre>\n<\/div>\n<p>No, you don&#8217;t know the number of files, but you don&#8217;t have to create a pipe and fill it with a few (or many) files just to decide whether or not to run a program. \u00a0Simply look at the Unix return code from our last command, if zero, then we know our files exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I remember quite some time ago when I was developing a an application when the personal computer was actually fairly slow. \u00a0Of course I happened to pick a technology that worked just fine on a newer computer but it actually &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/04\/13\/command-line-fun-testing-for-existence\/\">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,20],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/860"}],"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=860"}],"version-history":[{"count":4,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/860\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/860\/revisions\/864"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}