{"id":41480,"date":"2025-12-05T11:19:04","date_gmt":"2025-12-05T10:19:04","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=41480"},"modified":"2025-12-05T11:19:06","modified_gmt":"2025-12-05T10:19:06","slug":"shell-and-perl-in-one-script","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/","title":{"rendered":"Shell and Perl in one script"},"content":{"rendered":"\n<p>To run a Perl script directly like a binary from within the shell, you have to <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>set the executable flag on the script<\/li>\n\n\n\n<li>Add the shebang line (the 1st line (<code>#!...<\/code>) specifiyng the interpreter to use)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env perl\n#!\/usr\/bin\/perl\n#!\/u01\/app\/oracle\/product\/19\/perl\/bin\/perl<\/code><\/pre>\n\n\n\n<p>the 2nd and 3rd example will run the script with the given Perl-binary. <\/p>\n\n\n\n<p>The 1st example uses the 1st perl binary found in $PATH<\/p>\n\n\n\n<p>If you work with Oracle and want to use a Perl script connecting to the database, then the Perl from the OS (\/usr\/bin\/perl) is usually not usable because the Perl-modules for Oracle (DBI \/ DBD::Oracle) are missing.<\/p>\n\n\n\n<p>A solution could be to use the line of the 3rd example, but then, the script is hardcoded to only that ORACLE_HOME.<\/p>\n\n\n\n<p>As a solution, often a shell-wrapper script  is used to call the perl-script with the correct perl binary (both scripts are in the same directory and have the same name, but different suffixes: .sh and .pl)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env bash\ncd $(dirname `command -v $0`)  # change to script-directory\n$ORACLE_HOME\/perl\/bin\/perl $(basename $0 .sh).pl<\/code><\/pre>\n\n\n\n<p>But is it also possible to integrate both scripts in one file? Yes, it is possible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env bash\necho \"hello shell\"\n# additional code, e.g. setting the env. for a specific ORACLE_SID\n\n# Hand off to the embedded Perl section in this file:\nexec $ORACLE_HOME\/perl\/bin\/perl -x \"$0\" \"$@\"\n\n#!perl\n# the line above is essential to detect the begin of Perl-code\n# Everything below this line is treated as Perl\nprint \"hello perl\\n\";\nuse DBD::Oracle;  #if there is an error, the wrong Perl is used<\/code><\/pre>\n\n\n\n<p>And the output is as expected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hello shell\nhello perl<\/code><\/pre>\n\n\n\n<p>Both &#8220;hello *&#8221; appear and no error that DBD::Oracle is not found, because we use Oracle&#8217;s Perl.<\/p>\n\n\n\n<p>And what happens, if we call this script with Perl? <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># perl shell1.sh\nhello shell\nhello perl<\/code><\/pre>\n\n\n\n<p>For me, an unexpected behavior. Perl does not complain about the shell code (unknown syntax), it executes it as shell code according the shebang-line (#!\/usr\/bin\/env bash).<\/p>\n\n\n\n<p>But how can we call the script as a real Perl-script, without executing the shell-Part? We can hide it with the Perl Old Documentation tags (=pod \/ =cut). The shell will complain and ignore the unknown POD line and continues with the next one. Perl will skip this block from parsing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=pod 2&gt;\/dev\/null\necho \"hello shell\"\nexec $ORACLE_HOME\/perl\/bin\/perl -x \"$0\" \"$@\"\n=cut\n\n#!perl\nprint \"hello perl\\n\";\nuse DBD::Oracle;<\/code><\/pre>\n\n\n\n<p>Little disadvantage: We can no longer use the shebang line for the shell. The script uses the shell from which it was invoked. To explicitly use a specific shell, we have to call the script with the shell: <code>bash script2.sh<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>perl script2.sh   # perl references to \/usr\/bin\/perl\nCan't locate DBD\/Oracle.pm in @INC (...<\/code><\/pre>\n\n\n\n<p>OK, the script is really executed with the given Perl (which does not contain DBD::Oracle). Now, try it with the Oracle Perl.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ORACLE_HOME\/perl\/bin\/perl scrip2.sh\nhello perl<\/code><\/pre>\n\n\n\n<p>And success! Perl skips the shell-code and only runs the Perl-code.<\/p>\n\n\n\n<p>So, we have a script that can be used as a &#8220;normal&#8221; Perl-Script, and as a wrapper-script without maintaining 2 separate scripts.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To run a Perl script directly like a binary from within the shell, you have to the 2nd and 3rd example will run the script with the given Perl-binary. The 1st example uses the 1st perl binary found in $PATH If you work with Oracle and want to use a Perl script connecting to the [&hellip;]<\/p>\n","protected":false},"author":123,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,198,1,42,59],"tags":[1075,73,96,47,965,1156,3760,306],"type_dbi":[],"class_list":["post-41480","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-database-management","category-non-classifiee","category-operating-systems","category-oracle","tag-bash","tag-linux","tag-oracle","tag-perl","tag-script","tag-shell","tag-unix","tag-workshop"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Shell and Perl in one script - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Shell and Perl in one script\" \/>\n<meta property=\"og:description\" content=\"To run a Perl script directly like a binary from within the shell, you have to the 2nd and 3rd example will run the script with the given Perl-binary. The 1st example uses the 1st perl binary found in $PATH If you work with Oracle and want to use a Perl script connecting to the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-12-05T10:19:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-05T10:19:06+00:00\" \/>\n<meta name=\"author\" content=\"Martin Bracher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Martin Bracher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\"},\"author\":{\"name\":\"Martin Bracher\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\"},\"headline\":\"Shell and Perl in one script\",\"datePublished\":\"2025-12-05T10:19:04+00:00\",\"dateModified\":\"2025-12-05T10:19:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\"},\"wordCount\":384,\"commentCount\":0,\"keywords\":[\"Bash\",\"Linux\",\"Oracle\",\"Perl\",\"script\",\"Shell\",\"Unix\",\"Workshop\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"Database management\",\"Non classifi\u00e9(e)\",\"Operating systems\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\",\"name\":\"Shell and Perl in one script - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2025-12-05T10:19:04+00:00\",\"dateModified\":\"2025-12-05T10:19:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Shell and Perl in one script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\",\"name\":\"Martin Bracher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"caption\":\"Martin Bracher\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/martinbracher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Shell and Perl in one script - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/","og_locale":"en_US","og_type":"article","og_title":"Shell and Perl in one script","og_description":"To run a Perl script directly like a binary from within the shell, you have to the 2nd and 3rd example will run the script with the given Perl-binary. The 1st example uses the 1st perl binary found in $PATH If you work with Oracle and want to use a Perl script connecting to the [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/","og_site_name":"dbi Blog","article_published_time":"2025-12-05T10:19:04+00:00","article_modified_time":"2025-12-05T10:19:06+00:00","author":"Martin Bracher","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Martin Bracher","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/"},"author":{"name":"Martin Bracher","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e"},"headline":"Shell and Perl in one script","datePublished":"2025-12-05T10:19:04+00:00","dateModified":"2025-12-05T10:19:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/"},"wordCount":384,"commentCount":0,"keywords":["Bash","Linux","Oracle","Perl","script","Shell","Unix","Workshop"],"articleSection":["Database Administration &amp; Monitoring","Database management","Non classifi\u00e9(e)","Operating systems","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/","url":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/","name":"Shell and Perl in one script - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2025-12-05T10:19:04+00:00","dateModified":"2025-12-05T10:19:06+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/shell-and-perl-in-one-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Shell and Perl in one script"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e","name":"Martin Bracher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","caption":"Martin Bracher"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/martinbracher\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/41480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/123"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=41480"}],"version-history":[{"count":4,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/41480\/revisions"}],"predecessor-version":[{"id":41759,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/41480\/revisions\/41759"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=41480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=41480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=41480"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=41480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}