Where am I?
If you are writing a bash script and you need to know where you are you can use this:
#!/bin/sh
# always prints out the directory in which this script is lives
# no matter where it is run from
# doesnt cope with ../bin/script.sh, which should be ./script.sh anyway
bn=`basename $0`;
echo $0 | grep "^/" > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo $0 | grep "^\.\/$bn" > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo case 1 - relative ref
here=`dirname $PWD/$0 | sed 's/\.\///'`
else
echo case 2 - local ref
here=$PWD
fi
else
echo case 3 - root ref
here=`dirname $0`
fi
echo script home is $here
Thanks to Justin for this
Subscribe via RSS