PageRenderTime 50ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/bash.functions

http://github.com/brinkman83/bashrc
Bash | 272 lines | 246 code | 26 blank | 0 comment | 0 complexity | 617e02876614ecb686483dae53efe960 MD5 | raw file
  1#!/bin/bash
  2
  3##############################################
  4echo -e "sh /etc/bash.functions ::\n\n"
  5##############################################
  6
  7psnice()
  8{
  9	if [[ -z $1 || -z $2 ]] ; then
 10		echo "Usage: psnice procname [-/+ nice]"
 11		return 1
 12	fi
 13	ps ax | grep $1 | grep -v grep | awk '{ print $1 }' | xargs sudo renice $2 -p
 14}
 15
 16function chmog()
 17{
 18        if [ $# -ne 4 ] ; then
 19                echo "usage: chmog mode owner group file"
 20                return 1
 21        else
 22                chmod $1 $4
 23                chown $2 $4
 24                chgrp $3 $4
 25        fi
 26}
 27
 28psgrep()
 29{
 30        ps aux | grep $1 | grep -v grep
 31}
 32
 33pskill()
 34{
 35        local pid
 36
 37        pid=$(ps ax | grep $1 | grep -v grep | awk '{ print $1 }')
 38        echo -n "killing $1 (process $pid)..."
 39        kill -9 $pid
 40        echo "slaughtered."
 41}
 42
 43## Display a printout of the largest installed packages, useful for when diskspace is low.
 44function show-largest-packages {
 45	if [ -z $1 ]; then
 46		echo 'Usage: show-largest-packages [# pkgs to show]'
 47		return
 48	fi
 49	dpkg-query --show --showformat='${Package}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}' | tail -n $1
 50}
 51
 52## Attempt to auto add as many unknown keys to apt using default keyserver ##
 53function fix-apt-keys {
 54  if [ ! -x /usr/sbin/add-apt-key ]; then
 55    echo -e Needed add-apt-key util not installed, attempting to fix this...
 56    sudo apt-get install add-apt-key
 57  fi
 58  echo Running apt-get update -q to log unknown keys...
 59  sudo apt-get update -q 2>&1 | grep -Po -e '\w{16}$' | xargs -d '\n' -n1 sudo add-apt-key
 60  echo Removing temp file created to run fix-apt-keys...
 61  rm /tmp/apterr -fv
 62}
 63
 64## ack-grep files ##
 65function agrep {
 66  if [ ! -x /usr/bin/ack-grep ]; then
 67    echo Need to get ack-grep before we can use it...
 68    sudo apt-get install ack-grep
 69  fi
 70  if [ -z "$1" ]; then
 71    if [ -z "$2" ]; then
 72      echo usage: agrep [pattern] [path / file]
 73      return
 74    fi
 75  fi
 76  ack-grep --color -awHQ $1 $2
 77}
 78
 79## Reliably copies the id_rsa.pub to remote machines, reguardless of if ssh-agent is running or working! ##
 80function sshadd {
 81  if [ -z "$1" ]; then 
 82    echo usage: sshadd [user@]hostname
 83    return
 84  fi
 85  ssh-copy-id -i ~/.ssh/id_rsa.pub $1
 86}
 87
 88## Alias apt-get with wajig, but make sure its installed ##
 89function wajig {
 90  if [ -x /usr/bin/wajig ]; then
 91    sudo wajig $@
 92  else
 93    sudo apt-get -y install wajig wget fping debconf-english apt-move dpkg-repack alien fakeroot gkdebconf lynx elinks python-gtk2 python-glade2 python-gnome2 deborphan equivs debdelta vrms apt-show-versions apt-show-source
 94    sudo wajig $@
 95  fi
 96}
 97
 98## Dynamic icecc loader ##
 99function icecc {
100  if [ -x /usr/bin/icecc ]; then
101    sudo icecc $@
102  else
103    sudo apt-get -y install icecc
104    sudo icecc $@
105  fi
106}
107
108## Dynamic distcc loader ##
109function distcc {
110  if [ -x /usr/bin/distcc ]; then
111    sudo distcc $@
112  else
113    sudo apt-get -y install distcc
114    sudo distcc $@
115  fi
116}
117
118## Dynamic vfu loader ##
119function vfu {
120  if [ -x /usr/bin/vfu ]; then
121    sudo vfu $@
122  else
123    sudo apt-get -y install vfu
124    sudo vfu $@
125  fi
126}
127
128## Dynamic rdist loader ##
129function rdist {
130  if [ -x /usr/bin/rdist ]; then
131    sudo rdist $@
132  else
133    sudo apt-get -y install rdist
134    sudo rdist $@
135  fi
136}
137
138## Dynamic galternatives loader ##
139function galternatives {
140  if [ -x /usr/sbin/galternatives ]; then
141    gksudo /usr/sbin/galternatives
142  else
143    sudo apt-get -y install galternatives
144    gksudo /usr/sbin/galternatives
145  fi
146}
147
148## Dynamic htop loader ##
149function htop {
150  if [ -x /usr/bin/htop ]; then
151    sudo htop $@
152  else
153    sudo apt-get -y install htop
154    sudo htop $@
155  fi
156}
157
158## Dynamic renameutils (icp) loader ##
159function icp {
160  if [ -x /usr/bin/icp ]; then
161    sudo icp $@
162  else
163    sudo apt-get -y install renameutils
164    sudo icp $@
165  fi
166}
167
168## Dynamic renameutils (imv) loader ##
169function imv {
170  if [ -x /usr/bin/imv ]; then
171    sudo imv $@
172  else
173    sudo apt-get -y install renameutils
174    sudo imv $@
175  fi
176}
177
178## Dynamic dlocate loader ##
179function dlocate {
180  if [ -x /usr/bin/dlocate ]; then
181    sudo dlocate $@
182  else
183    sudo apt-get -y install dlocate
184    sudo dlocate $@
185  fi
186}
187
188## Dynamic apparix loader ##
189function apparix {
190  if [ -x /usr/bin/apparix ]; then
191    sudo apparix $@
192  else
193    sudo apt-get -y install apparix
194    sudo apparix $@
195  fi
196}
197
198### Apparix (cdargs fork) functions ###
199function to () {
200   if test "$2"; then
201     cd "$(apparix "$1" "$2" || echo .)";
202   else
203     cd "$(apparix "$1" || echo .)";
204   fi
205   pwd
206}
207
208function bm () {
209   if test "$2"; then
210      apparix --add-mark "$1" "$2";
211   elif test "$1"; then
212      apparix --add-mark "$1";
213   else
214      apparix --add-mark;
215   fi
216}
217
218function portal () {
219   if test "$1"; then
220      apparix --add-portal "$1";
221   else
222      apparix --add-portal;
223   fi
224}
225
226# function to generate list of completions from .apparixrc
227function _apparix_aliases ()
228{   cur=$2
229    dir=$3
230    COMPREPLY=()
231    if [ "$1" == "$3" ]
232    then
233        COMPREPLY=( $( cat $HOME/.apparix{rc,expand} | \
234                       grep "j,.*$cur.*," | cut -f2 -d, ) )
235    else
236        dir=`apparix -favour rOl $dir 2>/dev/null` || return 0
237        eval_compreply="COMPREPLY=( $(
238            cd "$dir"
239            \ls -d *$cur* | while read r
240            do
241                [[ -d "$r" ]] &&
242                [[ $r == *$cur* ]] &&
243                    echo \"${r// /\\ }\"
244            done
245            ) )"
246        eval $eval_compreply
247    fi
248    return 0
249}
250### (END) Apparix (cdargs fork) functions (END) ###
251
252### Apt-history replacement: invoke as apt-history ( install | upgrade | remove | rollback ) w/o parens ###
253function apt-history(){
254      case "$1" in
255        install)
256              cat /var/log/dpkg.log | grep 'install '
257              ;;
258        upgrade|remove)
259              cat /var/log/dpkg.log | grep $1
260              ;;
261        rollback)
262              cat /var/log/dpkg.log | grep upgrade | \
263                  grep "$2" -A10000000 | \
264                  grep "$3" -B10000000 | \
265                  awk '{print $4"="$5}'
266              ;;
267        *)
268              cat /var/log/dpkg.log
269              ;;
270      esac
271}
272### (END) Apt-history fn (END) ###