Extraction of header columns and comparing it with Header_format

set -x
for file in /Src/MEDIA_ASSET/*.csv;
do
Header_Format = 'VariantNumber|ERP_SYSTEM_CD|MediaType'
FILESTATUS = GOOD
File_Header = $(cut -d'|' -f1-3 ${file}|head -1)
  do
    if [${Header_Format} = ${File_Header} ];
    then
       ${FILESTATUS} = GOOD
    else
       ${FILESTATUS} = BAD
       break
    fi
  done
  if [${FILESTATUS} = "GOOD"];
  then
     echo "File is validated and ready for processing"
     mv ${file} /Tgtpath/MEDIA_ASSET/
  else
     echo "File is error file and not ready for processing"
     mv ${file} /Tgtpath/MEDIA_ASSET/
  fi
done

I am facing below error for this script

Header_Columns_Extraction.sh: line 20: syntax error near unexpected token `do'
Header_Columns_Extraction.sh: line 20: `  do'

I Just need to extract first 3 columns of the header and compare it with the defined Header_Format. Please help me here.

Remove the 'do' that error message complains about. 'do' is only used for loops, it shouldn't be there.

Also, if [${Header_Format} = ${File_Header} ]; is wrong, you need spaces between [ ] and whatever's inside them, and should double-quote your variables, like if [ "${Header_Format}" = "${File_Header}" ];

Assignments are the opposite, you must avoid spaces and not wrap the variable name in ${} like

FILESTATUS="GOOD"

Also, instead of two if-statements, you could do one if-statement and get rid of FILESTATUS completely. The code below a break or continue will not be reached so you know the file is okay if the code gets to that point.

You should use continue instead of break, unless you really intended the loop to completely stop at the first bad file.

if ! [ "${Header_Format}" = "${File_Header}" ]
then
        echo "File is error file and not ready for processing"
        continue # break will completely stop the loop, continue will restart with next item
fi

echo "File is validated and ready for processing"
echo mv "${file}" /Tgtpath/MEDIA_ASSET/ # Remove echo once tested

Thanks Corona688 it's working :)..And thanks for your suggestions i will follow those!!

1 Like