lots of updates
authorIan Kelling <ian@iankelling.org>
Sat, 21 May 2016 20:02:43 +0000 (13:02 -0700)
committerIan Kelling <ian@iankelling.org>
Thu, 4 May 2017 23:40:14 +0000 (16:40 -0700)
.bash_profile
.bashrc
.inputrc
path_add-function

index fd099aa593ad0ff9c683385624cde4c26f3b3c5f..d3a6a8fd56280c26a618bf525c38b66d35e8b63a 100644 (file)
@@ -13,5 +13,5 @@
 
 [[ -f ~/.bashrc ]] && . ~/.bashrc
 
-
-[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
+# ensure no bad programs appending to this file will have an affect
+return 0
diff --git a/.bashrc b/.bashrc
index f92ecc0e63cf1f347c22bd73331f54860b418ad5..118a9ccf5ec917557a0c0a8d8dacdc304ae70a98 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -1,16 +1,15 @@
-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
@@ -34,10 +33,17 @@ if [[ $SSH_CONNECTION ]] \
        && [[ $- != *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
+
 
 
 ############
@@ -65,7 +71,8 @@ export GLOBIGNORE=*/.:*/..
 # 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
@@ -150,7 +157,7 @@ HISTCONTROL=ignoredups
 # 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
 
@@ -161,24 +168,22 @@ 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 ###
@@ -247,39 +252,28 @@ fi
 #####################
 
 
+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
 }
 
 
@@ -287,9 +281,24 @@ a() {
     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:]#]' "$@"
 }
 
 
@@ -302,21 +311,65 @@ postconfin() {
     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 ~
@@ -332,12 +385,22 @@ bashrcpush () {
     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
@@ -359,25 +422,39 @@ cdiff() {
     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"
 }
 
@@ -510,7 +587,7 @@ ic() {
 
 # insensitive find
 ifn () {
-    find . -iname '*'"$*"'*'
+    find -L . -iname "*$**" 2>/dev/null
 }
 
 
@@ -596,7 +673,7 @@ perm_fix() {
     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
@@ -705,26 +782,37 @@ pfind() { #find *$1* in $PATH
 # 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
@@ -750,17 +838,48 @@ pick-trash() {
 
 # 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() {
@@ -830,11 +949,44 @@ tm() {
     (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")"
@@ -876,6 +1028,11 @@ ts() { # start editing a new file
 
 }
 
+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
@@ -888,7 +1045,22 @@ tx() { # toggle set -x, and the prompt so it doesn't spam
 }
 
 
+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
@@ -915,7 +1087,12 @@ else
     }
 fi
 
-
+khfix() { # known hosts fix
+    ssh-keygen -R $1
+    local x=$(host $1)
+    ssh-keygen -R ${x##* }
+    ssh $1 :
+}
 
 
 # todo, update this
@@ -949,8 +1126,7 @@ hl() { # history limit. Write extra history to archive file.
 
 rn() {
     pushd /sdx/test/sandbox/;
-    ./setup.sh;
-    ./run.sh;
+    ./setup.sh && ./run.sh
     popd
 }
 
@@ -1045,7 +1221,7 @@ if [[ $- == *i* ]]; then
     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
@@ -1079,7 +1255,6 @@ if [[ $- == *i* ]]; then
         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
@@ -1108,7 +1283,9 @@ if [[ $- == *i* ]]; 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
@@ -1153,8 +1330,18 @@ 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
index 76a38b2b04cb989520ef9e7cc72b8473410a427b..c47fe9453ae3f5c75656dc90cd5188675e88311b 100644 (file)
--- a/.inputrc
+++ b/.inputrc
@@ -24,7 +24,7 @@ set show-all-if-unmodified on
 
 # nice in bash, but messes up completion in emacs shell
 #set visible-stats on
-set visible-stats offmv f
+set visible-stats off
 
 # turn off pager for completion
 set page-completions off
index 0edf2bb62cd76cec2d7dc99addda53013902217b..0dbaf9fa58de4533c48d68826ce03c37416959ac 100644 (file)
@@ -9,7 +9,7 @@ path_add() {
 --end:      adds to end of path, which will give it lowest priority
 --ifexists: add to path only if the directory exists"
     local found x y z ifexists end loop newpath
-    ifexists=false
+    force=false
     end=false
     loop=true
     # portable substring matching is ugly http://mywiki.wooledge.org/BashFAQ/041
@@ -18,8 +18,8 @@ path_add() {
             --*)
                 if [ "$1" = --end ]; then
                     end=true
-                elif [ "$1" = --ifexists ]; then
-                    ifexists=true
+                elif [ "$1" = --force ]; then
+                    force=true
                 elif [ "$1" = --help ]; then
                     echo "$help"
                     return
@@ -53,7 +53,8 @@ path_add() {
     unset IFS
     PATH="$newpath"
     for x in "$@"; do
-        if ! $ifexists || [ -d "$x" ]; then
+        x="$(readlink -f "$x")"
+        if $force || [ -d "$x" ]; then
             if [ ! "$PATH" ]; then
                 PATH="$x"
             elif $end; then