Goodies

24 July 2019

From time to time there are some goodie scripts that I would like to share; (actually i put up my environment + some more goodies for bash scripting here ) Maybe you might find something interesting within this collection:

here are the instructions to set all this all up.

searching and editing source code

Other variants of grep (that’s seems to be my main occupation throughout the day ;-) Even doing a combination of find / xarg / grep is a science all by itself - if you find a file with a space it it then it will be skipped. find -print0 terminates each line with a 0 character, but then yo have to tell xargs -0 to get it back. Everything so damn complicated.

# grep in cpp sources
s()
{
  find . -type f \( -name '*.cpp' -o -name '*.cxx' -o -name '*.hpp' -o -name '*.hxx' -o -name '*.h' \) -print0 2>/dev/null | xargs -0 grep $*
}

# grep in python files
p()
{
  find . -name '*.py' -print0 2>/dev/null | xargs -0 grep $*
}
    

Building of ctags file, again doing that in the repo root (to get all files); ctags has quite an involved command line all by itself.

# build ctags
ctg()
{
  # find the top level directory for this git repository
  TOP_DIR=`git rev-parse --show-toplevel 2>/dev/null`
  if [ "x$TOP_DIR" != "x" ]; then
      pushd $TOP_DIR >/dev/null
      rm tags 2>/dev/null
      find . -type f \( -name '*.cpp' -o -name '*.cxx' -o -name '*.hpp' -o -name '*.hxx' -o -name '*.h' \) -print 0 | xargs -0 ctags -a --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++   
      popd >/dev/null
  else 
        echo "$PWD is not a git repo"
  fi
}
    

or the next function gitgrep - it always goes to the root directory of the current repository - also each search result shows the full path of the match.

    gitgrep()                                                                                                                                                                                                                                     
{                                                                                                                                                                                                                                                 # find the top level directory for this git repository                                                                                                                                                                                    
    TOP_DIR=`git rev-parse --show-toplevel 2>/dev/null`                                                                                                                                                                                       
    if [ "x$TOP_DIR" != "x" ]; then                                                                                                                                                                                                           
        pushd $TOP_DIR %gt;/dev/null                                                                                                                                                                                                             
        # search in all files, they are now relative to repo root dir; so prepend the repo dir to get full path                                                                                                                               
        git ls-files -z | xargs -0 grep $* | while IFS= read -r line; do printf '%s/%s\n' "$TOP_DIR" "$line"; done                                                                                                                           
                                                                                                                                                                                                                                              
        # no all the colors of git grep output are gone after pipng them through the next stage....                                                                                                                                           
        #git --no-pager grep $* | while IFS= read -r line; do printf '%s/%s\n' "$TOP_DIR" "$line"; done                                                                                                                                        
        popd >/dev/null                                                                                                                                                                                                                       
    else                                                                                                                                                                                                                                      
        echo "$PWD is not a git repo"                                                                                                                                                                                                         
    fi                                                                                                                                                                                                                                        
}     
    

Another useful thing is find replace in multiple files: here link to script is a script that is supposed to take the pain out of this. Here is the help text

./find-replace.sh -s <source filter> -f <from> -t <to> [-v -h]

apply replace to multiple input files

-s <source filter>      : specify input files; available values: cpp git go py shell 
-f <from>               : replace from
-t <to>                 : replace to
-r                      : report how many files were changed.

source filter runs find and then it pipes it into sed to replace it.
    

working with git

Having the name of the git branch in the bash command prompt - quite essential if you are working with multiple branches, it helps to avoid some effort while working on the wrong branch. Having the branch name without colors that’s fine; but when you want colors with bash then things become messy.

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1="[\u@\h \W\$(parse_git_branch)]\$ "

# strange: when I log in with the color prompt, then I have to resource the shell, else git branch doesn't display.
#PS1="[\e[0;34m\u@\h\e[m \W\\e[0;35m$(parse_git_branch)\e[m]\$ "

# but don't need to do that with uniform coloring ... as awayls strange hickups upon non-trivial usage...
#PS1="[\e[0;35m\u@\h \W\$(parse_git_branch)\e[m]\$ "
    

or another one: who are the most frequent contributors in the current git repository? (or a specific file if you pass it as argument)

#
# who are the most frequent authors in the current git repository?
#
function whoisauthor() {
    git log $1 | grep 'Author: ' | sort  | uniq -c | sort -k1rn | less
}


    

this alias shows deleted files in the git repository

# show deleted files in grep
alias gitshowdeleted='git log --diff-filter=D --summary | grep "delete mode"'
    

this one undeletes any one of them.

# bring back a deleted file in git
gitundodelete() {
  local bringbackfile=$1
  
  set -x
  commit=$(git rev-list -n 1 HEAD -- $bringbackfile)
  if [[ $? == 0 ]]; then
        git checkout ${commit}~1 $bringbackfile
  fi
  set +x
}
    

development environment

here are my vim customization vim customizations & addons

For tmux: the function ta takes a tmux session name as argument and attaches to it; now this one also does completion on the session name.


function ta {
    tmux attach -t $1
}

function _ta {
  local cur prev opts f
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  for f in $(tmux ls | awk '{ print $1 }'); do
    f=${f: : -1}
    opts="$opts $f" 
  done

  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )  
  return 0
}   

complete -F _ta ta 

                                                                                                                                                                                                                                              
alias tls='tmux ls'                                                                                                                                                                                                                           
alias tn='tmux new -s'                                                                                                                                                                                                                        
alias tk='tmux kill-session -t'  

    

also the setup.sh script (in the linked repo ) adds the tmux-resurrect and tmux-continuum plugins to the current users setup; so that the sessions are autosaved/restored.

a script that checks if a topic is available in multiple man pages, if it is then it displays the list of available pages and prompts you for the desired page.

    
# if argument exists in one man page - call man <argument>                                                                                                                                                                                 
# if argument exists in more than one man page - promt user for a choice of man page, then call man with the argument                                                                                                                         
function h                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                             
    local sterm                                                                                                                                                                                                                               
    local tmpfile                                                                                                                                                                                                                             
    local mpages                                                                                                                                                                                                                              
    local mpagecount                                                                                                                                                                                                                          
                                                                                                                                                                                                                                              
    sterm=$*                                                                                                                                                                                                                                  
    tmpfile=$(mktemp /tmp/sshtonode.XXXXXX)                                                                                                                                                                                                       mpagecount=0                                                                                                                                                                                                                              
                                                                                                                                                                                                                                              
    for m in 1 2 3 4 5 6 7 8 9; do                                                                                                                                                                                                            
        man $m searchterm $sterm >$tmpfile 2>/dev/null                                                                                                                                                                                        
        fsize=$(stat --printf="%s" $tmpfile)                                                                                                                                                                                                  
        if [[ $fsize != 0 ]]; then                                                                                                                                                                                                            
            mpages="$mpages $m"                                                                                                                                                                                                               
            ((mpagecount += 1))                                                                                                                                                                                                               
        fi                                                                                                                                                                                                                                    
        rm -f $tmpfile                                                                                                                                                                                                                        
    done                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                              
    if [ $mpagecount == 0 ]; then                                                                                                                                                                                                             
       echo "* no page found *"                                                                                                                                                                                                               
    else                                                                                                                                                                                                                                      
       if [[ $mpagecount < 1 ]]; then                                                                                                                                                                                                         
          echo "select page: $mpages"                                                                                                                                                                                                         
                                                                                                                                                                                                                                              
          local page                                                                                                                                                                                                                          
                                                                                                                                                                                                                                              
          echo -n "> "                                                                                                                                                                                                                        
          read page                                                                                                                                                                                                                           
                                                                                                                                                                                                                                              
          man $page $sterm                                                                                                                                                                                                                    
       else                                                                                                                                                                                                                                   
          man $sterm                                                                                                                                                                                                                          
       fi                                                                                                                                                                                                                                     
    fi                                                                                                                                                                                                                                        
}    
    

docker and kubernetes

And now a command to delete everything from the local docker registry, so that one can start from scratch.

    
# delete everything in docker registry 
dockerclean() 
{
    echo "*** stop all docker containers ***"
    docker stop $(docker ps -a -q)
    
    echo "*** delete all containers ***"
    docker rm -vf $(docker ps -a -q)

    echo "*** delete all imagess ***"
    docker rmi -f $(docker images -a -q)
}
    

a script that automates logging into a docker repository and that pushes a specified local image to the docker repository link to script by default the repository is quay.io

a script for debugging problems with pods: it describes the pod, extracts all containers from the pod spec and gets the logs for each container running inside the pod, along with the container spec link to script

a script for listing all deployments (globally or per namespace); for each deployment it displays information about its containers: name, image and command link to script