From 70fc8f255abee76abd646ff6dd8c7b1cd87f264e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 21 Jul 2016 18:39:48 -0400 Subject: qcd: Initial "add" command implementation Add the specified shortcut and path to the database file. Expand the given path to an absolute path so that it can be used from any working directory. Used "andsens"' answer on Stack Overflow for getting the absolute path: http://stackoverflow.com/questions/7126580/expand-a-possible-relative-path-in-bash/17076258#17076258 --- qcd | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/qcd b/qcd index 5c5d1aa..a4ee215 100755 --- a/qcd +++ b/qcd @@ -22,6 +22,10 @@ Usage: __EOF__ } +function absolute_path () { + (cd "$(dirname '$1')" &>/dev/null && printf "%s/%s" "$PWD" "${1##*/}") +} + function qcd () { if [[ $# < 1 ]]; then print_usage @@ -32,6 +36,10 @@ function qcd () { case $command in -a) + local shortcut=$2 + local path=$3 + + echo "${shortcut} $(absolute_path ${path})" >> $QCD_DATABASE_FILE ;; -c) ;; -- cgit v1.2.3