Skip to content

Instantly share code, notes, and snippets.

@premun
Last active July 9, 2021 14:26
Show Gist options
  • Save premun/3fa7bf5197822e61ec333ed6d81b772e to your computer and use it in GitHub Desktop.
Save premun/3fa7bf5197822e61ec333ed6d81b772e to your computer and use it in GitHub Desktop.
Folder bookmarks in bash
# This is a compilation of scripts that will allow you to manage directory bookmarks
#
# I recommend setting following macros:
alias ba='[where you put these]/set.sh'
alias bl='[where you put these]/get.sh'
alias br='[where you put these]/delete.sh'
g() {
cd "$(bl $1)"
}
#!/bin/bash
function die ()
{
echo "$1" 1>&2
exit 1
}
name="$1"
path="$2"
if [ -z "$name" ]; then
die "Provide name for the record";
fi
source="${BASH_SOURCE[0]}"
here="$( cd -P "$( dirname "$source" )" && pwd )"
db="$here/database.json"
if [ ! -f "$db" ]; then
echo '[]' > "$db"
fi
content=$(cat "$db" | jq ".[] | select(.name != \"$name\")" | jq -s --tab)
echo "$content" > "$db"
#!/bin/bash
name="$1"
source="${BASH_SOURCE[0]}"
here="$( cd -P "$( dirname "$source" )" && pwd )"
db="$here/database.json"
if [ ! -f "$db" ]; then
echo '[]' > "$db"
fi
if [ -z "$name" ]; then
for row in $(cat "$db" | jq "map(.name+\"@\"+.path)" | jq -r .[]); do
name=$(echo $row | cut -f1 -d@)
path=$(echo $row | cut -f2 -d@)
printf '%-10s' $name
echo $path
done
exit 0
fi
path=$(cat "$db" | jq -r ".[] | select(.name==\"$name\").path")
if [ -z "$path" ]; then
exit 3
fi
echo $path
#!/bin/bash
function die ()
{
echo "$1" 1>&2
exit 1
}
name="$1"
path="$2"
if [ -z "$name" ]; then
die "Provide name for the record";
fi
if [ -z "$path" ]; then
die "Provide path for the record";
fi
source="${BASH_SOURCE[0]}"
here="$( cd -P "$( dirname "$source" )" && pwd )"
db="$here/database.json"
if [ ! -f "$db" ]; then
echo '[]' > "$db"
fi
current="$($here/get.sh "$name")"
if [ -z "$current" ]; then
content=$(cat "$db" | jq ". += [{\"name\": \"$name\", \"path\": \"$path\"}]")
else
content=$(cat "$db" | jq "map(.path = if (.name == \"$name\") then \"$path\" else .path end )")
fi
echo "$content" > "$db"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment