lundi 7 août 2017

if loop in bash not being evaluated

I am trying to traverse into a set of directories, read a line from a file, parse it to assign the value of a variable, and then act if the variable matches a predefined string. I am running it using "#!/bin/bash. Here is the code.

23 for DIR in $(find . -name "H[A-Z]*" -maxdepth 1); do
 24   if $(test -d ${DIR}); then
 25     echo $(basename ${DIR});
 26     cd $(basename ${DIR});
 27 
 28     for fname in $(find . -name "*.DAT"); do
 29       echo $(basename $fname);
 30       calType=$(grep 'Level_Unit' $fname | sed -e 's/.*=\([A-Z]\)/\1/');
 31       echo $(basename $calType);
 32         if [ "$calType" == FPL ]
 33         then
 34           echo "This is a keeper " $calType;
 35         fi
 36     done
 37     cd ../
 38 
 39   fi;
 40 
 41 done

I am dumping the output on my terminal and here is what I see...

sumit$ ./harp_phaseI_calfilenames.sh 
+ rm harp_Phase1_calnames1.txt
+ touch harp_Phase1_calnames1.txt
+ calTest=FPL
++ find . -name 'H[A-Z]*' -maxdepth 1
+ for DIR in '$(find . -name "H[A-Z]*" -maxdepth 1)'
++ test -d ./HALAF032
++ basename ./HALAF032
+ echo HALAF032
HALAF032
++ basename ./HALAF032
+ cd HALAF032
++ find . -name '*.DAT'
+ for fname in '$(find . -name "*.DAT")'
++ basename ./08E21D00.DAT
+ echo 08E21D00.DAT
08E21D00.DAT
++ grep Level_Unit ./08E21D00.DAT
++ sed -e 's/.*=\([A-Z]\)/\1/'
+ calType=$'V\r'
++ basename $'V\r'
+ echo $'V\r'
V
+ '[' $'V\r' == FPL ']'
+ for fname in '$(find . -name "*.DAT")'

I banged my head against this for a while and am not getting why there is a dollar sign in "calType=$'V\r'" six lines from the bottom of the output. Then the main question is why the if statement is not being evaluated... " '[' $'V\r' == FPL ']'" on the second line from the bottom. I recognize that the instance I have supplied does not match the string against the variable. But the same thing happens when $calType is "FPL\r".

Thanks in advance.

Best, Sumit

Aucun commentaire:

Enregistrer un commentaire