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
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

)
Best regards and merry xmas
Fredrik Eriksson