【问题标题】:Unix determine if a file is emptyUnix判断文件是否为空
【发布时间】:2015-05-03 14:04:26
【问题描述】:

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

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

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

#check if input file exists

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

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

任何帮助将不胜感激

【问题讨论】:

    标签: unix if-statement command-line-arguments


    【解决方案1】:

    尝试使用统计数据

    stat -c %s 文件名

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        • 2019-01-07
        • 2011-09-05
        相关资源
        最近更新 更多