Shell 脚本文件,检查文本文件是否为空

Shell script file, checks if text file is empty or not

我想检查 .todo.txt 是否为空。 如果是空的,我想在文件中添加echo,如果已经有内容,我想做点别的。

我试过了:

hasData()
{
    echo "Hello"
}

isEmpty()
{
    echo -e "$n\t$comment\t$dueD/$dueM/$dueY " >> .todo.txt   
}

if [ -s $file ] ; then
    hasData()
else
    isEmpty()
fi

当我 运行 以上代码时,我得到以下信息:

./todo.sh: line 25: syntax error near unexpected token `else'
./todo.sh: line 25: `   else'

when i run my code i get the following ./todo.sh: line 25: syntax error near unexpected token else' ./todo.sh: line 25: else'

您必须使用圆括号来定义一个shell函数,但它们不参与调用一个函数。 shell 函数就像任何其他命令一样被调用:

if [ -s $file ] ; then
    hasData
else
    isEmpty
fi

如果您的函数带有参数,那么您会在函数名称之后列出它们——同样,就像任何其他命令一样。