-PATH="$PATH:/opt/midfin/common/lib"
# to debug
#set -x
-# redirect output to log file
-#exec 1>/a/tmp/bashlog
-#exec 2>/a/tmp/bashlog
+# redirect output to log file. this doesn't work. todo figure out why
+#exec 1>>/a/tmp/bashlog
+#exec 2>>/a/tmp/bashlog
# By default this file is sourced for ALL ssh commands. This is wonky.
-# Normally, this file is not sourced when a script is run, but we can override
-# that by having #!/bin/bash -l.
-# I want something similar for ssh commands. when a local script runs an ssh command,
-# this file should not be sourced by default, but we should be able to override that.
+# Normally, this file is not sourced when a script is run, but we can
+# override that by having #!/bin/bash -l. I want something similar for ssh
+# commands. when a local script runs an ssh command, this file should not be
+# sourced by default, but we should be able to override that.
#
# so here we test for conditions of a script under ssh and return if so. To test
# for an overriding condition, we have a few options. one is to use an
&& [[ $- != *i* ]] \
&& [[ ! $BASH_LOGIN_SHELL == true ]]; then
return
+else
+ source /etc/profile
fi
+# note, to catch errors in functions but not outside, do:
+# set -E -o pipefail
+# trap return ERR
+# trap 'trap ERR' RETURN
+
############
# still broken in wheezy
# still buggered in latest stable from the web, version 2.1
# perhaps its fixed in newer git version, which fails to make for me
-# this note is from 6-2014
+# this note is from 6-2014.
+# Also, enabling this before sourcing .bashrc makes PATH be empty.
#shopt -s nullglob
# make tab on an empty line do nothing
# works in addition to HISTCONTROL to do more flexible things
# it could also do the same things as HISTCONTROL and thus replace it,
# but meh
-HISTIGNORE="k *"
+HISTIGNORE='k *; *'
export BC_LINE_LENGTH=0
C_DEFAULT_DIR=/a
-
-
###################
## include files ###
###################
-for _x in $HOME/bin/bash-programs-by-ian/repos/*/*-function ~/bin/distro-functions/src/*; do
+for _x in /a/bin/distro-functions/src/* /a/bin/bash-programs-by-ian/repos/*/*-function; do
source "$_x"
done
unset _x
-source $HOME/bin/semi-private # so I can share my bashrc
-source $HOME/path_add-function
+source /a/bin/semi-private # so I can share my bashrc
+source $(dirname $(readlink -f $BASH_SOURCE))/path_add-function
path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
-path_add --ifexists --end /a/bin/sda/corpnext/cloudinit
# todo, these need to be renamed to be less generic.
# sync overrode something else useful
#path_add $HOME/bin/bash-programs-by-ian/utils
-source $HOME/mw_vars
+
+[[ -e $HOME/mw_vars ]] && source $HOME/mw_vars
###############
### aliases ###
#####################
+sdf() {
+ c /sdx/test/sandbox/
+}
-# netselect-apt finds a fast mirror.
-# but we need to replace the mirrors ourselves,
-# because it doesn't do that. best it can do is
-# output a basic sources file
-# here we get the server it found, get the main server we use
-# then substitute all instances of one for the other in the sources file
-# and backup original to /etc/apt/sources.list-original.
-# this is idempotent
debian_pick_mirror () {
- local x=$(mktemp -d)/f # safe way to get file name without creating one
- sudo netselect-apt -o "$x" || return 1
- x=$(_debian_pick_mirror_grep stable "$x")
+ # netselect-apt finds a fast mirror.
+ # but we need to replace the mirrors ourselves,
+ # because it doesn't do that. best it can do is
+ # output a basic sources file
+ # here we get the server it found, get the main server we use
+ # then substitute all instances of one for the other in the sources file
+ # and backup original to /etc/apt/sources.list-original.
+ # this is idempotent. the only way to identify debian sources is to
+ # note the original server, so we put it in a comment so we can
+ # identify it later.
+ local file=$(mktemp -d)/f # safe way to get file name without creating one
+ sudo netselect-apt -o "$file" || return 1
+ url=$(grep ^\\w $file | head -n1 | awk '{print $2}')
sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
- sudo sed -i "s/$(_debian_pick_mirror_grep wheezy)/$x/" /etc/apt/sources.list
- aptitude update
-}
-
-_debian_pick_mirror_grep () {
- local x="$(grep -oP "^deb [^ ]+ $1 " ${2-/etc/apt/sources.list})"
- x="${x#deb }"
- x="${x% $1 }"
- # replace / with \/ so we can use it with sed
- echo "${x//\//\\/}"
-}
-# portable daemon, for debian & fedora
-pdaemon () {
- if isdeb; then
- sudo /etc/init.d/$1 $2
- else
- sudo systemctl $2 $1.service
- fi
+ sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list
+ sudo apt-get update
}
beet "${@}"
}
+dus() {
+ du -sh ${@:-*} | sort -h
+}
t() {
- trash-put "$@"
+ local x
+ local -a args
+ if type -t trash-put >/dev/null; then
+ # skip args that don't exist, or else it's an err
+ for x in "$@"; do [[ ! -e $x ]] || args+=("$x"); done
+ [[ ! ${args[@]} ]] || trash-put "${args[@]}"
+ else
+ rm -rf "$@"
+ fi
+}
+
+ccat () { # config cat. see a config without extra lines.
+ grep '^\s*[^[:space:]#]' "$@"
}
mapfile -t
local s
[[ $EUID == 0 ]] || s=s
- $s postconf -v "${MAPFILE[@]}"
+ $s postconf -ev "${MAPFILE[@]}"
+}
+
+
+tclock() {
+ clear
+ date +%l:%_M
+ len=60
+ # this goes to full width
+ #len=${1:-$((COLUMNS -7))}
+ x=1
+ while true; do
+ if (( x == len )); then
+ end=true
+ d="$(date +%l:%_M) "
+ else
+ end=false
+ d=$(date +%l:%M:%_S)
+ fi
+ echo -en "\r"
+ echo -n "$d"
+ for ((i=0; i<x; i++)); do
+ if (( i % 6 )); then
+ echo -n _
+ else
+ echo -n .
+ fi
+ done
+ if $end; then
+ echo
+ x=1
+ else
+ x=$((x+1))
+ fi
+ sleep 5
+ done
}
gr() {
- grep -iI --color=auto "$@"
+ grep -iIP --color=auto "$@"
}
grr() {
- grep -riI --color=auto "$@"
+ if [[ ${#@} == 1 ]]; then
+ grep -riIP --color=auto "$@" .
+ else
+ grep -riIP --color=auto "$@"
+ fi
}
pub() {
rld /a/h/_site/ li:/var/www/iankelling.org/public_html
}
+mkc() {
+ mkdir "$1"
+ c "$1"
+}
+
bashrcpush () {
local startdir="$PWD"
cd ~
cd "$startdir"
}
+virshrm() {
+ for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
+}
+
whatismyip() { curl ipecho.net/plain ; echo; }
# history search
-k() { grep -P "$@" ${HISTFILE:-~/.bash_history} | tail -n 40; }
+k() { grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 40; }
calc() { echo "scale=3; $*" | bc -l; }
+# no having to type quotes, but also no command history:
+clc() {
+ local x
+ read -r x
+ echo "scale=3; $x" | bc -l
+}
# diff config files,
# setup for format of postfix
done < "$unified"
}
+cim() {
+ git commit -m "$*"
+}
+
+cam() {
+ git commit -am "$*"
+}
+
+shellck() {
+ # 2086 = unquoted $var
+ # 2046 = unquoted $(cmd)
+ # i had -x as an arg, but debian testing(stretch) doesn't support it
+ shellcheck -e 2086,2046,2068,2006,2119 "$@"
+}
# join options which are continued to multiples lines onto one line
_cdiff-prep() {
local first=true
grep -vE '^([ \t]*#|^[ \t]*$)' "$1" | while IFS= read -r line; do
- # remove leading spaces/tabs. assumes extglob
- if [[ $line == "[ ]*" ]]; then
- line="${line##+( )}"
- fi
- if $first; then
- pastline="$line"
- first=false
- elif [[ $line == *=* ]]; then
- echo "$pastline" >> "$2"
- pastline="$line"
- else
- pastline="$pastline $line"
- fi
- done
+ # remove leading spaces/tabs. assumes extglob
+ if [[ $line == "[ ]*" ]]; then
+ line="${line##+( )}"
+ fi
+ if $first; then
+ pastline="$line"
+ first=false
+ elif [[ $line == *=* ]]; then
+ echo "$pastline" >> "$2"
+ pastline="$line"
+ else
+ pastline="$pastline $line"
+ fi
+ done
echo "$pastline" >> "$2"
}
# insensitive find
ifn () {
- find . -iname '*'"$*"'*'
+ find -L . -iname "*$**" 2>/dev/null
}
if [[ $EUID == 0 ]]; then
te "$1" || touch "$1"
if [[ $(stat -c "%u" "$1") == 0 ]] ; then
- argdir=$(getdir "$1")
+ argdir=$(dirname "$1")
if [[ $(stat -c "%u" "$argdir") != 0 ]] ; then
if ! chown "--reference=$argdir" "$1"; then
echo failed to fix bad ownership file permissons
# do pwd + some other info.
#pwd() { echo "$(ll -d "$PWD") $USER@$HOSTNAME $(date +%r)"; }
-
-pwgen() { # generate a random password, with digits & punctuation and without
- arg=${1:-50}
- head -c 200 /dev/urandom | tr -cd '[:graph:]' | head -c "$arg"
- echo
- head -c 200 /dev/urandom | tr -cd '[:alnum:]' | head -c "$arg"
- head -c 5 /dev/urandom | tr -cd '[:alpha:]' | head -c "$arg"
- echo
-}
-
q() { # start / launch a program in the backround and redir output to null
"$@" &> /dev/null &
}
-
+pwgen() {
+ apg -s -m 10 -x 14 -t
+}
r() {
exit "$@" 2>/dev/null
}
+# connect to vms made with virt-install
+vspicy() {
+ spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
+ sed -r "s/.*port='([0-9]+).*/\1/")
+}
+rspicy() { # HOST DOMAIN
+ local port=$(ssh $1<<EOF
+sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
+ sed -rn "s/.*port='([0-9]+).*/\1/p"
+EOF
+ )
+ if [[ $port ]]; then
+ spicy -h $1 -p $port
+ else
+ echo "error: no port found. check that the domain is running."
+ fi
+}
+
+
# trash-restore lists everything that has been trashed at or below CWD
# This picks out files just in CWD, not subdirectories,
# which also match grep $1, usually use $1 for a time string
# rsync, root is required to keep permissions right.
# rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
-# --no-times --delete
+ # --no-times --delete
# basically, make an exact copy, use checksums instead of file times to be more accurate
rl() { rsync -ahvic --delete "$@"; }
# don't delete files on the target end which do not exist on the original end:
rld() { rsync -ahvic "$@"; }
+# useful for selectively sending dirs which have been synced with unison,
+# where the path is the same on both hosts.
+rlu() { # [OPTS] HOST PATH
+ # eg rlu -opts frodo testpath
+ o=o
+ opts=("${@:1:$#-2}") # 1 to last -2
+ path="${@:$#}"
+ host="${@:$#-1:1}"
+ if [[ $host == lj ]]; then
+ o=
+ fi
+ # rync options are without -g for group
+ s rsync -rlpthvi$o "${opts[@]}" "$path" "root@$host:$path";
+}
+
+lcn() { locate -i "*$**"; }
+
+vpn() {
+ s systemctl start openvpn@client&
+ journalctl --unit=openvpn@client -f -n0
+}
+vpnoff() {
+ s systemctl stop openvpn@client
+}
+
+
complete -F _rsync -o nospace rld rlt fl
# rl without preserving modification time. for some reason I had this as default before.
# perhaps that reason will come up again and I will document it.
rlt() { rsync -ahvic --delete --no-t "$@"; }
-
+if ! type service &>/dev/null; then
+ service() {
+ echo actually running: systemctl $2 $1
+ systemctl $2 $1
+ }
+fi
# use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
sb() {
(sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
}
+ser() {
+ local s; [[ $EUID != 0 ]] && s=sudo
+ if type -p systemctl &>/dev/null; then
+ $s systemctl $1 $2
+ else
+ $s service $2 $1
+ fi
+}
-myunison() {
- s unison -auto -owner -group
+sgo() { # service go
+ service=$1
+ ser start $service
+ if type -p systemctl &>/dev/null; then
+ ser enable $service
+ fi
+}
+
+
+vm-set-listen(){
+ local t=$(mktemp)
+ local vm=$1
+ local ip=$2
+ s virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
+ sed -r "s/listen='[^']+/listen='$ip/"> $t
+ s virsh undefine $vm
+ s virsh define $t
+}
+
+
+vmshare() {
+ vm-set-listen $1 0.0.0.0
+}
+
+vmunshare() {
+ vm-set-listen $1 127.0.0.1
}
+
tu() {
local s;
local dir="$(dirname "$1")"
}
+ediff() {
+ [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
+ emacs --eval "(ediff-files \"$1\" \"$2\")"
+}
+
tx() { # toggle set -x, and the prompt so it doesn't spam
if [[ $- == *x* ]]; then
set +x
}
+dat() { # do all tee, for more complex scripts
+ tee >(ssh frodo bash -l) >(bash -l) >(ssh x2 bash -l) >(ssh tp bash -l)
+}
+da() { # do all
+ local host
+ "$@"
+ for host in x2 tp treetowl; do
+ ssh $host "$@"
+ done
+
+}
+
+istext() {
+ grep -Il "" "$@" &>/dev/null
+}
if [[ $OS == Windows_NT ]]; then
# cygstart wrapper
}
fi
-
+khfix() { # known hosts fix
+ ssh-keygen -R $1
+ local x=$(host $1)
+ ssh-keygen -R ${x##* }
+ ssh $1 :
+}
# todo, update this
rn() {
pushd /sdx/test/sandbox/;
- ./setup.sh;
- ./run.sh;
+ ./setup.sh && ./run.sh
popd
}
if [[ $OS != Windows_NT ]]; then
GIT_PS1_SHOWDIRTYSTATE=true
fi
- # arch source location
+ # arch source lopip show -fcation
[[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
# fedora/debian source
[[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
local ps_char ps_color
unset IFS
history -a # save history
- history -n # read any new history
if [[ ! $DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
# from the screen man page
if [[ $TERM == screen* ]]; then
ps_color="$(get_term_color bold green)"
fi
fi
- PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
+ PS1="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
+ # emacs completion doesn't like the git prompt atm, so disabling it.
+ #PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
}
PROMPT_COMMAND=prompt_command
fi
# based on warning from rvmsudo
export rvmsudo_secure_path=1
-if [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
+
+if [[ -s "/usr/local/rvm/scripts/rvm" ]]; then
source "/usr/local/rvm/scripts/rvm"
+elif [[ -s $HOME/.rvm/scripts/rvm ]]; then
+ source $HOME/.rvm/scripts/rvm
+fi
+
+# https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
+# i added an extra condition as gentoo xorg guide says depending on
+# $DISPLAY is fragile.
+if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
+ exec startx
fi
-# make our last exit code be 0
-true
+# ensure no bad programs appending to this file will have an affect
+return 0