I've been using the following code to make sure a shell script only runs under a "safe" directory.
Comments/Improvements?
#!/bin/sh
#----------------------------------------------------------------------------#
#...[top].[ subdir.sh ]......................................................#
#----------------------------------------------------------------------------#
#.........william.o.yates...hackware.at.tru2life.net...tru2life.info.........#
#----------------------------------------------------------------------------#
if [ X"${2}" = "X" ];
then
echo -e "\a.\n..\n...\n...subdir.sh parent subdir\n...\n..\n.";
else
dir=${1};
dir_size=${#1};
sub=${2}; # oops... 2, not 1...
sub_size=${#2}; # oops... 2, not 1...
#---------------------------------------------------------------------------
# if sub_size is less than dir_size, it can't be under parent.
#---------------------------------------------------------------------------
if [ ${sub_size} -lt ${dir_size} ];
then
echo -e ".\n..\n...\n... sub_size -lt dir_size\n...\n..\.";
fi
#---------------------------------------------------------------------------
# substring sub to dir_size to then compare sub and dir
#---------------------------------------------------------------------------
xdir="$(echo ${sub} | head -c ${dir_size})";
if [ ${dir} = ${xdir} ];
then
echo -e ".\n..\n...\n... ${sub} IS a subdir of ${dir}\n...\n..\n.";
else
echo -e ".\n..\n...\n... ${sub} is NOT a subdir of ${dir}\n...\n..\n.";
fi
fi
#----------------------------------------------------------------------------#
#...[end].[ subdir.sh ]......................................................#
#----------------------------------------------------------------------------#