Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何检查zip文件中的文件长度_Bash_Shell_Zip_Unzip_File Length - Fatal编程技术网

Bash 如何检查zip文件中的文件长度

Bash 如何检查zip文件中的文件长度,bash,shell,zip,unzip,file-length,Bash,Shell,Zip,Unzip,File Length,我想检查zip中的文件是否为空。我知道unzip-l命令,但它提供了很多信息 [abc@localhost test]$ unzip -l empty_file_test.zip Archive: empty_file_test.zip Length Date Time Name --------- ---------- ----- ---- 0 07-05-2017 06:43 empty_first_20170505.csv 0

我想检查zip中的文件是否为空。我知道unzip-l命令,但它提供了很多信息

[abc@localhost test]$ unzip -l empty_file_test.zip
Archive:  empty_file_test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    0      07-05-2017 06:43   empty_first_20170505.csv
    0      07-05-2017 06:43   empty_second_20170505.csv
---------                     -------
    0                         2 files
我通过命令从zip文件中提取了文件名

file_names="$(unzip -Z1 empty_file_test.zip)
file_name_array=($file_names)
file1=${file_name_array[0]}
file2=${file_name_array[1]}
我尝试使用-s选项,但没有用

if [ -s $file1 ]; then
   echo "file is non zero"
else
   echo "file is empty"    
fi
它总是打印文件为空,即使文件不是空的

unzip -l empty_file_test.zip | awk 'NR>=4{if($1==0){print $4}}'
可以为你工作,也可以写为

unzip -l empty_file_test.zip | awk 'NR >= 4 && $1==0{print $4}'
可以为你工作,也可以写为

unzip -l empty_file_test.zip | awk 'NR >= 4 && $1==0{print $4}'

您可以将输出格式化为unzip-l

unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2
说明:

解压-l解压文件并返回deisred信息

awk“{print$1\t$4}”打印第1列和第4列的大小和文件名

tail-n+4从输出中去除头几行和不需要的信息

head-n-2从输出中删除最后两行,删除不需要的摘要

编辑:

要将空文件存储到阵列中,可以映射comand的输出:

read -r -a array <<< `unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2 | awk '{if($1==0) print $2}'`
read-r-a数组如上所述

4剥离收割台和不需要的输出 如果$1==0{print$4}}如果大小$0为0,则执行{print$4} {print$4}输出文件名
您可以将输出格式化为unzip-l

unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2
说明:

解压-l解压文件并返回deisred信息

awk“{print$1\t$4}”打印第1列和第4列的大小和文件名

tail-n+4从输出中去除头几行和不需要的信息

head-n-2从输出中删除最后两行,删除不需要的摘要

编辑:

要将空文件存储到阵列中,可以映射comand的输出:

read -r -a array <<< `unzip -l test.zip | awk '{print $1 "\t"  $4}' | tail -n+4 | head -n-2 | awk '{if($1==0) print $2}'`
read-r-a数组如上所述

4剥离收割台和不需要的输出 如果$1==0{print$4}}如果大小$0为0,则执行{print$4} {print$4}输出文件名
感谢您提供的解释+1。我应该怎么做才能将空文件存储在数组中?我在帖子中添加了一个编辑来回答你的问题。谢谢你的解释+1。我应该怎么做才能将空文件存储在数组中?我在帖子中添加了一个编辑来回答你的问题。谢谢。这两种解决方案对我都有效。此外,我还可以将空文件存储到数组中:谢谢这两种解决方案对我都有效。此外,我还可以将空文件存储到数组中: