Register
It is currently Wed Apr 23, 2014 11:32 am

make bookmarks for the command "cd"


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Dec 22, 2008 4:48 am   

Joined: Wed Sep 24, 2008 11:32 pm
Posts: 7
Hi guys!
If you work in terminal or console, it is almost inevitable that you have to use the command "cd". I write the following codes to make bookmarks for "cd". There is also something called "cdargs", but I never tried it though heard of its use ( and that was why write these codes).
As a newbie, I never expect my codes to be perfect. Any comment is welcome, especially corrections. Thx.

Code:

####################################################
############ cdk -- a cd bookmark ##################
#### append these codes to ~/.bashrc or ~/.profile ############
### !!! It doesn't work if you make it a shell scripts !!!#############
####################################################

bm=~/.cdbm   # where to keep the bookmarks
if [ -f $bm ]; then
   true
else
   touch $bm
fi

shownames() {
   sed -e 's/=[^=]*$//g' $bm
}
showpaths() {
   sed -e 's/^[^=]*=//g' $bm
}

cdh() {
   cat << EOF
Usage:
   cda ([NAME])   add the working path to bookmark with the name of NAME
   cdd [NAME]   delete the bookmark(s) with the name of NAME
   cdk [NAME]   change to working directory to the one with the name of NAME
   cdl ([KEYWORD])   list bookmark(s) with the keyword of KEYWORD
   cdh    show this help message
EOF
}

cda() {
   local bmpath=$(pwd)
   
   if [ $# -eq 0 ]; then
      local bmname=${bmpath##*/}
      read -p "Add current directory to bookmark with the name of $bmname?(y/n)" yon
      if [ $yon = y ]; then
         true
      else
         echo "Aborted."
         return
      fi
   elif [ $# -eq 1 ];then
      local bmname=$1
      if echo $bmname | grep =; then
         echo "NO \"=\" allowed"
         return
      fi
   else
      echo "Only ONE name needed"
      return
   fi
   
   # check name duplication
   if shownames | grep $bmname >>/dev/null; then
      echo "Name already exited"
      return
   else
      true
   fi   
   # check for name duplication finished
   
   echo "$bmname=$bmpath" >>$bm
}

cdk() {
   if [ $# -eq 0 ]; then
      echo "Where to go?"
      return
   elif [ $# -eq 1 ]; then
      if shownames | grep $1 >/dev/null; then
         matchone=$(grep $1 $bm)
         target=${matchone##*=}
         cd $target
      else
         echo "No entry matched $i"
      fi
   elif [ $# -gt 1 ]; then
      echo "TOO MANY REQUEST"
      return
   fi
}
cdl() {
   if [ $# -eq 0 ]; then
      echo -e "NAME\tPATH"
      echo -e "============="
      cat $bm | sed -e 's/=/\t/'
   elif [ $# -eq 1 ]; then
      if grep $1 $bm >>/dev/null;then
         echo -e "NAME\tPATH"
         echo -e "============="
         grep $1 $bm | sed -e 's/=/\t/'
      else
         echo "No matched entry"
      fi
   else
      echo "Too manay request"
   fi
}

cdd() {
   if [ $# -eq 0 ]; then
      echo "NAME(s) NEEDED"
      return
   else
      for i in $@; do
         if shownames | grep $i >>/dev/null; then
            cat $bm | grep "${i}="
            read -p "Delete this entry?(y/n)" yon
            if [ $yon = "y" ]; then
               sed -e "/^${i}=/d" -i $bm
               echo "The entry $i deleted"
            else
               echo "The entry $i kept"
            fi
         else
            echo "No matched of $i"
         fi
      done
   fi
}
########################## END of cdk


Top
 Profile  
 PostPosted: Mon Dec 22, 2008 5:38 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Actually, looks quite good and about as easy as you can make it :)
I would go for a more advanced method if you have a mysql at hand ;)
(this thou will require you to have a password and a username in clear text which isn't very secure)

Code:
#!/bin/bash
username="whatever"
password="whatever"
database="bookmark"

# Just a simple function to query the mysql database :)
mysql_query {
   mysql -u$username -p$password $database <<EOF
   $1
   EOF
}

# Compacting the code by doing this as function
check_yn {
    read -p "$1" ask
    case $ask in
        Y|y) retval=0 ;;
        N|n) retval=1 ;;
        *) retval=1 ;;
    esac
    return $retval
}

# Check if we got some syntaxes in $1, and then shift it and check again until $1 is blank
until [ -z $1 ]; do
   case $1 in
       -d|-D) goto="delete" ;;
       -h|-H) goto="help" ;;
       -l|-L) goto="list" ;;
       *)   name="$1"; [ -z $goto ] && goto="normal" ;;
   esac
   shift
done

# Preform all whatever option we choosed above :) or goto normal if nothing matched
case $goto in
   delete)
      if [ ! -z $2 ]; then
         if [ $(check_yn "Are you sure? (y|N) ") -eq 0 ]; then
            mysql_query "delete from bookmarks where bmname=\"$2\""
         fi
      fi
   ;;
   help)   
      cat << EOF
Usage:
   $0 ([NAME])   add the working path to bookmark with the name of NAME
   $0 [NAME]   delete the bookmark(s) with the name of NAME
   $0 [NAME]   change to working directory to the one with the name of NAME
   $0 ([KEYWORD])   list bookmark(s) with the keyword of KEYWORD
   $0    show this help message
EOF
   ;;
   list)
      if [ ! -z $2 ]; then
         mysql_query "select * from bookmarks where bmname like \'%$2%\'"
      fi
   ;;
   normal)
      $result = mysql_query "select * from bookmarks where bmname=\"$name\""
      IFS="\n"
      for i in $result; do
         echo "$i" | grep "$name"
         if [ $? -eq 0 ]; then
            cd "$i"
         else
            if [ $(check_yn "Do you want to add this path ($(pwd)) with name $name? (y/N)") -eq 0 ]; then
               mysql_query "insert into bookmarks set bmname=\"$1\", path=\"$(pwd)\""
            else
                exit
            fi
         fi
      done
      IFS=" ";
   ;;
esac


This might work, but since I don't have anything to test it with atm it's still somewhat experimental :P
Hope it gives you some kind of idea on how to improve your script with this :)
(And I'm sorry if this is a bit spammy, I'm somewhat bored at work :P)

Best regards and merry xmas
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: deb and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP