penguin/utils

my env utils

commit 00e123d9faf21d577b29f5fbba1167d36562891f

author斟酌 鵬兄 <tgckpg@gmail.com>
date2014-08-04T09:40:25Z
subjectStructual bashrc
commit 00e123d9faf21d577b29f5fbba1167d36562891f
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2014-08-04T09:40:25Z

    Structual bashrc
---
 bash/bashrc/package.sh                            | 23 ++++++++++++
 bash/bashrc/sources/09_ssh-agent                  | 24 ++++++++++++
 bash/bashrc/sources/10_aliases                    | 17 +++++++++
 bash/bashrc/sources/20_fast-greps                 | 34 +++++++++++++++++
 bash/bashrc/sources/30_mysql                      | 45 +++++++++++++++++++++++
 bash/{go_command => bashrc/sources/40_go-command} |  5 +--
 bash/bashrc/sources/50_blog                       | 22 +++++++++++
 bash/bashrc/sources/60_diagnostics                | 13 +++++++
 8 files changed, 180 insertions(+), 3 deletions(-)

diff --git a/bash/bashrc/package.sh b/bash/bashrc/package.sh
new file mode 100644
index 0000000..47ed720
--- /dev/null
+++ b/bash/bashrc/package.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+function __func_head() {
+    echo "Usage:" ${FUNCNAME[1]} $1
+}
+
+function __func_help() {
+#   echo "Usage:" ${FUNCNAME[1]} $1
+    echo "       ${FUNCNAME[1]} $1"
+}
+
+# Run through all sources
+SDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/sources
+
+echo "Begin source:"
+for i in $( find $SDIR -maxdepth 1 -type f -executable | sort )
+do
+    echo "  Source:" $( basename $i )
+    . $i
+    if [[ $? -ne 0 ]]; then
+        echo "    .. failed"
+    fi
+done
diff --git a/bash/bashrc/sources/09_ssh-agent b/bash/bashrc/sources/09_ssh-agent
new file mode 100755
index 0000000..3b21ced
--- /dev/null
+++ b/bash/bashrc/sources/09_ssh-agent
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+SSH_ENV="$HOME/.ssh/environment"
+
+function start_agent {
+    echo "Initialising new SSH agent..."
+    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
+    echo succeeded
+    chmod 600 "${SSH_ENV}"
+    . "${SSH_ENV}" > /dev/null
+    /usr/bin/ssh-add;
+}
+
+# Source SSH settings, if applicable
+
+if [ -f "${SSH_ENV}" ]; then
+    . "${SSH_ENV}" > /dev/null
+    #ps ${SSH_AGENT_PID} doesn't work under cywgin
+    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
+        start_agent;
+    }
+else
+    start_agent;
+fi
diff --git a/bash/bashrc/sources/10_aliases b/bash/bashrc/sources/10_aliases
new file mode 100755
index 0000000..f6a8710
--- /dev/null
+++ b/bash/bashrc/sources/10_aliases
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+# directory navigations
+alias p=pushd
+alias pp='pushd +2'
+alias p3='pushd +3'
+alias p4='pushd +4'
+alias o=popd
+alias d='dirs -v'
+
+# fast greps
+alias grep='grep --color=auto --exclude=\.svn'
+alias grepphp='grep -n --include=*.php'
+alias grepjs='grep -n --include=*.js'
+
+# Wget prefix
+alias wget='wget --directory-prefix="$HOME/Downloads"'
diff --git a/bash/bashrc/sources/20_fast-greps b/bash/bashrc/sources/20_fast-greps
new file mode 100755
index 0000000..8a08a9f
--- /dev/null
+++ b/bash/bashrc/sources/20_fast-greps
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+function grepmysql () {
+    if [[ -z "$1" ]]; then
+        __func_head "MySQL_SERVER_NAME [WITH_OPTIONS]"
+        echo
+    else
+        if [[ -z "$2" ]]; then
+            grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $2 }'
+        else
+            grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $2" "$3 }'
+        fi
+    fi
+}
+
+function grepdb () {
+    if [[ -z "$1" ]]; then
+        __func_head "DB_NAME"
+        echo
+    else
+        grep $1 ~/.db_pass | awk 'BEGIN{FS=":"}{ print $2 }'
+    fi
+}
+
+function greptype() {
+    if [[ -z "$1" ]]; then
+        __func_head "TYPE GREP_ARGS"
+        echo
+    else
+        CMD="grep --color=auto -n --include=*.$1"
+        shift
+        $CMD $@
+    fi
+}
diff --git a/bash/bashrc/sources/30_mysql b/bash/bashrc/sources/30_mysql
new file mode 100755
index 0000000..3f02cf9
--- /dev/null
+++ b/bash/bashrc/sources/30_mysql
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+function mysqlo () {
+    if [[ -z "$1" ]]; then
+        __func_head "SERVER_NAME"
+        echo
+    else
+        PASS=$(grepmysql $1 true)
+        if [[ -z "$PASS" ]]; then
+            echo "Server not found in config: $1"
+            return 1
+
+        fi
+
+        PS_SQL=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $4 }')
+        USER=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $5 }')
+
+        export MYSQL_PS1="$PS_SQL> "
+
+        shift
+
+        echo $__mysql
+        mysql -u $USER --password=$PASS $@
+        unset PS_SQL
+    fi
+}
+
+function mysqls () {
+    if [[ -z "$1" ]]; then
+        __func_head "SERVER_NAME DATABASE_NAME SQL_FILE"
+        echo
+    else
+        USER=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $5 }')
+        PASS=$(grepmysql $1 true)
+
+        if [[ -z "$PASS" ]]; then
+            echo "Server not found in config: $1"
+            return 1
+
+        fi
+
+        mysql -u $USER --password=$PASS -D $2 < $3
+    fi
+}
+
diff --git a/bash/go_command b/bash/bashrc/sources/40_go-command
old mode 100644
new mode 100755
similarity index 95%
rename from bash/go_command
rename to bash/bashrc/sources/40_go-command
index 75fa883..cc5ec76
--- a/bash/go_command
+++ b/bash/bashrc/sources/40_go-command
@@ -5,9 +5,8 @@ function go() {
     ARG1=$1; ARG2=$2; ARG3=$3;
 
     if [[ -z "$ARG1" ]]; then
-        echo
-        echo "Usage: go [MODE] SITE TARGET_DIR"
-        echo "          <up|down> [MODE] TARGET_DIR"
+        __func_head "[MODE] SITE TARGET_DIR"
+        __func_help "<up|down> [MODE] TARGET_DIR"
         echo
         return 1
     fi
diff --git a/bash/bashrc/sources/50_blog b/bash/bashrc/sources/50_blog
new file mode 100755
index 0000000..90fcb1f
--- /dev/null
+++ b/bash/bashrc/sources/50_blog
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+if [[ ! -f ~/wconvv/native/bin ]]; then
+    return 1
+fi
+
+export PATH=$PATH:~/wconv/native/bin/
+
+function blog () {
+    if [[ -z 'command -v cj' ]]; then
+        echo "Cannot find cj"
+        return 1
+    fi
+
+    if [[ -z "$1" ]]; then
+        __func_head "CONTENT(CJ)"
+        echo
+    else
+        FILE=$(date +%Y%m%d)
+        echo "$1 " | xargs -d' ' cj >> ~/blog/$FILE
+    fi
+}
diff --git a/bash/bashrc/sources/60_diagnostics b/bash/bashrc/sources/60_diagnostics
new file mode 100755
index 0000000..c3a3d1e
--- /dev/null
+++ b/bash/bashrc/sources/60_diagnostics
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+function catlog () {
+    if [[ -z "$1" ]]; then
+        __func_head "FILE"
+        echo "  Will exclude:"
+        cat ~/.settings/checklog_exclude | awk '{ print "    "$1 }'
+        echo 
+    else
+        EXCLUDE=$(awk '{ printf("(%s)|", $1) }' ~/.settings/checklog_exclude | sed 's/|$//')
+        egrep -v $EXCLUDE $1
+    fi
+}