#!/bin/sh # # locate_script -- Find a scripts location (for self doc, or configs) # # Small Script you can add to the beginning of your shell programs to # determine the location of the script. This lets you find things like # configuration files relative to the scripts location, or read the script # itself for things like self-documenting manuals (something I do a lot). # # It has worked for me for more than 30 years! And I have used it on # Sun3, Sun4, Ultrix, Solaris, Linux, MacOSX, with bourne shells, dash, # bash, ksh, and zsh. It should work for any Unix-like environment. # # Technically locating a running script has no solution, as it could be a # piped into a shell, but in practice it does work. # # See BASHFAQ: http://mywiki.wooledge.org/BashFAQ/028 # ### # # Simple -- Just the program name #PROGNAME=`basename $0` # Program basename (not $0 may not be good!) #PROGDIR=`dirname $0` # directory of script (may be relative) # # For BASH ONLY PROGNAME="${BASH_SOURCE##*/}" # script name (basename) PROGDIR="${BASH_SOURCE%/*}" # directory (dirname - may be relative) # OLD Discover where the shell script resides #PROGNAME=`type "$0" | awk '{print $3}'` # ask the shell #PROGDIR=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` echo "ARG ZERO : $0" echo "GIVEN NAME : $BASH_SOURCE" echo "PROGNAME : $PROGNAME" echo "PROGDIR : $PROGDIR" if [ ! -f "$PROGDIR/$PROGNAME" ]; then # This has NEVER happened to me! echo >&2 "$PROGNAME: Unable to locate the script -- ABORTING" exit 10 fi # Fully qualify directory path (remove relative components and symlinks) # This is important if you plan to change directories. # if "type" # NOTE: bash "pwd" only returns the users 'logical path' though symlinks. # But "pwd -P" works as a bash builtin, or as a binary command PROGDIR=`cd "$PROGDIR" && pwd -P || echo "$PROGDIR"` ORIGDIR=`pwd -P` # Using "readlink -f" if available. # It is not a bash built-in, and is NOT available on MacOS (Arrgghh) #ORIGDIR=`readlink -f .` #PROGDIR=`readlink -f "$PROGDIR"` echo "QUALIFIED DIR : $PROGDIR" echo "ORIGINAL FROM : $ORIGDIR" # ------------------------------------------------- # Now what do you want to do? # Where do you want to be running? # Caution: Any user provided file arguments should be accessed # relative to the original directory $ORIGDIR and not to $PROGDIR #cd "$PROGDIR" # Get a config file relative to scripts location (without cd) # config="$PROGDIR/../etc/$PROGNAME.conf" # config=`readlink -f "$config"` # clean up path # ------------------------------------------------- Usage() { # Report error and Synopsis line only echo >&2 "$PROGNAME:" "$@" sed >&2 -n '1,2d; /^###/q; /^#/!q; /^#$/q; s/^# */Usage: /p;' \ "$PROGDIR/$PROGNAME" echo >&2 "For help use $PROGNAME --help" exit 10; } Help() { # Output Full header comments as documentation sed >&2 -n '1d; /^###/q; /^#/!q; s/^#//; s/^ //; p' \ "$PROGDIR/$PROGNAME" exit 10; } Error() { # Just output an error condition and exit (no usage) echo >&2 "$PROGNAME:" "$@" exit 2 } echo "" # -------------------------------------------------