Bash | Cookbook
Configure Bash Environment
Using Bash Completion
Add git completion
Download git-completion.bash to $HOME/etc
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
. $HOME/etc/git-completion.sh
. $HOME/etc/git-completion.sh
. $HOME/etc/git-completion.sh
Add completion for Makefiles
Add this to .bashrc
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|)′?akefile|sed′s/[a−zA−Z0−9.−]∗//'`" make
complete -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|)′?akefile|sed′s/[a−zA−Z0−9.−]∗//'`" 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
mkdir−pHOME/etc
$ cd $HOME/etc
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
mkdir−pHOME/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
. $HOME/etc/git-prompt.sh
. $HOME/etc/git-prompt.sh
. $HOME/etc/git-prompt.sh
Configure prompt to display git branch
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"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="${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:8rm−rf_CURRDATE mkdir -p CURRDATEcp_LINE CURRDATEif[["_CURRDATE" = "LASTDATE"]];thenspinnerelseprintf"{SYMBOL_PASS}\nCURRDATE:"fiLASTDATE=_CURRDATE done printf "\n" fi</pre>
<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"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="${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:8rm−rf_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
$ brew install gnu-getopt$ brew install gnu-getopt$ brew install gnu-getoptBash Script Template
#------------------------------------------------------------------------------------------CMD_GETOPT=/usr/local/opt/gnu-getopt/bin/getoptS_OPTS="vdm:"L_OPTS="verbose,debug,versions,install:,init:"OPTS=$($CMD_GETOPT --options "$S_OPTS"--longoptions "$L_OPTS" -- "$@")eval set $OPTSshiftwhile [[ $# -gt 0 ]]; doecho "Param 1: '$1'"case "$1" in-v | --verbose) VERBOSE=true; ;;-d | --debug) DEBUGLEVEL="$2"; shift ;;--versions) MODE=GETVERSIONS ;;--install) TYPE="$2"; shiftMODE=INSTALL ;;--init) MODE=INIT ;;--) shift; break;;* ) break;;esacshiftdone#------------------------------------------------------------------------------------------ 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 doneShow progress with a spinner
#!/bin/bashCOMMAND="${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" ]]; thenprintf "_LASTDATE=for _LINE in *.csvdo_FILE="$(basename $_LINE)"_CURRDATE=${_FILE:19:8}rm -rf $_CURRDATEmkdir -p $_CURRDATEcp $_LINE $_CURRDATEif [[ "$_CURRDATE" = "$_LASTDATE" ]]; thenspinnerelseprintf "${SYMBOL_PASS}\n${_CURRDATE}: "fi_LASTDATE=$_CURRDATEdoneprintf "\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