LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   [: : integer expression expected if else condition (https://www.linuxquestions.org/questions/linux-server-73/%5B-integer-expression-expected-if-else-condition-4175438567/)

Iyyappan 11-24-2012 03:35 PM

[: : 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

acid_kewpie 11-24-2012 04:03 PM

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.

colucix 11-24-2012 04:32 PM

This condition identifies the fourth Sunday of the month:
Code:

[[ $(date +%w) -eq 0 && $(date +%d) -ge 22 && $(date +%d) -le 28 ]]
:twocents:

Iyyappan 11-24-2012 05:48 PM

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


colucix 11-25-2012 02:09 AM

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.


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