Skip to content

Commit

Permalink
add 'scriptdir' example to stash away that aweful pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ddopson committed Mar 21, 2013
1 parent 7b93e4b commit e5591f3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bin/scriptdir
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# This file is more of an example.

# This is the level of bullshit required to safely get the scriptdir in bash
scriptdir() {
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
echo "$( cd -P "$( dirname "$SOURCE" )" && pwd )"
}

echo "\$0=$0"
echo $(scriptdir)


0 comments on commit e5591f3

Please sign in to comment.