LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 11-24-2012, 04:35 PM   #1
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 229

Rep: Reputation: 4
[: : integer expression expected if else condition


Hi All,
I am in need of sending a mail on every fourth sunday of month. I have written the script like this
Code:
MAILSUBJECT="test mail"
MAILTO="test@test.org"
MAILBODY="Test Mail for you"
DATE=$(/bin/date -I)
DAY=$(date "+%d")
FOURTH_SUNDAY=$(cal | tail -n 2 | awk '{print $1}')

if [ "$DAY" -eq "$FOURTH_SUNDAY" ]; then

echo "success"
echo "$MAILBODY" | $MUTT -e 'set content_type="text/html"' $MAILTO -s "$MAILSUBJECT"
else 
echo "failure"
exit 0
fi
When I execute the script I receive
integer expression expected error:

If I manually specify the date instead of FOURTH_SUNDAY, script gets executed. Kindly let me know, if there is any work around

Last edited by colucix; 11-25-2012 at 02:40 AM. Reason: Added CODE tags to improve readability
 
Old 11-24-2012, 05:03 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
there is no "workaround", there's using commands properly ;-)

the string you're getting out of cal has two lines in it, a blank one at the end, so it's not an integer you're getting out of it. I expect there's a better way to get the last month of a month, but my brains switched off for now. If you "head -n1" the output, that'll ditch the blank line.
 
Old 11-24-2012, 05:32 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
This condition identifies the fourth Sunday of the month:
Code:
[[ $(date +%w) -eq 0 && $(date +%d) -ge 22 && $(date +%d) -le 28 ]]

Last edited by colucix; 11-25-2012 at 02:57 AM. Reason: Confused Sunday with Saturday: fixed.
 
Old 11-24-2012, 06:48 PM   #4
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 229

Original Poster
Rep: Reputation: 4
I tried with head command, but received same error

Code:
MAILSUBJECT="test mail"
MAILTO="test@test.org"
MAILBODY="Test Mail for you"
DATE=$(/bin/date -I)
DAY=$(date "+%d")
FOURTH_SUNDAY=$(cal | head -n 7 | tail -n 1 | awk '{print $1}')

if [ "$DAY" -eq "$FOURTH_SUNDAY" ]; then

echo "success"
echo "$MAILBODY" | $MUTT -e 'set content_type="text/html"' $MAILTO -s "$MAILSUBJECT"
else
echo "failure"
exit 0
fi

Last edited by colucix; 11-25-2012 at 02:40 AM. Reason: Idem
 
Old 11-25-2012, 03:09 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Your last example should work, since it retrieves a single value with no extra lines from the calendar. Anyway, using cal in this way is not a robust method to retrieve the 4th Sunday of the month: for example, compare July and November of this year:
Code:
      July 2012            November 2012
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7                1  2  3
 8  9 10 11 12 13 14    4  5  6  7  8  9 10
15 16 17 18 19 20 21   11 12 13 14 15 16 17
22 23 24 25 26 27 28   18 19 20 21 22 23 24
29 30 31               25 26 27 28 29 30
In the first case with your command you get the 5th Sunday of the month, in the second case the 4th.

Therefore, why not using a more robust method as suggested previously? For example, consider that the 4th Sunday of the month is always included in the week between the 22nd and the 28th of the month, so that:
Code:
if [[ $(date +%w) -eq 0 && $(date +%d) -ge 22 && $(date +%d) -le 28 ]]
then
  echo do something
fi
should do the trick.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 07:01 AM
[SOLVED] if condition error: integer expression expected Kashif_Bash Programming 6 04-18-2012 06:37 AM
C compiling error: expected expression before { token CoderMan Programming 3 03-31-2009 05:31 PM
expected primary expression before... Uint character question. RHLinuxGUY Programming 3 05-12-2006 01:08 PM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 06:38 AM


All times are GMT -5. The time now is 10:19 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration