Sunday, December 18, 2011

Shortcut Shell Script

I have a shell script I use to simplify my life on Unix systems. This is the "Git" version, saved as $HOME/bin/g:


usage() {
      sed -n '/^  *[a-zA-Z0-9][a-zA-Z0-9]*)/ {
s?^\(  *[a-zA-Z0-9][a-zA-Z0-9]*\)) *cmd="\(.*\)" *;;?\1  \2?
}' $PROG >&2


case $lookup in
   a) cmd="git add $*" ;;
   b) cmd="git branch" ;;
   c) cmd="git commit -m \"$*\"" ;;
   d) cmd="git checkout develop $*" ;;
   m) cmd="git checkout master $*" ;;
  pd) cmd="git push origin develop $*" ;;
  pm) cmd="git push origin master $*" ;;
   r) cmd="git checkout -b $*" ;;
   s) cmd="git status -s $*" ;;
  "") usage;;
   *) echo "Not valid." >&2

echo $cmd >&2
eval $cmd
Ideally, any frequently executed commands take only a few keystrokes (not counting any parameters that need to be passed):

~/dev/TuSC (develop)$ g s

git status -s
M  tusc.ahk
?? tusc.ahk.bak

~/dev/TuSC (develop)$ g c fixed multi-lock bug plus usage comments for GoApp

git commit -m "fixed multi-lock bug plus usage comments for GoApp"
[develop 1a39e4d] fixed multi-lock bug plus usage comments for GoApp
 1 files changed, 67 insertions(+), 23 deletions(-)

~/dev/TuSC (develop)$ g pd

git push origin develop
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.26 KiB, done.
Total 3 (delta 2), reused 0 (delta 0)
   ede7073..1a39e4d  develop -> develop

~/dev/TuSC (develop)$

Some people use aliases or functions for this kind of thing, but I like my little script because I feel like it's more portable and also has a built-in command list which I can access just by typing the command key by itself (plus Enter, of course):

~/dev/TuSC (develop)$ g
   a  git add $*
   b  git branch
   c  git commit -m \"$*\"
   d  git checkout develop $*
   m  git checkout master $*
  pd  git push origin develop $*
  pm  git push origin master $*
   r  git checkout -b $*
   s  git status -s $*

~/dev/TuSC (develop)$