{"id":4182,"date":"2019-02-10T16:49:33","date_gmt":"2019-02-10T15:49:33","guid":{"rendered":"http:\/\/blog.via-internet.de\/?p=4182"},"modified":"2024-01-05T15:34:26","modified_gmt":"2024-01-05T14:34:26","slug":"bash-cookbook","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2019\/02\/10\/bash-cookbook\/","title":{"rendered":"Bash | Cookbook"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Configure Bash Environment<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Using Bash Completion<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Add git completion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Download git-completion.bash to $HOME\/etc<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mkdir $HOME\/etc\n$ cd $HOME\/etc\n$ wget https:\/\/raw.githubusercontent.com\/git\/git\/master\/contrib\/completion\/git-completion.bash<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add git-prompt to  .bashrc<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">. $HOME\/etc\/git-completion.sh<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add completion for Makefiles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add this to .bashrc<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">complete -W \"`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's\/[^a-zA-Z0-9_.-]*$\/\/'`\" make<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customize Bash prompt<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Add git repository\/branch to prompt<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Download git-prompt.sh to $HOME\/etc<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mkdir -p $HOME\/etc\n$ cd $HOME\/etc\n$ wget https:\/\/raw.githubusercontent.com\/git\/git\/master\/contrib\/completion\/git-prompt.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add git-prompt to  .bashrc<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">. $HOME\/etc\/git-prompt.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Configure prompt to display git branch<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export PS1='[\\033[33;1m]\\w[\\033[m] [\\033[32m]$(__git_ps1 \" (\n\n\n\n<h2 class=\"wp-block-heading\">Writing Bash Scripts<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Set vi commands in bash-script<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parsing Parameter<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ brew install gnu-getopt<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Bash Script Template<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#------------------------------------------------------------------------------------------\nCMD_GETOPT=\/usr\/local\/opt\/gnu-getopt\/bin\/getopt\n\nS_OPTS=\"vdm:\"\nL_OPTS=\"verbose,debug,versions,install:,init:\"\n\nOPTS=$($CMD_GETOPT --options \"$S_OPTS\"--longoptions \"$L_OPTS\"    -- \"$@\")\n\neval set $OPTS\nshift\n\nwhile [[ $# -gt 0 ]]; do\n    echo \"Param 1: '$1'\"\n\n    case \"$1\" in\n        -v | --verbose)    VERBOSE=true;                 ;;\n        -d | --debug)      DEBUGLEVEL=\"$2\";  shift       ;;\n\n        --versions)        MODE=GETVERSIONS              ;;\n        --install)         TYPE=\"$2\";        shift\n                           MODE=INSTALL                  ;;\n\n        --init)            MODE=INIT                     ;;\n\n        --)                                  shift; break;;\n        * )                                         break;;\n    esac\n\n    shift\ndone<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Show progress with a spinner<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n \nCOMMAND=\"${1^^}\"\n \nSYMBOL_PASS=\"$(printf '\\e[0;32m\\xe2\\x9c\\x94\\e[0m')\"\nSPINNER_STATE='\\|\/-'\n \nspinner()\n{\n        local _lastpos=$((${#SPINNER_STATE}-1))\n        SPINNER_STATE=\"${SPINNER_STATE:$_lastpos:1}${SPINNER_STATE:0:$_lastpos}\"\n \n        printf \"\n}\n \n#---------------------------------------------------------------------------------------------------\n#\n#---------------------------------------------------------------------------------------------------\nif [[ \"$COMMAND\" = \"STEP1\" ]]; then\n        printf \"\n        _LASTDATE=\n        for _LINE in *.csv\n        do\n                _FILE=\"$(basename $_LINE)\"\n                _CURRDATE=${_FILE:19:8}\n                rm    -rf       $_CURRDATE\n                mkdir -p        $_CURRDATE\n                cp       $_LINE $_CURRDATE\n \n                if [[ \"$_CURRDATE\" = \"$_LASTDATE\" ]]; then\n                        spinner\n                else\n                        printf \"${SYMBOL_PASS}\\n${_CURRDATE}: \"\n                fi\n \n                _LASTDATE=$_CURRDATE\n        done\n        printf \"\\n\"\nfi<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Configure Bash Environment Using Bash Completion Add git completion Download git-completion.bash to $HOME\/etc Add git-prompt to .bashrc Add completion for Makefiles Add this to .bashrc Customize Bash prompt Add git repository\/branch to prompt Download git-prompt.sh to $HOME\/etc Add git-prompt to .bashrc Configure prompt to display git branch Writing Bash Scripts Set vi commands in bash-script Parsing Parameter Bash Script Template Show progress with a spinner<\/p>\n","protected":false},"author":1,"featured_media":4265,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[92,16,72],"tags":[],"class_list":["post-4182","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-cookbook","category-shell"],"jetpack_featured_media_url":"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2019\/02\/bash-full_colored_dark.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/4182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=4182"}],"version-history":[{"count":1,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/4182\/revisions"}],"predecessor-version":[{"id":9786,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/4182\/revisions\/9786"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media\/4265"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=4182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=4182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=4182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}