function 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ##### My (demuredemeanor) bashrc sub source function script
  2. # Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
  3. # https://notabug.org/demure/dotfiles/
  4. # legacy repo http://github.com/demure/dotfiles
  5. # vim:set syntax=sh:
  6. ### Universal Custom Commands ### {{{
  7. ### Extract Function ### {{{
  8. ## Extract most types of compressed files
  9. function extract {
  10. echo Extracting $1 ...
  11. if [ -f $1 ] ; then
  12. case $1 in
  13. *.tar.bz2) tar xjf $1 ;;
  14. *.tar.gz) tar xzf $1 ;;
  15. *.bz2) bunzip2 $1 ;;
  16. *.rar) rar x $1 ;;
  17. *.gz) gunzip $1 ;;
  18. *.tar) tar xf $1 ;;
  19. *.tbz2) tar xjf $1 ;;
  20. *.tgz) tar xzf $1 ;;
  21. *.zip) unzip $1 ;;
  22. *.Z) uncompress $1 ;;
  23. *.7z) 7z x $1 ;;
  24. *) echo "'$1' cannot be extracted via extract()" ;;
  25. esac
  26. else
  27. echo "'$1' is not a valid file"
  28. fi
  29. }
  30. ### End Extract ### }}}
  31. ### Man Function ### {{{
  32. ## Color man pages with less (is a `most` way too...)
  33. man() {
  34. env \
  35. LESS_TERMCAP_mb=$(printf "\e[1;31m") \
  36. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  37. LESS_TERMCAP_me=$(printf "\e[0m") \
  38. LESS_TERMCAP_se=$(printf "\e[0m") \
  39. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  40. LESS_TERMCAP_ue=$(printf "\e[0m") \
  41. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  42. man "$@"
  43. }
  44. ### End Man ### }}}
  45. ### Tmux Display Funtion ### {{{
  46. ## For resyncing $DISPLAY
  47. # used to refresh ssh connection for tmux
  48. # http://justinchouinard.com/blog/2010/04/10/fix-stale-ssh-environment-variables-in-gnu-screen-and-tmux
  49. function r() {
  50. if [[ -n $TMUX ]]; then
  51. NEW_DISPLAY=`tmux showenv|grep ^DISPLAY|cut -d = -f 2`
  52. if [[ -n $NEW_DISPLAY ]] && [[ -S $NEW_DISPLAY ]]; then
  53. DISPLAY=$NEW_DISPLAY
  54. fi
  55. fi
  56. }
  57. ### End Tmux Display ### }}}
  58. ### Git Files Hist Function ### {{{
  59. function git_files_hist() {
  60. git log --pretty=%h | while read hash; do git ls-tree -r $hash | awk '{print $4}'; done | sort -u
  61. }
  62. ### End Git Hist ### }}}
  63. ### Backup File ### {{{
  64. # http://xmodulo.com/useful-bash-aliases-functions.html
  65. backup() { cp "$1"{,.bak};}
  66. ### End Backup ### }}}
  67. ### IP Look Up ### {{{
  68. ipinfo() {
  69. local IP1 IP2
  70. IP1="$1"
  71. IP2="$(echo "${IP1}" | grep -Ec '^(([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}|([0-9]{1,3}\.){3}[0-9]{1,3})$')"
  72. if [[ ${IP2} == "1" ]]; then
  73. curl ipinfo.io/"$IP1"
  74. else
  75. ipawk=($(host "$IP1" | awk '/address/ { print $NF }'))
  76. curl ipinfo.io/${ipawk[1]}
  77. fi
  78. echo
  79. }
  80. ### End IP Look Up ### }}}
  81. ###End Universal Commands ### }}}