From e306be78a574f324af674db992e8a6b26bf32a5d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 Mar 2024 09:46:07 -0700 Subject: pd: implement -f option. Problem: when the cdlog contains a directory that cannot be entered, for instance due to not existing any more, we would like to delete it with "pd", but cannot. * cdlog.sh (cdlog.pop): Implement -f (force) option which will ignore the failing cd command and delete the entry anyway. Fix typo in header comment. * README.md: Mention -f in description of `pd` alias. --- README.md | 3 +++ cdlog.sh | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6be49c..8fd46a7 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ most recent directory in the log, and removes that entry from the log. The secon recent directory becomes most recent and so on. If it is given an argument value in the range 1 to 9, it changes to the specified entry, and removes it, moving the others down to close the gap. Thus `pd 1` is equivalent to `pd`. +`pd` takes a `-f` option which means "force". This is useful when it's +not possible to change to the indicated directory, in which case `-f` +causes it to be removed in spite of this. * `cdlog` function shows a listing of the four most recent entries in the log, or all nine if given the `-l` argument. The `cl` command is an alias for diff --git a/cdlog.sh b/cdlog.sh index f6e2b15..81126bb 100644 --- a/cdlog.sh +++ b/cdlog.sh @@ -233,13 +233,18 @@ cdlog.rot() } -# Change to most recent diretory in cdlog and remove it +# Change to most recent directory in cdlog and remove it # from the log. cdlog.pop() { declare -n d=cdlog_hist local n=1 local i + local force= + + if [ $# -gt 0 -a "$1" = "-f" ] ; then + force=y; shift + fi if [ $# -gt 0 ] ; then n=$1; shift @@ -247,7 +252,7 @@ cdlog.pop() case $n in ( [1-9] ) - if command cd "${d[$n]}" ; then + if command cd "${d[$n]}" || [ $force ]; then for ((i = n; i < ${#d[@]}; i++)); do d[$i]=${d[$((i + 1))]} done -- cgit v1.2.3