Unix判断一个文件是否为空

Unix determine if a file is empty

我正在尝试制作一个脚本来检查文件中是否有任何 tyext。我开发了以下脚本。我已经让它检查是否正好有 2 个参数,看看文件是否存在,但我无法检查文件中的文本。代码如下:

#!/bin/ksh
#check if number of arguments are 2

if [ $# -ne 2 ]; then
    echo "Does not equal two arguments"
    echo "Usage [=10=] inputfile outputfile"
    exit 1
fi

#check if input file exists

if [ ! -f  ]; then
    echo " not found!"
    exit 1
fi

#Check if input file is null
#This next block of code is where the issue is
if [ grep -q  -eq 0 ]; then
    echo " must have text within the file"
    exit 1
fi

如有任何帮助,我们将不胜感激

尝试使用统计

stat -c %s 文件名

test 的“-s”选项检查文件是否为空——参见 manual。所以你的最后一块会变成

#Check if input file is null
#This next block of code is where the issue is
if [ ! -s  ]; then
    echo " must have text within the file"
    exit 1
fi