LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2015, 05:34 AM   #1
newuser85
LQ Newbie
 
Registered: Aug 2015
Posts: 15

Rep: Reputation: Disabled
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.
 
Old 09-09-2015, 06:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 09-10-2015, 05:13 AM   #3
newuser85
LQ Newbie
 
Registered: Aug 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-10-2015, 05:38 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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.
 
Old 09-10-2015, 06:06 AM   #5
newuser85
LQ Newbie
 
Registered: Aug 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-10-2015, 07:12 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
please use [code]here comes your code[/code] to keep formatting.

Try to define variable first before the loop
 
Old 09-10-2015, 07:43 AM   #7
newuser85
LQ Newbie
 
Registered: Aug 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-10-2015, 08:38 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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

Last edited by chrism01; 09-10-2015 at 08:39 PM.
 
1 members found this post helpful.
Old 09-11-2015, 03:28 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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 ))
 
1 members found this post helpful.
Old 09-11-2015, 08:09 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Conditional Statements Based on Success of Last Command metallica1973 Programming 3 10-30-2013 03:46 AM
[SOLVED] Question about conditional statements. jason13131414 Linux - Newbie 4 04-05-2013 04:18 PM
Cannot get conditional statements to work in xDialog/Dialog Stevithen Programming 7 02-05-2011 06:03 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
conditional statements in gmake on Linux malik Linux - Software 0 04-28-2004 10:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration