Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. ## One command to update them all
  2. function update ()
  3. {
  4. brew update
  5. brew upgrade
  6.  
  7. gisty pull_all
  8. gisty sync_delete
  9.  
  10. sudo gem update
  11. sudo pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -U -q
  12. }
  13.  
  14. ## Get colors in manual pages
  15. function man()
  16. {
  17. env \
  18. LESS_TERMCAP_mb=$(printf "\e[1;31m") \
  19. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  20. LESS_TERMCAP_me=$(printf "\e[0m") \
  21. LESS_TERMCAP_se=$(printf "\e[0m") \
  22. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  23. LESS_TERMCAP_ue=$(printf "\e[0m") \
  24. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  25. man "$@"
  26. }
  27.  
  28. ## Change directory to current Finger directory
  29. function cdf()
  30. {
  31. target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
  32. if [ "$target" != "" ]; then
  33. cd "$target"; pwd
  34. else
  35. echo 'No Finder window found' >&2
  36. fi
  37. }
  38.  
  39. ## Faster downloading with Aria
  40. function get()
  41. {
  42. aria2c -x 16 -s 16 "${1}"
  43. }
  44.  
  45. ## Use a RSA private key in git
  46. function kit ()
  47. {
  48. trap 'rm -f /tmp/.git_ssh.$$' 0
  49. SSH_KEY="~/.ssh/id_rsa"
  50. echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
  51. chmod +x /tmp/.git_ssh.$$
  52. export GIT_SSH=/tmp/.git_ssh.$$
  53. [ "$1" = "git" ] && shift
  54. git "$@"
  55. }
  56.  
  57. ## Google Auth Token Generator
  58. function token ()
  59. {
  60. google-authenticator --force --time-based --disallow-reuse --rate-limit=3 \
  61. --rate-time=30 --window-size=10
  62. }
  63.  
  64. ## Accept security dialogs until they no-longer exist
  65. function Accepts ()
  66. {
  67. osascript <<EOF
  68. tell application "System Events"
  69. repeat while exists (processes where name is "SecurityAgent")
  70. tell process "SecurityAgent" to click button "Allow" of window 1
  71. delay 0.2
  72. end repeat
  73. end tell
  74. EOF
  75. }
  76.  
  77. ## Accept security dialogs until they no-longer exist, adds credentials
  78. function AcceptWithCreds ()
  79. {
  80. username="$1"
  81. password="$2"
  82.  
  83. [ -z "${password}" ] && return 1
  84.  
  85. osascript 2>/dev/null <<EOF
  86. set appName to "${username}"
  87. set appPass to "${password}"
  88.  
  89. tell application "System Events"
  90. repeat while exists (processes where name is "SecurityAgent")
  91. tell process "SecurityAgent"
  92. if exists (text field 1 of window 1) then
  93. set value of text field 1 of window 1 to appName
  94. set value of text field 2 of window 1 to appPass
  95. end if
  96. end tell
  97. tell process "SecurityAgent" to click button "Allow" of window 1
  98. delay 0.2
  99. end repeat
  100. end tell
  101. EOF
  102.  
  103. echo 'Finished...'
  104. }
  105.  
  106. ## Sublime cannot open null files, so create them first
  107. function subl()
  108. {
  109. [ ! -f "$1" ] && {
  110. [ -z "$1" ] && {
  111. /bin/subl
  112. } || {
  113. touch "$1"
  114. }
  115. }
  116. /bin/subl "$1"
  117. }
  118.  
  119. ## Completely forgot why i wrote this one....
  120. function iterate()
  121. {
  122. [ ! -z "$1" ] || [ ! -z "$2" ] && {
  123. while read line; do
  124. eval "$2"
  125. done < <(eval "$1")
  126. }
  127. }
  128.  
  129. ## Explode a deb file
  130. function undeb()
  131. {
  132. ar p "$1" data.tar.xz |tar -Jxvf
  133. }
  134.  
  135. ## Wrap common git commands
  136. function gush()
  137. {
  138. [ ! -z "$1" ] && {
  139. git add -A . ; git commit -m"$1" ; git push
  140. } || {
  141. echo 'Must use a commit message'
  142. }
  143. }
  144.  
  145. ##
  146. # display a default hash, of md5 of input
  147. # Usage: qsh -> 22d84b304278ebbc5d5b86e9942d7414
  148. # Usage: qsh foo -> d3b07384d113edec49eaa6238ad5ff00
  149. # Usage: qsh $(date +"%T") -> ecfb1f72e032fef204eb2c3ecda70321
  150. ##
  151.  
  152. function qsh ()
  153. {
  154. # Set default for variable
  155. val="$1" ; : ${val:="foobarbazbleh"} ; md5sum <<<"$val" |awk '{print $1}'
  156. }
  157.  
  158. ##
  159. # high compression tarball of a directory or file
  160. # Usage: compress '/some/folder' -> folder.tar.xz
  161. # Usage: compress '/thefile.nfo' -> thefile.tar.xz
  162. ##
  163.  
  164. function compress ()
  165. {
  166. shortname=$(basename "$1" | awk -NF '.' '{print $1}')
  167. XZ_OPT=-9 tar -Jcvf "${shortname}.tar.xz" "$1"
  168. }
  169.  
  170. ##
  171. # send yourself something
  172. # Usage: push '/some/file.tar.gz' you@domain.com
  173. ##
  174.  
  175. function push ()
  176. {
  177. # Raise sendmails size limit to 50mb
  178. # sudo echo -e '\n# unlimited size\nmessage_size_limit = 419430400' >> /etc/postfix/main.cf
  179. # sudo /etc/init.d/postfix restart
  180.  
  181. echo ' ' | mutt -s "Received ${1}" -a "${1}" -- $2
  182. }
  183.  
  184. ## Open a OSX Terminal tab
  185. function tab ()
  186. {
  187. osascript 2>/dev/null <<EOF
  188. tell application "System Events"
  189. tell process "Terminal" to keystroke "t" using command down
  190. end
  191. tell application "Terminal"
  192. activate
  193. do script with command "exec bash; cd \"$PWD\"; $*" in window 1
  194. do script with command "clear" in window 1
  195. end tell
  196. EOF
  197. }
  198.  
  199. ## Quite terminal
  200. function close ()
  201. {
  202. osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "w" using command down'
  203. }
  204.  
  205.  
  206. #-------------------------------------------------------------
  207. # File & strings related functions:
  208. #-------------------------------------------------------------
  209.  
  210.  
  211. # Find a file with a pattern in name:
  212. function ff() { find . -type f -iname '*'"$*"'*' -ls ; }
  213.  
  214. # Find a file with pattern $1 in name and Execute $2 on it:
  215. function fe() { find . -type f -iname '*'"${1:-}"'*' \
  216. -exec ${2:-file} {} \; ; }
  217.  
  218. # Find a pattern in a set of files and highlight them:
  219. #+ (needs a recent version of egrep).
  220. function fstr()
  221. {
  222. OPTIND=1
  223. local mycase=""
  224. local usage="fstr: find string in files.
  225. Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
  226. while getopts :it opt
  227. do
  228. case "$opt" in
  229. i) mycase="-i " ;;
  230. *) echo "$usage"; return ;;
  231. esac
  232. done
  233. shift $(( $OPTIND - 1 ))
  234. if [ "$#" -lt 1 ]; then
  235. echo "$usage"
  236. return;
  237. fi
  238. find . -type f -name "${2:-*}" -print0 | \
  239. xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
  240.  
  241. }
  242.  
  243.  
  244. function swap()
  245. { # Swap 2 filenames around, if they exist (from Uzi's bashrc).
  246. local TMPFILE=tmp.$$
  247.  
  248. [ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
  249. [ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
  250. [ ! -e $2 ] && echo "swap: $2 does not exist" && return 1
  251.  
  252. mv "$1" $TMPFILE
  253. mv "$2" "$1"
  254. mv $TMPFILE "$2"
  255. }
  256.  
  257. function extract() # Handy Extract Program
  258. {
  259. if [ -f $1 ] ; then
  260. case $1 in
  261. *.tar.bz2) tar xvjf $1 ;;
  262. *.tar.gz) tar xvzf $1 ;;
  263. *.bz2) bunzip2 $1 ;;
  264. *.rar) unrar x $1 ;;
  265. *.gz) gunzip $1 ;;
  266. *.tar) tar xvf $1 ;;
  267. *.tbz2) tar xvjf $1 ;;
  268. *.tgz) tar xvzf $1 ;;
  269. *.zip) unzip $1 ;;
  270. *.Z) uncompress $1 ;;
  271. *.7z) 7z x $1 ;;
  272. *) echo "'$1' cannot be extracted via >extract<" ;;
  273. esac
  274. else
  275. echo "'$1' is not a valid file!"
  276. fi
  277. }
  278.  
  279.  
  280. # Creates an archive (*.tar.gz) from given directory.
  281. function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
  282.  
  283. # Create a ZIP archive of a file or folder.
  284. function makezip() { zip -r "${1%%/}.zip" "$1" ; }
  285.  
  286. # Make your directories and files access rights sane.
  287. function sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
  288.  
  289. #-------------------------------------------------------------
  290. # Process/system related functions:
  291. #-------------------------------------------------------------
  292.  
  293.  
  294. function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
  295. function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
  296.  
  297.  
  298. function killps() # kill by process name
  299. {
  300. local pid pname sig="-TERM" # default signal
  301. if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
  302. echo "Usage: killps [-SIGNAL] pattern"
  303. return;
  304. fi
  305. if [ $# = 2 ]; then sig=$1 ; fi
  306. for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} )
  307. do
  308. pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
  309. if ask "Kill process $pid <$pname> with signal $sig?"
  310. then kill $sig $pid
  311. fi
  312. done
  313. }
  314.  
  315. function mydf() # Pretty-print of 'df' output.
  316. { # Inspired by 'dfc' utility.
  317. for fs ; do
  318.  
  319. if [ ! -d $fs ]
  320. then
  321. echo -e $fs" :No such file or directory" ; continue
  322. fi
  323.  
  324. local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
  325. local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
  326. local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
  327. local out="["
  328. for ((j=0;j<20;j++)); do
  329. if [ ${j} -lt ${nbstars} ]; then
  330. out=$out"*"
  331. else
  332. out=$out"-"
  333. fi
  334. done
  335. out=${info[2]}" "$out"] ("$free" free on "$fs")"
  336. echo -e $out
  337. done
  338. }
  339.  
  340.  
  341. function my_ip() # Get IP adress on ethernet.
  342. {
  343. MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' |
  344. sed -e s/addr://)
  345. echo ${MY_IP:-"Not connected"}
  346. }
  347.  
  348. function ii() # Get current host related info.
  349. {
  350. echo -e "\nYou are logged on ${BRed}$HOST"
  351. echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
  352. echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
  353. cut -d " " -f1 | sort | uniq
  354. echo -e "\n${BRed}Current date :$NC " ; date
  355. echo -e "\n${BRed}Machine stats :$NC " ; uptime
  356. echo -e "\n${BRed}Memory stats :$NC " ; free
  357. echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
  358. echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
  359. echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
  360. echo
  361. }
  362.  
  363. #-------------------------------------------------------------
  364. # Misc utilities:
  365. #-------------------------------------------------------------
  366.  
  367. function repeat() # Repeat n times command.
  368. {
  369. local i max
  370. max=$1; shift;
  371. for ((i=1; i <= max ; i++)); do # --> C-like syntax
  372. eval "$@";
  373. done
  374. }
  375.  
  376.  
  377. function ask() # See 'killps' for example of use.
  378. {
  379. echo -n "$@" '[y/n] ' ; read ans
  380. case "$ans" in
  381. y*|Y*) return 0 ;;
  382. *) return 1 ;;
  383. esac
  384. }
  385.  
  386. function corename() # Get name of app that created a corefile.
  387. {
  388. for file ; do
  389. echo -n $file : ; gdb --core=$file --batch | head -1
  390. done
  391. }
  392.  
  393. function now()
  394. {
  395. date +"%Y-%m-%d @ %H:%M:%S"
  396. }
  397.  
  398. function sizeof ()
  399. {
  400. du -h "$1" |tail -n1
  401. }
  402.  
  403.  
  404. # rewrite_commit_date(commit, date_timestamp)
  405. #
  406. # !! Commit has to be on the current branch, and only on the current branch !!
  407. #
  408. # Usage example:
  409. #
  410. # 1. Set commit 0c935403 date to now:
  411. #
  412. # rewriteCommitDate 1a888939
  413. #
  414. # 2. Set commit 0c935403 date to 1402221655:
  415. #
  416. # rewriteCommitDate 1a888939 1439311554
  417. #
  418. # date +"%s" -d "Tue Aug 11 09:45:54 PDT 2015 ## 1439311554
  419.  
  420. rewriteCommitDate () {
  421. local commit="$1" date_timestamp="$2"
  422. local date temp_branch="temp-rebasing-branch"
  423. local current_branch="$(git rev-parse --abbrev-ref HEAD)"
  424.  
  425. if [[ -z "$commit" ]]; then
  426. date="$(date -R)"
  427. else
  428. date="$(date -R --date "@$date_timestamp")"
  429. fi
  430.  
  431. git checkout -b "$temp_branch" "$commit"
  432. GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
  433. git checkout "$current_branch"
  434. git rebase "$commit" --onto "$temp_branch"
  435. git branch -d "$temp_branch"
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement