I'm trying to set up a script that will call a code scanner in multiple directories in a file tree. The code scanner is in two steps (a 'make' build and a scan) which take some arguments. I want to distill this down to one step that will automatically switch directories and run the two step scan.
My problem is that when my script goes through a list of directories (predefined in the script), it tries to detect the existence of the directory, but it keeps coming up with an error that it doesn't exist. I make a call to "if [-d ]", but it keeps going to the ELSE part of the statement in failure.
I looked up how to loop through the array of sub-directories and tried a couple different ways to loop. I also tried to change directories and how to access the variables in the script. I looked at a couple of Stack Overflow answers, but those didn't help either.
As part of my script, I call a function that parses a file to find another directory to change to before a build is done. This directory is where the build occurs. Then the scan is done in another directory. I'm switching to these directories on the fly. I double-checked my local variables and tried to change how I return my return variable in the function. All with no luck.
Here is the bulk of my script. I tried to distill it down as best I can.
declare -a SCANDIRS=("atpsw" "emsw" "lssw")
SRCHOME=`pwd`
CONFIGFILE='scan.properties'
function get_build_dir()
{
while read -r line
do
set -- `echo $line | tr '=' ' '`
local key=$1
local value=$2
if [ "$key" == "scan.sources" ]
then
break
fi
done < "$CONFIGFILE"
#return the value as a string
echo "$value"
}
for i in ${SCANDIRS[@]}
do (
if [ -d "$i" ]
then
echo "SRCHOME DIR: $SRCHOME"
echo "Scanning $i ..."
cd "$i"
if [ -f "$CONFIGFILE" ]
then
echo "Property File Exists"
# Switch this comment to make it work
todir="$(get_build_dir)"
#todir="src"
builddir="$PWD/${todir}"
echo "Switching to directory: ${builddir}"
if [ -d ${builddir} ]
then
cd ${builddir}
echo `pwd`
else
echo "--->>>> ${builddir} does not exist"
fi
else
echo "Property File does not exist"
fi
cd "${SRCHOME}"
echo `pwd`
echo
fi
) done
The output I get is:
SRCHOME DIR: /home/sorton/src/LWSD
Scanning atpsw ...
Property File Exists
Switching to directory: /home/sorton/src/LWSD/atpsw/.
--->>>> does not existc/LWSD/atpsw/.
/home/sorton/src/LWSD
SRCHOME DIR: /home/sorton/src/LWSD
Scanning emsw ...
Property File Exists
Switching to directory: /home/sorton/src/LWSD/emsw/src
--->>>> does not existc/LWSD/emsw/src
/home/sorton/src/LWSD
SRCHOME DIR: /home/sorton/src/LWSD
Scanning lssw ...
Property File Exists
Switching to directory: /home/sorton/src/LWSD/lssw/./ls
--->>>> does not existc/LWSD/lssw/./ls
/home/sorton/src/LWSD
for each loop the "Scanning ..." directory changes to one of the array items. The property file is checked and parsed, then the build directory is checked which fails every time. You'll notice at the arrows in the output, the string is garbled and is truncated and out of order. I had to add the arrows because they were not printed out at run-time.
I narrowed down the problem to calling the function and returning the directory to switch to. It is this directory that is checked before 'cd'ing to it. If I comment out the function call that gets the value of "todir" and replace it with the line directly after it, the script works as expected.
I understand I could probably get MAKE to do this work, but this problem is really stumping me and I want to get to the bottom of this problem just in case it comes up again.
I'm sorry if this seems rather long. Any insight into this interesting problem would be greatly appreciated.