如何使用shell脚本从tar文件中获取文件名和文件大小

时间:2018-01-19 20:04:11

标签: bash shell

我正在shell脚本中进行文件验证,我们需要从tar文件中读取文件大小和文件名,并验证文件大小是否大于特定大小,并且tar文件包含所有必需文件列表。
                下面是我编写的代码,有没有什么方法可以在一次循环中检查文件名和文件大小。

  

任何输入都表示赞赏。提前谢谢......

#!/bin/bash
minimumsize=90000
mandatoryFiles=(party.dat test1.dat test2.dat) 


ALL_FILE_NAMES=`tar -tvf testData/test_daily.tgz | awk '{print $6}' `  #get file names in tar file
ALL_FILE_SIZES=`tar -tvf testData/test_daily.tgz | awk '{print $3}' `  #get file sizez in bytes

echo "File names :::::::::::"$ALL_FILE_NAMES
echo "File sizes :::::::::::"$ALL_FILE_SIZES



#condition to check file size is greater than minimum size
for actualsize in $ALL_FILE_SIZES; do

   if [ $actualsize -ge $minimumsize ]; then
          echo size is over $minimumsize bytes
   else
         echo size is under $minimumsize bytes
         exit 0
   fi
done


#condition to check all the mandatory files are included in the taz file.
for afile in $ALL_FILE_NAMES; do

    if [[ ${mandatoryFiles[*]} =~ $afile ]]; then
        echo $afile is present
    else   
        echo $afile not present so existing the bash
            exit 0
    fi 

done

1 个答案:

答案 0 :(得分:2)

你的方法有点尴尬。您只需要捕获所有文件名以循环访问必需文件,但是您无法按照自己的方式进行检查,或者归档中的任何其他文件(超出强制文件)都会导致测试失败。

更简洁的方法是使用进程替换将大小和文件名提供给循环,允许您测试每个文件大小(任何小于minimumsize的文件都将导致归档在填充all_names数组的同时失败)。那时你完成了读循环。

all_names的最终循环检查mandatoryFiles中是否存在mandatoryFiles并递增计数器,以便检查是否存在与#!/bin/bash fname="${1:-testData/test_daily.tgz}" ## filename to read minimumsize=90000 ## min size mandatoryFiles=(party.dat test1.dat test2.dat) ## mandatory files declare -a all_names ## all_names array declare -i mandatory_count=0; ## mandatory count while read -r size name; do ## read/compare sizes, fill array all_names+=( "${name##*/}" ); ## store each file name in array w/o path #condition to check file size is greater than minimum size if [ "$size" -ge $minimumsize ]; then echo "$size is over $minimumsize bytes" else echo "$size is under $minimumsize bytes" exit 0 fi done < <(tar -tzvf "$fname" | awk '{print $3, $6}') #condition to check all the mandatory files are included in the taz file. for afile in "${all_names[@]}"; do if [[ ${mandatoryFiles[@]} =~ "$afile" ]]; then ((mandatory_count++)) ## increment mandatory_count fi done ## test if mandatory_count less than number of mandatory files if [ "$mandatory_count" -lt "${#mandatoryFiles[@]}" ]; then echo "mandatoryFiles not present - exiting" exit 1 fi echo "all files good" 中的每一个匹配的内容。

一种方法是:

.tgz

注意:如果文件是'z'(g-zipped tar存档),则需要添加const office_area = parseFloat(item.office_area[0]); const office_price = parseInt(item.office_price[0]); const max_area = parseFloat(areaList.areaMax[priceInputVal]); const min_area = parseFloat(areaList.areaMin[priceInputVal]); const max_price = parseInt(priceList.priceMax[priceInputVal]); const min_price = parseInt(priceList.priceMin[priceInputVal]); outputCont.html( office_data.map(item => { if (office_area > max_area) { return "area is too large"; } if (office_area < min_area) { return "area is too small"; } if (office_price > max_price) { return "price is too high"; } if (office_price < min_price) { return "price is too low"; } return ` <div class="col-sm-4 mb-3"> <div class="card" style="width: 18rem;"> <img class="card-img-top" src="http://www.decorouscontract.com/wp-content/uploads/Vitra-Workspace-by-Pernilla-Ohrstedt_dezeen_468_3.jpg" alt="Card image cap"> <div class="card-body"> <h5 class="card-title">${item.title.rendered}</h5> </div> <ul class="list-group list-group-flush"> <li class="list-group-item">Area: ${item.office_area}</li> <li class="list-group-item">Price: ${item.office_price}</li> <li class="list-group-item">Status: </li> </ul> </div> </div> `; }).join('') ); 选项,如上所述)

仔细看看,如果您有其他问题,请告诉我。

相关问题