altonbr
June 13th, 2008, 07:47 PM
I just ran into a problem where my script can only accept absolute paths. Since I don't have a blog, I thought I'd post my solution here:
if [ "`echo $1 | cut -d '/' -f 1`" != "" ]; then
echo 'Absolute paths only!
exit 1
fi
Examples:
$ echo './' | cut -d '/' -f 1 # local directory
.
$ echo '../' | cut -d '/' -f 1 # local directory, back one
..
$ echo 'var/' | cut -d '/' -f 1 # local directory
var
$ echo '/var' | cut -d '/' -f 1 # absolute directory (this is the only type of path that is accepted in the script above)
if [ "`echo $1 | cut -d '/' -f 1`" != "" ]; then
echo 'Absolute paths only!
exit 1
fi
Examples:
$ echo './' | cut -d '/' -f 1 # local directory
.
$ echo '../' | cut -d '/' -f 1 # local directory, back one
..
$ echo 'var/' | cut -d '/' -f 1 # local directory
var
$ echo '/var' | cut -d '/' -f 1 # absolute directory (this is the only type of path that is accepted in the script above)