Last active
December 1, 2021 14:42
-
-
Save juancampa/da0dc8d37f3d6e7cbdc89267499b9805 to your computer and use it in GitHub Desktop.
pind: Pins a command to a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Any command invoked with the pind prefix gets stored along with its $PWD so | |
# subsequent invocations are executed in the same directory. For example: | |
# | |
# $ pind pwd | |
# /home/juan | |
# $ cd /etc | |
# $ pind pwd | |
# /home/juan | |
# | |
# You can also put an arbitrary value in the PIND environment variable to have | |
# different variants of the same exact command | |
# $ PIND=1 pwd | |
# $ PIND=2 pwd # In some other directory | |
pind() { | |
set -euo pipefail | |
CONFIG="$HOME/.local/share/pind.db" | |
mkdir -p $(dirname "$CONFIG") | |
touch "$CONFIG" | |
HASH="$(echo "$@" "${PIND:-}" | sha1sum | awk '{ print $1 }')" | |
LINE=$(grep "$HASH" "$CONFIG" || echo) | |
if [ ! -z "$LINE" ]; then | |
DIR="$(printf "%s" "$LINE" | sed 's/.*://')" | |
(cd "$DIR"; $@) | |
else | |
echo "$HASH:$PWD" >> "$CONFIG" | |
$@ | |
fi | |
} | |
pind "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment