0

I have an script that will read a csv then find a line and concatenate the result into a string. I have my script working fine when executing locally via git bash, but when executing same code on bitbucket it is adding some carriage that I am not been able to remove. This is very frustrating because I'm stuck for several days with this.

Here is the bash script :

function getTestClassLst(){ 
    file="apex-cls-map.csv"   

    while IFS="," read -r col_clsname col_clsList
    do
            
        if [[ $col_clsname == $1 ]];
        then 
            
            textsize=$(expr length $col_clsList)

            if [[ $textsize -gt 1 ]];
            then                     
                unitclsLst=$(echo "$col_clsList" | sed "s/.cls//g")                   
            fi    
                     
        fi      
    
    done < <(tail -n +2 $file)
}

function getTstForClassFromCommit(){     
    clsLst=""       
    unitclsLst=""

    while read line; do  
        #filter only for classes  
        if [[ $line == *.cls ]];
        then            
            clsname=${line#*classes/}                
            getTestClassLst $clsname           
            echo "## test class : $unitclsLst"
        fi  
        #filter for trigger
        if [[ $line == *.trigger ]];
        then        
            triggername=${line#*triggers/}                
            getTestClassLst $triggername
            echo "## test class for trigger : $unitclsLst"
        fi  

        #check if matched found (not empty)
        if [ ! -z "$unitclsLst" ];
        then
            #check if test class not already included
            if [[ $clsLst != *$unitclsLst* ]];
            then                
                clsLst+="$unitclsLst,"            
                echo "## list : $clsLst"
            fi           
        fi                  
            
    done < cpath.txt
    #remove last char which is a ,    
    cl=${clsLst%?}        
    #remove " from string for those with multiple test class for one class    
    echo "## TEST_CLS_LST =  $cl" | sed 's/\"//g'
}

The getTestClassLst function loop through a csv an return a column value in that csv The getTstForClassFromCommit function loop through a text file with a list of filename to search within the csv.

When running in git bash locally I get this :

## test class : User_DMN_TEST
## list : User_DMN_TEST,
## test class for trigger : DocuSignStatus_DMN_TST
## list : User_DMN_TEST,DocuSignStatus_DMN_TST,
## test class for trigger : DocuSignStatus_DMN_TST
## TEST_CLS_LST =  User_DMN_TEST,DocuSignStatus_DMN_TST

When running via pipeline, I get this :

## test class : User_DMN_TEST
## list : User_DMN_TEST
,
## test class for trigger : DocuSignStatus_DMN_TST
## list : User_DMN_TEST
,DocuSignStatus_DMN_TST
,
## test class for trigger : DocuSignStatus_DMN_TST
## TEST_CLS_LST =  User_DMN_TEST
,DocuSignStatus_DMN_TST

I tried different ways to replace the carriage using following codes, but none is working :

echo "## TEST_CLS_LST =  $cl" | sed -e 's/\"//g' -e 's/\n//g'
echo "## TEST_CLS_LST =  $cl" | sed -e 's/\"//g' -e 's/\\n//g'
echo $(tr -d '\n' < testCls.lst) #where I store the string into a file then replace and display
#store the string in an array then replace array delimeter with , and display the text
IFS=',';echo "${arrayOfCls[*]}";IFS=$' \t\n'
vanessen
  • 1,125
  • 1
  • 12
  • 19
  • 1
    As the [tag:bash] tag you used instructs - " For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here.". Once you've fixed the issues that shellcheck will tell you about, if you still have a problem then [edit] your question to show the corrected code. – Ed Morton May 15 '23 at 12:01
  • 1
    The sed command to remove Carriage Returns would be `s/\r//g'`, not `s/\\n//g'`, and with tr it'd be `tr -d '\r'`, not `tr -d '\n'`. See [why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it](https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it) – Ed Morton May 15 '23 at 12:06
  • Found that the csv ending format was the cause. CRLF vs LF. On my windows , no worry , same ending format. When running on bitbucket, it was different. Thus, I forced my csv to be LF, it was working fine on bitbucket also. Found this after days :( – vanessen May 15 '23 at 13:27

0 Answers0