Bash | Cookbook

Configure Bash Environment

Using Bash Completion

Add git completion

Download git-completion.bash to $HOME/etc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdirHOME/etc
$ cd $HOME/etc
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
mkdirHOME/etc cdHOME/etc $ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
$ mkdir $HOME/etc
$ cd $HOME/etc
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

Add git-prompt to .bashrc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
. $HOME/etc/git-completion.sh
. $HOME/etc/git-completion.sh
. $HOME/etc/git-completion.sh

Add completion for Makefiles

Add this to .bashrc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|)?akefile|seds/[azAZ09.]//'`" make
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|)?akefile|seds/[azAZ09.]//'`" make
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's/[^a-zA-Z0-9_.-]*$//'`" make

Customize Bash prompt

Add git repository/branch to prompt

Download git-prompt.sh to $HOME/etc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdirpHOME/etc
$ cd $HOME/etc
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
mkdirpHOME/etc cdHOME/etc $ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
$ mkdir -p $HOME/etc
$ cd $HOME/etc
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

Add git-prompt to .bashrc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
. $HOME/etc/git-prompt.sh
. $HOME/etc/git-prompt.sh
. $HOME/etc/git-prompt.sh

Configure prompt to display git branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export PS1='[\033[33;1m]\w[\033[m] [\033[32m]$(__git_ps1 " (
<h2 class="wp-block-heading">Writing Bash Scripts</h2>
<h3 class="wp-block-heading">Set vi commands in bash-script</h3>
<h3 class="wp-block-heading">Parsing Parameter</h3>
<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><p>Bash Script Template</p><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="">#------------------------------------------------------------------------------------------
CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getopt
S_OPTS="vdm:"
L_OPTS="verbose,debug,versions,install:,init:"
OPTS=(CMD_GETOPT --options "SOPTS"longoptions"L_OPTS" -- "$@")
eval set $OPTS
shift
while [[ $# -gt 0 ]]; do
echo "Param 1: '$1'"
case "$1" in
-v | --verbose) VERBOSE=true; ;;
-d | --debug) DEBUGLEVEL="$2"; shift ;;
--versions) MODE=GETVERSIONS ;;
--install) TYPE="$2"; shift
MODE=INSTALL ;;
--init) MODE=INIT ;;
--) shift; break;;
* ) break;;
esac
shift
done</pre><h2 class="wp-block-heading">Show progress with a spinner</h2><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
COMMAND="${1^^}"
SYMBOL_PASS="$(printf '\e[0;32m\xe2\x9c\x94\e[0m')"
SPINNER_STATE='\|/-'
spinner()
{
local _lastpos=$((${#SPINNER_STATE}-1))
SPINNER_STATE="SPINNERSTATE:$lastpos:1{SPINNER_STATE:0:$_lastpos}"
printf "
}
#---------------------------------------------------------------------------------------------------
#
#---------------------------------------------------------------------------------------------------
if [[ "$COMMAND" = "STEP1" ]]; then
printf "
_LASTDATE=
for _LINE in *.csv
do
_FILE="(basename_LINE)"
_CURRDATE=${_FILE:19:8}
rm -rf $_CURRDATE
mkdir -p $_CURRDATE
cp $_LINE $_CURRDATE
if [[ "$_CURRDATE" = "$_LASTDATE" ]]; then
spinner
else
printf "SYMBOLPASS\n{_CURRDATE}: "
fi
_LASTDATE=$_CURRDATE
done
printf "\n"
fi</pre>
export PS1='[\033[33;1m]\w[\033[m] [\033[32m](__git_ps1 " (



<h2 class="wp-block-heading">Writing Bash Scripts</h2>



<h3 class="wp-block-heading">Set vi commands in bash-script</h3>



 



<h3 class="wp-block-heading">Parsing Parameter</h3>



 



<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><p>Bash Script Template</p><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="">#------------------------------------------------------------------------------------------ CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getopt S_OPTS="vdm:" L_OPTS="verbose,debug,versions,install:,init:" OPTS=(CMD_GETOPT --options "SOPTS"longoptions"L_OPTS" -- "@")evalsetOPTS shift while [[ # -gt 0 ]]; do
    echo "Param 1: '
1'" case "1"inv|verbose)VERBOSE=true;;;d|debug)DEBUGLEVEL="2"; shift ;; --versions) MODE=GETVERSIONS ;; --install) TYPE="2";        shift
                           MODE=INSTALL                  ;;

        --init)            MODE=INIT                     ;;

        --)                                  shift; break;;
        * )                                         break;;
    esac

    shift
done</pre><h2 class="wp-block-heading">Show progress with a spinner</h2><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
 
COMMAND="
{1^^}" SYMBOL_PASS="(printf '\e[0;32m\xe2\x9c\x94\e[0m')"
SPINNER_STATE='\|/-'
 
spinner()
{
        local _lastpos=$((${#SPINNER_STATE}-1))
        SPINNER_STATE="${SPINNER_STATE:$_lastpos:1}${SPINNER_STATE:0:$_lastpos}"
 
        printf "
}
 
#---------------------------------------------------------------------------------------------------
#
#---------------------------------------------------------------------------------------------------
if [[ "
COMMAND" = "STEP1" ]]; then printf " _LASTDATE= for _LINE in *.csv do _FILE="(basename_LINE)" _CURRDATE=FILE:19:8rmrf_CURRDATE mkdir -p CURRDATEcp_LINE CURRDATEif[["_CURRDATE" = "LASTDATE"]];thenspinnerelseprintf"{SYMBOL_PASS}\nCURRDATE:"fiLASTDATE=_CURRDATE done printf "\n" fi</pre>
export PS1='[\033[33;1m]\w[\033[m] [\033[32m]$(__git_ps1 " (



Writing Bash Scripts

Set vi commands in bash-script

Parsing Parameter

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ brew install gnu-getopt
$ brew install gnu-getopt
$ brew install gnu-getopt

Bash Script Template

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#------------------------------------------------------------------------------------------
CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getopt
S_OPTS="vdm:"
L_OPTS="verbose,debug,versions,install:,init:"
OPTS=$($CMD_GETOPT --options "$S_OPTS"--longoptions "$L_OPTS" -- "$@")
eval set $OPTS
shift
while [[ $# -gt 0 ]]; do
echo "Param 1: '$1'"
case "$1" in
-v | --verbose) VERBOSE=true; ;;
-d | --debug) DEBUGLEVEL="$2"; shift ;;
--versions) MODE=GETVERSIONS ;;
--install) TYPE="$2"; shift
MODE=INSTALL ;;
--init) MODE=INIT ;;
--) shift; break;;
* ) break;;
esac
shift
done
#------------------------------------------------------------------------------------------ CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getopt S_OPTS="vdm:" L_OPTS="verbose,debug,versions,install:,init:" OPTS=$($CMD_GETOPT --options "$S_OPTS"--longoptions "$L_OPTS" -- "$@") eval set $OPTS shift while [[ $# -gt 0 ]]; do echo "Param 1: '$1'" case "$1" in -v | --verbose) VERBOSE=true; ;; -d | --debug) DEBUGLEVEL="$2"; shift ;; --versions) MODE=GETVERSIONS ;; --install) TYPE="$2"; shift MODE=INSTALL ;; --init) MODE=INIT ;; --) shift; break;; * ) break;; esac shift done
#------------------------------------------------------------------------------------------
CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getopt

S_OPTS="vdm:"
L_OPTS="verbose,debug,versions,install:,init:"

OPTS=$($CMD_GETOPT --options "$S_OPTS"--longoptions "$L_OPTS"    -- "$@")

eval set $OPTS
shift

while [[ $# -gt 0 ]]; do
    echo "Param 1: '$1'"

    case "$1" in
        -v | --verbose)    VERBOSE=true;                 ;;
        -d | --debug)      DEBUGLEVEL="$2";  shift       ;;

        --versions)        MODE=GETVERSIONS              ;;
        --install)         TYPE="$2";        shift
                           MODE=INSTALL                  ;;

        --init)            MODE=INIT                     ;;

        --)                                  shift; break;;
        * )                                         break;;
    esac

    shift
done

Show progress with a spinner

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
COMMAND="${1^^}"
SYMBOL_PASS="$(printf '\e[0;32m\xe2\x9c\x94\e[0m')"
SPINNER_STATE='\|/-'
spinner()
{
local _lastpos=$((${#SPINNER_STATE}-1))
SPINNER_STATE="${SPINNER_STATE:$_lastpos:1}${SPINNER_STATE:0:$_lastpos}"
printf "
}
#---------------------------------------------------------------------------------------------------
#
#---------------------------------------------------------------------------------------------------
if [[ "$COMMAND" = "STEP1" ]]; then
printf "
_LASTDATE=
for _LINE in *.csv
do
_FILE="$(basename $_LINE)"
_CURRDATE=${_FILE:19:8}
rm -rf $_CURRDATE
mkdir -p $_CURRDATE
cp $_LINE $_CURRDATE
if [[ "$_CURRDATE" = "$_LASTDATE" ]]; then
spinner
else
printf "${SYMBOL_PASS}\n${_CURRDATE}: "
fi
_LASTDATE=$_CURRDATE
done
printf "\n"
fi
#!/bin/bash COMMAND="${1^^}" SYMBOL_PASS="$(printf '\e[0;32m\xe2\x9c\x94\e[0m')" SPINNER_STATE='\|/-' spinner() { local _lastpos=$((${#SPINNER_STATE}-1)) SPINNER_STATE="${SPINNER_STATE:$_lastpos:1}${SPINNER_STATE:0:$_lastpos}" printf " } #--------------------------------------------------------------------------------------------------- # #--------------------------------------------------------------------------------------------------- if [[ "$COMMAND" = "STEP1" ]]; then printf " _LASTDATE= for _LINE in *.csv do _FILE="$(basename $_LINE)" _CURRDATE=${_FILE:19:8} rm -rf $_CURRDATE mkdir -p $_CURRDATE cp $_LINE $_CURRDATE if [[ "$_CURRDATE" = "$_LASTDATE" ]]; then spinner else printf "${SYMBOL_PASS}\n${_CURRDATE}: " fi _LASTDATE=$_CURRDATE done printf "\n" fi
#!/bin/bash
 
COMMAND="${1^^}"
 
SYMBOL_PASS="$(printf '\e[0;32m\xe2\x9c\x94\e[0m')"
SPINNER_STATE='\|/-'
 
spinner()
{
        local _lastpos=$((${#SPINNER_STATE}-1))
        SPINNER_STATE="${SPINNER_STATE:$_lastpos:1}${SPINNER_STATE:0:$_lastpos}"
 
        printf "
}
 
#---------------------------------------------------------------------------------------------------
#
#---------------------------------------------------------------------------------------------------
if [[ "$COMMAND" = "STEP1" ]]; then
        printf "
        _LASTDATE=
        for _LINE in *.csv
        do
                _FILE="$(basename $_LINE)"
                _CURRDATE=${_FILE:19:8}
                rm    -rf       $_CURRDATE
                mkdir -p        $_CURRDATE
                cp       $_LINE $_CURRDATE
 
                if [[ "$_CURRDATE" = "$_LASTDATE" ]]; then
                        spinner
                else
                        printf "${SYMBOL_PASS}\n${_CURRDATE}: "
                fi
 
                _LASTDATE=$_CURRDATE
        done
        printf "\n"
fi