You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Bash日历脚本时遭遇unexpected end of file错误求助

问题分析与解决

报错unexpected end of file是语法错误导致脚本解析中断,你脚本里的关键错误在第71行:

if [ ! isMonthName $word2 ] ;

这里写法有误,[ ]是test命令的语法,不能直接在其中调用自定义函数isMonthName。正确写法需去掉方括号,直接用逻辑非判断函数返回值:

if ! isMonthName "$word2" ;

修复后的完整代码

#!/bin/bash

# addagenda--Prompts the user to add a new event for the agenda script

agendafile="$HOME/.agenda"


isDayName()
{
    # Return 0 if all is well, 1 on error.
    case $(echo "$1" | tr '[[:upper:]]' '[[:lower:]]') in
    sun*|mon*|tue*|wed*|thu*|fri*|sat*) retval=0 ;;
    *) retval=1 ;;
    esac
    return $retval
}


isMonthName()
{
    case $(echo "$1" | tr '[[:upper:]]' '[[:lower:]]') in
    jan*|feb*|mar*|apr*|may|jun*) return 0 ;;
    jul*|aug*|sep*|oct*|nov*|dec*) return 0 ;;
    *) return 1 ;;
    esac
}


normalize()
{
    # Return string with first char uppercase, next two lowercase.
    /bin/echo -n "$1" | cut -c1 | tr '[[:lower:]]' '[[:upper:]]'
    echo "$1" | cut -c2-3 | tr '[[:upper:]]' '[[:lower:]]'
}


if [ ! -w "$HOME" ] ;
then
    echo "$0: cannot write in your home directory ($HOME)" >&2
    exit 1
fi

echo "Agenda: The Unix Reminder Service"
/bin/echo -n "Date of event (day mon, day month year, or dayname): "
read word1 word2 word3 junk

if isDayName "$word1" ;
then
    if [ ! -z "$word2" ] ;
    then
        echo "Bad dayname format: just specify the day name by itself." >&2
        exit 1
    fi
    date="$(normalize "$word1")"

else
    if [ -z "$word2" ] ;
    then
        echo "Bad dayname format: unknown day name specified" >&2
        exit 1
    fi
    if [ ! -z "$(echo "$word1" | sed 's/[[:digit:]]//g')" ] ; then
        echo "Bad date format: please specify day first, by day number" >&2
        exit 1
    fi
    if [ "$word1" -lt 1 -o "$word1" -gt 31 ] ;
    then
        echo "Bad date format: day number can only be in range 1-31" >&2
        exit 1
    fi
    if ! isMonthName "$word2" ;
    then
        echo "Bad date format: unknown month name specified." >&2
        exit 1
    fi

    word2="$(normalize "$word2")"

    if [ -z "$word3" ] ;
    then
        date="$word1$word2"
    else
        if [ ! -z "$(echo "$word3" | sed 's/[[:digit:]]//g')" ] ;
        then
            echo "Bad date format: third field should be year." >&2
            exit 1
        elif [ "$word3" -lt 2000 -o "$word3" -gt 2500 ] ;
        then
            echo "Bad date format: year value should be 2000-2500" >&2
            exit 1
        fi
        date="$word1$word2$word3"
    fi
fi

/bin/echo -n "One-line description: "
read description

# Ready to write to data file

echo "$(echo "$date" | sed 's/ //g')|$description" >> "$agendafile"

exit 0

额外优化说明:

  • 给所有变量引用添加双引号,避免空格或特殊字符引发的解析错误
  • 拆分紧凑的case语句换行,提升代码可读性

内容的提问来源于stack exchange,提问作者burble_gurp007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:25:21