babysharkc
babysharkc

Reputation: 1

Syntax error unexpected end of file fi' what could be the cause?

#!/bin/bash

read -p "Enter your birthyear:  " year
read -p "Enter your birthmonth: " month


yearnow=$(date '+%Y')
monthnow=$(date '+%m')
daynow=$(date '+%d')
lastyear=$(date -d "last year" '+%Y')

agey=$(($yearnow-$year))
agem=$(($monthnow-$month))


if [ $month<$monthnow ]; then

        agem=$(($month-$monthnow)); agey=$(($lastyear-$year))
else

if [ $monthnow > $month ]; then

        agem=$(($monthnow-$month)); agey=$(($yearnow-$year))
fi

echo "You are $agey years and $agem months old."

I'm getting the syntax error unexpected end of file but I can't find the problem. What could it be? I want it to calculate the age for given birthyear and birthmonth.

Upvotes: 0

Views: 38

Answers (1)

Dominique
Dominique

Reputation: 17543

Your code looks like this:

if
then
else if
     then
     fi

The fi closes the nested if-loop, but what about the first if-loop?

Your code should look like this:

if
then
else if
     then
     fi
fi

Also, good indenting might be helpful, as you can see here:

if [ $month<$monthnow ]; then
   agem=$(($month-$monthnow));
   agey=$(($lastyear-$year))
else if [ $monthnow > $month ]; then
        agem=$(($monthnow-$month));
        agey=$(($yearnow-$year))
     fi

You immediately see that there's a level with an if but without a fi.

Upvotes: 2

Related Questions