LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Error in Conditional Statements (https://www.linuxquestions.org/questions/linux-newbie-8/error-in-conditional-statements-4175553016/)

newuser85 09-09-2015 05:34 AM

Error in Conditional Statements
 
Hi All,

I am trying to execute a script using Cygwin on windows environment, am getting error for the two if conditional statements in the below snippet and also the expr in DiffYear

********************************************************************
TodayDOY=$DAYOFYEAR
DiffYear=`expr $TodayYear - $StampYear`

if [ ${DiffYear} -eq 0 ]
then
DiffDOY=`expr $TodayDOY - $StampDOY`
else
#Calculate Previous Year DOY with 365 Day offset
DiffDOY=`expr $TodayDOY + 365`
DiffDOY=`expr $DiffDOY - StampDOY`
fi

#Compare Days Since Last Transfer with Notify Limit.
if [ ${DiffDOY} -ge "${DownLoadNotifyDaysNoFtp}" ]
then
emailSubj="A File Transfer Has Not Downloaded to $pl For $DiffDOY Days"
echo "\n${emailSubj}"
sendEmailFtp
echo
else
case ${DiffDOY}
in
0) echo "File Transfer Completed Today.";;
1) echo " File Transfer Completed $DiffDOY day ago.";;
*) echo " File Transfer Completed $DiffDOY days ago.";;
esac
fi
echo

****************************************************************
O/P

expr: syntax error
ftpAMEXPSA.ksh: line 524: [: -eq: unary operator expected
expr: non-integer argument
ftpAMEXPSA.ksh: line 534: [: -ge: unary operator expected

*****************************************************************

Please advice.

rtmistler 09-09-2015 06:39 AM

The few comments I have are that it is unknown whether you are running BASH or some other script language because you did not post enough of your script. Please use [code] tags in the future, see my signature or the LQ FAQ for further help with that.

The most common complaint for such a test like that is that the variable is not resolved or defined to be a value. Check what the output of $DiffYear is using echo. As far as the second comparison, the variable $DownLoadNotifyDaysNoFtp is not defined, at least not in the section shown.

A further recommendation is to use all CAPS for variable names.

Just prior to the section you can use "set -xv" to enable more debug and then after the section "set +xv" to revert back to normal. So when you run the script in a terminal you'll see details of variable assignments, what values are being compared, and what the outcomes are, same as if you used echo to output all.

Sample script:
Code:

#!/bin/sh

set -xv

VAL=5

if [ ${VAL} -eq 5 ]
then
    echo "true"
fi

set +xv

if [ ${VAL} -ge 4 ]
then
    echo "also true"
fi

Sample output:
Code:

$ ./compare.sh

VAL=5
+ VAL=5

if [ ${VAL} -eq 5 ]
then
    echo "true"
fi
+ '[' 5 -eq 5 ']'
+ echo true
true

set +xv
+ set +xv
also true


newuser85 09-10-2015 05:13 AM

Hi,

Thanks for your reply. Actually I am using bash script. Regarding the error at both the conditional statements I have found the reason, that the values being sent to expr (DiffYear= `expr $TodayYear - $StampYear`) is not getting assigned outside the do-while loop. StampYear variable's assigned value inside do-while loop is not getting reflected outside the same.

testLastDownload () {
StampYear=
StampDOY=
echo "Inside test Download"
#Test Download Date Stamp
file=${LocalDownloadDir}\\${DownLoadStampFile}
if [ ! -f "${LocalDownloadDir}\\${DownLoadStampFile}" ]
then
echo "\nDownload Date Stamp File Not Found To Verify Last Download\n"
return
fi


head -1 ${LocalDownloadDir}\\${DownLoadStampFile} | \
while read l
do
StampYear=`echo ${l} | cut -d"." -f1 | cut -c1-4`
StampDOY=`echo ${l} | cut -d"." -f2`
echo "StampYear : $StampYear"
echo "StampDOY : $StampDOY"
done

TodayYear=`echo ${DATE} | cut -c1-4`
TodayDOY=$DAYOFYEAR
echo "TodayYear : $TodayYear"
echo "TodayDOY : $TodayDOY"


DiffYear= `expr $TodayYear - $StampYear`
#echo `DiffYear=`expr ${TodayYear} - ${StampYear}``
echo "DiffYear : $DiffYear"

if [ ${DiffYear} -eq 0 ]
then
DiffDOY=`expr $TodayDOY - $StampDOY`
else
#Calculate Previous Year DOY with 365 Day offset
DiffDOY=`expr $TodayDOY + 365`
DiffDOY=`expr $DiffDOY - StampDOY`
fi

#Compare Days Since Last Transfer with Notify Limit.
if [ ${DiffDOY} -ge "${DownLoadNotifyDaysNoFtp}" ]
then
emailSubj=" File Transfer Has Not Downloaded to $pl For $DiffDOY Days"
echo "\n${emailSubj}"
sendEmailFtp
echo
else
case ${DiffDOY}
in
0) echo "AmEx File Transfer Completed Today.";;
1) echo "AmEx File Transfer Completed $DiffDOY day ago.";;
*) echo "AmEx File Transfer Completed $DiffDOY days ago.";;
esac
fi
echo

}
********************************************

O/P

Inside test Download
StampYear : 2015
StampDOY : 049
TodayYear : 2015
TodayDOY : 253
DiffYear :

DiffYear is empty despite StampYear inside do-while loop has a value. Please advice.

pan64 09-10-2015 05:38 AM

please use [code]here comes your code[/code] to keep formatting.
Use:
DiffYear=$((TodayYear - StampYear))
But first of all you must not put space before/after the equal sign.

newuser85 09-10-2015 06:06 AM

Hi,

Thanks for your reply. Corrected the space after = .

But still my problem is not resolved.

code snippet

head -1 ${LocalDownloadDir}\\${DownLoadStampFile} | \
while read l
do
StampYear=`echo ${l} | cut -d"." -f1 | cut -c1-4`
StampDOY=`echo ${l} | cut -d"." -f2`
echo "StampYear : $StampYear"
echo "StampDOY : $StampDOY"
done

TodayYear=`echo ${DATE} | cut -c1-4`
TodayDOY=$DAYOFYEAR
echo "TodayYear : $TodayYear"
echo "TodayDOY : $TodayDOY"


DiffYear=`expr $TodayYear - $StampYear` (Value of StampYear is empty outside the do while loop where its actually gets assigned a vaue - StampYear=`echo ${l} | cut -d"." -f1 | cut -c1-4` )
DiffYear=$((TodayYear - StampYear)) (Same StampYear value is 0 and not actual one assigned inside do while loop)

I want the value of StampYear variable assigned inside while do loop to reflect outside the loop. Hope I have explained whats my expectation. Please advice.

pan64 09-10-2015 07:12 AM

please use [code]here comes your code[/code] to keep formatting.

Try to define variable first before the loop

newuser85 09-10-2015 07:43 AM

Hi,

Thanks for your reply.

I tried defining the variable before the starting of the loop, but still the value getting assigned to that variable inside the loop is not reflecting outside. can anyone suggest any work around for the same.

chrism01 09-10-2015 08:38 PM

You're suffering from the pipe/while loop subshell problem http://mywiki.wooledge.org/BashFAQ/024 which explains why and offers some solns.

Here's another option
Code:

# t.t
2015.09
some rubbish  and another date 0123.45

# t.sh
for f in $(head -1 t.t)
do
    year=`echo $f  | cut -d"." -f1 | cut -c1-4`
    day=`echo ${f} | cut -d"." -f2`
    echo "$year"
    echo "$day"
done

echo "$year"
echo "$day"

# o/p
./t.sh
2015
09
2015
09


grail 09-11-2015 03:28 AM

As chrism01 has answered the pipe issue, I would add that you can do the entire arithmetic inside the round brackets and then you can space it for clarity (if you like):
Code:

(( DiffYear = TodayYear - StampYear ))

chrism01 09-11-2015 08:09 AM

Can I also recommend (in general) to use [[ ]] instead of [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS


All times are GMT -5. The time now is 11:05 AM.