Home Shell Scripting errors in `if` statement with computations
Reply: 1

Shell Scripting errors in `if` statement with computations

raht
1#
raht Published in 2018-01-12 15:54:43Z
TODAYSYEAR=$(date +"%Y")
echo $TODAYSYEAR

TODAYSMONTH=$(date +"%m")
echo $TODAYSMONTH

TODAYSDAY=$(date +"%d")
echo $TODAYSDAY

FCreatationYear=2017

FCreatationMonth=12

FCreatationDay=11

YEARDIFF=$(($TODAYSYEAR-$FCreatationYear))
echo $YEARDIFF

MONTHDIFF=$(($TODAYSMONTH-$FCreatationMonth))
echo $MONTHDIFF

THIRTYDAYSFROMFCDAY=$((30-$FCreatationDay))
echo $THIRTYDAYSFROMFCDAY

DAYGAPTHIRTY=$(($TODAYSDAY+$THIRTYDAYSFROMFCDAY))
echo $DAYGAPTHIRTY

TWENTYEIGHTDAYSFROMFCDAY=$((28-$FCreatationDay))
echo $TWENTYEIGHTDAYSFROMFCDAY

DAYGAPTWENTYEIGHT=$(($TODAYSDAY+$TWENTYEIGHTDAYSFROMFCDAY))
echo $DAYGAPTWENTYEIGHT

THIRTYONEDAYSFROMFCDAY=$((31-$FCreatationDay))
echo $THIRTYONEDAYSFROMFCDAY

DAYGAPTHIRTYONE=$(($TODAYSDAY+$THIRTYONEDAYSFROMFCDAY))
echo $DAYGAPTHIRTYONE

if((TODAYSMONTH==1)&&(YEARDIFF==1)&&(MONTHDIFF==-11)&&(DAYGAPTHIRTYONE>31)); then echo "file needs to be removed";
if((TODAYSMONTH==1)&&(YEARDIFF==1)&&(MONTHDIFF==-11)&&(DAYGAPTHIRTYONE<=31)); then echo "file stays";
fi;
fi;

I am getting errors, in regards to my if statements. What correction do I need to make?

Jens
2#
Jens Reply to 2018-01-12 16:12:11Z

You should understand that (( is a single token with a special meaning. The second parenthesis is not treated as an opening parenthesis, so can't be closed with ). But you try to do that in

if((TODAYSMONTH==1)...

For more on this, man bash and search forward for ARITHMETIC EVALUATION.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316225 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO