Tuesday, November 20, 2012

[Bash] change current workspace by calling a script function

# to change current path using a customized script function
# instead of using alias. Need put this function in .bashrc
#    e.g.,
#            mycd ex1 ==> cd ~/scripttest/ex1
#            mycd ex2 ==> cd ~/scripttest/ex2
#            mycd eX2 ==> cd ~/scripttest/ex2
#   @ http://scriptdemo.blogspot.com 

function mycd (){
 if [ $# -ne 1 ]; then
    echo "Usage:"
    echo " mycd foldername"
 else
    targetF=`echo $1 | awk '{print tolower($1)}'`
    targetP=${HOME}/scripttest/${targetF}
    if [ -d ${targetP} ]; then
       eval "cd ${targetP}"
    else
       echo "${targetP} does not exist!"
    fi
    unset targetF
 fi
}

ShowCalendar