{"id":810,"date":"2016-06-01T23:08:46","date_gmt":"2016-06-01T23:08:46","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=810"},"modified":"2016-06-01T23:09:15","modified_gmt":"2016-06-01T23:09:15","slug":"command-line-fun-getting-to-the-base-of-things","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/01\/command-line-fun-getting-to-the-base-of-things\/","title":{"rendered":"command line fun &#8211; getting to the base of things"},"content":{"rendered":"<p>Just a short time back I received a mail from a colleague with an error message. \u00a0The question from him was about the error and more importantly was this my program. \u00a0I did find some code that generated the same message but it wasn&#8217;t installed. \u00a0Well, it appeared to be installed but it wasn&#8217;t being run.<\/p>\n<p>I was really in the middle of another issue\u00a0so I asked my colleague to\u00a0take a closer\u00a0look into it. \u00a0She actually found a slightly modified version of the code and it wasn&#8217;t installed in the normal location.\u00a0She didn&#8217;t think that this was being run either as no log file was placed into the log directory.<\/p>\n<p>Too clever by far. \u00a0The developer did a few small changes using the <em><strong>basename<\/strong><\/em> and <em><strong>dirname<\/strong><\/em> commands and leaving almost everything else the same.<\/p>\n<p>These commands are essentially a ying and yang to each other. \u00a0The basename command will strip off the path and return only the command, while the dirname command will remove the program and return the path.<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>command<\/th>\n<th>argument<\/th>\n<th>result<\/th>\n<\/tr>\n<tr>\n<td>basename<\/td>\n<td>\/home\/bob\/listing.sh<\/td>\n<td>listing.sh<\/td>\n<\/tr>\n<tr>\n<td>dirname<\/td>\n<td>\/home\/bob\/listing.sh<\/td>\n<td>\/home\/bob<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Changed both the input and output directories relative to the installation directory. \u00a0Using either basename or dirname is a rather exotic way of choosing where to get input or place output. \u00a0A much simpler choice would be to use the current working directory.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\nLOGFILE=`pwd`\/logfile.txt\r\necho $((5 + 6)) &gt;$LOGFILE<\/code><\/pre>\n<\/div>\n<p>Yet this won&#8217;t work unless you run it from the installation directory. \u00a0If you run this script from another directory, the output will end up there. \u00a0The same general fate will occur if you try and run the script from the crontab.<\/p>\n<p>When running scripts from the crontab the script is run from the root directory. \u00a0So this script would probably not work as the output cannot be written to the root. \u00a0Thus the clever solution is the one that is necessary.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\nLOGFILE=`dirname $0`\/logfile.txt\r\necho $((5 + 6)) &gt;$LOGFILE<\/code><\/pre>\n<\/div>\n<p>Using the dirname command on the name of the script that is being run will then actually be the same effect as having a variable for the installation directory.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a short time back I received a mail from a colleague with an error message. \u00a0The question from him was about the error and more importantly was this my program. \u00a0I did find some code that generated the same &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/01\/command-line-fun-getting-to-the-base-of-things\/\">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\/810"}],"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=810"}],"version-history":[{"count":3,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/810\/revisions"}],"predecessor-version":[{"id":837,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/810\/revisions\/837"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}