From 594318f73b5188f3d3bebfbcd9f08b69993615fa Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 21 Jul 2016 21:20:55 -0400 Subject: qcd: Only add shortcut to database if it's new If an existing shortcut in the database matches the one you're trying to add, it doesn't get added. In the future we might want to output an error message to let users know that there was a problem. --- qcd | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/qcd b/qcd index 1b0cd3d..ebd68a4 100755 --- a/qcd +++ b/qcd @@ -30,6 +30,11 @@ function absolute_path () { (cd "$(dirname '$1')" &>/dev/null && printf "%s/%s" "$PWD" "${1##*/}") } +function find_shortcut () { + local shortcut=$1 + return $(awk '{ print $1 }' $QCD_DATABASE_FILE | grep "^${shortcut}$" >/dev/null 2>&1; echo $?) +} + function qcd () { if [[ $# < 1 ]]; then print_usage @@ -45,7 +50,10 @@ function qcd () { local shortcut=$2 local path=$3 - echo "${shortcut} $(absolute_path ${path})" >> $QCD_DATABASE_FILE + # Don't add the shortcut if it already exists + if ! find_shortcut $shortcut; then + echo "${shortcut} $(absolute_path ${path})" >> $QCD_DATABASE_FILE + fi ;; -c) ;; -- cgit v1.2.3