[Solved] 0403-057 Syntax error for if statement

I am getting the following error when I am running a script in ksh when trying to execute an if statement comparing two numerical values

tstmb.sh[26]: 1.5321e+08: 0403-057 Syntax error

Below is my code snippet.

#!/bin/ksh
set -x
TODAY=$(date +%y%m%d)

for file in $(ls -rt  *.log | tail -1)
do

V=$file
V1=${V#*_}
V2=${V1%%_*}

if [ "$V2" -ne "$TODAY" ];
then

echo "abc"

elif <condition>
then
echo "xyz"
else
echo "123"

fi
done
TODAY=130830
+ V=ABC_130830_001.log
+ V1=130830_000004.log
+ V2=130830
+ [ 130830 != 130830 ]
test.sh[12]: 1.5321e+08: 0403-057 Syntax error

Please let me know where I am doing wrong..Thanks,

"$V2" the way its written is a string and quite sure $TODAY also...

What I meant was comparing two numbers:

ie., 130830 != 130830

This does not seem to work and so does using '-ne'
any idea why :confused:

because -ne is for numeric and what your declared are chars...
Where did you declare as numeric?

Your test:

if [ "$V2" -ne "$TODAY" ];

is comparing strings (because your variables ar in quotes) using a numeric (-eq) operator.

It should be:

if [ $V2 -ne $TODAY ];

You are not showing us the correct output from your script... or not showing the correct script:

+ [ 130830 != 130830 ]
test.sh[12]: 1.5321e+08: 0403-057 Syntax error

Does not match...!!!
What have you hidden?

That makes no difference, really. A number is a number by virtue of containing numerals whether it is quoted, or not. And if the two numbers are equal as strings it doesn't matter if you use != or -ne.

Ok, thanks for the inputs..I have tried to declare them as integers and run the script but still getting the error '1.5321e+08: 0403-057 Syntax error'

	typeset -i var1=$V2
	typeset -i var2=$TODAY

Typesetting the variables as integers won't help.

You still are not showing us the part of the script where the error is.

First it's on line 26, then line 12. The trace shows "... != ..." but the code you have shown uses "... -ne ..."...

well I was testing using different scripts and hence the difference in line numbers..sorry for that.
However I think I found what the problem is..
It has to do with the adding of numbers in a directory which is returning a huge value and hence getting the errors

S1=1.53013e+08
1.53013e+08: 0403-057 Syntax error

Below us my code for reference..

#!/bin/ksh

DIR='/var/mqsi/backup'
TODAY=$(date +%y%m%d)
cd $DIR
S1=$(ls -l *.zip| awk '{sum+=$5; n++} END {print sum/n}')
S2=$(ls -ltr *.zip| tail -1 | awk '{print$5}')

for file in $(ls -rt  *.zip | tail -1)
do
VAR1=$file
VAR2=${VAR1#*_}
VAR3=${VAR2%%_*}

if [ "$VAR3" != "$TODAY" ]
then
echo "ABC"

elif [[ "$S2" -lt "$S1" ]]
then
echo "XYZ"

else
echo "123"
fi
done

The same script is working for directories where the file size is less.
In my case the total is summing up to '3256523652' and there are around 20 files.
Please help me in modifying the script to add the sum of file sizes which are huge.
Thanks in advance!!

It wasn't just the difference in line numbers; your original post did not even include the source of the problem (the awk print statement). Until your most recent post, you had been wasting everyone's time (yours included). In the future, please do be sure to post the exact code that corresponds to the error messages.

If you don't want the result of that division to use exponential notation, but still want it to be a float, set awk's OFMT variable appropriately.

If you want to truncate to an integer, just wrap the division in int(...).

Regards,
Alister

1 Like

I have'nt figured out that the problm was with the awk statement initially and did not want to post lengthy code hence posted the other part which was giving error:o
However point noted and will ensure to post the entire script frm now on. :b:

Thanks for the tip and now the script is working after wrapping the division in int as follows:

{print int(sum/n)}