elif if not expected

Hi All,

I write an script will have some functions... I am getting elif not expected error..

Even tried by using set -x for debug but no use..

Could you please help me out in this

Variables
....
....
....
TEST_SRC()
{
 cat ${filesrc} >> ${filetgt}
    if [[ $? != 0 ]]; then
  echo "Concatenation failed. Exiting"
  exit -1
    fi
}
 
if [ ${A_CHECK} -eq "F" ] then
 
   ARCHIVE()
 
   TEST_SRC()
elif [ ${A_TGT} -eq ${A_DLT_SRC} ] then
.....
else
.....
fi

you need to put a semicolon before the 'then' keyword like:

if [ ${A_CHECK} -eq "F" ] ; then

same thing in

elif [ ${A_TGT} -eq ${A_DLT_SRC} ] ; then

see ya
fra

Even I had done this earlier but getting same error...

hi,
try omitting the double round parenthesis when calling the functions inside the if statement.

fra

Use double quotes...

elif [ "${A_TGT}" = "${A_DLT_SRC}" ] ; then

-eq is used for numerical comparison...

HTH
--ahamed

Thanks fra & ahamed....I tired both options: but still error exists :wall:

if [ "$A_CHECK" -eq "F" ]; then
 
   ARCHIVE()
 
   TEST_SRC()
elif [ "$A_TGT" -eq "$A_DLT_SRC" ]; then
.....
else
.....
fi

Can you post the entire code code around if loop and which shell/OS are you using?

--ahamed

---------- Post updated at 02:27 AM ---------- Previous update was at 02:24 AM ----------

On a second thought, post the -x output...
I think there must be a new line or carriage return with the data stored in those variables use in if loop!

--ahamed

 
SRCFILE_DATA_CHECK()
{
echo 'Sourcefile_Record count is:'$COUNT_SRC
if [[ $COUNT_SRC -le 0 ]]; then
echo 'Record count is zero in Sourcefile Exiting'
exit 0
else
echo "Source file is not empty"
fi
}
ARCHIVE_TGT()
{
mv ${filetgt} ${filetgt_bkp} 
if [[ $? != 0 ]]; then
echo "Archiving of Target File failed. Exiting"
exit -1
fi
}
ARCHIVE_SRC()
{
cp ${filesrc} ${filesrc_bkp}

if [[ $? != 0 ]]; then
echo "Archiving of Source File failed. Exiting"
exit -1
fi
}
RENAME_SRC()
{
mv ${filesrc} ${filetgt}

if [[ $? != 0 ]]; then
echo "Renaming of Source File failed. Exiting"
exit -1
fi
}
CHK_COUNT()
{
if [[ ${COUNT_SRC} == ${COUNT_DELTA_SRC} ]]; then 
echo "Count's are matching between Source & RecordCount File"
else
echo "Count match between Source & RecordCount File Failed. Exiting"
exit -1
fi
}
ACCUMULATE_SRC()
{
echo "Period is same in Source and Target file. Concatenating Source and Target file"
cat ${filesrc} >> ${filetgt}
if [[ $? != 0 ]]; then
echo "Concatenation failed. Exiting"
exit -1
fi
}
 
if [ "${PROCESS_DELTA_CHECK}" -eq "F" ] ; then

ARCHIVE_TGT()

ARCHIVE_SRC()

RENAME_SRC()

CHK_COUNT()

 
elif [ "${PERIOD_TGT}"-eq"$PERIOD_DELTA_SRC" ] ; then

ACCUMULATE_SRC()

Timestamp to the target file
ARCHIVE_SRC()

CHK_COUNT()

 
else
 
ARCHIVE_TGT()

ARCHIVE_SRC()

RENAME_SRC()

CHK_COUNT()
fi

---------- Post updated at 04:24 PM ---------- Previous update was at 04:22 PM ----------

-x output is:

+ + echo /data/datastage/cat/dev/temp1
+ awk -F/ {print $5}
P5=dev
+ srcpath=/data/datastage/cat/dev/flatfiles
+ tgtpath=/data/datastage/cat/dev/flatfiles
+ archpath=/data/datastage/cat/dev/archive
+ srcfilename=WFP_3259_Source.txt
+ srcdeltafilename=WFP_3259_RecordCount.txt
+ tgtfilename=RTR_IDD3295_ECC_Accumulated_Source.txt
+ + date +%Y%m%d%H%M
tgtfilename_new=RTR_IDD3295_ECC_Accumulated_Source_201112300550.txt
+ + date +%Y%m%d%H%M
srcfilename_bkp=WFP_3259_Source_201112300550.txt
+ cd /data/datastage/cat/dev/flatfiles
+ filesrc=/data/datastage/cat/dev/flatfiles/WFP_3259_Source.txt
+ filesrc_bkp=/data/datastage/cat/dev/archive/WFP_3259_Source_201112300550.txt
+ filedelta=/data/datastage/cat/dev/flatfiles/WFP_3259_RecordCount.txt
+ filetgt=/data/datastage/cat/dev/flatfiles/RTR_IDD3295_ECC_Accumulated_Source.txt
+ filetgt_bkp=/data/datastage/cat/dev/archive/RTR_IDD3295_ECC_Accumulated_Source_201112300550.txt
+ + tail -r /data/datastage/cat/dev/flatfiles/RTR_IDD3295_ECC_Accumulated_Source.txt
+ head -1
+ cut -d , -f 4
PERIOD_TGT=009
+ + head -1 /data/datastage/cat/dev/flatfiles/WFP_3259_RecordCount.txt
+ cut -d , -f 1
COUNT_DELTA_SRC=06
+ + cut -d , -f 2
+ head -1 /data/datastage/cat/dev/flatfiles/WFP_3259_RecordCount.txt
PERIOD_DELTA_SRC=009
+ + head -1 /data/datastage/cat/dev/flatfiles/WFP_3259_RecordCount.txt
+ cut -d , -f 3
PROCESS_DELTA_CHECK=F
+ + cat /data/datastage/cat/dev/flatfiles/WFP_3259_Source.txt
+ awk {n++} END {print n}
COUNT_SRC=6
+ echo Period on Target File is:009
Period on Target File is:009
+ echo Count of Record Count File is:06
Count of Record Count File is:06
+ echo Period on Record Count File is:009
Period on Record Count File is:009
+ echo File is an Delta (D) or Full (F):F
File is an Delta (D) or Full (F):F
+ echo Count of Source File is:6
Count of Source File is:6
+ set -x
./MainScript.sh[122]: 0403-057 Syntax error at line 136 : `elif' is not expected.
 
 

(Crossed post - but still largely current)

Don't forget the correction from post #4.
When creating the function we have the brackets "TEST_SRC()". When invoking the function we do not have the brackets "TEST_SRC".

Should be:

elif [ "$A_TGT" = "$A_DLT_SRC" ]; then

Please post the whole script and mention what Operating System and version you have and what Shell you are using.

If at any time this script has been edited in a Microsoft editor, please post the output from this command which is designed to make control characters visible:

sed -n l scriptname

ADDENDA: After seeing the latest script:

Spaces missing around operator and wrong operator and missing { }:

elif [ "${PERIOD_TGT}" = "${PERIOD_DELTA_SRC}" ] ; then

Wrong operator.

if [ "${PROCESS_DELTA_CHECK}" = "F" ] ; then

Convention says exit with a positive number if you fail so you can test $? in any caling script.

exit 1

Presumably this is a comment line.

#Timestamp to the target file

Yes its working fine... Thanks a lot all for your help I forget to remove () while calling the functions

I dont know if its a typo or something cause I don't find any space between tokens here... And use "=" if they are numbers...

elif [ "${PERIOD_TGT}"-eq"$PERIOD_DELTA_SRC" ] ; then
elif [ "${PERIOD_TGT}" -eq "$PERIOD_DELTA_SRC" ] ; then

--ahamed

its typing mistake only.. Script working fine... Thanks all

if [ "${PROCESS_DELTA_CHECK}" -eq "F" ] ; then
ARCHIVE_TGT()
ARCHIVE_SRC()
RENAME_SRC()
CHK_COUNT()
elif [ "${PERIOD_TGT}"-eq"$PERIOD_DELTA_SRC" ] ; then
ACCUMULATE_SRC()
#Timestamp to the target file
ARCHIVE_SRC()
CHK_COUNT()
else
ARCHIVE_TGT()
ARCHIVE_SRC()
RENAME_SRC()
CHK_COUNT()
fi
This would be easier to follow as:
if [ "${PROCESS_DELTA_CHECK}" = "F" -a "${PERIOD_TGT}" = "${PERIOD_DELTA_SRC}" ]
then
        ACCUMULATE_SRC
        #Timestamp to the target file
        ARCHIVE_SRC
        CHK_COUNT
else
        ARCHIVE_TGT
        ARCHIVE_SRC
        RENAME_SRC
        CHK_COUNT
fi
1 Like