#!/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.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.