{"id":1032,"date":"2015-06-05T16:28:35","date_gmt":"2015-06-05T14:28:35","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?p=1032"},"modified":"2020-08-18T16:34:13","modified_gmt":"2020-08-18T14:34:13","slug":"smart-bash-prompt","status":"publish","type":"post","link":"https:\/\/www.ludovicocaldara.net\/dba\/smart-bash-prompt\/","title":{"rendered":"Smart Bash Prompt for Oracle"},"content":{"rendered":"<p>If you are an Oracle customer who has several database versions running, you have to deal with scripts that become more and more complex to maintain. Depending on the version or the edition of your database, you may want to run different pieces of code. This forces you to get programmatically more information about your database version and edition (e.g., in order to run a statspack or AWR report if your software is either Enterprise or Standard).<br \/>\nThe most common way to get information about the software is connecting to the database and getting it through a couple of selects. But what if you don\u2019t have any running databases?<br \/>\nThe ORACLE_HOME inventory has such information, and you can get it with a short shell function:<\/p>\n<pre class=\"lang:sh decode:true\">function ohversion ()\r\n{\r\n    ORACLE_VERSION=`grep \"&lt;PATCH NAME=\\\"oracle.server\\\"\" $ORACLE_HOME\/inventory\/ContentsXML\/comps.xml 2&gt;\/dev\/null | tr ' ' '\\n' | grep ^VER= | awk -F\\\" '{print $2}'`;\r\n    if [ -z \"$ORACLE_VERSION\" ]; then\r\n        ORACLE_VERSION=`grep \"&lt;COMP NAME=\\\"oracle.server\\\"\" $ORACLE_HOME\/inventory\/ContentsXML\/comps.xml 2&gt;\/dev\/null | tr ' ' '\\n' | grep ^VER= | awk -F\\\" '{print $2}'`;\r\n    fi;\r\n    if [ -z \"$ORACLE_VERSION\" ]; then\r\n        echo \"OH not set\";\r\n    fi;\r\n    ORACLE_MAJOR=`echo $ORACLE_VERSION |  cut -d . -f 1`;\r\n    case $ORACLE_MAJOR in\r\n        11 | 12)\r\n            EDITION=`grep \"oracle_install_db_InstallType\" $ORACLE_HOME\/inventory\/globalvariables\/oracle.server\/globalvariables.xml 2&gt;\/dev\/null | tr ' ' '\\n' | grep VALUE | awk -F\\\" '{print $2}'`\r\n        ;;\r\n        10)\r\n            EDITION=`grep \"s_serverInstallType\" $ORACLE_HOME\/inventory\/Components21\/oracle.server\/*\/context.xml 2&gt;\/dev\/null | tr ' ' '\\n' | grep VALUE | awk -F\\\" '{print $2}'`\r\n        ;;\r\n        *)\r\n\r\n        ;;\r\n    esac;\r\n    export ORACLE_VERSION EDITION;\r\n    echo $ORACLE_VERSION $EDITION\r\n}<\/pre>\n<p>The snippet searches for a patchset entry in comps.xml to get the patch version rather than the base version (for releases prior to 11gR2 where out-of-place patching occurs). If a patchset cannot be found, it looks for the base version. Depending on the major release, the information about the edition is either in globalvariables.xml (11g, 12c) or in context.xml (10g).<br \/>\nWhen you call this \u201cohversion\u201d function, you get both the Oracle version and the edition of your current ORACLE_HOME.<br \/>\nIf you\u2019re using the bash as user shell, you may want to take one step forward and\u00a0 include this information in a much fancier bash prompt than the prompt by default:<\/p>\n<pre class=\"lang:sh decode:true  \">function ora_prompt ()\r\n{\r\n    PSERR=$?;\r\n    colylw='\\033[0;33m';\r\n    colcyn='\\033[0;36m';\r\n    colured='\\033[4;31m';\r\n    colugrn='\\033[4;32m';\r\n    coluylw='\\033[4;33m';\r\n    colrst='\\033[0m';\r\n    PS1=\"\\n# [ \\u@\\h:${colcyn}$PWD${colrst} [\\\\t] [${colylw}\\$(ohversion) SID=${coluylw}${ORACLE_SID:-\\\"not set\\\"}${colrst}] \\$( if [[ \\$PSERR -eq 0 ]]; then echo \\\"${colugrn}0${colrst}\\\" ; else echo \\\"${colured}\\$PSERR${colrst}\\\";fi) ] #\\\\n# \"\r\n}\r\n\r\nexport PROMPT_COMMAND=ora_prompt<\/pre>\n<p><a href=\"https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1033\" src=\"https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i.png\" alt=\"2015_06_05_16_22_56_sso0419i\" width=\"1020\" height=\"176\" srcset=\"https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i.png 1020w, https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i-300x52.png 300w, https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i-500x86.png 500w, https:\/\/www.ludovicocaldara.net\/dba\/wp-content\/uploads\/2015\/06\/2015_06_05_16_22_56_sso0419i-900x155.png 900w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><\/a>Although this prompt may seem long, it has several advantages that save you a lot of typing:<br \/>\n\u2022 The newline character inside the prompt let\u2019s you start typing commands on an almost empty line so you don\u2019t have to worry about how long your command is.<br \/>\n\u2022 The full username@host:path can be copied and pasted quickly for scp commands.<br \/>\n\u2022 The time inside the square brackets is helpful to track timings.<br \/>\n\u2022 The indication of the current environment (version, edition, SID) lets you know which environment you\u2019re working on.<br \/>\n\u2022 The leading number is the exit code of the last command ($?). It\u2019s green when the exit code is zero and red for all other exit codes.<br \/>\n\u2022 Hash characters before and after the prompt mitigate the risk of copying and pasting the wrong line by mistake inside your session.<\/p>\n<p>Note: this post originally appeared on IOUG Tips &amp; Best Practices Booklet 9th edition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are an Oracle customer who has several database versions running, you have to deal with scripts that become more and more complex to maintain. Depending on the version or the edition of your database, you may want to &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/smart-bash-prompt\/\">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":{"footnotes":""},"categories":[5,326,3,330,132],"tags":[26,227,285,9,136,27,226],"class_list":["post-1032","post","type-post","status-publish","format-standard","hentry","category-linux","category-oracle","category-oracledb","category-oracle-inst-upg","category-triblog","tag-bash","tag-edition","tag-linux","tag-oracle","tag-oracle_home","tag-prompt","tag-version"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=1032"}],"version-history":[{"count":2,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"predecessor-version":[{"id":1037,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1032\/revisions\/1037"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}