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
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.
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(...).
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.
Thanks for the tip and now the script is working after wrapping the division in int as follows: