{"id":5769,"date":"2019-11-27T12:51:27","date_gmt":"2019-11-27T17:51:27","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5769"},"modified":"2019-11-27T12:51:27","modified_gmt":"2019-11-27T17:51:27","slug":"shell-script-path-to-script-2","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5769","title":{"rendered":"Shell Script: Path To Script"},"content":{"rendered":"<p>We occasionally have to re-home our shell scripts, which means updating any static path values used within scripts. It&#8217;s quick enough to build a sed script to convert \/old\/server\/path to \/new\/server\/path, but it&#8217;s still extra work.<\/p>\n<p>The dirname command works to provide a dynamic path value, provided you use the fully qualified path to run the script &#8230; but it fails spectacularly whens someone runs .\/scriptFile.sh and you&#8217;re trying to use that path in, say, EXTRA_JAVA_OPTS. The &#8220;path&#8221; is just . &#8212; and Java doesn&#8217;t have any idea what to do with &#8220;-Xbootclasspath\/a:.\/more\/path\/goes\/here.jar&#8221;<\/p>\n<p>Voila, <em>realpath<\/em> gives you the fully qualified file path for \/new\/server\/path\/scriptFile.sh, .\/scriptFile.sh, or even bash scriptFile.sh &#8230; and the dirname of a realpath is the fully qualified path where scriptFile.sh resides:<\/p>\n<p style=\"min-height: 8pt; padding: 0px;\">\n<pre>#!\/bin\/bash \r\nDIRNAME=`dirname $(realpath \"$0\")`\r\necho ${DIRNAME}<\/pre>\n<p>Hopefully next time we&#8217;ve got to re-home our batch jobs, it will be a simple scp &amp; sed the old crontab content to use the new paths.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We occasionally have to re-home our shell scripts, which means updating any static path values used within scripts. It&#8217;s quick enough to build a sed script to convert \/old\/server\/path to \/new\/server\/path, but it&#8217;s still extra work. The dirname command works to provide a dynamic path value, provided you use the fully qualified path to run &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,30],"tags":[632,634],"class_list":["post-5769","post","type-post","status-publish","format-standard","hentry","category-coding","category-system-administration","tag-bash","tag-shell-script"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5769"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5769\/revisions"}],"predecessor-version":[{"id":5770,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5769\/revisions\/5770"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}