Advertisement
Yunga

aliases_functions.sh

May 25th, 2013
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 25.29 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. unalias -a                                      # Delete all aliases
  4.  
  5. #> new name, i don't *think* it conflicts with something
  6. #= same as original command, *shouldn't* modify the normal behaviour
  7. #! same as original command, *probably* breaks normal behaviour
  8.  
  9. #######################################################################
  10. #_ Aliases: ~/.bash/aliases_functions.sh -- by yunga.palatino@gmail.com
  11. alias aliasinfo="perl -nlE'say qq@\n\n***** \$1@ if /#_\s+(.*)/;say qq@\$1\t\$2@ if /^alias\s+(\w+?)=.*?#([>!=:]\s+.*)/' ~/.bash/aliases_functions.sh | less" #> Display this help
  12. alias reloadaliases="source ~/.bash/aliases_functions.sh"                                 #> Reload aliases, deletes ALL aliases
  13. alias realias="vim ~/.bash/aliases_functions.sh; source ~/.bash/aliases_functions.sh"     #> Edit and reload aliases, deletes ALL aliases
  14. #alias cliref="less ~/.bash/docs/cliref.md"                                               #> Unix CLI Reference
  15. alias cliref="curl -s http://pastebin.com/raw.php?i=yGmGiDQX | less -i"                   #> Unix CLI Reference (online)
  16.  
  17. #######################################################################
  18. #_ Navigate
  19. alias cd..='cd ..'                              #> cd: yes, i was using dos first !#@?
  20. alias ..='cd ..'                                #> cd: Norton Dos, in fact.... (which was better than 4Dos IMHO)
  21. alias ...='cd ../..'                            #> cd:
  22. alias ....='cd ../../..'                        #> cd: and i like long directory trees ;-)
  23. alias .....="cd ../../../.."
  24. alias -- -='cd -'                               #> cd: go back to previous directory
  25. alias tree='tree --dirsfirst -aDF'              #! tree: dirs first, all files, show last modified date
  26. alias mkdir='mkdir -p'                          #= mkdir: create nested subdirs too
  27.  
  28. #######################################################################
  29. #_ List files, ls should be used to list files, and not be an alias
  30. #alias ls='ls --color=auto --quoting-style=shell-always --group-directories-first -Fx'       #! ls: colors, quoted, dirs first, (F)ile type, horizontaly (x)
  31. alias  l='ls --color=auto --quoting-style=shell-always --group-directories-first -FxX'       #> ls: grouped by e(X)tensions
  32. alias l.='ls --color=auto --quoting-style=shell-always --group-directories-first -Fxad .*'   #> ls: (a)ll files, don't list (d)ir content and don't derefernce, starting with .
  33. alias ll='ls --color=auto --quoting-style=shell-always --group-directories-first -Flh'       #> ls: (l)ong, (h)uman-readable
  34. alias la='ls --color=auto --quoting-style=shell-always --group-directories-first -Flha'      #> ls: (l)ong, (h)uman-readable, (a)ll files
  35. alias lt='ls --color=auto --quoting-style=shell-always -Flhart'                              #> ls: (l)ong, (h(uman-readable, (a)ll files, by (r)reverse (t)ime (recent goes last)
  36. alias lT='ls --color=auto --quoting-style=shell-always -Flhat'                               #> ls: (l)ong, (h(uman-readable, (a)ll files, by (t)ime (oldest goes last)
  37. alias ltc='ls --color=auto --quoting-style=shell-always -Flharct'                            #> ls: (l)ong, (h(uman-readable, (a)ll files, by (r)reverse (c)(t)ime (recent goes last)
  38. alias lTc='ls --color=auto --quoting-style=shell-always -Flhact'                             #> ls: (l)ong, (h(uman-readable, (a)ll files, by (c)(t)ime (oldest goes last)
  39. alias lz='ls --color=auto --quoting-style=shell-always -FlharS'                              #> ls: (l)ong, (h(uman-readable, (a)ll files, (r)everse si(z)e (biggest goes last)
  40. alias lZ='ls --color=auto --quoting-style=shell-always -FlhaS'                               #> ls: (l)ong, (h(uman-readable, (a)ll files, (r)everse si(z)e (smallest goes last)
  41. alias lx='ls --color=auto --quoting-style=shell-always --group-directories-first -FxXlah'    #> ls: horizontaly (x), grouped by e(X)tensions, (a)ll, (h)uman-readable, extension
  42. alias lsd='ls --color=auto --quoting-style=shell-always --group-directories-first -Fxad */'  #> ls: (a)ll files, don't list (d)ir content and don't derefernce, glob on directories
  43. # [ATTN] nocaseglob and [[:regex:]] doesnt cooperate nicely, this is just for listing
  44. alias lU='shopt -u nocaseglob; ls --color=auto --quoting-style=shell-always --group-directories-first -Fdl [[:upper:]]*; shopt -s nocaseglob' #> ls: files/dirs begining with an uppercase letter !this is buggy!nocaseglob!
  45. alias lu='shopt -u nocaseglob; ls --color=auto --quoting-style=shell-always --group-directories-first -Fdl [[:lower:]]*; shopt -s nocaseglob' #> ls: files/dirs begining with an lowercase letter !this is buggy!nocaseglob!
  46.  
  47. #######################################################################
  48. #_ Find files
  49. alias ff='find . -iname'                        #> find: files subdirs, case (i)nsensitive
  50. alias fl='find .'                               #> find: list all files in subdirs (with ./ relative path)
  51. alias flist="find . | bzip2 -9 > flist.txt.bz2" #> find: fl > flist.txt.bz2
  52. alias flabs='find `pwd`'                        #> find: list all files in subdirs (with absolute filename)
  53. alias loc='locate -i'                           #> locate: find files from update db, case insensitive
  54. alias locf='locate -bei'                        #> locate: find files from update db, only the basename, case insensitive, and if file still exists
  55.  
  56. #######################################################################
  57. #_ Look at files
  58. alias P='less'                                  #> more/less/most: default pager
  59. alias H='head -$(($LINES/5*3))'                 #> head: Show the first lines of a file
  60. alias T='tail -$(($LINES/5*3))'                 #> tail: Show the last lines of a file
  61. alias Tf='tail -$(($LINES/5*3)) -f'             #> tail: Show the last lines of a file and follow
  62. alias skip='gawk "NR<=1{next}{print}"'          #> gawk: skip first line of file [TODO] should be a function
  63. alias skeep="gawk 'NR<=1{printf \"!\"}{print}'" #> gawk: prepend "!" to the first line (so it is sorted first, this and awful hack i know)
  64. alias trimspc='perl -plE"s/\s+/ /g; s/^\s+//g; s/\s+$//g"' #> perl: remove spaces (start/end/double)
  65. alias C='column -t'                             #> column: Format space separted value in colmns (nicely?), add -ns "sep" to define a separator
  66. alias L='wc -l'                                 #> wc: count (l)ines
  67. alias filez='file -kLpsz'                       #> file: search more than one filetype, dereference links, preserve date (pretend not having read them), special files, uncompress
  68. alias g='grep --color=auto -Pi'                 #> egrep: colors, (P)erl regexes, (R)ecurse dirs and case (i)nsensitive (i assume LC_ALL=C)
  69. alias G='grep --color=auto -Piv'                #> egrep: in(v)ert match, colors, (P)erl regexes, (R)ecurse dirs and case (i)nsensitive (i assume LC_ALL=C)
  70. alias g+='agrep -i'                             #> agrep: case (i)sensitive, for grepping differently (AND and OR operators, checkit!)
  71. alias stringz='strings -a'                      #> strings: show (a)ll
  72. alias stringx='strings -atx'                    #> strings: show (a)ll, print hex offsets
  73. alias E='vim -'                                 #> vim: Edit stdin
  74. alias V='mcview'                                #> mcView:
  75. alias X='mcedit'                                #> mcedit: (for the heX editor)
  76. #alias hex='hexdump -C'                          #> Canonical hexdump ofs hex ascii (you can use od/xxd instead)
  77. alias hex='xxd -a -c 32 -g 4'                   #> xxd: hexdump, (a)utoskip 32bytes/line (xxd comes with vim)  -> hex file.org dump
  78. alias hexpatch='xxd -r -a -c 32 -g 4'           #> xxd: apply hexdump -> hexpatch modified_dump file.org
  79. alias ddiff='LC_ALL=C TZ=GMT0 diff -Naur'       #>normalise diffs for distribution
  80. #diff='diff -bBiEwu
  81.  
  82. #######################################################################
  83. #_ Sorting
  84. alias S='sort -f'                               #> sort: case insensitive (f)olding (see LC_COLLATE)
  85. alias SP='sort -f | P'                          #> sort: Sort, case insensitive (f)olding, results in pager
  86. alias SU='sort -uf'                             #> sort: case insensitive, (u)nique lines
  87. alias SD='sort -f | uniq -id'                   #> sort:uniq: case insensitive (f)olding, (d)uplicate lines
  88. alias SN='sort -fn'                             #> sort: case insensitive (f)olding, (n)umerical
  89. alias SUP='sort -uf | P'                        #> sort: case insensitive, (u)nique lines, results in pager
  90. alias SDP='sort -f | uniq -id | P'              #> sort:uniq: case insensitive (f)olding, (d)uplicate lines, results in pager
  91. alias SNP='sort -fn | P'                        #> sort: case insensitive (f)olding, (n)umerical, results in pager
  92. alias SUL='sort -uf | wc -l'                    #> sort:wc: sort, uniq, count lines
  93. # [TODO] comm: add aliases for union, intersection and delta
  94. # see: http://www.catonmat.net/blog/set-operations-in-unix-shell/
  95.  
  96. #######################################################################
  97. #_ Access rights
  98. alias R='chmod -Rc'                             #> chmod: (R)ecurse, (c) verbose
  99. alias O='chown -Rc'                             #> chown: (R)ecurse, (c) verbose
  100.  
  101. #######################################################################
  102. #_ Used/Free mounted disk/memory
  103. alias dfs='df -PTah --sync | sort -f | column -t'                            #> df: (P)osix format, show fs (T)ype, (a)ll, (h)uman read-able, sync first
  104. alias dfi='df -PTai --sync | sort -f | column -t'                            #> df: (P)osix format, show fs (T)ype, (a)ll, show (i)nodes informations instead of blocks, sync first
  105. alias dfw='watch --differences -n 5 "df -PTah --sync | sort -f | column -t"' #> df: watch df, update every 5sec, highlight differences
  106. alias mounted='mount | sort -f | column -t'     #> mount: formatted nicely
  107. alias uspc='du -h --max-depth=1'                #> du: current dir and subdirectorires
  108. alias free='free -tm'                           #! free mem, swap, (t)otal in (m)ega bytes
  109.  
  110. #######################################################################
  111. #_ Perl
  112. alias p='perl -nlE'                             #> perl: process file by line, chomp
  113. alias pcsv='perl -naF"," -lE'                   #> perl: autosplit for csv files
  114. alias ptsv='perl -naF"\t" -lE'                  #> perl: autosplit for tab separated values files
  115. alias pspc='perl -naF"\s+" -lE'                 #> perl: autosplit for blank space separated values files
  116. alias pp='perl -plE'                            #> perl: process file by line, chomp and print
  117. alias pdbg='perl -dE 1'                         #> perl: perl debugger
  118. alias dpl='perl -MO=Deparse'                    #> perl: show how perl see this code
  119. alias dpl2='perl -MO=Deparse,-p,-q,-sCT'        #> perl: show how perl see this code, with parens and more
  120. alias cpan='perl -MCPAN -E shell'               #> perl: Start CPAN
  121.  
  122. function pmod {  perl -M$1 -E 'print join "\t", $ARGV[0], $ARGV[0]->VERSION' $1; }
  123.  
  124. #######################################################################
  125. #_ Processes and jobs
  126. alias psaux='ps ax --cols 1024 --format pid,euser,egroup,pri,nice,start,etime,%cpu,vsize,tty,args | less' #> ps: custom format...
  127. alias pstree='pstree -Aplnu | P'                #! pstree: (A)SCII, (p)ids, (l)ong lines, sort by ancestor pid (n)umber, show (u)id changes
  128. alias j='jobs -l'                               #> jobs: (l)ist process id too
  129. alias top10="uname -a; uptime; echo; ps -eo pcpu,rss,stime,user,pid,cmd --sort -pcpu | head -11 | column -t | cut -c -$COLUMNS; echo; ps -eo vsize,rss,size,stime,user,pid,cmd --sort -rss | head -11 | column -t | cut -c -$COLUMNS; echo; free -mt" #> top 10 cpu/memory processes
  130. #alias psg='ps aux | egrep -i'                   #> use pgrep instead
  131.  
  132. #######################################################################
  133. #_ Debugging
  134. alias strace='strace -Ff -ttt -T -C -v -a 128'  #! strace: trace (F)orked child, output to (f)ile.pid, unix (ttt)imestamp, elapsed (T)ime, (C) time/calls/errors + summary, print en(v)ironment, ret v(a)l on column 128
  135.  
  136. #######################################################################
  137. #_ Network
  138. alias N='netstat -tulpn'                        #> netstat: Show Netword Connections, don't resolve hotnames
  139. alias NN='ss -rape | P'                         #> ss: all types, processes infos, detailled infos
  140. alias NNN='netstat -tupae'                      #> netstat: Show Network Connection, resolve hostnames
  141. alias NS='netstat -nat | awk '\''NR<=2{next} {print $6}'\'' | sort | uniq -c; echo; ss -s'        #> netstat:ss: Summary of Network Connections States
  142. # [TODO] unhide-tcp, lsof, mtr, netwox
  143. alias sprunge='curl -F "sprunge=<-" http://sprunge.us'                                            #> curl: Send stdin to http://sprunge.us/ see also pastebinit
  144. alias internet_addr='lynx -dump http://ifconfig.me/all | egrep "ip_addr|remote_host"'             #> lynx: fetch http://ifconfig.me/all and display ip address and hostname as seen from the intertubes
  145. alias W='lynx'                                                                                    #> lynx/links
  146. alias WS='lynx -source'                                                                           #> lynx: Web Page Source
  147. alias WD='lynx -dump -justify -hiddenlinks=merge -image_links -ismap'                             #> lynx: Web Page Dump (formatted html)
  148. alias WL='lynx -dump -justify -hiddenlinks=merge -image_links -ismap -nonumbers -listonly'        #> lynx: Web Page Links Dump
  149. alias WF='lynx -dump -justify -hiddenlinks=merge -image_links -ismap -nonumbers -listonly -stdin' #> lynx: Format HTML Document from stdin
  150. alias WH='lynx -dump -head'                                                                       #> lunx: dump webserver headers
  151. alias Wcp='wget --recursive --no-clobber --page-requisites --adjust-extension --convert-links --restrict-file-names=windows,lowercase --no-parent --user-agent="Googlebot/2.1 (+http://www.googlebot.com/bot.html)"' #> wget: copy website "http://example.com/dir/*" add -nd to save without the directories, see also httrack
  152. # [TODO] wget conditional: add cookies support  / referrer
  153. alias FreeDNS='echo -e "DnsAdvantage: 156.154.70.1   156.154.71.1\nDynDNS:       216.146.35.35  216.146.36.36\nEpimeros:     46.4.27.2\nGeekNode:     81.93.248.69   81.93.248.68   91.194.60.196 193.17.192.53\nGoogle:       8.8.8.8        8.8.4.4\nLegtux:       88.190.27.51\nNorton:       198.153.192.1  198.153.194.1\nOpenDNS:      208.67.222.222 208.67.220.220\nScrubIt:      67.138.54.100  207.225.209.66\nTelecomix:    91.191.136.152 85.229.85.109\nVerizon:      4.2.2.1        4.2.2.2        4.2.2.3  4.2.2.4  4.2.2.5  4.2.2.6"' #> echo: display a list of dns servers (not crippled) 2001:4860:4860::8888 and/or 2001:4860:4860::8844
  154. alias httpserv="python -m 'SimpleHTTPServer'"   #> python: serve the current directory as a static website
  155.  
  156.  
  157. #######################################################################
  158. #_ Packages Management (needs apt-file)
  159. alias pkgprovides='dpkg -S'                     #> dpkg: which package provides an installed file?
  160. alias pkginstalls='dpkg -L'                     #> dpkg: show the files a packages installs
  161. alias pkginstalled='dpkg --get-selections'      #> dpkg: list installed packages
  162. alias pkginfo='apt-cache show'                  #> apt-cache: show packages informations
  163. alias pkgsearch='apt-cache --names-only search' #> apt-cache: grep packages names
  164. alias pkgsearchdesc='apt-cache search'          #> apt-cache: grep packages names and descriptions
  165. alias pkgcontains='apt-file -i list'            #> apt-file: search filenames (-l for pkgnames)
  166. alias pkgseek='apt-file search'                 #> apt-file: search files and directories (-l for pkgnames)
  167. alias pkgsim='apt-get --simulate --no-install-recommends install' #> apt-get: simulate install
  168. alias pkgsimfull='apt-get --simulate install'   #> apt-get: simulate install with recommendations
  169. alias pkgupdate='sudo apt-get update && sudo apt-get upgrade' #> apt-get: update packages
  170.  
  171. #######################################################################
  172. #_ Calculations and Units convertions
  173. alias Q="qalc"                                  #> qalc: quick calculations/units & money conversion (http://qalculate.sf.net/)
  174. alias bc='bc -ql'                               #= bc: should be quiet, and with mathlib
  175. alias units='units -qv'                         #! units: should be quiet AND verbose (guess that one)
  176. alias cls='echo -e \\033c;reset;clear'          #> reset:clear: Reset terminal, clear screen
  177.  
  178. #######################################################################
  179. #_ Various
  180. alias dmsg='dmesg | less'                       #> dmesg: List kernel boot messages
  181. #alias h='pman'                                 #> pman: manual page browser
  182. alias mrproper='find . \( -type f -and -iname \*.bak -or -iname \*.bck -or -iname \*~ \) -exec rm -f "{}" \;' #> Delete backups and other temp files
  183. alias notes="vim $NOTES"                    #> $EDITOR $NOTES [TODO] should be a function  with gpg
  184. alias t='todo'                                  #> (Gina Trapani's http://todotxt.com/)
  185. alias tar='tar -b 32768 --atime-preserve'       #! tar: (b)lock size ~16M are ok (faster/smaller), no access time modifications
  186. alias Z='sudo'                                  #> sudo (should add alias judo='sudo !!', sometimes you know you suck with ubuntu)
  187. alias hr='echo -e "\n##==------------------------------------\n"' # echo horizontal line of 40 chars (starting with ##==--)
  188. alias minicom='minicom -c on'                   #! minicom: enable (c)olors (hell, yes, it's been years i haven't used that, but i'm kind of nostalgic about it)
  189. alias XArgs='xargs -I{} -P8 -n1'                #> xargs: 8 processes simultaneously
  190. alias xevkeys='xev | grep -A2 --line-buffered "^KeyRelease" | sed -n "/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p"' #> xev: show X11 keycodes and keynames
  191. alias isprime="perl -wle 'print qq!Prime! if (1 x shift) !~ /^1?$|^(11+?)\1+$/'" #> check if a number below 65536 is prime
  192. alias githubmd='curl https://api.github.com/markdown/raw -s -XPOST -H Content-Type:text/plain -d @-'
  193.  
  194. #######################################################################
  195. # Use the same editor
  196. #alias gvim="gvim --servername VIM_$USER --remote-silent" #! use the same gvim for every edit
  197. #alias vim="vim --servername VIM_$USER --remote-silent" #! use the same vim for every edit
  198. #alias vi="vim --servername VIM_$USER --remote-silent" #! no vi
  199.  
  200. #######################################################################
  201. #_ Stupid aliases
  202. alias fortune='fortune -ao'                     #= fortune: Take (a)ll/(o)ffensive fortunes
  203. alias snes='snes9x -y3 -dfr'                    #> snes9x: 2xSaI mode, show framerate
  204.  
  205. #######################################################################
  206. #_ Date/Time Stamps
  207. alias ts="date -u +%Y%m%d_%H%M%S"               #> date: Output a timestamp (UTC). Touch `ts`-newfile. Format: +%Y%m%d_%H%M%S
  208. alias tsd="date -u +%Y%m%d"                     #> date: Output only the date
  209. alias tsh="date -u +%H%M%S"                     #> date: Output only the time (tst conflicts pvm, besides it's misleading)
  210.  
  211. #alias terminfo="infocmp -1 | sed -nu 's/^[ \000\t]*//;s/[ \000\t]*$//;/[^ \t\000]\{1,\}/!d;/acsc/d;s/=.*,//p'|column -c80" #> infocmp: display terminal capabilities
  212.  
  213. #######################################################################
  214. #_ X aliases
  215. alias G="gqview"                               #> gqview
  216. alias D0='export DISPLAY=:0'                    #> export DISPLAY=:0
  217. alias DS='echo DISPLAY=$DISPLAY TERM=$TERM LINES=$LINES COMLUMNS=$COLUMNS' #> shows $DISPLAY $TERM $LINES and $COLUMNS
  218. alias DExp='xhost +'                            #> xhost: + you can replace with +localhost or +si:localuser:username +local:local
  219. alias xterm='xterm -fb "" -bg black -fg gray -fa "Sans Mono" -fs 9 +sb -sl 3000 -g 80x50+1+1' #! xterm: font and color preferences
  220.  
  221. #######################################################################
  222. #_ Clipboard
  223. alias CC='xclip -selection clipboard'           #> xclip: Copy stdin to X clipboard, add -f to use as a filter
  224. alias CV='xclip -o -selection clipboard'        #> xclip: Paste (o)uput clipboard to stdout
  225. alias CS='xclip -o -selection primary'          #> xclip: Paste selection
  226.  
  227. # Cygwin's clipboard
  228. if [ `uname -o` = "Cygwin" ]; then
  229.     alias CC='putclip -n'
  230.     alias CV='getclip -n'
  231.     alias CS='getclip -n'
  232. fi
  233.  
  234. #######################################################################
  235. # Being cautious on mondays...
  236. #if [[ $(date +%u) -eq 1 ]]; then
  237. #   alias rm='rm -i'
  238. #   alias cp='cp -i'
  239. #   alias mv='mv -i'
  240. #fi
  241.  
  242. #######################################################################
  243. #_ Directories operations: mc() md() rd mdtmp()
  244. # MC
  245. unset -f mc
  246. function mc () {                                #! mc: cd to last selected directory (from Torben Fjerdingstad)
  247.     mkdir -pm 0700 $HOME/.mc/tmp 2>/dev/null;
  248.     OLD_TERM=$TERM
  249.     TERM='xterm'
  250.     MC=$HOME/.mc/tmp/mc-$$;
  251.     TERM=$OLD_TERM
  252.     /usr/bin/mc -a -P $MC "$@";
  253.     cd "`cat $MC`";
  254.     rm -f "$MC";
  255.     unset -v OLD_TERM MC;
  256. }
  257.  
  258. # Directory creations
  259. unset -f md
  260. function md () {                                #! mkdir:cd: mkdir and cd into it
  261.     mkdir -p "$*" && cd "$*"
  262. }
  263.  
  264. #! rm: Remove non-empty directories
  265. alias rd="rm -Rf"
  266.  
  267. unset -f mdtmp                                  #! create a temporary dir and set $TSTMP
  268. function mdtmp () {
  269.     export TSTMP="~/tmp/`date -u +%Y%m%d_%H%M%S`-$1";
  270.     mkdir -p "$TSTMP";
  271. }
  272.  
  273.  
  274. #######################################################################
  275. #_ Save a copy of the file: save() edit()
  276. unset -f save
  277. function save() {
  278.     cp "$1" "$1.`date -u +%Y%m%d_%H%M%S`.$USER.sav"
  279. }
  280.  
  281. # Safe Edit
  282. unset -f edit
  283. function edit () {
  284.     cp "$1" "$1.`date -u +%Y%m%d_%H%M%S`.$USER.sav"
  285.     vim "$1"
  286. }
  287.  
  288.  
  289. #######################################################################
  290. #_ Scripts Record/Replay: scriptrec() scriptplay()
  291. # Save scripts for replay
  292. unset -f scriptrec
  293. function scriptrec () {
  294.     local SCRIPT="$1.script"
  295.     local TMPDIR="$TMP/`date -u +%Y%m%d_%H%M%S`-$SCRIPT"
  296.     local TIMINGS="$SCRIPT.timings"
  297.     local OUTFILE="$SCRIPT.tar.bz2"
  298.  
  299.     mkdir -pm 0700 "$TMPDIR"
  300.     script -t "$TMPDIR/$SCRIPT" 2> "$TMPDIR/$TIMINGS"
  301.     echo Usage: scriptreplay "$SCRIPT" "$TIMINGS" 3.5 > $TMPDIR/README
  302.     tar -cj -C "$TMPDIR" -f "$OUTFILE" "$SCRIPT" "$TIMINGS" README
  303.     rm -Rf $TMPDIR
  304. }
  305.  
  306. # Replay a saved script with turbo 3.5x (looks good here)
  307. unset -f scriptplay
  308. function scriptplay () {
  309.     local SCRIPT=${1%.tar.bz2}
  310.     local TMPDIR="$TMP/`date -u +%Y%m%d_%H%M%S`-$SCRIPT"
  311.     local TIMINGS="$SCRIPT.timings"
  312.  
  313.     mkdir -pm 0700 "$TMPDIR"
  314.     tar -xj -C "$TMPDIR" -f "$1"
  315.     scriptreplay "$TMPDIR/$SCRIPT.timings" "$TMPDIR/$SCRIPT" 3.5
  316.     rm -Rf $TMPDIR
  317. }
  318.  
  319.  
  320. #######################################################################
  321. #_ From bash_aliases docs for csh compatibility: setenv() unsetenv
  322. alias unsetenv=unset
  323.  
  324. unset -f setenv
  325. function setenv () {
  326.     export $1="$2"
  327. }
  328.  
  329.  
  330. #######################################################################
  331. #_ Path tools: showpath loadpath() appendpath() prependpath()
  332. alias showpath='echo -e ${PATH//:/\\n}'
  333.  
  334. unset -f loadpath
  335. function loadpath () {
  336.     local LINE=1
  337.     local DIR
  338.     local NEWPATH
  339.  
  340.     while read -r DIR; do
  341.         DIR=${DIR%#*}                # remove comments
  342.         DIR=${DIR##*([$' \t\n'])}    # trim trailling spaces
  343.         DIR=${DIR%%*([$' \t\n'])}
  344.         eval "DIR=\"$DIR\""          # evaluate vars
  345.  
  346.         if [[ $DIR ]]; then
  347.             if [[ -d "$DIR" && -r "$DIR" ]]; then
  348.                 NEWPATH="$NEWPATH:$DIR"
  349.             else
  350.                 echo loadpath: Discarded "$DIR" at line $LINE from your PATH.
  351.             fi
  352.         fi
  353.  
  354.         let LINE=LINE+1
  355.     done < $1
  356.  
  357.     if [[ $NEWPATH ]]; then
  358.         export PATH=$NEWPATH
  359.     else
  360.         echo loadpath: Empty new path, your old PATH has been preserved.
  361.     fi
  362.  
  363.     unset -v LINE DIR NEWPATH
  364. }
  365.  
  366. # add to the end of path
  367. unset -f appendpath
  368. function appendpath () {
  369.     if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" ; then
  370.         eval "$1=\$$1:$2"
  371.     fi
  372. }
  373.  
  374. # add to the front of path
  375. unset -f prependpath
  376. prepend_path() {
  377.     if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" ; then
  378.         eval "$1=$2:\$$1"
  379.     fi
  380. }
  381.  
  382.  
  383. #######################################################################
  384. #_ Escape a filename properly: shquot()
  385. shquot () {
  386.     local quoted=${0/\'/\'\\\'\'}
  387.     echo "'$quoted'"
  388. }
  389.  
  390.  
  391. #######################################################################
  392. #_ Repeat n times command: repeat()
  393. function repeat() {
  394.     local i max
  395.     max=$1; shift;
  396.     for ((i=1; i <= max ; i++)); do  # --> C-like syntax
  397.         eval "$@";
  398.     done
  399. }
  400.  
  401.  
  402. #######################################################################
  403. #_ Get some informations: hi()
  404. function hi() {
  405.     echo -ne "\nHi $USER!\n\nYou are logged on: "uname -a
  406.     echo -ne "\n*** On `date -u +%Y%m%d_%H%M%S`";
  407.     echo -ne "Uptime is ";              uptime
  408.     echo -ne "\n*** Memory stats:\n";   free -tm
  409.     echo -ne "\n*** Users logged on: "; w -h
  410.  
  411.     local MY_IP=$(ifconfig eth0 | awk '/inet/ { print $2 } ' | sed -e s/addr://)
  412.     local INET_IP=$(wget -qO - icanhazip.com)
  413.     echo -ne "\n*** Local IP Address: ";           echo ${MY_IP:-"Not connected"}
  414.     echo -ne "*** Internet IP Address: ";          echo ${INET_IP:-"Not connected"}
  415. }
  416.  
  417.  
  418. #######################################################################
  419. #_ Ask Yes/No: ask()
  420. function ask() {
  421.     local ans
  422.     echo -n "$@" '[y/N] ' ; read ans
  423.     case "$ans" in
  424.         y*|Y*) return 0 ;;
  425.         *) return 1 ;;
  426.     esac
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement