简体   繁体   English

语法错误意外结束文件fi'可能是什么原因?

[英]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.我希望它计算给定出生年份和出生月份的年龄。

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? fi关闭了嵌套的 if 循环,但是第一个 if 循环呢?

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 .您会立即看到有一个带有if但没有fi的关卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM