4

I've got bash script for checking CRC of multiple zip files:

#! /bin/bash

fileFormat="*.zip"

for entry in `ls $fileFormat`; do
    echo $entry >> plikiZip
done

fileName=$(< plikiZip)

for file in "${fileName[@]}"; do
        zip -T $file >> wynik.txt
done

I don't know why, byt my wynik.txt contains:

  adding: mf.gov.pl_1133863230349.zip (stored 0%)
  adding: mf.gov.pl_1133863293588.zip (stored 0%)
  adding: mf.gov.pl_1133863748942.zip (stored 0%)
  adding: noweprzetargi.msgaz.pl_1133848724906.zip (stored 0%)
  adding: swps.pl_1133864085863.zip (stored 0%)
  adding: swps.pl_1133864308647.zip (stored 0%)
  adding: swps.pl_1133864438352.zip (stored 0%)
test of mf.gov.pl_1133863028119.zip OK

What should I change to have "OK" or "BAD" for all entries instead of

adding:

and

(stored X%)

?

Thor1990
  • 63
  • 1
  • 8

3 Answers3

1

You always can use find command:

find . -name "*.zip" -exec zip -T {} \; >> wynik.txt

And here you can use -maxdepth N options to control the depth of finding.

Laser
  • 6,652
  • 8
  • 54
  • 85
  • @anishsane Thanks for the note!, I've fixed my solution – Laser Apr 22 '15 at 10:46
  • This is works for me but what if I want to do this for files in subfolders? `ls -R $fileFormat` could be good (or not ;)). – Thor1990 Apr 22 '15 at 11:22
  • @Thor1990 in your case is better to use just find command with exec option – Laser Apr 22 '15 at 12:34
  • I'll try it tomorow at work. `maxdepth 1` will "search" my top directory eg. `/foo/` and sub directories: `/foo/bar1/`, `/foo/bar2/`, etc for zip files? – Thor1990 Apr 22 '15 at 18:47
  • @Thor1990 yes:, Use it this way: `find . -maxdepth 1 -name "*.zip" -exec zip -T {} \; >> wynik.txt` #`Dot symbol` - means current path (also you may use full path) – Laser Apr 23 '15 at 05:33
0

Replace following line

zip -T $file && echo "OK" >> wynik.txt || echo "BAD" >> wynik.txt

If the result is failure && will fail and "BAD" will be outputted

If the result is successful && will not fail and "OK" will be outputted

deimus
  • 9,565
  • 12
  • 63
  • 107
  • Unfortunatly, but in console I can see: `root@WA-NAS 1133 # ./check_zip.sh updating: mf.gov.pl_1133863230349.zip (stored 0%) updating: mf.gov.pl_1133863293588.zip (stored 0%) updating: mf.gov.pl_1133863748942.zip (stored 0%) updating: noweprzetargi.msgaz.pl_1133848724906.zip (stored 0%) updating: swps.pl_1133864085863.zip (stored 0%) updating: swps.pl_1133864308647.zip (stored 0%) updating: swps.pl_1133864438352.zip (stored 0%) test of mf.gov.pl_1133863028119.zip OK OK` and file wynik.txt isn't created. – Thor1990 Apr 22 '15 at 11:19
  • What can you see ? please be more specific – deimus Apr 22 '15 at 11:20
  • if you want to suppress the standard output try `zip -T $file &>/dev/null && echo "OK" || echo "BAD" >> wynik.txt` – deimus Apr 22 '15 at 11:27
  • Now I see: `root@WA-NAS 1133 # ./check_zip.sh OK root@WA-NAS 1133 # ` and file: `wynik.txt` isn't created... Shouldn't it be that `"OK"` and `BAD` should be shown/written in file for every zip? – Thor1990 Apr 22 '15 at 11:33
  • `>>` will create the file if it doesn't exist. Check if you have enough permission for `rw` wherever your trying to create the file. – deimus Apr 22 '15 at 12:22
  • I've got `>>` and I've got write permissions wher I want create file. – Thor1990 Apr 22 '15 at 18:48
  • sorry my bad, just updated the answer, you need to add extra `>> wynik.txt` after echoing `OK` – deimus Apr 22 '15 at 19:00
0

You can also use xargs and unzip:

ls -1 | xargs -L1 -I {} unzip -t  {}

and if they are subfolders tree would do:

tree -fai . | xargs -L1 -I {} unzip -t  {}
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179