Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

638 linhas
16 KiB

  1. #!/bin/bash
  2. # If not running interactively, don't do anything
  3. [[ $- != *i* ]] && return
  4. ##########################################
  5. null(){
  6. "$@" >/dev/null 2>&1
  7. }
  8. __try_exec(){
  9. type $1 >/dev/null 2>&1 && "$@"
  10. }
  11. ##########################
  12. # system type
  13. alias ismsys=false
  14. alias iscygwin=false
  15. alias iswindows=false
  16. alias isdarwin=false
  17. alias islinux=false
  18. alias with_coreutils=false # for mac
  19. case `uname` in
  20. (MINGW*) alias ismsys=true ;;
  21. (CYGWIN*) alias iscygwin=true ;;
  22. (Darwin*) alias isdarwin=true ;;
  23. (Linux*) alias islinux=true ;;
  24. esac
  25. null ls --version && alias with_coreutils=true
  26. ( ismsys || iscygwin ) && alias iswindows=true
  27. #################################
  28. export PS1 # PS1 is defined later
  29. # PROMPT_COMMAND=prompt_function
  30. if false iswindows
  31. then
  32. export PAGER='tr -d \\r | less'
  33. else
  34. export PAGER="less"
  35. fi
  36. export LESS="-iRMXF"
  37. if null type vim
  38. then
  39. export EDITOR=vim
  40. else
  41. export EDITOR=vi
  42. fi
  43. export LANG=ja_JP.UTF-8
  44. export LC_MESSAGES=C
  45. # export CDPATH=".:~"
  46. export VISUAL="$EDITOR"
  47. export GIT_PAGER="$PAGER"
  48. export GIT_EDITOR="$EDITOR"
  49. echo "$TERM" | grep '^screen' >/dev/null 2>&1 || export TERM_ORIG=$TERM
  50. test "$TERM" = screen && test "$TERM_ORIG" = xterm-256color && TERM=screen-256color
  51. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  52. mkdir -p "$TMP"
  53. ! iswindows && null type stty && {
  54. stty stop undef # unbind C-s to stop displaying output
  55. # stty erase '^h'
  56. }
  57. if iswindows; then
  58. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  59. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  60. export USER=$USERNAME
  61. fi
  62. _tmux_prefs(){
  63. null type tmux || return 1
  64. tmux set -g mode-keys vi
  65. }
  66. #######################
  67. iswindows && alias tty="echo cmd.exe"
  68. type fortune >/dev/null 2>&1 && {
  69. echo
  70. fortune
  71. echo
  72. fortune -o
  73. echo
  74. }
  75. uname -a
  76. echo TERM $TERM $(tput colors) colors connected to $(tty), running $BASH $BASH_VERSION
  77. echo
  78. ###################################
  79. # some aliases and functions
  80. ( ! with_coreutils && isdarwin ) || test "$TERM" == dumb || _coloroption=" --color=auto"
  81. ( ! with_coreutils && isdarwin ) || iswindows || _timeoption=" --time-style=long-iso"
  82. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  83. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  84. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  85. alias ls="ls -hCF${_coloroption}${_timeoption}"
  86. # export GREP_OPTIONS=""
  87. alias grep="grep -n${_coloroption}"
  88. iswindows && alias grep="grep -n"
  89. # alias ll="ls -l"
  90. # alias la="ls -A"
  91. # alias lla="ls -Al"
  92. # alias less=""
  93. alias em="emacs -nw"
  94. null type vim && alias vi=vim
  95. alias pstree="LANG=C pstree"
  96. alias cp="cp -v"
  97. alias mv="mv -v"
  98. alias psaux="ps auxww"
  99. alias q=exit
  100. alias e3=e3em
  101. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  102. alias dirs="dirs -v -l"
  103. alias po=popd
  104. alias pu=pushd
  105. alias sudo="sudo " # use aliases through sudo
  106. alias halt="sudo halt"
  107. alias reboot="sudo reboot"
  108. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  109. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  110. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  111. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  112. alias rand="echo \$RANDOM"
  113. alias xunp="file-roller -h"
  114. alias pc="sudo \paco -D"
  115. alias pycalc="python -i -c 'from math import *' "
  116. alias py3=python3
  117. alias py2=python2
  118. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  119. # alias mytime="date +%Y%m%d-%H%M%S"
  120. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  121. # type trash >/dev/null 2>&1 && alias rm=trash
  122. alias mpg123="mpg123 -C -v --title"
  123. export PLAYER="mpg123 -C -v --title"
  124. alias screen="screen -e^z^z"
  125. alias zcd="cd \`zenity --file-selection --directory\`"
  126. alias pad=notepad
  127. null type gedit && alias pad=gedit
  128. null type leafpad && alias pad=leafpad
  129. alias wic=wicd-curses
  130. alias wil="wicd-cli -y -l | head"
  131. #alias wicn="wicd-cli -y -c -n"
  132. wicn(){
  133. if test $# -eq 0
  134. then
  135. local num
  136. wicd-cli -y -l | head
  137. echo -n "input num: "
  138. read num
  139. test -n "$num" && wicd-cli -y -c -n $num
  140. else
  141. wicd-cli -y -c -n $1
  142. fi
  143. }
  144. alias aptin="apt-get install"
  145. alias aptsearch="apt-cache search"
  146. alias aptshow="apt-cache show"
  147. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  148. /usr/share/vim/vim73/macros/less.sh
  149. do
  150. test -f $f && alias vl=$f && break
  151. done
  152. alias yt=yaourt
  153. null type pacman-color && {
  154. alias pacman=pacman-color
  155. export pacman_program=pacman-color # used by pacmatic
  156. export PACMAN=pacman-color # used by yaourt
  157. }
  158. null type pacmatic && {
  159. alias pacman="pacmatic"
  160. export PACMAN="pacmatic"
  161. }
  162. null type apt-get && \
  163. alias aupgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  164. null type port && \
  165. alias port="port -v"
  166. alias pupgrade="sudo port -v selfupdate && { sudo port -v upgrade outdated; sudo port -v uninstall leaves; }"
  167. if iscygwin; then
  168. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  169. alias cygsu="cygstart /cygwinsetup.exe"
  170. alias emacs="CYGWIN=tty emacs -nw"
  171. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  172. fi
  173. alias g=git
  174. if null type _git # enable programmable completion for g
  175. then
  176. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  177. || complete -o default -o nospace -F _git g
  178. fi
  179. netwait(){
  180. while ! ping example.com -c 1
  181. do
  182. true
  183. done
  184. echo network works.
  185. }
  186. cd(){
  187. if test $# -eq 0
  188. then
  189. pushd ~/ >/dev/null
  190. elif test $1 = -
  191. then
  192. local pwd="$PWD"
  193. popd >/dev/null
  194. pushd -n "$pwd" >/dev/null # stack last dir
  195. else
  196. pushd "$@" >/dev/null
  197. fi
  198. }
  199. pushd(){
  200. local pwd="$PWD"
  201. for l in $(\dirs -v -l | \grep "^ [0-9]\+ *${pwd}$" | cut -d " " -f 2 | tac)
  202. do
  203. echo $l
  204. test $l -eq 0 && continue
  205. popd +$l -n
  206. done
  207. command pushd "$@" >/dev/null
  208. }
  209. input(){
  210. local foo
  211. stty -echo
  212. read foo
  213. stty echo
  214. echo $foo
  215. }
  216. tmux(){
  217. if test $# -eq 0
  218. then
  219. if command tmux has -t main
  220. then
  221. command tmux attach -t main
  222. else
  223. command tmux new -s main
  224. fi
  225. else
  226. command tmux "$@"
  227. fi
  228. }
  229. mcrypt-stream(){
  230. test $# -eq 2 || return 1
  231. case $1 in
  232. en)
  233. mcrypt --key $2 | base64 ;;
  234. de)
  235. base64 -d | mcrypt -d --key $2 ;;
  236. esac
  237. }
  238. gpg-stream(){
  239. test $# -eq 2 || return 1
  240. case $1 in
  241. en)
  242. gpg --passphrase $2 -c --batch |base64 ;;
  243. de)
  244. base64 -d|gpg --passphrase $2 -d --batch ;;
  245. esac
  246. }
  247. alias enst="gpg-stream en"
  248. alias dest="gpg-stream de"
  249. showinfo(){
  250. echo "Japanese letters are 表示可能"
  251. __try_exec diskinfo
  252. ! isdarwin && test -n "${DISPLAY}" && {
  253. __try_exec xrandr | \grep --color=never ^Screen
  254. }
  255. iswindows || __try_exec finger $USER
  256. LANG=C __try_exec id
  257. __try_exec xset q
  258. }
  259. x(){
  260. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  261. #mkdir -p ~/.my/log
  262. # nohup startx >~/.my/log/xorg.log 2>&1 &
  263. # exit
  264. exec startx
  265. else
  266. echo "X cant be started! Maybe another X is already running or something." 1>&2
  267. fi
  268. }
  269. bak(){
  270. for file in "$@"
  271. do
  272. cp -v ${file} ${file}.bak
  273. done
  274. }
  275. di(){
  276. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  277. then
  278. local diffcmd=colordiff
  279. else
  280. local diffcmd=diff
  281. fi
  282. ${diffcmd} -u "$@" | ${PAGER}
  283. }
  284. tb(){
  285. local tb=~/.my/tb
  286. mkdir -p $tb
  287. for file in "$@"
  288. do
  289. mv $file $tb
  290. done
  291. }
  292. mkcd(){
  293. mkdir -p $1
  294. cd $1
  295. }
  296. catclip(){
  297. if iswindows
  298. then
  299. cat /dev/clipboard | tr -d \\r
  300. elif isdarwin
  301. then
  302. pbpaste
  303. else
  304. xclip -o -selection "clipboard"
  305. fi
  306. }
  307. setclip(){
  308. if iswindows
  309. then
  310. if test $# -eq 0
  311. then
  312. sed -e 's/$/\r/' | tee /dev/clipboard
  313. else
  314. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  315. fi
  316. elif isdarwin
  317. then
  318. if test $# -eq 0
  319. then
  320. pbcopy
  321. else
  322. cat "$@" | pbcopy
  323. fi
  324. else
  325. if test $# -eq 0
  326. then
  327. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  328. else
  329. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  330. fi
  331. fi
  332. }
  333. open_file(){
  334. if iswindows
  335. then
  336. cmd.exe //c start "" "$@"
  337. elif isdarwin
  338. then
  339. touch "$@"
  340. open "$@"
  341. elif islinux
  342. then
  343. touch "$@"
  344. if null type pcmanfm; then
  345. LC_MESSAGES= pcmanfm "$@"
  346. else
  347. LC_MESSAGES= xdg-open "$@" &
  348. fi
  349. else
  350. cat "$@"
  351. fi
  352. }
  353. o(){
  354. if test $# -eq 0
  355. then
  356. open_file .
  357. else
  358. for f in "$@"
  359. do
  360. open_file "$f"
  361. done
  362. fi
  363. }
  364. convmv-sjis2utf8-test(){
  365. convmv -r -f sjis -t utf8 *
  366. }
  367. convmv-sjis2utf8-notest(){
  368. convmv -r -f sjis -t utf8 * --notest
  369. }
  370. __my_moc_state(){
  371. type mocp >/dev/null 2>&1 || return
  372. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  373. printf "$1" "`mocp -Q %title 2>/dev/null`"
  374. }
  375. __my_parse_svn_branch() {
  376. local LANG=C
  377. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  378. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  379. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  380. }
  381. __my_svn_ps1(){
  382. if svn status >/dev/null 2>&1
  383. then
  384. local svn_branch=$(__my_parse_svn_branch)
  385. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  386. fi
  387. }
  388. #Change ANSI Colors
  389. _chengecolors(){
  390. echo -e \
  391. "\e]P0000000" \
  392. "\e]P1cd0000" \
  393. "\e]P200cd00" \
  394. "\e]P3cdcd00" \
  395. "\e]P41e90ff" \
  396. "\e]P5cd00cd" \
  397. "\e]P600cdcd" \
  398. "\e]P7353535" \
  399. "\e]P8666666" \
  400. "\e]P9ff9999" \
  401. "\e]Pa99ff99" \
  402. "\e]Pbffff99" \
  403. "\e]Pc9999ff" \
  404. "\e]Pdff99ff" \
  405. "\e]Pe99ffff" \
  406. "\e]Pfffffff"
  407. }
  408. # printf "\e]P7353535" \
  409. _colors(){
  410. echo -e \
  411. "\e[30mBlack" \
  412. "\e[31mRed" \
  413. "\e[32mGreen" \
  414. "\e[33mYellow" \
  415. "\e[34mBlue" \
  416. "\e[35mMagenta" \
  417. "\e[36mCyan" \
  418. "\e[37mWhite"
  419. echo -e \
  420. "\e[30;1mBright Black" \
  421. "\e[31;1mBright Red" \
  422. "\e[32;1mBright Green" \
  423. "\e[33;1mBright Yellow" \
  424. "\e[34;1mBright Blue" \
  425. "\e[35;1mBright Magenta" \
  426. "\e[36;1mBright Cyan" \
  427. "\e[37;1mBright White\n" \
  428. "\e[0m"
  429. }
  430. winln(){
  431. # for windose make link (actually junction)
  432. if test $# -eq 0
  433. then
  434. {
  435. echo "usage: winln TARGET LINK_NAME"
  436. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  437. echo "About other features run 'junction'."
  438. } 1>&2
  439. return 1
  440. else
  441. junction "$2" "$1"
  442. fi
  443. }
  444. __my_battery_status(){
  445. local dir=/sys/class/power_supply/BAT0
  446. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && test -r $dir/charge_now
  447. then
  448. local st=$(cat $dir/status)
  449. local full=$(cat $dir/charge_full)
  450. local now=$(cat $dir/charge_now)
  451. local rate=$(expr $now \* 100 / $full)
  452. printf "$1" "${st}:${rate}%"
  453. fi
  454. }
  455. alias bat='__my_battery_status %s\\n'
  456. ip-address(){
  457. test type ifconfig >/dev/null 2>&1 || return 1
  458. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  459. test -n "$ip" && printf $1 $ip
  460. }
  461. __my_ps1_scale(){
  462. local last=$?
  463. echo "[LC:${LINES}x${COLUMNS}]"
  464. return $last
  465. }
  466. __my_ps1_tmux(){
  467. local last=$?
  468. local tmuxc="$(__try_exec tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  469. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  470. return $last
  471. }
  472. __my_ps1_moc(){
  473. local last=$?
  474. __my_moc_state "[MOC:%s]"
  475. return $last
  476. }
  477. test -r /usr/share/git/git-prompt.sh && . /usr/share/git/git-prompt.sh
  478. __my_ps1_git(){
  479. local last=$?
  480. null type __git_ps1 || return $last
  481. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  482. return $last
  483. }
  484. __my_ps1_ipaddr(){
  485. local last=$?
  486. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  487. return $last
  488. }
  489. __my_ps1_bttry(){
  490. local last=$?
  491. local bst="${TMP}/batterystatus"
  492. if test -z "$DISPLAY" && ! iswindows
  493. then
  494. test -f $bst && local bstr="$(cat $bst)"
  495. test -n "$bstr" && echo "[Battery:$bstr]"
  496. __my_battery_status %s >$bst &
  497. fi
  498. return $last
  499. }
  500. __my_ps1_dirs(){
  501. dirs | wc -l
  502. }
  503. __my_ps1_jobs(){
  504. jobs | wc -l
  505. }
  506. if test "$TERM" != dumb
  507. then
  508. __my_c1="\[\e[1;31m\]" # color for PWD
  509. __my_c2="\[\e[0;36m\]" # color for user
  510. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  511. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  512. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;33m\]"
  513. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  514. else __my_c4="\[\e[1;32m\]" # color for ::
  515. fi
  516. __my_cdef="\[\e[0m\]"
  517. fi
  518. _PS1="\
  519. ${__my_c4}:: ${__my_cdef}[${__my_c1}\w/${__my_cdef}<${__my_c3}\${OLDPWD}${__my_cdef}]\$(__my_ps1_scale)\$(__my_ps1_tmux)\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  520. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  521. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} cnum:\# jobs:\j last:\$? \$ "
  522. PS1=$_PS1
  523. __my_set_title(){
  524. title="$(echo $@)"
  525. case $TERM in
  526. (rxvt*|xterm*|aterm|screen*)
  527. test -t 1 &&
  528. test -n "$DISPLAY" &&
  529. test -z "$EMACS" &&
  530. echo -n -e "\033]0;${title}\007"
  531. ;;
  532. esac
  533. }
  534. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD}"
  535. # copied from https://wiki.archlinux.org/index.php/X_resources
  536. invader(){
  537. # ANSI color scheme script featuring Space Invaders
  538. #
  539. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  540. # Modified by lolilolicon
  541. #
  542. f=3 b=4
  543. for j in f b; do
  544. for i in {0..7}; do
  545. printf -v $j$i %b "\e[${!j}${i}m"
  546. done
  547. done
  548. bld=$'\e[1m'
  549. rst=$'\e[0m'
  550. cat << EOF
  551. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  552. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  553. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  554. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  555. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  556. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  557. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  558. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  559. $f7▌$rst
  560. $f7▌$rst
  561. $f7 ▄█▄ $rst
  562. $f7▄█████████▄$rst
  563. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  564. EOF
  565. }
  566. #/etc/lsb-release